import{a as f,g as w,c as M,b as O,r as y,u as U}from"./js.cookie.C0BS3qSL.js";let u=[],c=0;const o=4;let h=0,p=t=>{let e=[],r={get(){return r.lc||r.listen(()=>{})(),r.value},lc:0,listen(n){return r.lc=e.push(n),()=>{for(let i=c+o;i<u.length;)u[i]===n?u.splice(i,o):i+=o;let a=e.indexOf(n);~a&&(e.splice(a,1),--r.lc||r.off())}},notify(n,a){h++;let i=!u.length;for(let s of e)u.push(s,r.value,n,a);if(i){for(c=0;c<u.length;c+=o)u[c](u[c+1],u[c+2],u[c+3]);u.length=0}},off(){},set(n){let a=r.value;a!==n&&(r.value=n,r.notify(a))},subscribe(n){let a=r.listen(n);return n(r.value),a},value:t};return r};const N=5,m=6,g=10;let Q=(t,e,r,n)=>(t.events=t.events||{},t.events[r+g]||(t.events[r+g]=n(a=>{t.events[r].reduceRight((i,s)=>(s(i),i),{shared:{},...a})})),t.events[r]=t.events[r]||[],t.events[r].push(e),()=>{let a=t.events[r],i=a.indexOf(e);a.splice(i,1),a.length||(delete t.events[r],t.events[r+g](),delete t.events[r+g])}),R=1e3,x=(t,e)=>Q(t,n=>{let a=e(n);a&&t.events[m].push(a)},N,n=>{let a=t.listen;t.listen=(...s)=>(!t.lc&&!t.active&&(t.active=!0,n()),a(...s));let i=t.off;return t.events[m]=[],t.off=()=>{i(),setTimeout(()=>{if(t.active&&!t.lc){t.active=!1;for(let s of t.events[m])s();t.events[m]=[]}},R)},()=>{t.listen=a,t.off=i}}),A=(t,e,r)=>{Array.isArray(t)||(t=[t]);let n,a,i=()=>{if(a===h)return;a=h;let d=t.map(l=>l.get());if(!n||d.some((l,v)=>l!==n[v])){n=d;let l=e(...d);l&&l.then&&l.t?l.then(v=>{n===d&&s.set(v)}):(s.set(l),a=h)}},s=p(void 0),T=s.get;s.get=()=>(i(),T());let C=i;return x(s,()=>{let d=t.map(l=>l.listen(C));return i(),()=>{for(let l of d)l()}}),s},L=(t,e)=>A(t,e);async function _(t){let e=f.get("cartId"),r;if(e&&(r=await w(e)),(!e||!r)&&(r=await M(),e=r.id,f.set("cartId",e)),!t)return"Missing product variant ID";try{await O(e,[{merchandiseId:t,quantity:1}])}catch{return"Error adding item to cart"}}async function F(t){const e=f.get("cartId");if(!e)return"Missing cart ID";try{await y(e,[t])}catch{return"Error removing item from cart"}}async function S(t){const e=f.get("cartId");if(!e)return"Missing cart ID";const{lineId:r,variantId:n,quantity:a}=t;try{a===0&&await y(e,[r]),await U(e,[{id:r,merchandiseId:n,quantity:a}])}catch{return"Error updating item quantity"}}const E=p(null),V=L(E,t=>t?t.totalQuantity:0),q=p("card");function P(t){q.set(t)}async function I(){const t=f.get("cartId");if(t){const e=await w(t);E.set(e)}}async function Y(t){try{return await _(t),await I(),"Added to cart"}catch(e){throw new Error(e.message||"Failed to add to cart")}}async function k(t){try{return await F(t),await I(),"Removed from cart"}catch(e){throw new Error(e.message||"Failed to remove item from cart")}}async function z(t){try{return await S(t),await I(),"Cart updated"}catch(e){throw new Error(e.message||"Failed to update cart")}}export{I as a,Y as b,E as c,q as l,k as r,P as s,V as t,z as u};
