customElements.get("quick-add-bulk")||customElements.define("quick-add-bulk",class extends HTMLElement{constructor(){super(),this.quantity=this.querySelector("quantity-input");const debouncedOnChange=debounce(event=>{parseInt(event.target.dataset.cartQuantity)===0?this.addToCart(event):this.updateCart(event)},ON_CHANGE_DEBOUNCE_TIMER);this.addEventListener("change",debouncedOnChange.bind(this)),this.listenForActiveInput(),this.listenForKeydown(),this.lastActiveInputId=null;const pageParams=new URLSearchParams(window.location.search);window.pageNumber=decodeURIComponent(pageParams.get("page")||"")}connectedCallback(){this.cartUpdateUnsubscriber=subscribe(PUB_SUB_EVENTS.cartUpdate,event=>{event.source!=="quick-add"&&this.onCartUpdate().then(()=>{this.listenForActiveInput(),this.listenForKeydown()})})}disconnectedCallback(){this.cartUpdateUnsubscriber&&this.cartUpdateUnsubscriber()}getInput(){return this.querySelector("quantity-input input")}selectProgressBar(){return this.querySelector(".progress-bar-container")}listenForActiveInput(){this.classList.contains("hidden")||this.getInput().addEventListener("focusin",event=>event.target.select()),this.isEnterPressed=!1}listenForKeydown(){this.getInput().addEventListener("keydown",event=>{event.key==="Enter"&&(this.getInput().blur(),this.isEnterPressed=!0)})}resetQuantityInput(id){const input=document.getElementById(id);input.value=input.getAttribute("value"),this.isEnterPressed=!1}cleanErrorMessageOnType(event){event.target.addEventListener("keypress",()=>{event.target.setCustomValidity("")},{once:!0})}onCartUpdate(){return new Promise((resolve,reject)=>{fetch(`${this.getSectionsUrl()}?section_id=${this.closest(".collection").dataset.id}`).then(response=>response.text()).then(responseText=>{const sourceQty=new DOMParser().parseFromString(responseText,"text/html").querySelector(`#quick-add-bulk-${this.dataset.id}-${this.closest(".collection").dataset.id}`);sourceQty&&(this.innerHTML=sourceQty.innerHTML),resolve()}).catch(e=>{console.error(e),reject(e)})})}updateCart(event){this.lastActiveInputId=event.target.getAttribute("data-index"),this.quantity.classList.add("quantity__input-disabled"),this.selectProgressBar().classList.remove("hidden");const body=JSON.stringify({quantity:event.target.value,id:event.target.getAttribute("data-index"),sections:this.getSectionsToRender().map(section=>section.section),sections_url:this.getSectionsUrl()});fetch(`${routes.cart_change_url}`,{...fetchConfig("javascript"),body}).then(response=>response.text()).then(state=>{const parsedState=JSON.parse(state);if(this.quantity.classList.remove("quantity__input-disabled"),parsedState.description||parsedState.errors){event.target.setCustomValidity(parsedState.description),event.target.reportValidity(),this.resetQuantityInput(event.target.id),this.selectProgressBar().classList.add("hidden"),event.target.select(),this.cleanErrorMessageOnType(event);return}this.renderSections(parsedState),publish(PUB_SUB_EVENTS.cartUpdate,{source:"quick-add",cartData:parsedState})}).catch(error=>{console.log(error,"error")})}addToCart(event){this.quantity.classList.add("quantity__input-disabled"),this.selectProgressBar().classList.remove("hidden"),this.lastActiveInputId=event.target.getAttribute("data-index");const body=JSON.stringify({items:[{quantity:parseInt(event.target.value),id:parseInt(this.dataset.id)}],sections:this.getSectionsToRender().map(section=>section.section)});fetch(`${routes.cart_add_url}`,{...fetchConfig("javascript"),body}).then(response=>response.text()).then(state=>{const parsedState=JSON.parse(state);if(this.quantity.classList.remove("quantity__input-disabled"),parsedState.description||parsedState.errors){event.target.setCustomValidity(parsedState.description),event.target.reportValidity(),this.resetQuantityInput(event.target.id),this.selectProgressBar().classList.add("hidden"),event.target.select(),this.cleanErrorMessageOnType(event);return}this.renderSections(parsedState),publish(PUB_SUB_EVENTS.cartUpdate,{source:"quick-add",cartData:parsedState})}).catch(error=>{console.error(error)})}getSectionsToRender(){return[{id:`quick-add-bulk-${this.dataset.id}-${this.closest(".collection-quick-add-bulk").dataset.id}`,section:this.closest(".collection-quick-add-bulk").dataset.id,selector:`#quick-add-bulk-${this.dataset.id}-${this.closest(".collection-quick-add-bulk").dataset.id}`},{id:"cart-icon-bubble",section:"cart-icon-bubble",selector:".shopify-section"},{id:"CartDrawer",selector:"#CartDrawer",section:"cart-drawer"}]}getSectionsUrl(){return window.pageNumber?`${window.location.pathname}?page=${window.pageNumber}`:`${window.location.pathname}`}getSectionInnerHTML(html,selector){return new DOMParser().parseFromString(html,"text/html").querySelector(selector).innerHTML}renderSections(parsedState){this.getSectionsToRender().forEach(section=>{const sectionElement=document.getElementById(section.id);sectionElement&§ionElement.parentElement&§ionElement.parentElement.classList.contains("drawer")&&(parsedState.items.length>0?sectionElement.parentElement.classList.remove("is-empty"):sectionElement.parentElement.classList.add("is-empty"),setTimeout(()=>{document.querySelector("#CartDrawer-Overlay").addEventListener("click",this.cart.close.bind(this.cart))}));const elementToReplace=sectionElement&§ionElement.querySelector(section.selector)?sectionElement.querySelector(section.selector):sectionElement;elementToReplace&&(elementToReplace.innerHTML=this.getSectionInnerHTML(parsedState.sections[section.section],section.selector))}),this.isEnterPressed&&this.querySelector(`#Quantity-${this.lastActiveInputId}`).select(),this.listenForActiveInput(),this.listenForKeydown()}}); //# sourceMappingURL=/cdn/shop/t/11/assets/quick-add-bulk.js.map?v=61945936209057739771719931490