import{j as M}from"./app-CffHYkHW.js";import{u as z,c as W,B as tt}from"./bluetooth-context-CLrE8g1i.js";import{t as g}from"./index-Dw4sHwMN.js";import"./createLucideIcon-DAR-hy5d.js";import"./index-Cy5bElus.js";import"./index-DPkgfxsD.js";import"./utils-C8zp8JNC.js";import"./index-C9NeyPdJ.js";function pt({tx:s,paperWidth:i=32}){const{printerServer:$,isBluetoothConnected:h}=z(),l=(r,o=i)=>{if(!r)return"";if(r.length>=o)return r;const t=Math.floor((o-r.length)/2);return" ".repeat(t)+r},e=(r,o,t=i)=>{const a=r.trim(),c=String(o).trim(),u=a.length+c.length,p=" ".repeat(Math.max(t-u,2));return`${a}${p}${c}`},R=async(r,o,t=200)=>{for(let a=0;a<o.length;a+=t){const c=o.slice(a,a+t);await r.writeValue(c),await new Promise(u=>setTimeout(u,50))}},q=async()=>{var a,c,u,p,b;if(!$||!h){g.error("❌ Printer Bluetooth belum terhubung");return}const r=await fetch(`/transactions/${s.id}/nota-bt`),{store:o,trx:t}=await r.json();try{const d=new TextEncoder,E=new Date().toLocaleString("id-ID",{day:"2-digit",month:"2-digit",year:"numeric",hour:"2-digit",minute:"2-digit"}),A=[e("Invoice",t.invoice??"-"),e("Tanggal",E),e("Jenis",t.order_type_label??"-"),(a=t.table)!=null&&a.number?e("Meja",String(t.table.number)):null,e("Pelanggan",((c=t.customer)==null?void 0:c.name)??t.customer_name??"Umum"),e("Kasir",((p=(u=t.cashier_shift)==null?void 0:u.user)==null?void 0:p.name)??"-")].filter(Boolean).join(`
`),K=t.transaction_details.map(n=>{var I,N,T,B,P;const f=((N=(I=n.items)==null?void 0:I.product)==null?void 0:N.name)??((T=n.items)==null?void 0:T.name)??"Produk";let m="";((P=(B=n.items)==null?void 0:B.product_variant_values)==null?void 0:P.length)>0&&(m="["+n.items.product_variant_values.map(D=>{var L,x,C;return`${(x=(L=D.variant_value)==null?void 0:L.variant_option)==null?void 0:x.name}: ${(C=D.variant_value)==null?void 0:C.name}`}).join(", ")+"]");const _=`${f} ${m}`.trim(),Q=_.match(new RegExp(`.{1,${i}}`,"g"))||[_],v=Number(n.quantity??1),k=Number(n.price??0),S=Number(n.discount??0),X=(n.discount_type??"nominal")==="percentage"?Math.floor(k*S/100):S,j=Math.max(0,k-X),Y=j*v,Z=e(`${v} x ${j.toLocaleString("id-ID")}`,Y.toLocaleString("id-ID"),i);return[...Q,Z].join(`
`)}).join(`
`),w=Number(s.subtotal??0),y=Number(s.discount??0),U=Number(String(s.grand_total).replace(/[.,]/g,""))||w,G=t.tax_rows||[],et=Number(t.ppn_amount||0),nt=Number(t.opr_amount||0),J=[e("Subtotal",w.toLocaleString("id-ID")),y?e("Diskon",`-${y.toLocaleString("id-ID")}`):null,...G.map(n=>e(n.name,n.value.toLocaleString("id-ID"))),e("Total",U.toLocaleString("id-ID"))].filter(Boolean).join(`
`),V=[e(`Bayar (${s.payment_method??"-"})`,s.pay??"-"),e("Kembali",s.change??"-")].join(`
`),F=`
${l(((b=o.name)==null?void 0:b.toUpperCase())||"")}
${l(o.addr||"")}
${o.phone?l("Telp: "+o.phone):""}
${"-".repeat(i)}
${A}
${"-".repeat(i)}
${K}
${"-".repeat(i)}
${J}
${"-".repeat(i)}
${V}
${"-".repeat(i)}
${l("Terima kasih - Simpan struk ini")}
${l("sebagai bukti transaksi.")}
\r
\r
`,H=d.encode(F),O=await $.getPrimaryServices();for(const n of O){const f=await n.getCharacteristics();for(const m of f)if(m.properties.write||m.properties.writeWithoutResponse){await R(m,H),g.success("Struk berhasil dikirim!");return}}g.error("Printer tidak mendukung mode write")}catch(d){console.error(d),g.error("Gagal Cetak: "+d.message)}};return M.jsxs(W,{onClick:h?q:void 0,disabled:!h,children:[M.jsx(tt,{})," Cetak Nota Bluetooth"]})}export{pt as default};
