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 CartItems extends HTMLElement{constructor(){super(),this.lineItemStatusElement=document.getElementById("shopping-cart-line-item-status"),this.currentItemCount=Array.from(this.querySelectorAll('[name="updates[]"]')).reduce((total,quantityInput)=>total+parseInt(quantityInput.value),0),this.debouncedOnChange=debounce(event=>this.onChange(event),300),this.addEventListener("change",this.debouncedOnChange.bind(this)),this.addEventListener("change",this.onChangeQuantity.bind(this))}onChangeQuantity(event){const quantity=event.target.value,line=event.target.dataset.index;this.updateLineItemQuantity(line,quantity)}onChange(event){this.updateQuantity(event.target.dataset.index,event.target.value,document.activeElement.getAttribute("name")).then(()=>window.dispatchEvent(new CustomEvent("cart:updated")))}getSectionsToRender(){return[{id:"main-cart",section:document.getElementById("main-cart").dataset.id,selector:".s-cart"},{id:"header",section:"header",selector:".s-header__buttons"},{id:"cart-live-region-text",section:"cart-live-region-text",selector:".shopify-section"}]}reRenderSection(parsedState,section){const elementToReplace=document.getElementById(section.id).querySelector(section.selector)||document.getElementById(section.id);if(!elementToReplace||!parsedState){console.error(`ERROR Updating cart side sections. Section: ${section.id}`);return}elementToReplace.innerHTML=this.getSectionInnerHTML(parsedState.sections[section.section],section.selector)}updateLineItemQuantity(line,quantity){const cartItemCountElement=document.querySelector(`#CartItem-${line} .count__value`);cartItemCountElement&&(cartItemCountElement.textContent=quantity)}updateQuantity(line,quantity,productName){const body=JSON.stringify({line,quantity,sections:this.getSectionsToRender().map(section=>section.section),sections_url:window.location.pathname});return this.enableLoading(line),fetch(`${routes.cart_change_url}`,{...fetchConfig(),body}).then(response=>{if(response.ok)return response.text();throw Error()}).then(state=>this.handleSuccessUpdate(line,state,productName)).catch(()=>this.handleErrorUpdate())}handleSuccessUpdate(line,state,productName){const parsedState=JSON.parse(state);this.classList.toggle("is-empty",parsedState.item_count===0),this.getSectionsToRender().forEach(section=>this.reRenderSection(parsedState,section)),document.dispatchEvent(new Event("cart-list-updated")),this.updateLiveRegions(line,parsedState.item_count);const lineItem=document.getElementById(`CartItem-${line}`);lineItem&&lineItem.querySelector(`[name="${productName}"]`)&&lineItem.querySelector(`[name="${productName}"]`).focus()}handleErrorUpdate(){const cartSectionName=document.getElementById("main-cart")?.dataset.id;fetch(`${window.Shopify.routes.root}?section_id=${cartSectionName}`).then(res=>res.text()).then(state=>{this.reRenderSection({sections:{[cartSectionName]:state}},this.getSectionsToRender()[0]),document.dispatchEvent(new Event("cart-list-updated"));const errors=document.getElementById("cart-errors");errors.textContent=window.cartStrings.error,errors.classList.remove("d-none")})}updateLiveRegions(line,itemCount){if(this.currentItemCount===itemCount){const lineItemError=document.getElementById(`Line-item-error-${line}`),quantityElement=document.getElementById(`Quantity-${line}`);lineItemError.querySelector(".cart-item__error-text").innerHTML=window.cartStrings.quantityError.replace("[quantity]",quantityElement.value)}this.currentItemCount=itemCount,this.lineItemStatusElement&&this.lineItemStatusElement.setAttribute("aria-hidden",!0);const cartStatus=document.getElementById("cart-live-region-text");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){const cartItemSpinnerElements=this.querySelectorAll(`#CartItem-${line} [data-spinner]`),cartItemButtons=this.querySelectorAll(".quantity__button");[...cartItemSpinnerElements].forEach(overlay=>overlay.hidden=!1),[...cartItemButtons].forEach(button=>button.disabled=!0),document.activeElement.blur(),this.lineItemStatusElement&&this.lineItemStatusElement.setAttribute("aria-hidden",!1)}}customElements.define("cart-items",CartItems),customElements.get("cart-note")||customElements.define("cart-note",class extends HTMLElement{constructor(){super(),this.addEventListener("change",debounce(event=>{const body=JSON.stringify({note:event.target.value});fetch(`${routes.cart_update_url}`,{...fetchConfig(),body})},300))}});
//# sourceMappingURL=/cdn/shop/t/89/assets/cart.js.map?v=112290093285020791351739533048