(function(){
        (function (src, position, payload) {
    try {
        const script = document.createElement('script');
        script.setAttribute('type', 'text/javascript');
        script.setAttribute('src', src);
        switch (position) {
            case 'body':
                document.body.appendChild(script);
                return;
            case 'head':
                document.head.appendChild(script);
                return;
            case 'conversion':
                const Shopify = window.Shopify;
                if (Shopify?.Checkout && Shopify.Checkout.step === 'thank_you') {
                    const formatByObj = (msg, obj) => msg.replace(/\{(\w+)\}/g, (m, k) => obj[k]);
                    const replaceComment = (str) => str.replace('<!--', '').replace('//-->', '');
                    const subtotal_price = Number(Shopify.checkout.subtotal_price);
                    const reducer = (accumulator, currentValue) => accumulator + currentValue;
                    let tax = 0;
                    if (Shopify.checkout.taxes_included) {
                        tax = Shopify.checkout.line_items
                            .map((line_item) => Number(line_item.tax_lines[0].price))
                            .reduce(reducer);
                    }
                    const amount = subtotal_price - tax;
                    const orderId = Shopify.checkout.order_id.toString();
                    // dependent third party js
                    document.body.appendChild(script);
                    // fire conversion script
                    const payloadStr = formatByObj(payload.replace(/"/g, "'"), {
                        _price: amount.toString(),
                        _buid: orderId,
                    });
                    const parser = new DOMParser();
                    const parsed = parser.parseFromString(payloadStr, 'text/html');
                    const element = parsed.getElementsByTagName('script');
                    if (element?.item(0)) {
                        const jsCodeStr = replaceComment(element.item(0)?.innerText || '');
                        script.onload = function () {
                            eval(jsCodeStr);
                        };
                    }
                }
                else {
                    console.log('Not Conversion Page');
                    console.log('Shopify', Shopify);
                    console.log('Shopify.Checkout', Shopify.Checkout);
                }
                return;
            case 'gtm':
                const gtmScriptTag = document.createElement('script');
                gtmScriptTag.innerHTML = `
      (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
      new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
      j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
      'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
      })(window,document,'script','dataLayer','${src}');
      `;
                document.head.appendChild(gtmScriptTag);
                return;
            default:
                console.error('invalid custom tag position');
        }
    }
    catch (error) {
        console.error(error);
    }
})('https://affiliate-script-pr.firebaseapp.com/script.js?shop=wvwxcd-gk.myshopify.com&gorillaImId=1022810&fromTheme=true&ac=202604220307', 'body', '');})();