import{am as g,an as h,I as d,ao as l,ap as f,aq as y,ar as m,as as b,at as w,au as _,av as k,aw as S,b as R,g as c,d as x,e as C}from"../index.js";const D=g("updates",{state:()=>({items:[]}),actions:{reset(){this.$reset()},addItems(i){this.items=h.exports.uniqBy([...this.items,...i],n=>n.join())},push(i){d.updateDBOption("last_synced",i.last_synced),this.addItems(i.items)},pop(i){const[n,s]=this.items.reduce(([e,a],t)=>t[1]===i?[[...e,t],a]:[e,[...a,t]],[[],[]]);this.items=s;const o=n.filter(([,,e])=>e==="update"),u=n.filter(([,,e])=>e==="delete");return{update:o.map(([e])=>e),delete:u.map(([e])=>e)}},async update(){const i=l();if(!i.syncing){const n=this.pop("coupon"),s=this.pop("product"),o=this.pop("product_category"),u=this.pop("user"),e=[];s.update.length&&e.push(await f({origin:"update",include:s.update})),s.delete.length&&e.push(await y({origin:"update",include:s.delete})),n.update.length&&e.push(await m({origin:"update",include:n.update,blocking:!1})),n.delete.length&&e.push(await b({origin:"update",include:n.delete,blocking:!1})),u.update.length&&e.push(await w({origin:"update",include:u.update,blocking:!1})),u.delete.length&&e.push(await _({origin:"update",include:u.delete,blocking:!1})),o.update.length&&e.push(await k({origin:"update",include:o.update,blocking:!1})),o.delete.length&&e.push(await S({origin:"update",include:o.delete,blocking:!1})),i.enqueue(e)}}}});var v=R(async({router:i,store:n})=>{const s=l(n),o=D(n);setInterval(async()=>{if(s.syncing&&s.syncOrigin==="load")return;const a=await d.getDBOption("last_synced",0),t=["product","product_category"];c("pos","cache_customers")&&t.push("user"),c("wc","coupons_enabled",!1)&&c("pos","cache_coupons",!0)&&t.push("coupon"),x("check_updates",{last_synced:String(a),collections:t.join(","),register_id:String(C("id"))}).then(r=>{if(r.ok){const p=r.body;o.push(p)}})},c("pos","check_updates_interval",15)*1e3),o.$subscribe((a,t)=>{t.items.length&&o.update()}),s.$subscribe((a,t)=>{t.tasks.length&&!t.currentTask&&(s.background=!0,s.next().then(r=>{if(r!==!0)throw new Error(r.message)}).catch(r=>{s.error=`Failed: ${(r==null?void 0:r.message)||"Unknown error"}`}))});const u=a=>!!(!a.name||["login","error-register-in-use"].includes(a.name)),e=a=>a.name?!["login","error-register-in-use","error-unauthorized"].includes(a.name):!1;i.beforeEach((a,t)=>{u(t)&&e(a)&&s.sync()})});export{v as default};
