"use strict";(function(h){const p=document,E="/apps/wishlist/save",w=localStorage,P="custom",$="wishlist",d=console.error,L={metafieldId:"",products:[],removedList:[]},T=new DOMParser,v=p.getElementById.bind(p),I=i=>{if(!i)return null;const t=T.parseFromString(i,"image/svg+xml"),{documentElement:s}=t;return s.nodeName==="svg"?(s.setAttribute("style","width:100%;height:100%"),s.setAttribute("fill","currentColor"),new XMLSerializer().serializeToString(t.documentElement)):null},y=()=>{try{const i=w.getItem("wishlist");return i?j(i):L}catch(i){return d("Cannot decode wishlist from browser, flushed!",i),w.removeItem("wishlist"),L}},m=(i,t,s)=>{const e=y(),n=e.metafieldId!==i||![...e.products,...t,...e.removedList,...s].every(r=>e.products.includes(r)&&t.includes(r)&&e.removedList.includes(r)&&s.includes(r));return w.setItem("wishlist",g({metafieldId:i,products:t,removedList:s.filter(r=>!t.includes(r))})),n},l=i=>[...new Set(i)],O=(i,t)=>{i.className.split(" ").includes(t)||(i.className+=` ${t}`)},f=(i,t)=>{i.className=i.className.split(" ").filter(s=>s!==t).join(" ")},C=p.createElement("textarea"),M=i=>(C.innerHTML=i,C.value),b="cart-icon-bubble cart-drawer cart-live-region-text cart-notification-button cart-notification-product cart-items cart-footer".split(" ").filter(i=>!!v(i)).join(","),{stringify:g,parse:j}=JSON;class N{init=!1;fetched=!1;elems=[];customer="";metafield="";prefix="";products=[];removed=[];constructor(t){const{prefix:s}=t??{};this.prefix=s??"",this.setCustomer(t)}async setCustomer(t){const{id:s,products:e}=t||{};if(s&&s!==this.customer){this.customer=s,this.products=e?.map(u=>u.toString())??[];const n=y(),{metafieldId:r,removedList:c}=n;this.metafield=`${r||""}`,this.removed=c||[]}await this.mergeWishlist(),console.info("Customer Wishlist successfully loaded."),h.dispatchEvent(new CustomEvent("wishlist:init",{detail:{target:this}})),this.init=!0}async mergeWishlist(){try{const t=y(),s=l([...t.products]),e=l([...t.removedList]);return this.products=l([...this.products,...s]),this.removed=l([...this.removed,...e]),m(this.metafield,this.products,this.removed),this.products}catch(t){throw t}}onCustomerChanges(t){return this.products=t.products.map(s=>`${s}`),this.metafield=t.metafieldId,m(this.metafield,this.products,this.removed)}async add(t){await this.mergeWishlist();const s=l([...this.products,`${t}`]),e=this.removed.indexOf(`${t}`);if(e!==-1&&this.removed.splice(e,1),s.length!==this.products.length)return(await this.storeWishlist(s)).success;throw new Error("Wishlist did not change")}async remove(t){await this.mergeWishlist();const s=l([...this.products]),e=s.indexOf(`${t}`);if(e===-1)throw new Error("Wishlist did not change");return s.splice(e,1),this.removed.push(`${t}`),this.removed=l(this.removed),(await this.storeWishlist(s)).success}async storeWishlist(t){if(this.customer){m(this.metafield,t,this.removed);const s=new URLSearchParams({param:btoa(g({customer:this.customer,namespace:P,key:$,metafieldId:this.metafield,ids:t}))}),e=await fetch(`${E}?${s}`,{cache:"no-cache",headers:{"ngrok-skip-browser-warning":"true"}});if(e.status===200){const n=await e.json();return this.onCustomerChanges(n),{success:!0}}else throw e.status===500&&(this.metafield=""),new Error(`${e.status}: ${e.statusText}`)}else return this.products=t,{success:m("",t,this.removed)}}}class S{node;wishlist;htmlOn;htmlOff;textOn;textOff;product;isOn=!1;constructor(t,s,e,n,r,c){const{prefix:u}=t,o=v(`${u}wl-${s}`);if(!o)throw new Error(`Can't find placeholder for product: ${s}`);this.wishlist=t,this.node=o,this.product=""+s,this.htmlOn=r||"",this.htmlOff=c||"",this.textOn=e,this.textOff=n,o.addEventListener("click",async()=>{O(o,"loading");try{const a=await this.isListed();(a?await this.remove(s):await this.add(s))&&this.setListed(!a)}catch(a){d(g(a))}finally{f(o,"loading")}}),this.isListed().then(a=>{this.setListed(a),f(o,"loading")})}async isListed(){try{return(await this.wishlist.mergeWishlist()).includes(this.product)}catch(t){throw t}}setListed(t){this.isOn=t,this.node.innerHTML=M(t?this.htmlOn:this.htmlOff),this.node.setAttribute("title",t?this.textOn:this.textOff),t?O(this.node,"active"):f(this.node,"active")}async add(t){return this.isOn?!1:this.wishlist.add(t)}async remove(t){return this.isOn?this.wishlist.remove(t):!1}}const W=(i,t,s,e,n,r)=>{i.elems.push(new S(i,t,s,e,n,r))};Object.assign(h,{prepareSvg:I,initWishList:i=>{const{prefix:t,prdId:s,textOn:e,textOff:n,htmlOn:r,htmlOff:c,...u}=i,o=`${t}wl`;h[o]||(h[o]=new N({...u,prefix:t}));const a=h[o];if(s&&e&&n&&r&&c){const x=()=>W(a,s,e,n,r,c);a.hasInit?x():h.addEventListener("wishlist:init",x)}return a},initWishlistProduct:W,declareMissingComponents:()=>{},addItemFromWishlist:async(i,t)=>{O(t,"loading");try{const s=await fetch("/cart/add",{method:"POST",headers:{"Content-Type":"application/json"},body:g({quantity:1,id:i})});if(s.ok&&b){const e=await fetch(`/?sections=${encodeURIComponent(b)}`,{headers:{"Content-Type":"application/json"}});if(e.ok){const n=await e.json();b.split(",").forEach(r=>{const c=v(r);c&&n[r]&&(c.innerHTML=n[r])})}else d(e.statusText)}else d(s.statusText)}catch(s){d(s)}finally{f(t,"loading")}}})})(window);