/*! Copyright (c) Safe As Milk. All rights reserved. */import Cart from"cart-store";class ProductForm extends HTMLElement{#boundSubmit;#eventAttached;static#formsBeingProcessed=[];constructor(){super(),this.#boundSubmit=this.submit.bind(this)}connectedCallback(){this.#eventAttached=!1,setTimeout(()=>{this.form=this.querySelector("form"),this.idInput=this.form.querySelector('input[name="id"]'),this.submitButton=this.form.elements.add,this.stagedAction=this.querySelector("staged-action"),this.idInput.removeAttribute("disabled"),this.notifyOnAdd=this.hasAttribute("notify-on-add"),this.redirectOnAdd=this.getAttribute("redirect-on-add"),this.#eventAttached=!0,this.form.addEventListener("submit",this.#boundSubmit)})}disconnectedCallback(){this.#eventAttached&&(this.#eventAttached=!1,this.form.removeEventListener("submit",this.#boundSubmit))}async submit(e){e.preventDefault();const data=new FormData(e.target),item=Object.fromEntries(data.entries());try{this.removeError(),this.redirectOnAdd&&!ProductForm.#formsBeingProcessed.includes(this.form.id)&&ProductForm.#formsBeingProcessed.push(this.form.id),this.stagedAction&&await this.stagedAction.setState("DOING"),await Cart.add(item),this.redirectOnAdd&&(ProductForm.#formsBeingProcessed=ProductForm.#formsBeingProcessed.filter(id=>id!==this.form.id),ProductForm.#formsBeingProcessed.length===0&&(window.location.href=this.redirectOnAdd)),this.stagedAction&&await this.stagedAction.setState("DONE");const quickShop=this.closest("quick-shop");if(quickShop&&await quickShop.close(),this.notifyOnAdd){if(await Cart.resetVariantsBeingAdded(),!this.closest(".modal--cart")){const cart=document.querySelector(".modal--cart, .popup--cart-notification");cart&&!cart.shown?cart.open():cart&&cart.interval&&(cart.interval.reset(),cart.interval.start())}}else await Cart.resetLatestAddedProduct(),await Cart.resetVariantsBeingAdded(item.id)}catch(error){this.#renderError(error.message),this.stagedAction&&await this.stagedAction.setState("IDLE"),this.redirectOnAdd&&(ProductForm.#formsBeingProcessed=ProductForm.#formsBeingProcessed.filter(id=>id!==this.form.id))}}removeError(){const error=this.querySelector(".errors");error&&error.remove()}#renderError(message){if(this.removeError(),this.submitButton){const error=document.createElement("div");error.classList.add("errors","qty-error","u-small"),error.innerHTML=message,this.submitButton.before(error)}}}customElements.define("product-form",ProductForm); //# sourceMappingURL=/cdn/shop/t/100/assets/product-form.js.map?v=37779107406454908591751575324