(() => {
const insertChatWidget = () => {
  const chatLoader = document.createElement('script');
  chatLoader.type = 'text/javascript';
  chatLoader.src = 'https://chat-widget-degy9npph-outdoorly.vercel.app/index.js';
  document.head.appendChild(chatLoader);
}
 const insertInlineActivator = async () => {
    const wait = ms => new Promise((r) => setTimeout(r, ms));
    const MAX_ATTEMPTS = 3;
    const insertActivator = () => {
      const inlineLoaderParent = document.createElement('div');
      inlineLoaderParent.id = 'remark-inline-activator-parent';
      inlineLoaderParent.style.width = '100%';
      inlineLoaderParent.style.height = 'calc(63px + 0px)';
      inlineLoaderParent.style.position = 'relative';
      if (!document.getElementById('remark-inline-activator-parent')) {
        const inlineLoader = document.createElement('div');
        inlineLoader.id = 'remark-inline-activator-loader';
        inlineLoader.style.height = 'calc(55px + 0px)';
        inlineLoader.style.background = '#ff4713';
        inlineLoader.style.borderRadius = '0px';
        inlineLoader.style.width = '100%';
        inlineLoader.style.display = 'block';
        inlineLoader.style.position = 'absolute';
        inlineLoader.style.top = '4px';
        inlineLoader.style.zIndex = '0';
        inlineLoaderParent.appendChild(inlineLoader);
        if('AFTER' === 'BEFORE') {
          document.querySelector('#product_addtocart_form > div.product-options-bottom > div > div > div.actions')?.before(inlineLoaderParent);
        } else {
          document.querySelector('#product_addtocart_form > div.product-options-bottom > div > div > div.actions')?.after(inlineLoaderParent);
        }
      }
      const inlineActivator = document.createElement('script');
      inlineActivator.type = 'text/javascript';
      inlineActivator.src = 'https://chat-widget-degy9npph-outdoorly.vercel.app/inline-activator/index.js';
      document.head.appendChild(inlineActivator);
    }
    for (let attempts = 0; attempts < MAX_ATTEMPTS; attempts++) {
      if (document.querySelector('#product_addtocart_form > div.product-options-bottom > div > div > div.actions')) {
        insertActivator();
        break;
      }
      await wait(3000);
    }
  }

  insertChatWidget();
  const params = new URLSearchParams(window.location.search);
  const shouldShowPreview = params.get('remark_preview_inline_elements');
  if(shouldShowPreview) {
    insertInlineActivator();
  }

})();
(function(){const e=()=>{if(typeof window.remark==="function"){return}const e=window.remark??{};const n=[];let t=false;const s=(n,...t)=>{const s=e[n];if(Array.isArray(s)){s.forEach((e=>e(...t)))}else if(typeof s==="function"){s(...t)}};const o=(n,t)=>{if(Array.isArray(e[n])){e[n].push(t)}else if(n in e){e[n]=[e[n],t]}else{e[n]=t}};const i=(n,t)=>{const s=e[n];if(!s){return}if(Array.isArray(s)){const o=s.findIndex((e=>e==t));e[n].splice(o,1)}else{if(s==t)delete e[n]}};const c=()=>{Object.keys((n=>{delete e[n]}))};const r=()=>{let e;while(e=n.shift()){s(...e)}};const f=(...e)=>{if(!t){n.push(e)}else{s(...e)}};const l=()=>{n.unshift(["connect"]);r();t=true};window.remark=Object.assign(f,{on:o,off:i,offAll:c,connect:l})};e()})();
(() => {
const enhanceSEOPage = () => {

  const activatorButtons = document.querySelectorAll(`[remark-id="chat-activator"]`);
  activatorButtons.forEach((button) => {
    button.onclick = function () {
      if (window && window.remark) window.remark('open');
    };
  });
  const PRODUCT_PRICE_QUERY = `
  query ProductPriceById($id: ID!) {
    product(id: $id) {
      id
      price(currency: USD) {
        id
        currency
        price
      }
      externalUrl
    }
  }
`;
  const prices = document.querySelectorAll(`[remark-id="product-price"]`);
  prices.forEach(async (priceElement) => {
    const id = priceElement.getAttribute("remark-product-id");
    await fetch('https://api.withremark.com/graphql', {
      method: 'POST',
      headers: { 'content-type': 'application/json' },
      body: JSON.stringify({
        operationName: 'ProductPriceById',
        query: PRODUCT_PRICE_QUERY,
        variables: {
          id,
        }
      })
    })
      .then((resp) => resp.json())
      .then(({ data, errors }) => {

        const price = data?.product?.price?.price;
        const currency = data?.product?.price?.currency;
        const formattedPrice = price && currency
          ? new Intl.NumberFormat(window.navigator.language, {
              style: 'currency',
              currency: data?.product?.price?.currency
            }).format(price)
          : undefined;
        if(formattedPrice) {
          priceElement.innerHTML = formattedPrice;
        }
        const buyNowButton = document.querySelector(`[remark-id="purchase-button"][remark-product-id="${id}"]`)
        if (data?.product?.externalUrl && buyNowButton) {
          buyNowButton.onclick = function () {
            if (window) window.open(data.product.externalUrl, "_blank", "noopener");
          }
        }
        return data;
        
      });
  });
};
const retryEnhanceSeoPage = async () => {
  const wait = ms => new Promise((r) => setTimeout(r, ms));
  const MAX_ATTEMPTS = 3;
  for (let attempts = 0; attempts < MAX_ATTEMPTS; attempts++) {
    if (document.querySelector(`[remark-id="chat-activator"]`)) {
      enhanceSEOPage();
      break;
    }
    await wait(3000);
  }
}
retryEnhanceSeoPage();
})();
