import{Component}from"@theme/component";import{ThemeEvents}from"@theme/events";import{StandardEvents}from"@shopify/events";class PricePerItemComponent extends Component{#priceBreaks=[];#abortController=new AbortController;connectedCallback(){super.connectedCallback(),this.#parsePriceBreaks(),this.#attachEventListeners(),this.#updatePriceDisplay()}disconnectedCallback(){super.disconnectedCallback(),this.#abortController.abort()}#parsePriceBreaks(){const minQuantity=parseInt(this.dataset.minQuantity||"")||1,{variantPrice,priceBreaks:priceBreaksData}=this.dataset;if(variantPrice&&this.#priceBreaks.push({quantity:minQuantity,price:variantPrice}),priceBreaksData){const breaks=JSON.parse(priceBreaksData);for(const{quantity,price}of breaks)quantity&&price&&this.#priceBreaks.push({quantity:parseInt(quantity),price})}this.#priceBreaks.sort((a,b)=>b.quantity-a.quantity)}#attachEventListeners(){const{signal}=this.#abortController;document.addEventListener(ThemeEvents.quantitySelectorUpdate,this.#handleQuantityUpdate,{signal}),document.addEventListener(StandardEvents.cartLinesUpdate,this.#handleCartUpdate,{signal})}#handleQuantityUpdate=event=>{const form=this.closest("product-form-component");!form||!(event.target instanceof Node)||!form.contains(event.target)||this.#updatePriceDisplay()};#handleCartUpdate=event=>{event.promise?.then(()=>this.#updatePriceDisplay()).catch(error=>{error?.name!=="AbortError"&&console.warn("[price-per-item] Cart update promise rejected:",error)})};#getCurrentQuantity(){const quantityInput=this.closest("product-form-component")?.querySelector('input[name="quantity"]');if(!quantityInput)return 1;const cartQty=parseInt(quantityInput.getAttribute("data-cart-quantity")||"0")||0,inputQty=parseInt(quantityInput.value)||1;return cartQty+inputQty}updatePriceDisplay(){if(!this.#priceBreaks.length||!this.refs.pricePerItemText)return;const quantity=this.#getCurrentQuantity(),priceBreak=this.#priceBreaks.find(pb=>quantity>=pb.quantity)??this.#priceBreaks[this.#priceBreaks.length-1];priceBreak&&(this.refs.pricePerItemText.innerHTML=`${this.dataset.atText} ${priceBreak.price}/${this.dataset.eachText}`)}#updatePriceDisplay=()=>{this.updatePriceDisplay()}}customElements.get("price-per-item")||customElements.define("price-per-item",PricePerItemComponent);
//# sourceMappingURL=/s/files/1/0677/1399/0759/t/1/assets/price-per-item.js.map?v=1783078800
