window.quantityUpdateChange = function(event) {
    if (window.CAU_THEME_NAME.indexOf('venture') > -1) {
        setTimeout(triggerCartUpdate, 1250);
    }
    else {
        triggerCartUpdate();
    }
}

function CAU_getLsWithExpiry(key) {
	const itemStr = localStorage.getItem(key)
	if (!itemStr) {
		return null
	}

	const item = JSON.parse(itemStr)
	const now = new Date()
	if (now.getTime() > item.expiry) {
		localStorage.removeItem(key)
		return null
	}

	return item.value.toString();
}

function CAU_setLsWithExpiry(key, value, ttl) {
    value = value.toString();
	const now = new Date()
	const item = {
		value: value,
		expiry: now.getTime() + ttl,
	}
	localStorage.setItem(key, JSON.stringify(item))
}

function triggerCartUpdate() {
    if (jQuery("*[name='update']")[0] === undefined && jQuery("*[class*='update btn item']")[0] === undefined && jQuery("*[class*='btn-update']")[0] === undefined && jQuery("*[class*='update-cart']")[0] === undefined) {
        jQuery('form[action*="/cart"]')[0].submit();
    }
    else {
        jQuery("*[name='update']").trigger("click");
        jQuery("*[class*='update btn item']").trigger("click");
        jQuery("*[class*='btn-update']").trigger("click");
        jQuery("*[class*='update-cart']").trigger("click");
    }
}

window.inputFieldQuantityUpdateChange = function(e) {
    if ([48,49,50,51,52,53,54,55,56,57,96,97,98,99,100,101,102,103,104,105].indexOf(e.keyCode) === -1) {
        return;
    }

    setTimeout(function() {
        if (jQuery("*[name='update']")[0] === undefined && jQuery("*[class*='update btn item']")[0] === undefined && jQuery("*[class*='btn-update']")[0] === undefined && jQuery("*[class*='update-cart']")[0] === undefined) {
            jQuery('form[action*="/cart"]')[0].submit();
        }
        else {
            jQuery("*[name='update']").trigger("click");
            jQuery("*[class*='update btn item']").trigger("click");
            jQuery("*[class*='btn-update']").trigger("click");
            jQuery("*[class*='update-cart']").trigger("click");
        }
    }, 1250);
}

window.CartAutoUpdate = function() {
    if ('true' === 'true') {
        jQuery("*[name='update']").css("display", "none");
        jQuery("*[class*='update btn item']").css("display", "none");
        jQuery("*[class*='btn-update']").css("display", "none");
        jQuery("*[class*='update-cart']").css("display", "none");
    }
    
    jQuery("input[name='updates[]'], input[name*='updates'], input[class='cart-qty'], input[id*='updates']").on('keydown', function(event) {
        window.inputFieldQuantityUpdateChange(event);
    });

    jQuery("input[name='updates[]'], input[name*='updates'], input[class='cart-qty'], input[id*='updates']").on('change', function(event) {
        setTimeout(function() {
            window.quantityUpdateChange(event);
        }, 750);
    });


    jQuery('*[class*="-plus"]:not([class*="site-header"]), *[class*="-add"]:not(".cart__note-add"), *[class="increase"], *[class*=" plus"]').bind('click', function(event) {
        event.preventDefault();
        window.quantityUpdateChange(event);
    });

    jQuery('*[class*="-minus"], *[class="decrease"], *[class*=" minus"]').bind('click', function(event) {
        event.preventDefault();
        window.quantityUpdateChange(event);
    });

    jQuery('*[class*="qtyplus"], *[class="plus button"]').bind('click', function(event) {
        setTimeout(function() { window.quantityUpdateChange(event);}, 300);
    });

    jQuery('*[class*="qtyminus"],  *[class="minus button"]').bind('click', function(event) {
        setTimeout(function() { window.quantityUpdateChange(event);}, 300);
    });

    jQuery('*[id*="quantity"]').on('change paste input', function(event) {
        setTimeout(function() { window.quantityUpdateChange(event);}, 300);
    });

    jQuery('*[class*="qty-btn"]').on('click', function(event) {
        setTimeout(function() { window.quantityUpdateChange(event);}, 300);
    });

    jQuery('*[class*="inc button"], *[class*="dec button"]').on('click', function(event) {
        setTimeout(function() { window.quantityUpdateChange(event);}, 300);
    });

    jQuery('*[data-minus=""], *[data-plus=""]').on('click', function(event) {
        setTimeout(function() { window.quantityUpdateChange(event);}, 300);
    });

    jQuery('*[class*="reduced items"], *[class*="increase items"]').on('click', function(event) {
        setTimeout(function() { window.quantityUpdateChange(event);}, 300);
    });

    jQuery('*[class*="cart-item-increase"], *[class*="cart-item-decrease"]').on('click', function(event) {
        setTimeout(function() { window.quantityUpdateChange(event);}, 300);
    });
}

