import{Component}from"@theme/component";import{fetchConfig,onAnimationEnd,preloadImage}from"@theme/utilities";import{ThemeEvents,CartAddEvent,CartErrorEvent}from"@theme/events";import{cartPerformance}from"@theme/performance";import{morph}from"@theme/morph";export const ADD_TO_CART_TEXT_ANIMATION_DURATION=2e3;export class AddToCartComponent extends Component{requiredRefs=["addToCartButton"];#animationTimeout;#cleanupTimeout;connectedCallback(){super.connectedCallback(),this.addEventListener("pointerenter",this.#preloadImage)}disconnectedCallback(){super.disconnectedCallback(),this.#animationTimeout&&clearTimeout(this.#animationTimeout),this.#cleanupTimeout&&clearTimeout(this.#cleanupTimeout),this.removeEventListener("pointerenter",this.#preloadImage)}disable(){this.refs.addToCartButton.disabled=!0}enable(){this.refs.addToCartButton.disabled=!1}handleClick(event){if(!this.closest("form")?.checkValidity())return;this.animateAddToCart(),this.dataset.addToCartAnimation==="true"&&!event.target.closest(".quick-add-modal")&&this.#animateFlyToCart()}#preloadImage=()=>{const image=this.dataset.productVariantMedia;image&&preloadImage(image)};#animateFlyToCart(){const{addToCartButton}=this.refs,cartIcon=document.querySelector(".header-actions__cart-icon"),image=this.dataset.productVariantMedia;if(!cartIcon||!addToCartButton||!image)return;const flyToCartElement=document.createElement("fly-to-cart");flyToCartElement.style.setProperty("background-image",`url(${image})`),flyToCartElement.source=addToCartButton,flyToCartElement.destination=cartIcon,document.body.appendChild(flyToCartElement)}animateAddToCart(){const{addToCartButton}=this.refs;this.#animationTimeout&&clearTimeout(this.#animationTimeout),this.#cleanupTimeout&&clearTimeout(this.#cleanupTimeout),addToCartButton.classList.contains("atc-added")||addToCartButton.classList.add("atc-added"),this.#animationTimeout=setTimeout(()=>{this.#cleanupTimeout=setTimeout(()=>{this.refs.addToCartButton.classList.remove("atc-added")},10)},ADD_TO_CART_TEXT_ANIMATION_DURATION)}}customElements.get("add-to-cart-component")||customElements.define("add-to-cart-component",AddToCartComponent);class ProductFormComponent extends Component{requiredRefs=["variantId","liveRegion"];#abortController=new AbortController;#timeout;connectedCallback(){super.connectedCallback();const{signal}=this.#abortController,target=this.closest(".shopify-section, dialog, product-card");target?.addEventListener(ThemeEvents.variantUpdate,this.#onVariantUpdate,{signal}),target?.addEventListener(ThemeEvents.variantSelected,this.#onVariantSelected,{signal})}disconnectedCallback(){super.disconnectedCallback(),this.#abortController.abort()}handleSubmit(event){const{addToCartTextError}=this.refs;if(event.preventDefault(),this.#timeout&&clearTimeout(this.#timeout),this.refs.addToCartButtonContainer?.refs.addToCartButton?.getAttribute("disabled")==="true")return;const form=this.querySelector("form");if(!form)throw new Error("Product form element missing");const formData=new FormData(form),cartItemsComponents=document.querySelectorAll("cart-items-component");let cartItemComponentsSectionIds=[];cartItemsComponents.forEach(item=>{item instanceof HTMLElement&&item.dataset.sectionId&&cartItemComponentsSectionIds.push(item.dataset.sectionId),formData.append("sections",cartItemComponentsSectionIds.join(","))});const fetchCfg=fetchConfig("javascript",{body:formData});fetch(Theme.routes.cart_add_url,{...fetchCfg,headers:{...fetchCfg.headers,Accept:"text/html"}}).then(response=>response.json()).then(response=>{if(response.status){if(this.dispatchEvent(new CartErrorEvent(form.getAttribute("id")||"",response.message,response.description,response.errors)),!addToCartTextError)return;addToCartTextError.classList.remove("hidden");const textNode=addToCartTextError.childNodes[2];if(textNode)textNode.textContent=response.message;else{const newTextNode=document.createTextNode(response.message);addToCartTextError.appendChild(newTextNode)}this.#setLiveRegionText(response.message),this.#timeout=setTimeout(()=>{addToCartTextError&&(addToCartTextError.classList.add("hidden"),this.#clearLiveRegionText())},1e4),this.dispatchEvent(new CartAddEvent({},this.id,{didError:!0,source:"product-form-component",itemCount:Number(formData.get("quantity"))||Number(this.dataset.quantityDefault),productId:this.dataset.productId}));return}else{const id=formData.get("id");if(addToCartTextError&&(addToCartTextError.classList.add("hidden"),addToCartTextError.removeAttribute("aria-live")),!id)throw new Error("Form ID is required");if(this.refs.addToCartButtonContainer?.refs.addToCartButton){const addedText=this.refs.addToCartButtonContainer.refs.addToCartButton.querySelector(".add-to-cart-text--added")?.textContent?.trim()||Theme.translations.added;this.#setLiveRegionText(addedText),setTimeout(()=>{this.#clearLiveRegionText()},5e3)}this.dispatchEvent(new CartAddEvent({},id.toString(),{source:"product-form-component",itemCount:Number(formData.get("quantity"))||Number(this.dataset.quantityDefault),productId:this.dataset.productId,sections:response.sections}))}}).catch(error=>{console.error(error)}).finally(()=>{cartPerformance.measureFromEvent("add:user-action",event)})}#setLiveRegionText(text){const liveRegion=this.refs.liveRegion;liveRegion.textContent=text}#clearLiveRegionText(){const liveRegion=this.refs.liveRegion;liveRegion.textContent=""}#onVariantUpdate=event=>{if(event.detail.data.newProduct)this.dataset.productId=event.detail.data.newProduct.id;else if(event.detail.data.productId!==this.dataset.productId)return;const{variantId,addToCartButtonContainer}=this.refs,currentAddToCartButton=addToCartButtonContainer?.refs.addToCartButton,newAddToCartButton=event.detail.data.html.querySelector('[ref="addToCartButton"]');if(currentAddToCartButton&&(event.detail.resource==null||event.detail.resource.available==!1?(addToCartButtonContainer.disable(),this.refs.acceleratedCheckoutButtonContainer?.setAttribute("hidden","true")):(addToCartButtonContainer.enable(),this.refs.acceleratedCheckoutButtonContainer?.removeAttribute("hidden")),newAddToCartButton&&morph(currentAddToCartButton,newAddToCartButton),variantId.value=event.detail.resource.id??"",event.detail.resource)){const productVariantMedia=event.detail.resource.featured_media?.preview_image?.src;productVariantMedia&&addToCartButtonContainer?.setAttribute("data-product-variant-media",productVariantMedia+"&width=100")}};#onVariantSelected=()=>{this.refs.addToCartButtonContainer?.disable()}}customElements.get("product-form-component")||customElements.define("product-form-component",ProductFormComponent);class FlyToCart extends HTMLElement{source;destination;connectedCallback(){this.#animate()}#animate(){const rect=this.getBoundingClientRect(),sourceRect=this.source.getBoundingClientRect(),destinationRect=this.destination.getBoundingClientRect(),offset={x:rect.width/2,y:rect.height/2},startPoint={x:sourceRect.left+sourceRect.width/2-offset.x,y:sourceRect.top+sourceRect.height/2-offset.y},endPoint={x:destinationRect.left+destinationRect.width/2-offset.x,y:destinationRect.top+destinationRect.height/2-offset.y},controlPoint1={x:startPoint.x,y:startPoint.y-200},controlPoint2={x:endPoint.x-300,y:endPoint.y-100};let startTime=null;const duration=600;this.style.opacity="1";const animate=currentTime=>{startTime||(startTime=currentTime);const elapsed=currentTime-startTime,progress=Math.min(elapsed/duration,1),position=bezierPoint(progress,startPoint,controlPoint1,controlPoint2,endPoint);this.style.setProperty("--x",`${position.x}px`),this.style.setProperty("--y",`${position.y}px`);const scale=1-progress*.5;this.style.setProperty("--scale",`${scale}`),progress<1?requestAnimationFrame(animate):(this.style.opacity="0",onAnimationEnd(this,()=>this.remove()))};this.style.setProperty("--x",`${startPoint.x}px`),this.style.setProperty("--y",`${startPoint.y}px`),requestAnimationFrame(animate)}}function bezierPoint(t,p0,p1,p2,p3){const cX=3*(p1.x-p0.x),bX=3*(p2.x-p1.x)-cX,aX=p3.x-p0.x-cX-bX,cY=3*(p1.y-p0.y),bY=3*(p2.y-p1.y)-cY,aY=p3.y-p0.y-cY-bY,x=aX*Math.pow(t,3)+bX*Math.pow(t,2)+cX*t+p0.x,y=aY*Math.pow(t,3)+bY*Math.pow(t,2)+cY*t+p0.y;return{x,y}}customElements.get("fly-to-cart")||customElements.define("fly-to-cart",FlyToCart); //# sourceMappingURL=/cdn/shop/t/47/assets/product-form.js.map?v=127515301325557712001757451905