function LISL_loadScript(url, callback=null) {
     var script = document.createElement('script');
     script.src = url;
     var head = document.getElementsByTagName('head')[0],
     done = false;
     head.appendChild(script);
     script.onload = script.onreadystatechange = function() {
        if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
             done = true;
             script.onload = script.onreadystatechange = null;
             head.removeChild(script);
             if (callback !== null) {
                callback();
             }
        }
    };
}

function LISL_findGetParameter(parameterName) {
    var result = undefined,
        tmp = [];
    location.search
        .substr(1)
        .split("&")
        .forEach(function (item) {
          tmp = item.split("=");
          if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
        });
    return result;
}

function LISL_detectAddToCartButton() {
    var resultElement = null;
    var adcButtonSelectors = [
        '*[lis-override="true"]:not([disabled])',
        '*[id="new-form-atc"]:not([disabled])',
        'input[name="add"]:not([disabled]):not([id*="CartSpecificUpsellButton"])',
        'button[name="add"]:not([disabled]):not([id*="CartSpecificUpsellButton"])',
        'button[id="add"]:not([disabled]):not([id*="CartSpecificUpsellButton"])',
        'input[id="add"]:not([disabled]):not([id*="CartSpecificUpsellButton"])',
        'button[id="addToCart"]:not([disabled])',
        'input[id="addToCart"]:not([disabled])',
        'input[name="AddToCart"]:not([disabled])',
        'button[name="AddToCart"]:not([disabled])',
        'input[id="AddToCart"]:not([disabled])',
        'button[id="AddToCart"]:not([disabled])',
        '*[data-pf-type="ProductATC"]:not([disabled])',
        '*[id="add-to-cart"]:not([disabled])',
        '*[id="adbtn"]:not([disabled])',
        '*[class*="product-form--atc-button"]:not([disabled])',
        '*[class*="product__add-to-cart"]:not([disabled])',
        '*[id="shopify_add_to_cart"]:not([disabled])',
        '*[class*="product-form--add-to-cart"]:not([disabled])',
        '*[class*="ProductForm__AddToCart"]:not([disabled])',
        '*[class*="add-to-cart"]:not([disabled])',
        '*[class*="addtocart"]:not([disabled]):not([class*="ga-product_addtocart"])',
        '*[class*="AddToCart"]:not([disabled])',
        '*[data-action="add-to-cart"]:not([disabled])',
        '*[data-add-to-cart]:not([disabled])',
        '*[data-add-to-cart-text]:not([disabled])',
        '*[name="add"]:not([disabled]):not([id*="CartSpecificUpsellButton"])',
        '*[data-add-button]:not([disabled])',
        '*[data-buy-button]:not([disabled])',
        '*[data-lhi="trans_buynow"]:not([disabled])',
        '*[class*="add_to_cart_button"]:not([disabled])',
        '*[class*="product-buy-buttons--cta"]:not([disabled])',
        '*[data-aid="product-form-submit-button"]:not([disabled])',
        '*[class*="lh-add_to_cart"]:not([disabled])',
        '*[id="ProductPopup-submit"]:not([disabled])',
        '*[class*="add_to_cart"]:not([disabled])',
        '*[class*="f8pr-buy-button"]:not([disabled])',
    ];

    for (var selector of adcButtonSelectors) {
        var adcElements = document.querySelectorAll(selector);
        if (adcElements.length === 1) {
            var element = adcElements[0];
            if (element !== null && element.innerText !== undefined && element.innerText.toLowerCase().indexOf("sold out") === -1) {
                return element;
            }
        }
    }

    return null;
}

function LISL_getHomePageProductVariantId() {
    if (window.LIS_HOME_PAGE_PRODUCT_VARIANT_ID !== undefined) {
        return window.LIS_HOME_PAGE_PRODUCT_VARIANT_ID;
    }
    else if (window.ShopifyAnalytics.meta.selectedVariantId !== undefined) {
        return window.ShopifyAnalytics.meta.selectedVariantId;
    }
    else {
        return undefined;
    }
}

function LISL_main() {
    if (window.LISL_SCRIPT_INJECTED === undefined) {
        window.LISL_SCRIPT_INJECTED = true;
        window.effectiveAppsLisSp = '';
        window.effectiveAppsLisAllowedProducts = window.effectiveAppsLisSp.split(',');
        if (Shopify.designMode === true) {
            if (document.getElementById('eaLisStockCounterContainer') !== null) {
                document.getElementById('eaLisStockCounterContainer').innerHTML = "THE STOCK COUNTER WILL APPEAR INSTEAD OF THIS TEXT";
            }

            LISL_loadScript('https://aglis-app.herokuapp.com/get_lis/e1b8485a80c811eba3ed860c2122a678.js' + '?productId=editor_demo');
        }
        else if ("product" === ShopifyAnalytics.meta.page.pageType || location.href.indexOf("/products") > -1) {
            var newVariantId = window.ShopifyAnalytics.meta.selectedVariantId;
            if (newVariantId === undefined || newVariantId === "") {
                newVariantId = LISL_findGetParameter('variant');
            }
            if (undefined === newVariantId || newVariantId === "") {
                if (window.effectiveAppsLisSp.length > 0) {
                    if (window.effectiveAppsLisAllowedProducts.indexOf(ShopifyAnalytics.meta.product.id.toString()) > -1) {
                        LISL_loadScript('https://aglis-app.herokuapp.com/get_lis/e1b8485a80c811eba3ed860c2122a678.js' + '?productId=' + ShopifyAnalytics.meta.product.id);
                    }
                }
                else {
                    LISL_loadScript('https://aglis-app.herokuapp.com/get_lis/e1b8485a80c811eba3ed860c2122a678.js' + '?productId=' + ShopifyAnalytics.meta.product.id);
                }
            }
            else {
                if (window.effectiveAppsLisSp.length > 0) {
                    if (window.effectiveAppsLisAllowedProducts.indexOf(ShopifyAnalytics.meta.product.id.toString()) > -1) {
                        LISL_loadScript('https://aglis-app.herokuapp.com/get_lis/e1b8485a80c811eba3ed860c2122a678.js' + '?productId=' + ShopifyAnalytics.meta.product.id + '&variant=' + newVariantId);
                    }
                }
                else {
                    LISL_loadScript('https://aglis-app.herokuapp.com/get_lis/e1b8485a80c811eba3ed860c2122a678.js' + '?productId=' + ShopifyAnalytics.meta.product.id + '&variant=' + newVariantId);
                }
            }
        }
        else {
            if (window.effectiveAppsLisSp.length === 0) {
                if ("home" === ShopifyAnalytics.meta.page.pageType || location.pathname === "/" || location.pathname.length === 3) {
                    detectedATCButton = LISL_detectAddToCartButton();
                    let homePageProductVariantId = LISL_getHomePageProductVariantId();
                    if (detectedATCButton !== null && homePageProductVariantId !== undefined) {
                        LISL_loadScript('https://aglis-app.herokuapp.com/get_lis/e1b8485a80c811eba3ed860c2122a678.js' + '?onlyVariantId=' + homePageProductVariantId);
                    }
                }
            }
        }
    }
}

if (typeof jQuery === 'undefined') {
    LISL_loadScript('https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js', LISL_main);
}
else {
    LISL_main();
}