function CAU_loadjQuery(url, success) {
     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;
             success();
             script.onload = script.onreadystatechange = null;
             head.removeChild(script);
        }
    };
}

function CAU_getShopifyThemeName() {
    try {
        if (window.BOOMR !== undefined && window.BOOMR.themeName !== undefined) {
            return window.BOOMR.themeName.toLowerCase();
        }
        else {
            return Shopify.theme.name.toLowerCase();
        }
    }
    catch (err) {
        return "";
    }
}

function hideATCButton() {
    appendCssToHead('input[name="add"]{display: none !important;} button[name="add"]{display: none !important;} button[id="add"]{display: none !important;} input[id="add"]{display: none !important;} button[id="addToCart"]{display: none !important;} input[id="addToCart"]{display: none !important;} input[name="AddToCart"]{display: none !important;} button[name="AddToCart"]{display: none !important;} input[id="AddToCart"]{display: none !important;} button[id="AddToCart"]{display: none !important;} [class*="product-form--atc-button"]{display: none !important;} [class*="product__add-to-cart"]{display: none !important;} [id="shopify_add_to_cart"]{display: none !important;} [class*="product-form--add-to-cart"]{display: none !important;} [class*="ProductForm__AddToCart"]{display: none !important;} [class*="add-to-cart"]{display: none !important;} [class*="addtocart"]{display: none !important;} [class*="AddToCart"]{display: none !important;} [data-action="add-to-cart"]{display: none !important;} [data-add-to-cart]{display: none !important;} [name="add"]{display: none !important;} [id="add-to-cart"]{display: none !important;} [data-lhi="trans_buynow"]{display: none !important;} [data-add-to-cart-text]{display: none !important;} [id="multivariants_add_to_cart_button"]{visibility: hidden !important;}');
}

function hideBINButton() {
    appendCssToHead('[class*="shopify-payment-button"]{display: none !important;} [class="td-submit"]{display: none !important;}');
}

function hideACB() {
    appendCssToHead('[class*="additional-checkout-buttons"]{display: none !important;}');
}

function appendCssToHead(css) {
    var head = document.getElementsByTagName('head')[0];
    var s = document.createElement('style');
    s.setAttribute('type', 'text/css');
    if (s.styleSheet) {
        s.styleSheet.cssText = css;
    } else {
        s.appendChild(document.createTextNode(css));
    }

    head.appendChild(s);
}

function hideCartButton() {
    if (window.CAU_THEME_NAME.indexOf('express') > -1) {
        appendCssToHead('[href="/cart"]{visibility: hidden !important;} [class*="header__icon--cart"]{visibility: hidden !important;}');
    }
    else {
        appendCssToHead('[href="/cart"]{display: none !important;} [class*="header__icon--cart"]{display: none !important;}');
    }
}

function CAU_main() {
    console.log('%c------ Poof: Store Elements Remover by Effective Apps is Initializing ------', 'color: cyan');
    console.log('%c------ Contact us at support@effectify.co for help and questions about the app ------', 'color: cyan');
    window.CAU_THEME_NAME = CAU_getShopifyThemeName();
    window.effectiveAppsCauAtcSp = '';
    window.effectiveAppsCauAtcAllowedProducts = window.effectiveAppsCauAtcSp.split(',');
    if (window.CAU_SCRIPT_INJECTED === undefined) {
        window.CAU_SCRIPT_INJECTED = true;
        if ('3' === '7' && null === CAU_getLsWithExpiry('cau_payg_i')) {
            jQuery.get('https://quanter-cqu.herokuapp.com/ri/d5774058d08d11ecb6b4429b12a89b14.js');
            CAU_setLsWithExpiry('cau_payg_i', 'true', 604800000);
        }

        if ('False' === 'True' && ShopifyAnalytics.meta.page.pageType === "product") {
            if (window.effectiveAppsCauAtcSp.length > 0) {
                if (window.effectiveAppsCauAtcAllowedProducts.indexOf(ShopifyAnalytics.meta.product.id.toString()) > -1) {
                    hideATCButton();
                }
            }
            else {
                hideATCButton();
            }
        }

        if ('True' === 'True') {
            if (window.effectiveAppsCauAtcSp.length > 0 && ShopifyAnalytics.meta.page.pageType === "product") {
                if (window.effectiveAppsCauAtcAllowedProducts.indexOf(ShopifyAnalytics.meta.product.id.toString()) > -1) {
                    hideBINButton();
                }
            }
            else {
                hideBINButton();
            }
        }

        if ('True' === 'True') {
            hideACB();
        }

        if ('False' === 'True') {
            hideCartButton();
            if (window.location.pathname.indexOf("/cart") > -1) {
                window.location.href = "/checkout";
            }
        }

        if (1 === 1 && window.location.pathname.indexOf("/cart") > -1) {
            window.CartAutoUpdate();
        }
    }
}

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