import{Component}from"@theme/component";import{fetchConfig,debounce,onAnimationEnd,prefersReducedMotion,resetShimmer}from"@theme/utilities";import{morphSection,sectionRenderer}from"@theme/section-renderer";import{ThemeEvents,CartUpdateEvent,DiscountUpdateEvent}from"@theme/events";import{cartPerformance}from"@theme/performance";class CartItemsComponent extends Component{#debouncedOnChange=debounce(this.#onQuantityChange,300).bind(this);connectedCallback(){super.connectedCallback(),document.addEventListener(ThemeEvents.cartUpdate,this.#handleCartUpdate),document.addEventListener(ThemeEvents.discountUpdate,this.handleDiscountUpdate),document.addEventListener(ThemeEvents.quantitySelectorUpdate,this.#debouncedOnChange)}disconnectedCallback(){super.disconnectedCallback(),document.removeEventListener(ThemeEvents.cartUpdate,this.#handleCartUpdate),document.removeEventListener(ThemeEvents.quantitySelectorUpdate,this.#debouncedOnChange)}#onQuantityChange(event){const{quantity,cartLine:line}=event.detail;if(!line)return;if(quantity===0)return this.onLineItemRemove(line);this.updateQuantity({line,quantity,action:"change"});const lineItemRow=this.refs.cartItemRows[line-1];if(!lineItemRow)return;lineItemRow.querySelector("text-component")?.shimmer()}onLineItemRemove(line){this.updateQuantity({line,quantity:0,action:"clear"});const cartItemRowToRemove=this.refs.cartItemRows[line-1];if(!cartItemRowToRemove)return;const remove=()=>cartItemRowToRemove.remove();if(prefersReducedMotion())return remove();cartItemRowToRemove.style.setProperty("--row-height",`${cartItemRowToRemove.clientHeight}px`),cartItemRowToRemove.classList.add("removing"),onAnimationEnd(cartItemRowToRemove,remove)}updateQuantity(config){const cartPerformaceUpdateMarker=cartPerformance.createStartingMarker(`${config.action}:user-action`);this.#disableCartItems();const{line,quantity}=config,{cartTotal}=this.refs,cartItemsComponents=document.querySelectorAll("cart-items-component"),sectionsToUpdate=new Set([this.sectionId]);cartItemsComponents.forEach(item=>{item instanceof HTMLElement&&item.dataset.sectionId&&sectionsToUpdate.add(item.dataset.sectionId)});const body=JSON.stringify({line,quantity,sections:Array.from(sectionsToUpdate).join(",")});cartTotal?.shimmer(),fetch(`${Theme.routes.cart_change_url}`,fetchConfig("json",{body})).then(response=>response.text()).then(responseText=>{const parsedResponseText=JSON.parse(responseText);if(resetShimmer(this),parsedResponseText.errors){this.#handleCartError(line,parsedResponseText);return}const newCartHiddenItemCount=new DOMParser().parseFromString(parsedResponseText.sections[this.sectionId],"text/html").querySelector('[ref="cartItemCount"]')?.textContent,newCartItemCount=newCartHiddenItemCount?parseInt(newCartHiddenItemCount,10):0;this.dispatchEvent(new CartUpdateEvent({},this.sectionId,{itemCount:newCartItemCount,source:"cart-items-component",sections:parsedResponseText.sections})),morphSection(this.sectionId,parsedResponseText.sections[this.sectionId])}).catch(error=>{console.error(error)}).finally(()=>{this.#enableCartItems(),cartPerformance.measureFromMarker(cartPerformaceUpdateMarker)})}handleDiscountUpdate=event=>{this.#handleCartUpdate(event)};#handleCartError=(line,parsedResponseText)=>{const quantityInput=this.refs.quantitySelectors[line-1]?.querySelector("input");if(!quantityInput)throw new Error("Quantity input not found");quantityInput.value=quantityInput.defaultValue;const cartItemError=this.refs[`cartItemError-${line}`],cartItemErrorContainer=this.refs[`cartItemErrorContainer-${line}`];if(!(cartItemError instanceof HTMLElement))throw new Error("Cart item error not found");if(!(cartItemErrorContainer instanceof HTMLElement))throw new Error("Cart item error container not found");cartItemError.textContent=parsedResponseText.errors,cartItemErrorContainer.classList.remove("hidden")};#handleCartUpdate=event=>{if(event instanceof DiscountUpdateEvent){sectionRenderer.renderSection(this.sectionId,{cache:!1});return}if(event.target===this)return;const cartItemsHtml=event.detail.data.sections?.[this.sectionId];cartItemsHtml?morphSection(this.sectionId,cartItemsHtml):sectionRenderer.renderSection(this.sectionId,{cache:!1})};#disableCartItems(){this.classList.add("cart-items-disabled")}#enableCartItems(){this.classList.remove("cart-items-disabled")}get sectionId(){const{sectionId}=this.dataset;if(!sectionId)throw new Error("Section id missing");return sectionId}}customElements.get("cart-items-component")||customElements.define("cart-items-component",CartItemsComponent);
//# sourceMappingURL=/s/files/1/0677/1399/0759/t/1/assets/component-cart-items.js.map?v=1783128466
