import{Component}from"@theme/component";import{morphSection}from"@theme/section-renderer";import{DiscountUpdateEvent}from"@theme/events";import{fetchConfig}from"@theme/utilities";import{cartPerformance}from"@theme/performance";class CartDiscount extends Component{requiredRefs=["cartDiscountError","cartDiscountErrorDiscountCode","cartDiscountErrorShipping"];#activeFetch=null;#createAbortController(){this.#activeFetch&&this.#activeFetch.abort();const abortController=new AbortController;return this.#activeFetch=abortController,abortController}applyDiscount=async event=>{const{cartDiscountError,cartDiscountErrorDiscountCode,cartDiscountErrorShipping}=this.refs;event.preventDefault(),event.stopPropagation();const form=event.target;if(!(form instanceof HTMLFormElement))return;const discountCode=form.querySelector('input[name="discount"]');if(!(discountCode instanceof HTMLInputElement)||typeof this.dataset.sectionId!="string")return;const discountCodeValue=discountCode.value,abortController=this.#createAbortController();try{const existingDiscounts=this.#existingDiscounts();if(existingDiscounts.includes(discountCodeValue))return;cartDiscountError.classList.add("hidden"),cartDiscountErrorDiscountCode.classList.add("hidden"),cartDiscountErrorShipping.classList.add("hidden");const config=fetchConfig("json",{body:JSON.stringify({discount:[...existingDiscounts,discountCodeValue].join(","),sections:[this.dataset.sectionId]})}),data=await(await fetch(Theme.routes.cart_update_url,{...config,signal:abortController.signal})).json();if(data.discount_codes.find(discount=>discount.code===discountCodeValue&&discount.applicable===!1)){discountCode.value="",this.#handleDiscountError("discount_code");return}const newHtml=data.sections[this.dataset.sectionId],section=new DOMParser().parseFromString(newHtml,"text/html").getElementById(`shopify-section-${this.dataset.sectionId}`),discountCodes=section?.querySelectorAll(".cart-discount__pill")||[];if(section){const codes=Array.from(discountCodes).map(element=>element instanceof HTMLLIElement?element.dataset.discountCode:null).filter(Boolean);if(codes.length===existingDiscounts.length&&codes.every(code=>existingDiscounts.includes(code))&&data.discount_codes.find(discount=>discount.code===discountCodeValue&&discount.applicable===!0)){this.#handleDiscountError("shipping"),discountCode.value="";return}}document.dispatchEvent(new DiscountUpdateEvent(data,this.id)),morphSection(this.dataset.sectionId,newHtml)}catch{}finally{this.#activeFetch=null,cartPerformance.measureFromEvent("discount-update:user-action",event)}};removeDiscount=async event=>{if(event.preventDefault(),event.stopPropagation(),event instanceof KeyboardEvent&&event.key!=="Enter"||!(event instanceof MouseEvent)||!(event.target instanceof HTMLElement)||typeof this.dataset.sectionId!="string")return;const pill=event.target.closest(".cart-discount__pill");if(!(pill instanceof HTMLLIElement))return;const discountCode=pill.dataset.discountCode;if(!discountCode)return;const existingDiscounts=this.#existingDiscounts(),index=existingDiscounts.indexOf(discountCode);if(index===-1)return;existingDiscounts.splice(index,1);const abortController=this.#createAbortController();try{const config=fetchConfig("json",{body:JSON.stringify({discount:existingDiscounts.join(","),sections:[this.dataset.sectionId]})}),data=await(await fetch(Theme.routes.cart_update_url,{...config,signal:abortController.signal})).json();document.dispatchEvent(new DiscountUpdateEvent(data,this.id)),morphSection(this.dataset.sectionId,data.sections[this.dataset.sectionId])}catch{}finally{this.#activeFetch=null}};#handleDiscountError(type){const{cartDiscountError,cartDiscountErrorDiscountCode,cartDiscountErrorShipping}=this.refs,target=type==="discount_code"?cartDiscountErrorDiscountCode:cartDiscountErrorShipping;cartDiscountError.classList.remove("hidden"),target.classList.remove("hidden")}#existingDiscounts(){const discountCodes=[],discountPills=this.querySelectorAll(".cart-discount__pill");for(const pill of discountPills)pill instanceof HTMLLIElement&&typeof pill.dataset.discountCode=="string"&&discountCodes.push(pill.dataset.discountCode);return discountCodes}}customElements.get("cart-discount-component")||customElements.define("cart-discount-component",CartDiscount); //# sourceMappingURL=/cdn/shop/t/47/assets/cart-discount.js.map?v=107077853990124422021757451905