const s={INIT:[27,64],ALIGN_LEFT:[27,97,0],ALIGN_CENTER:[27,97,1],TEXT_NORMAL:[27,33,0],TEXT_BOLD_ON:[27,69,1],TEXT_BOLD_OFF:[27,69,0],TEXT_LARGE:[27,33,48],TEXT_TALL:[27,33,16],CUT_PARTIAL:[29,86,1],DRAWER_PIN2:[27,112,0,25,250],CODEPAGE_CP858:[27,116,19]},O=t=>{const i=t.reduce((o,l)=>o+l.length,0),n=new Uint8Array(i);let e=0;for(const o of t)n.set(o,e),e+=o.length;return n},r=t=>{const i=[];for(let n=0;n<t.length;n++){const e=t.charCodeAt(n);i.push(e>255?63:e)}return new Uint8Array(i)},v=t=>Number(t)===58?32:48,R=(t,i)=>{const n=String(t??"");return n.length>=i?n.substring(0,i):n+" ".repeat(i-n.length)},I=(t,i)=>{const n=String(t??"");return n.length>=i?n.substring(0,i):" ".repeat(i-n.length)+n},u=t=>(Number(t)||0).toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g," "),A=(t,i,n)=>{const e=String(i),o=Math.max(1,n-t.length-e.length);return t+" ".repeat(o)+e},U=(t,i)=>{const n=[];let e=String(t||"");for(;e.length>i;){let o=e.lastIndexOf(" ",i);o<=0&&(o=i),n.push(e.substring(0,o).trimEnd()),e=e.substring(o).trimStart()}return e&&n.push(e),n},E=(t,i="-")=>i.repeat(t);function D(t,i=80){const n=v(i),e=[];if(e.push(new Uint8Array(s.INIT)),e.push(new Uint8Array(s.CODEPAGE_CP858)),e.push(new Uint8Array(s.ALIGN_CENTER)),e.push(new Uint8Array(s.TEXT_LARGE)),t.societe?.nom&&e.push(r(t.societe.nom+`
`)),e.push(new Uint8Array(s.TEXT_NORMAL)),t.societe?.adresse)for(const c of U(t.societe.adresse,n))e.push(r(c+`
`));t.societe?.tel&&e.push(r("Tel: "+t.societe.tel+`
`)),t.societe?.ice&&e.push(r("ICE: "+t.societe.ice+`
`)),e.push(r(E(n,"=")+`
`)),e.push(new Uint8Array(s.ALIGN_LEFT));const o=t.entete?.date?new Date(t.entete.date):new Date,l=String(o.getDate()).padStart(2,"0")+"/"+String(o.getMonth()+1).padStart(2,"0")+"/"+o.getFullYear()+" "+String(o.getHours()).padStart(2,"0")+":"+String(o.getMinutes()).padStart(2,"0");if(e.push(new Uint8Array(s.TEXT_BOLD_ON)),e.push(r(A("Ticket N°"+(t.entete?.nbl||""),l,n)+`
`)),e.push(new Uint8Array(s.TEXT_BOLD_OFF)),t.entete?.caisse&&e.push(r("Caisse: "+t.entete.caisse+`
`)),t.entete?.utilisateur&&e.push(r("Caissier: "+t.entete.utilisateur+`
`)),t.entete?.commercial&&e.push(r("Vendeur: "+t.entete.commercial+`
`)),t.entete?.client_nom&&e.push(r("Client: "+t.entete.client_nom+`
`)),e.push(r(E(n)+`
`)),n>=48){const c=n-22;for(const h of t.lignes||[]){const w=Number(h.qte)||0,f=Number(h.pu_ttc)||0,T=Number(h.remise)||0,m=w*f,N=m-m*T,y=String(h.libelle||h.code_art||""),d=`${w} x ${u(f)}`,L=U(y,c);e.push(r(R(L[0],c)+" "+I(d,12)+" "+I(u(N),9)+`
`));for(let b=1;b<L.length;b++)e.push(r(L[b]+`
`));T>0&&e.push(r("  Remise "+(T*100).toFixed(0)+"% : -"+u(m*T)+`
`))}}else for(const c of t.lignes||[]){const h=Number(c.qte)||0,w=Number(c.pu_ttc)||0,f=Number(c.remise)||0,T=h*w,m=T-T*f,N=String(c.libelle||c.code_art||"");for(const d of U(N,n))e.push(r(d+`
`));const y=`${h} x ${u(w)}`;e.push(r(A("  "+y,u(m),n)+`
`)),f>0&&e.push(r("  Remise "+(f*100).toFixed(0)+"% : -"+u(T*f)+`
`))}e.push(r(E(n)+`
`));const a=Number(t.entete?.ht)||0,g=Number(t.entete?.tva)||0,p=Number(t.entete?.ttc_dh||t.entete?.ttc)||0;e.push(r(A("Total HT",u(a),n)+`
`)),e.push(r(A("TVA",u(g),n)+`
`)),e.push(new Uint8Array(s.TEXT_BOLD_ON)),e.push(new Uint8Array(s.TEXT_TALL)),e.push(r(A("TOTAL TTC",u(p),n)+`
`)),e.push(new Uint8Array(s.TEXT_NORMAL)),e.push(new Uint8Array(s.TEXT_BOLD_OFF)),e.push(r(E(n,"=")+`
`));for(const c of t.paiements||[])e.push(r(A(c.mode||"Paiement",u(c.montant),n)+`
`));const _=Number(t.entete?.mt_rendu)||0;_>0&&(e.push(new Uint8Array(s.TEXT_BOLD_ON)),e.push(r(A("RENDU",u(_),n)+`
`)),e.push(new Uint8Array(s.TEXT_BOLD_OFF)));const C=Number(t.entete?.points_fidelite)||0;return C>0&&(e.push(r(`
`)),e.push(new Uint8Array(s.ALIGN_CENTER)),e.push(r("+"+C+` points fidélité gagnés
`)),e.push(new Uint8Array(s.ALIGN_LEFT))),e.push(r(`
`)),e.push(new Uint8Array(s.ALIGN_CENTER)),e.push(r(`Merci de votre visite
`)),e.push(r(`


`)),e.push(new Uint8Array(s.CUT_PARTIAL)),O(e)}function x(){return typeof navigator<"u"&&!!navigator.usb}async function G(){if(!x())throw new Error("WebUSB non supporté par ce navigateur (utiliser Chrome ou Edge)");const t=await navigator.usb.requestDevice({filters:[]});return{vendorId:t.vendorId,productId:t.productId}}async function F(t,i){return x()&&(await navigator.usb.getDevices()).find(e=>e.vendorId===t&&e.productId===i)||null}async function S(t,i,n){if(!x())throw new Error("WebUSB non supporté");const e=await F(t,i);if(!e)throw new Error(`Imprimante non autorisée (vendor=${t}, product=${i}). Cliquez sur "Sélectionner l'imprimante" dans la configuration.`);await e.open();try{e.configuration===null&&await e.selectConfiguration(1);let o=-1,l=-1;for(const a of e.configuration.interfaces){const p=a.alternates[0].endpoints.find(_=>_.direction==="out");if(p){o=a.interfaceNumber,l=p.endpointNumber;break}}if(o<0)throw new Error("Aucune interface USB OUT trouvée sur ce device");await e.claimInterface(o),await e.transferOut(l,n),await e.releaseInterface(o)}finally{await e.close()}}async function P(t){const{vendorId:i,productId:n,largeur:e,data:o,ouvrirTiroir:l}=t;let a=D(o,e);if(l){const g=new Uint8Array(s.DRAWER_PIN2),p=new Uint8Array(a.length+g.length);p.set(a,0),p.set(g,a.length),a=p}await S(i,n,a)}async function X(t,i){await S(t,i,new Uint8Array(s.DRAWER_PIN2))}async function B(t,i,n=80){const e=v(n),o=[new Uint8Array(s.INIT),new Uint8Array(s.CODEPAGE_CP858),new Uint8Array(s.ALIGN_CENTER),new Uint8Array(s.TEXT_LARGE),r(`TEST IMPRIMANTE
`),new Uint8Array(s.TEXT_NORMAL),r(E(e,"=")+`
`),new Uint8Array(s.ALIGN_LEFT),r(`Connexion WebUSB OK
`),r("Largeur: "+n+"mm ("+e+` cols)
`),r("Date: "+new Date().toLocaleString("fr-FR")+`
`),r(`Accents: éàèçôû €
`),r(E(e,"=")+`
`),r(`


`),new Uint8Array(s.CUT_PARTIAL)];await S(t,i,O(o))}export{x as a,B as b,P as i,X as o,G as r};
