<script>
    $(document).ready(function() {
        function loaded() {
            $('.is-waiting').removeClass('is-waiting');
        }
        window.addEventListener('variantChanged', function (event) {
            loaded();
        });

        window.addEventListener('productNoVariant', function (event) {
            loaded();
        })
    });
    let isMobile = false
    @mobile
        isMobile = true
    @endmobile
    var isAbTest = 0;
    var base_api_url_add_to_cart = "<?= config('cart::sa.api_url') ?>";
    var useDefaultMiniCart = <?= config('cart::sa.use_default_minicart') ? 'true' : 'false' ?>;
    var priceTemplateAddCart = "{{ getCurrencyTemplate() }}";
    var getShipping = <?= isset($getShipping) && $getShipping ? 1 : 0 ?>;
    var priceConfigAddCart = <?= json_encode(getPriceConfig()) ?>;
    var useBusinessDay = "<?= config('cart::sa.use_business_day', false) ?>";
    const monthNames = [
        "{{ __('Jan. #date') }}", "{{ __('Feb. #date') }}", "{{ __('Mar. #date') }}", "{{ __('Apr. #date') }}", "{{ __('May. #date') }}", "{{ __('Jun. #date') }}",
        "{{ __('Jul. #date') }}", "{{ __('Aug. #date') }}", "{{ __('Sep. #date') }}", "{{ __('Oct. #date') }}", "{{ __('Nov. #date') }}", "{{ __('Dec. #date') }}"
    ];
    var buyMoreForFree = "{{ __('Free Shipping on orders over #amount  (#cate)') }}"
    var noAddingFee = "{{ __('No extra shipping fee for additional items (#cate)') }}"
    var transactionFeeText = "{{ __('Handling fee') }}";
    var totalText = "{{ __('Total') }}";
    var shippingFeeText = "{{ __('Shipping fee') }}";
    var addedToCartText = "{{ __('Added to Cart') }}";
    var viewCartText = "{{ __('View Cart') }}";
    var buyMoreText = "{{ __('Buy more') }}";
    var arrivesBeforeText = "{{ __('Arrives before #event') }}";
    var readyToShipInText = "{{ __('Ready to ship in:') }}";
    var locatedInText = "{{ __('Located in:') }}";
    var oneBusinessDayText = "{{ __('#day business day') }}";
    var oneBusinessDaysText = "{{ __('#day business days') }}";
    var titleArrivesText = "{{ __('Arrives by #date if you order today. Hooray!') }}";
    var fromToText = "{{ __('#from - #to') }}";
    var betweenFromToText = "{{ __('#key between #from - #to') }}";
    var freeText = "{{ __('Free ') }}";
    var csrfToken = "{{ csrf_token() }}";
    var contents = <?= isset($product)?json_encode([['id' => $product['sku'],'quantity' => 1]]):'{}' ?>;
    var currencyUnitAddToCart = "{{ isset($_COOKIE['currency']) ? $_COOKIE['currency'] : config('default.currency_unit') }}";
    var defaultCurrencyUnitAddToCart = "{{ config('default.currency_unit') }}";
    var keepOriginal = <?= json_encode(config('cart::sa.keep_original_price', [])) ?>;
    var stripeFee = "<?= config("cart::stripe.applepay_include_fee", 0) ?>";
    var useCheckoutInProductPage = "<?=config('cart::stripe.use_checkout_in_product_page')?>";
    var validCountry = <?= json_encode(config('cart::sa.valid_country', [])) ?>;
    var keyStripe = "<?=config('cart::stripe.public_key')?>";
    var keyStripeTest = "<?=config('cart::stripe.test_public_key')?>";
    var token = '{{ getFingerprint() }}';
    var cjeventCookie = "{{ isset($_COOKIE['cje']) ? $_COOKIE['cje'] : '' }}";
    var freeShippingOnOrderOverText = "{{ __('Free shipping on orders over #overAmount (#cateName)') }}";
    var noExtraFeeText = "{{ __('No extra shipping fee for additional items (#cateName)') }}";
    var isUseNewInterface = true;
    var cartDelayClickAddToCartInterval = null;
    var otherTaxFee = "<?= config("cart::sa.other_tax_fee", 0) ?>";
    var otherFeeText = "{{ __('International Emergency Economic Powers Act (IEEPA)') }}";
    var paypalFee = "<?=config('cart::paypal.include_fee')?>";
    var validPaypalExpressCheckout = "<?=config('cart::paypal.use_paypal_express_checkout')?>";
    var useSpecialShippingFee = "<?= config("cart::sa.use_special_shipping_fee", false) ?>";
    var shouldUseNewInterface = "a";

    var shippingFeeByLevel = [4.99, 0];
    if (shouldUseNewInterface == 'b') {
        shippingFeeByLevel = [5.99, 0];
    }
    @if (isset($_GET['ab_testing']) && $_GET['ab_testing'] == '1')
        useSpecialShippingFee = true;
    @endif
    function triggerDelayClickAddToCart() {
        if (cartDelayClickAddToCartInterval) {
            clearInterval(cartDelayClickAddToCartInterval);
        }
        cartDelayClickAddToCartInterval = setInterval(() => {
            const element = $("#product-page .product-addtocart").first();
            if (!$(element).hasClass('is-waiting')) {
                $(element).click();
                clearInterval(cartDelayClickAddToCartInterval);
            }
        }, 250); 
    }

    var cartAddToCartBtnElements = document.querySelectorAll(".product-addtocart");
    for (let element of cartAddToCartBtnElements) {
        element.addEventListener('click', (event) => {
            if (element && element.classList.contains('is-waiting')) {
                event.preventDefault();
                element.classList.remove('is-initing');
                triggerDelayClickAddToCart();
                return;
            }
            if (window.doneLoadAddToCartScript) {
                cartHandleAddToCart(event.currentTarget);
            } else {
                triggerDelayClickAddToCart();
                return;
            }
        })
    }
</script>
<script src="{{ asset('modules/cart/js/add-to-cart.js') }}?v={{ config('sa.version') }}"></script>
