{
  "version": 3,
  "sources": ["/cdn/shop/t/186/assets/quick-add.js"],
  "sourcesContent": ["import { morph } from '@theme/morph';\nimport { Component } from '@theme/component';\nimport { CartUpdateEvent, ThemeEvents } from '@theme/events';\nimport { DialogComponent, DialogCloseEvent } from '@theme/dialog';\nimport { mediaQueryLarge, isMobileBreakpoint } from '@theme/utilities';\nimport { CartService } from './cart-service.js';\n\nexport class QuickAddComponent extends Component {\n  /** @type {AbortController | null} */\n  #abortController = null;\n  /** @type {Document | null} */\n  #cachedProductHtml = null;\n\n  get cachedProductHtml() {\n    return this.#cachedProductHtml;\n  }\n\n  get productPageUrl() {\n    return /** @type {HTMLAnchorElement} */ (this.closest('product-card')?.querySelector('a[ref=\"productCardLink\"]'))\n      ?.href;\n  }\n\n  connectedCallback() {\n    super.connectedCallback();\n\n    mediaQueryLarge.addEventListener('change', this.#closeQuickAddModal);\n  }\n\n  disconnectedCallback() {\n    super.disconnectedCallback();\n\n    mediaQueryLarge.removeEventListener('change', this.#closeQuickAddModal);\n  }\n\n  /**\n   * Handles quick add button click\n   * @param {Event} event - The click event\n   */\n  handleClick = async (event) => {\n    event.preventDefault();\n\n    if (!this.#cachedProductHtml) {\n      await this.fetchProductPage(this.productPageUrl);\n    }\n\n    if (this.#cachedProductHtml) {\n      // Create a fresh copy of the cached HTML to avoid modifying the original\n      const freshHtmlCopy = new DOMParser().parseFromString(\n        this.#cachedProductHtml.documentElement.outerHTML,\n        'text/html'\n      );\n\n      await this.updateQuickAddModal(freshHtmlCopy);\n    }\n\n    this.#openQuickAddModal();\n  };\n\n  /** @param {QuickAddDialog} dialogComponent */\n  #stayVisibleUntilDialogCloses(dialogComponent) {\n    this.toggleAttribute('stay-visible', true);\n\n    dialogComponent.addEventListener(DialogCloseEvent.eventName, () => this.toggleAttribute('stay-visible', false), {\n      once: true,\n    });\n  }\n\n  #openQuickAddModal = () => {\n    const dialogComponent = document.getElementById('quick-add-dialog');\n    if (!(dialogComponent instanceof QuickAddDialog)) return;\n\n    this.#stayVisibleUntilDialogCloses(dialogComponent);\n\n    dialogComponent.showDialog();\n  };\n\n  #closeQuickAddModal = () => {\n    const dialogComponent = document.getElementById('quick-add-dialog');\n    if (!(dialogComponent instanceof QuickAddDialog)) return;\n\n    dialogComponent.closeDialog();\n  };\n\n  /**\n   * Fetches the product page content\n   * @param {string} productPageUrl - The URL of the product page to fetch\n   * @returns {Promise<void>}\n   * @throws {Error} If the fetch request fails or returns a non-200 response\n   */\n  async fetchProductPage(productPageUrl) {\n    if (!productPageUrl) return;\n\n    // We use this to abort the previous fetch request if it's still pending.\n    this.#abortController?.abort();\n    this.#abortController = new AbortController();\n\n    try {\n      const response = await fetch(productPageUrl, {\n        signal: this.#abortController.signal,\n      });\n\n      if (!response.ok) {\n        throw new Error(`Failed to fetch product page: HTTP error ${response.status}`);\n      }\n\n      const responseText = await response.text();\n      const html = new DOMParser().parseFromString(responseText, 'text/html');\n\n      // Store the HTML for later use\n      this.#cachedProductHtml = html;\n    } catch (error) {\n      if (error.name === 'AbortError') {\n        return;\n      } else {\n        throw error;\n      }\n    } finally {\n      this.#abortController = null;\n    }\n  }\n\n  /**\n   * Re-renders the variant picker.\n   * @param {Document} newHtml - The new HTML.\n   */\n  async updateQuickAddModal(newHtml) {\n    const productGrid = newHtml.querySelector('[data-product-grid-content]');\n    const modalContent = document.getElementById('quick-add-modal-content');\n\n    if (!productGrid || !modalContent) return;\n\n    if (isMobileBreakpoint()) {\n      const productDetails = productGrid.querySelector('.product-details');\n      if (!productDetails) return;\n      const productFormComponent = productGrid.querySelector('product-form-component');\n      const variantPicker = productGrid.querySelector('variant-picker');\n      const productPrice = productGrid.querySelector('product-price');\n      const promotionCallout = productGrid.querySelector('.product-promotion-callout');\n      const productTitle = document.createElement('a');\n      productTitle.textContent = this.dataset.productTitle || '';\n\n      // Make product title as a link to the product page\n      productTitle.href = this.productPageUrl;\n\n      if (!productFormComponent || !variantPicker || !productPrice || !productTitle) return;\n\n      const productHeader = document.createElement('div');\n      productHeader.classList.add('product-header');\n\n      productHeader.appendChild(productTitle);\n      productHeader.appendChild(productPrice);\n      // Add promotion callout after price if it exists\n      if (promotionCallout) {\n        productHeader.appendChild(promotionCallout);\n      }\n      productGrid.appendChild(productHeader);\n      productGrid.appendChild(variantPicker);\n      productGrid.appendChild(productFormComponent);\n      productDetails.remove();\n    }\n\n    morph(modalContent, productGrid);\n  }\n}\n\nif (!customElements.get('quick-add-component')) {\n  customElements.define('quick-add-component', QuickAddComponent);\n}\n\nclass QuickAddDialog extends DialogComponent {\n  #abortController = new AbortController();\n\n  connectedCallback() {\n    super.connectedCallback();\n\n    this.addEventListener(ThemeEvents.cartUpdate, this.handleCartUpdate, { signal: this.#abortController.signal });\n    this.addEventListener(ThemeEvents.variantUpdate, this.#updateProductTitleLink);\n  }\n\n  disconnectedCallback() {\n    super.disconnectedCallback();\n\n    this.#abortController.abort();\n  }\n\n  /**\n   * Closes the dialog\n   * @param {CartUpdateEvent} event - The cart update event\n   */\n  handleCartUpdate = async (event) => {\n    if (event.detail.data.didError) return;\n    \n    // Sync confirmed store from localStorage to cart attributes\n    // This ensures preferred store is available in cart after adding items via quick-add\n    CartService.syncConfirmedStoreFromLocalStorage();\n    \n    this.closeDialog();\n  };\n\n  #updateProductTitleLink = (/** @type {CustomEvent} */ event) => {\n    const anchorElement = /** @type {HTMLAnchorElement} */ (\n      event.detail.data.html?.querySelector('.view-product-title a')\n    );\n    const viewMoreDetailsLink = /** @type {HTMLAnchorElement} */ (this.querySelector('.view-product-title a'));\n    const mobileProductTitle = /** @type {HTMLAnchorElement} */ (this.querySelector('.product-header a'));\n\n    if (!anchorElement) return;\n\n    if (viewMoreDetailsLink) viewMoreDetailsLink.href = anchorElement.href;\n    if (mobileProductTitle) mobileProductTitle.href = anchorElement.href;\n  };\n}\n\nif (!customElements.get('quick-add-dialog')) {\n  customElements.define('quick-add-dialog', QuickAddDialog);\n}\n"],
  "mappings": "AAAA,OAAS,UAAa,eACtB,OAAS,cAAiB,mBAC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