1import{c as f}from"./WithApiClient.DyDsqC0I.min.js";import{c as w,a as y}from"./cart-sections.WoVMkNAv.min.js";import{c as p}from"./consume.CHsRYPSi.min.js";import{d as g}from"./index.CKJCYK2x.min.js";import"./lit-element.mvcqtviJ.min.js";import{r as _}from"./state.Cmay8SOG.min.js";var b=Object.defineProperty,d=(h,r,l,e)=>{for(var t=void 0,s=h.length-1,i;s>=0;s--)(i=h[s])&&(t=i(r,l,t)||t);return t&&b(r,l,t),t};const m=new Set([]),S="cart-drawer-dialog",T={cart:!0,sections:!0},I=h=>{class r extends h{constructor(){super(...arguments),this.shopifyCartLoading=!1,this.shopifyCartError=null,this.loadingCartPromise=null,this.client=f({baseUrl:`${window.Shopify.routes.root}cart`,headers:{"Content-Type":"application/json"},defaultMethod:"POST",withQueue:!0,queueKey:"shopify-cart",onRequestStart:()=>{this.shopifyCartLoading=!0,this.classList.add("loading"),Array.from(this.querySelectorAll("button")).forEach(e=>{e.classList.add("disabled")})},onRequestEnd:()=>{this.shopifyCartLoading=!1,this.classList.remove("loading"),Array.from(this.querySelectorAll("button")).forEach(e=>{e.classList.remove("disabled")})}})}connectedCallback(){super.connectedCallback();const e=this.getAttribute("section-id"),t=this.hasAttribute("register-for-cart-updates");e&&typeof e=="string"&&t&&m.add(e)}get sectionsToFetch(){return Array.from(m).slice(0,5).join(",")}async addItemToCart(e){try{const t=await this.client("/add.js",{body:{items:e,sections:this.sectionsToFetch,sections_url:window.location.pathname}});return await this._handleCartAdd([t.items],this),await this._handleCartChange(),t.sections&&this._handleCartSectionsChange(t.sections),t}catch(t){throw this._handleCartError(t),t}}async addItemToWithForm(e){try{e.append("sections",this.sectionsToFetch),e.append("sections_url",window.location.pathname);const t=await this.client("/add",{body:e,headers:{"X-Requested-With":"XMLHttpRequest",Accept:"application/json"}},{rawBody:!0,removeContentType:!0});return await this._handleCartAdd([t],this),await this._handleCartChange(),t.sections&&this._handleCartSectionsChange(t.sections),t}catch(t){throw this._handleCartError(t),t}}async updateQuantityToCart(e){try{const t=await this.client("/update.js",{body:{updates:e,sections:this.sectionsToFetch,sections_url:window.location.pathname}});await this._handleCartChange(t),t.sections&&this._handleCartSectionsChange(t.sections)}catch(t){throw this._handleCartError(t),t}}normalizeExistingDiscountCodes(e){var t,s;return[...((t=e.discount_codes)==null?void 0:t.map(i=>
1i.code).filter(Boolean))??[],...((s=e.cart_level_discount_applications)==null?void 0:s.filter(i=>!!i&&typeof i=="object"&&"title"in i).map(i=>i.title))??[],...(e.items??[]).flatMap(i=>{var n;return((n=i.line_level_discount_allocations)==null?void 0:n.filter(o=>!!o&&typeof o=="object"&&"discount_application"in o).map(o=>o.discount_application.title))??[]})].filter(Boolean)}buildMergedDiscount(e){if(e===void 0)return;const t=e.trim();if(t==="")return"";const s=this.normalizeExistingDiscountCodes(this.cart);s.length>0&&!this.cart.discount_codes&&(this.cart.discount_codes=s.map(o=>({code:o,applicable:!0})));const i=s.flatMap(o=>o.split(",").map(a=>a.trim())).filter(Boolean);return new Set(i.map(o=>o.toLowerCase())).has(t.toLowerCase())||i.push(t),i.join(",")}async updateCartNoteAndAttributes(e,t=T){const s=this.buildMergedDiscount(e.discount),i={sections:this.sectionsToFetch,sections_url:window.location.pathname};e.note!==void 0&&(i.note=e.note),e.attributes&&(i.attributes=e.attributes),s!==void 0&&(i.discount=s);const n=await this.client("/update.js",{body:i});await this._handleCartChange(n),t.sections&&n.sections&&this._handleCartSectionsChange(n.sections)}async removeDiscountCode(e){var n,o;const t=[...((n=this.cart.discount_codes)==null?void 0:n.map(({code:a})=>a).filter(Boolean))??[],...((o=this.cart.cart_level_discount_applications)==null?void 0:o.filter(a=>a.type==="discount_code").map(a=>a.title).filter(Boolean))??[],...(this.cart.items??[]).flatMap(a=>{var c;return((c=a.line_level_discount_allocations)==null?void 0:c.filter(C=>{var u;return((u=C.discount_application)==null?void 0:u.type)==="discount_code"}).map(C=>C.discount_application.title).filter(Boolean))??[]})],s=new Set,i=t.filter(a=>{const c=a.toLowerCase();return s.has(c)?!1:(s.add(c),c!==e.toLowerCase())}).join(",");try{const a=await this.client("/update.js",{body:{discount:i,sections:this.sectionsToFetch,sections_url:window.location.pathname}});await this._handleCartChange(a),a.sections&&this._handleCartSectionsChange(a.sections)}catch(a){throw this._handleCartError(a),a}}async removeItemFromCart(e){const t=await this.client("/update.js",{body:{updates:e.reduce((s,i)=>(s[i]=0,s),{}),sections:this.sectionsToFetch,sections_url:window.location.pathname}});await this._handleCartChange(t),t.sections&&this._handleCartSectionsChange(t.sections)}async changeItemToCart(e){const t=await this.client("/change.js",{body:{...e,sections:this.sectionsToFetch,sections_url:window.location.pathname}});await this._handleCartChange(),t.sections&&this._handleCartSectionsChange(t.sections)}async clearCart(){const e=await this.client("/clear.js",{body:{sections:this.sectionsToFetch,sections_url:window.location.pathname}});await this._handleCartChange(e),e.sections&&this._handleCartSectionsChange(e.sections)}async getCart(e=!1){var s;if(this.loadingCartPromise)return this.loadingCartPromise.promise;this.loadingCartPromise=g();const t=await this.client(".js",{method:"GET"});return(s=this.loadingCartPromise)==null||s.resolve(t),this.loadingCartPromise=null,e&&await this.getCartSections(),t}async _handleCartChange(e){this.$emit("cart:updated",e??await this.getCart())}async _handleCartAdd(e,t){this.$emit("cart:items-added",{items:e,target:t})}async getCartSections(){const t=await(await fetch(`${window.location.pathname}?sections=${this.sectionsToFetch}`)).json();return this._handleCartSectionsChange(t),t}_handleCartSectionsChange(e){e&&this.$emit("cart:updated-sections",e)}_handleCartError(e){this.$emit("cart:error",e),this.shopifyCartError=e}getCartLinesForVariant(e){return this.cart.items.filter(t=>t.variant_id===e)}openCartDrawer(){this.$emit("modal-dialog::open",S)}}return d([p({context:w,subscribe:!0})],r.prototype,"cart"),d([p({context:y,subscribe:!0})],r.prototype,"cartSections"),d([_()],r.prototype,"shopifyCartLoading"),d([_()],r.prototype,"shopifyCartError"),r};export{I as W};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.