{
  "version": 3,
  "sources": ["/cdn/shop/t/1/assets/product-recommendations.js?blabla=nwxnuk3hpj"],
  "sourcesContent": ["class ProductRecommendations extends HTMLElement {\n  /**\n   * The observer for the product recommendations\n   * @type {IntersectionObserver}\n   */\n  #intersectionObserver = new IntersectionObserver(\n    (entries, observer) => {\n      if (!entries[0]?.isIntersecting) return;\n\n      observer.disconnect();\n      this.#loadRecommendations();\n    },\n    { rootMargin: '0px 0px 400px 0px' }\n  );\n\n  /**\n   * Observing changes to the elements attributes\n   * @type {MutationObserver}\n   */\n  #mutationObserver = new MutationObserver((mutations) => {\n    for (const mutation of mutations) {\n      // Only attribute changes are interesting\n      if (mutation.target !== this || mutation.type !== 'attributes') continue;\n\n      // Ignore error attribute changes\n      if (mutation.attributeName === 'data-error') continue;\n\n      // Ignore addition of hidden class because it means there's an error with the display\n      if (mutation.attributeName === 'class' && this.classList.contains('hidden')) continue;\n\n      // Ignore when the data-recommendations-performed attribute has been set to 'true'\n      if (\n        mutation.attributeName === 'data-recommendations-performed' &&\n        this.dataset.recommendationsPerformed === 'true'\n      )\n        continue;\n\n      // All other attribute changes trigger a reload\n      this.#loadRecommendations();\n      break;\n    }\n  });\n\n  /**\n   * The cached recommendations\n   * @type {Record<string, string>}\n   */\n  #cachedRecommendations = {};\n\n  /**\n   * An abort controller for the active fetch (if there is one)\n   * @type {AbortController | null}\n   */\n  #activeFetch = null;\n\n  connectedCallback() {\n    this.#intersectionObserver.observe(this);\n    this.#mutationObserver.observe(this, { attributes: true });\n  }\n\n  /**\n   * Load the product recommendations\n   */\n  #loadRecommendations() {\n    const { productId, recommendationsPerformed, sectionId, intent } = this.dataset;\n    const id = this.id;\n\n    if (!productId || !id) {\n      throw new Error('Product ID and an ID attribute are required');\n    }\n\n    // If the recommendations have already been loaded, accounts for the case where the Theme Editor\n    // is loaded the section from the editor's visual preview context.\n    if (recommendationsPerformed === 'true') {\n      return;\n    }\n\n    this.#fetchCachedRecommendations(productId, sectionId, intent)\n      .then((result) => {\n        if (!result.success) {\n          // The Theme Editor will place a section element element in the DOM whose section_id is not available\n          // to the Section Renderer API. In this case, we can safely ignore the error.\n          if (!Shopify.designMode) {\n            this.#handleError(new Error(`Server returned ${result.status}`));\n          }\n          return;\n        }\n\n        const html = document.createElement('div');\n        html.innerHTML = result.data || '';\n        const recommendations = html.querySelector(`product-recommendations[id=\"${id}\"]`);\n\n        if (recommendations?.innerHTML && recommendations.innerHTML.trim().length) {\n          this.dataset.recommendationsPerformed = 'true';\n          this.innerHTML = recommendations.innerHTML;\n        } else {\n          this.#handleError(new Error('No recommendations available'));\n        }\n      })\n      .catch((e) => {\n        this.#handleError(e);\n      });\n  }\n\n  /**\n   * Fetches the recommendations and cached the result for future use\n   * @param {string} productId\n   * @param {string | undefined} sectionId\n   * @param {string | undefined} intent\n   * @returns {Promise<{ success: true, data: string } | { success: false, status: number }>}\n   */\n  async #fetchCachedRecommendations(productId, sectionId, intent) {\n    const url = `${this.dataset.url}&product_id=${productId}&section_id=${sectionId}&intent=${intent}`;\n\n    const cachedResponse = this.#cachedRecommendations[url];\n    if (cachedResponse) {\n      return { success: true, data: cachedResponse };\n    }\n\n    this.#activeFetch?.abort();\n    this.#activeFetch = new AbortController();\n\n    try {\n      const response = await fetch(url, { signal: this.#activeFetch.signal });\n      if (!response.ok) {\n        return { success: false, status: response.status };\n      }\n\n      const text = await response.text();\n      this.#cachedRecommendations[url] = text;\n      return { success: true, data: text };\n    } finally {\n      this.#activeFetch = null;\n    }\n  }\n\n  /**\n   * Handle errors in a consistent way\n   * @param {Error} error\n   */\n  #handleError(error) {\n    console.error('Product recommendations error:', error.message);\n    this.classList.add('hidden');\n    this.dataset.error = 'Error loading product recommendations';\n  }\n}\n\nif (!customElements.get('product-recommendations')) {\n  customElements.define('product-recommendations', ProductRecommendations);\n}\n"],
  "mappings": "AAAA,MAAM,+BAA+B,WAAY,CAK/C,sBAAwB,IAAI,qBAC1B,CAAC,QAAS,WAAa,CAChB,QAAQ,CAAC,GAAG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