import{morph}from"@theme/morph";import{Component}from"@theme/component";import{ThemeEvents}from"@theme/events";import{DialogComponent,DialogCloseEvent}from"@theme/dialog";import{mediaQueryLarge,isMobileBreakpoint}from"@theme/utilities";export class QuickAddComponent extends Component{#abortController=null;#cachedProductHtml=null;get cachedProductHtml(){return this.#cachedProductHtml}get productPageUrl(){return this.closest("product-card")?.querySelector('a[ref="productCardLink"]')?.href}connectedCallback(){super.connectedCallback(),mediaQueryLarge.addEventListener("change",this.#closeQuickAddModal)}disconnectedCallback(){super.disconnectedCallback(),mediaQueryLarge.removeEventListener("change",this.#closeQuickAddModal)}handleClick=async event=>{if(event.preventDefault(),this.#cachedProductHtml||await this.fetchProductPage(this.productPageUrl),this.#cachedProductHtml){const freshHtmlCopy=new DOMParser().parseFromString(this.#cachedProductHtml.documentElement.outerHTML,"text/html");await this.updateQuickAddModal(freshHtmlCopy)}this.#openQuickAddModal()};#stayVisibleUntilDialogCloses(dialogComponent){this.toggleAttribute("stay-visible",!0),dialogComponent.addEventListener(DialogCloseEvent.eventName,()=>this.toggleAttribute("stay-visible",!1),{once:!0})}#openQuickAddModal=()=>{const dialogComponent=document.getElementById("quick-add-dialog");dialogComponent instanceof QuickAddDialog&&(this.#stayVisibleUntilDialogCloses(dialogComponent),dialogComponent.showDialog())};#closeQuickAddModal=()=>{const dialogComponent=document.getElementById("quick-add-dialog");dialogComponent instanceof QuickAddDialog&&dialogComponent.closeDialog()};async fetchProductPage(productPageUrl){if(productPageUrl){this.#abortController?.abort(),this.#abortController=new AbortController;try{const response=await fetch(productPageUrl,{signal:this.#abortController.signal});if(!response.ok)throw new Error(`Failed to fetch product page: HTTP error ${response.status}`);const responseText=await response.text(),html=new DOMParser().parseFromString(responseText,"text/html");this.#cachedProductHtml=html}catch(error){if(error.name==="AbortError")return;throw error}finally{this.#abortController=null}}}async updateQuickAddModal(newHtml){const productGrid=newHtml.querySelector("[data-product-grid-content]"),modalContent=document.getElementById("quick-add-modal-content");if(!(!productGrid||!modalContent)){if(isMobileBreakpoint()){const productDetails=productGrid.querySelector(".product-details");if(!productDetails)return;const productFormComponent=productGrid.querySelector("product-form-component"),variantPicker=productGrid.querySelector("variant-picker"),productPrice=productGrid.querySelector("product-price"),productTitle=document.createElement("a");if(productTitle.textContent=this.dataset.productTitle||"",productTitle.href=this.productPageUrl,!productFormComponent||!variantPicker||!productPrice||!productTitle)return;const productHeader=document.createElement("div");productHeader.classList.add("product-header"),productHeader.appendChild(productTitle),productHeader.appendChild(productPrice),productGrid.appendChild(productHeader),productGrid.appendChild(variantPicker),productGrid.appendChild(productFormComponent),productDetails.remove()}morph(modalContent,productGrid)}}}customElements.get("quick-add-component")||customElements.define("quick-add-component",QuickAddComponent);class QuickAddDialog extends DialogComponent{#abortController=new AbortController;connectedCallback(){super.connectedCallback(),this.addEventListener(ThemeEvents.cartUpdate,this.handleCartUpdate,{signal:this.#abortController.signal}),this.addEventListener(ThemeEvents.variantUpdate,this.#updateProductTitleLink)}disconnectedCallback(){super.disconnectedCallback(),this.#abortController.abort()}handleCartUpdate=event=>{event.detail.data.didError||this.closeDialog()};#updateProductTitleLink=event=>{const anchorElement=event.detail.data.html?.querySelector(".view-product-title a"),viewMoreDetailsLink=this.querySelector(".view-product-title a"),mobileProductTitle=this.querySelector(".product-header a");anchorElement&&(viewMoreDetailsLink&&(viewMoreDetailsLink.href=anchorElement.href),mobileProductTitle&&(mobileProductTitle.href=anchorElement.href))}}customElements.get("quick-add-dialog")||customElements.define("quick-add-dialog",QuickAddDialog);
//# sourceMappingURL=/s/files/1/0677/1399/0759/t/1/assets/quick-add.js.map?v=1783128617
