import{i as e,t}from"./module.esm-BwdFUNDF.js";import{t as n}from"./browser-B5BHjE4y.js";import{n as r}from"./cfd-broadcast-DEi4VeCE.js";var i=e(n(),1);function a(e={}){return{channel:e.channel||`pos-cfd:default`,transport:e.transport||`same_machine`,pollUrl:e.poll_url||null,welcome:e.welcome||``,tagline:e.tagline||``,thankyou:e.thankyou||``,labels:e.labels||{},attract:e.attract||{media:[],seconds:8},promoIx:0,state:`idle`,snap:{last_line:null,lines:[],totals:{},customer:null,payment:null,thankyou:null},clock:``,payQr:``,receiptQr:``,_payQrUrl:null,_receiptQrUrl:null,_lastSeq:-1,_unsub:null,get linesNewestFirst(){return[...this.snap.lines||[]].reverse()},get itemCountLabel(){let e=Number(this.snap?.totals?.item_count??0);return`${this.snap?.totals?.item_count??0} ${e===1?this.labels.item:this.labels.items}`},get hasAttract(){return Array.isArray(this.attract.media)&&this.attract.media.length>0},init(){if(this._tick(),setInterval(()=>this._tick(),2e4),this.transport===`separate_device`&&this.pollUrl?this._startPolling():this._unsub=r(this.channel,e=>this._apply(e)),this.hasAttract&&this.attract.media.length>1){let e=Math.max(3,Number(this.attract.seconds)||8)*1e3;setInterval(()=>{this.promoIx=(this.promoIx+1)%this.attract.media.length},e)}},_apply(e){!e||typeof e!=`object`||typeof e.seq==`number`&&e.seq<=this._lastSeq||(this._lastSeq=typeof e.seq==`number`?e.seq:this._lastSeq,this.snap=e,this.state=[`sale`,`payment`,`thankyou`].includes(e.state)?e.state:`idle`,this._maybeQr())},async _maybeQr(){let e=this.snap?.payment?.pay_url||null;e!==this._payQrUrl&&(this._payQrUrl=e,this.payQr=e?await this._toQr(e):``);let t=this.snap?.thankyou?.receipt_url||null;t!==this._receiptQrUrl&&(this._receiptQrUrl=t,this.receiptQr=t?await this._toQr(t,200):``)},async _toQr(e,t=320){try{return await i.toDataURL(e,{width:t,margin:1,errorCorrectionLevel:`M`})}catch{return``}},_tick(){let e=new Date;this.clock=e.toLocaleTimeString(void 0,{hour:`2-digit`,minute:`2-digit`})},toggleTheme(){let e=document.documentElement.classList.toggle(`dark`);try{localStorage.setItem(`pos_cfd_theme`,e?`dark`:`light`)}catch{}},_startPolling(){let e=async()=>{try{let e=await fetch(this.pollUrl,{headers:{Accept:`application/json`},credentials:`same-origin`,cache:`no-store`});if(!e.ok)return;let t=await e.json();t&&typeof t.seq==`number`&&this._apply(t)}catch{}};e(),setInterval(e,1500)}}}document.addEventListener(`alpine:init`,()=>{t.data(`customerDisplay`,a)}),window.Alpine=t,t.start();