class CartRemoveButton extends HTMLElement{constructor(){super(),this.addEventListener("click",event=>{event.preventDefault(),(this.closest("cart-items")||this.closest("cart-drawer-items")).updateQuantity(this.dataset.index,0)})}}customElements.define("cart-remove-button",CartRemoveButton);class CartSelectVariant extends HTMLElement{constructor(){super(),this.select=this.querySelector("select"),this.select.addEventListener("change",this.onSelectChange.bind(this.select))}onSelectChange(event){event.preventDefault(),event.stopPropagation();const currentEl=event.target,cartItems=currentEl.closest("cart-items");let message=window.cartStrings.error;var line=currentEl.getAttribute("data-index"),len=$("cart-items .cart-item").length,updatesObj={},changeItemId=parseInt($(this).find("option:selected").val()),oldItemId=currentEl.getAttribute("data-oldid"),tempId=0,tempQty=0,tempKey,templineinex0=parseInt(line)-1;if(cartItems.enableLoading(line),updatesObj[oldItemId]=0,$(`cart-items .cart-item[data-idth="${changeItemId}"]`).length)for(var i=len-1;i>=0;i--)parseInt($("cart-items .cart-item").eq(i).attr("data-idth"))==changeItemId?(tempQty=parseInt($(this).parents(".cart-item").find('input[name="updates[]"]').val())+parseInt($(`cart-items .cart-item[data-idth="${changeItemId}"]`).attr("data-qty")),window.limit_item[changeItemId]!=null?updatesObj[changeItemId]=window.limit_item[changeItemId]:updatesObj[changeItemId]=tempQty):(tempId=parseInt($("cart-items .cart-item").eq(i).find(".item-variants-select option:selected").val())||parseInt($("cart-items .cart-item").eq(i).attr("data-idth")),tempQty=parseInt($("cart-items .cart-item").eq(i).find("input[name='updates[]']")),changeItemId!=tempId&&(window.limit_item[tempId]!=null?updatesObj[tempId]=window.limit_item[tempId]:updatesObj[tempId]=tempQty));else for(var i=len-1;i>=0;i--)tempId=parseInt($("cart-items .cart-item").eq(i).find(".item-variants-select option:selected").val())||parseInt($("cart-items .cart-item").eq(i).attr("data-idth")),tempQty=parseInt($("cart-items .cart-item").eq(i).find("input[name='updates[]']").val()),window.limit_item[tempId]!=null?updatesObj[tempId]=window.limit_item[tempId]:updatesObj[tempId]=tempQty,templineinex0==i&&(tempKey=$("cart-items .cart-item").eq(i).attr("key"),updatesObj[tempKey]=0);fetch(`${routes.cart_update_url}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({updates:updatesObj})}).then(response=>{if(response.ok)cartItems.onCartUpdate(),cartItems.disableLoading(line),window.cart_proxy.isActive=!0;else throw response.status===422&&console.log("Inventory shortage!"),new Error("failed")}).catch(error=>{console.error("Error:",error)})}}customElements.define("select-variant",CartSelectVariant);class CartItems extends HTMLElement{constructor(){super(),this.lineItemStatusElement=document.getElementById("shopping-cart-line-item-status")||document.getElementById("CartDrawer-LineItemStatus");const debouncedOnChange=debounce(event=>{this.onChange(event)},ON_CHANGE_DEBOUNCE_TIMER);this.addEventListener("change",debouncedOnChange.bind(this))}cartUpdateUnsubscriber=void 0;connectedCallback(){this.cartUpdateUnsubscriber=subscribe(PUB_SUB_EVENTS.cartUpdate,event=>{event.source!=="cart-items"&&this.onCartUpdate()})}disconnectedCallback(){this.cartUpdateUnsubscriber&&this.cartUpdateUnsubscriber()}resetQuantityInput(id){const input=this.querySelector(`#Quantity-${id}`);input.value=input.getAttribute("value"),this.isEnterPressed=!1}setValidity(event,index,message){event.target.setCustomValidity(message),event.target.reportValidity(),this.resetQuantityInput(index),event.target.select()}validateQuantity(event){const inputValue=parseInt(event.target.value),index=event.target.dataset.index;let message="";inputValueparseInt(event.target.max)?message=window.quickOrderListStrings.max_error.replace("[max]",event.target.max):inputValue%parseInt(event.target.step)!==0&&(message=window.quickOrderListStrings.step_error.replace("[step]",event.target.step)),message?this.setValidity(event,index,message):(event.target.setCustomValidity(""),event.target.reportValidity(),this.updateQuantity(index,inputValue,document.activeElement.getAttribute("name"),event.target.dataset.quantityVariantId))}onChange(event){event.target.hasAttribute("custom-variants-selector")?console.log("is select variant"):this.validateQuantity(event)}onCartUpdate(){this.tagName==="CART-DRAWER-ITEMS"?fetch(`${routes.cart_url}?section_id=cart-drawer`).then(response=>response.text()).then(responseText=>{const html=new DOMParser().parseFromString(responseText,"text/html"),selectors=[".cart-count-bubble",".drawer__header",".sider-cart-top-count",".drawer__inner-empty","cart-drawer-items",".cart-drawer__footer"];for(const selector of selectors){const targetElement=document.querySelector(selector),sourceElement=html.querySelector(selector);targetElement&&sourceElement&&targetElement.replaceWith(sourceElement)}}).catch(e=>{console.error(e)}):fetch(`${routes.cart_url}`).then(response=>response.text()).then(responseText=>{const html=new DOMParser().parseFromString(responseText,"text/html"),selectors=[".cart-count-bubble",".cart-items-js-contents",".subtotal-content"];for(const selector of selectors){const targetElement=document.querySelector(selector),sourceElement=html.querySelector(selector);targetElement&&sourceElement&&targetElement.replaceWith(sourceElement)}}).catch(e=>{console.error(e)}),window.cart_proxy.isActive=!1}getSectionsToRender(){return[{id:"main-cart-items",section:document.getElementById("main-cart-items").dataset.id,selector:".js-contents"},{id:"cart-icon-bubble",section:"cart-icon-bubble",selector:".shopify-section"},{id:"cart-live-region-text",section:"cart-live-region-text",selector:".shopify-section"},{id:"main-cart-footer",section:document.getElementById("main-cart-footer").dataset.id,selector:".js-contents"},{id:"cart-empty",section:document.getElementById("cart-empty").dataset.id,selector:".js-contents"}]}updateQuantity(line,quantity,name,variantId){this.enableLoading(line);const body=JSON.stringify({line,quantity,sections:this.getSectionsToRender().map(section=>section.section),sections_url:window.location.pathname});fetch(`${window.routes.cart_change_url}`,{...fetchConfig(),body}).then(response=>response.text()).then(state=>{const parsedState=JSON.parse(state),quantityElement=document.getElementById(`Quantity-${line}`)||document.getElementById(`Drawer-quantity-${line}`),items=document.querySelectorAll(".cart-item");if(parsedState.errors){quantityElement.value=quantityElement.getAttribute("value"),this.updateLiveRegions(line,parsedState.errors);return}this.classList.toggle("is-empty",parsedState.item_count===0);const cartDrawerWrapper=document.querySelector("cart-drawer"),cartitemlist=document.getElementById("main-cart-items"),cartFooter=document.getElementById("main-cart-footer"),CartEmptySection=document.getElementById("cart-empty"),cartbestsellerSection=document.getElementById("cart-bestseller");cartitemlist&&cartitemlist.classList.toggle("is-empty",parsedState.item_count===0),cartFooter&&cartFooter.classList.toggle("is-empty",parsedState.item_count===0),cartDrawerWrapper&&cartDrawerWrapper.classList.toggle("is-empty",parsedState.item_count===0),CartEmptySection&&CartEmptySection.classList.toggle("hidden",parsedState.item_count!=0),cartbestsellerSection&&cartbestsellerSection.classList.toggle("is-empty",parsedState.item_count===0),this.getSectionsToRender().forEach(section=>{const elementToReplace=document.getElementById(section.id).querySelector(section.selector)||document.getElementById(section.id);elementToReplace.innerHTML=this.getSectionInnerHTML(parsedState.sections[section.section],section.selector)}),window.location.href.indexOf("/cart")>0&&fetch(`${routes.cart_url}`).then(response=>response.text()).then(responseText=>{const html=new DOMParser().parseFromString(responseText,"text/html"),selectors=[".cart-count-bubble",".cart-items-js-contents",".subtotal-content","#cart-empty .js-contents"];for(const selector of selectors){const targetElement=document.querySelector(selector),sourceElement=html.querySelector(selector);targetElement&&sourceElement&&targetElement.replaceWith(sourceElement)}}).catch(e=>{console.error(e),window.location.reload()});const updatedValue=parsedState.items[line-1]?parsedState.items[line-1].quantity:void 0;let message="";items.length===parsedState.items.length&&updatedValue!==parseInt(quantityElement.value)&&(typeof updatedValue>"u"?message=window.cartStrings.error:message=window.cartStrings.quantityError.replace("[quantity]",updatedValue)),this.updateLiveRegions(line,message);const lineItem=document.getElementById(`CartItem-${line}`)||document.getElementById(`CartDrawer-Item-${line}`);lineItem&&lineItem.querySelector(`[name="${name}"]`)?cartDrawerWrapper?trapFocus(cartDrawerWrapper,lineItem.querySelector(`[name="${name}"]`)):lineItem.querySelector(`[name="${name}"]`).focus():parsedState.item_count===0&&cartDrawerWrapper?trapFocus(cartDrawerWrapper.querySelector(".drawer__inner-empty"),cartDrawerWrapper.querySelector("a")):document.querySelector(".cart-item")&&cartDrawerWrapper&&trapFocus(cartDrawerWrapper,document.querySelector(".cart-item__name")),publish(PUB_SUB_EVENTS.cartUpdate,{source:"cart-items",cartData:parsedState,variantId})}).catch(()=>{this.querySelectorAll(".loading__spinner").forEach(overlay=>overlay.classList.add("hidden"));const errors=document.getElementById("cart-errors")||document.getElementById("CartDrawer-CartErrors");errors.textContent=window.cartStrings.error}).finally(()=>{this.disableLoading(line),window.cart_proxy.isActive=!0})}updateLiveRegions(line,message){const lineItemError=document.querySelector("cart-drawer")?.classList.contains("active")?document.getElementById(`CartDrawer-LineItemError-${line}`):document.getElementById(`Line-item-error-${line}`);lineItemError&&(lineItemError.querySelector(".cart-item__error-text").innerHTML=message),fetch(`${routes.cart_url}`).then(response=>response.text()).then(responseText=>{const html=new DOMParser().parseFromString(responseText,"text/html"),selectors=[".cart-count-bubble",".cart-items-js-contents",".subtotal-content","cart-drawer-items",".cart-drawer__footer",".drawer__heading",".item-count"];for(const selector of selectors){const targetElement=document.querySelector(selector),sourceElement=html.querySelector(selector);targetElement&&sourceElement&&targetElement.replaceWith(sourceElement)}}).catch(e=>{console.error(e)}),this.lineItemStatusElement.setAttribute("aria-hidden",!0);const cartStatus=document.getElementById("cart-live-region-text")||document.getElementById("CartDrawer-LiveRegionText");cartStatus.setAttribute("aria-hidden",!1),setTimeout(()=>{cartStatus.setAttribute("aria-hidden",!0)},1e3)}getSectionInnerHTML(html,selector){return new DOMParser().parseFromString(html,"text/html").querySelector(selector).innerHTML}enableLoading(line){(document.getElementById("main-cart-items")||document.getElementById("CartDrawer-CartItems")).classList.add("cart__items--disabled");const cartItemElements=this.querySelectorAll(`#CartItem-${line} .loading__spinner`),cartDrawerItemElements=this.querySelectorAll(`#CartDrawer-Item-${line} .loading__spinner`);[...cartItemElements,...cartDrawerItemElements].forEach(overlay=>overlay.classList.remove("hidden")),document.activeElement.blur(),this.lineItemStatusElement.setAttribute("aria-hidden",!1)}disableLoading(line){(document.getElementById("main-cart-items")||document.getElementById("CartDrawer-CartItems")).classList.remove("cart__items--disabled");const cartItemElements=this.querySelectorAll(`#CartItem-${line} .loading__spinner`),cartDrawerItemElements=this.querySelectorAll(`#CartDrawer-Item-${line} .loading__spinner`);cartItemElements.forEach(overlay=>overlay.classList.add("hidden")),cartDrawerItemElements.forEach(overlay=>overlay.classList.add("hidden"))}}customElements.define("cart-items",CartItems),customElements.get("cart-note")||customElements.define("cart-note",class extends HTMLElement{constructor(){super(),this.addEventListener("input",debounce(event=>{const body=JSON.stringify({note:event.target.value});fetch(`${routes.cart_update_url}`,{...fetchConfig(),body})},ON_CHANGE_DEBOUNCE_TIMER))}}); //# sourceMappingURL=/cdn/shop/t/106/assets/cart.js.map?v=156882995664044780321755659350