// source --> https://trottbike-experience.com/wp-content/plugins/aco-woo-dynamic-pricing/assets/js/frontend.js?ver=4.5.10 
jQuery(document).ready(function ($) {

    var $wdpTable           = $('.wdp_table ');
    var $dynamicPricing     = awdajaxobject.dynamicPricing;
    var $variablePricing    = awdajaxobject.variablePricing;

    // Function to check if a string is Base64-encoded
    function isBase64(str) {
        try {
        return btoa(atob(str)) == str;
        } catch (e) {
        return false;
        }
    }

    if ( typeof wp !== 'undefined' && wp.hooks) {
    //     wp.hooks.addFilter('wcpa_product_price', 'wcpa', (productPrice, product_id, variation_id) => {
    //         $.ajax({ url: awdajaxobject.url, data: {action: 'wdpDiscountedPrice', prodID: product_id, varID: variation_id} }).done(function(data) {
    //             productPrice = data ? data : productPrice;
    //             return productPrice;
    //         }).fail(function(data){
    //             return productPrice;
    //         });
    //     }, 10);
    //     document.dispatchEvent(new Event("wcpaTrigger", {bubbles: true}));
    //     $( '.variations_form' ).on( 'woocommerce_variation_select_change', function() {
    //         wp.hooks.addFilter('wcpa_product_price', 'wcpa', (productPrice, product_id, variation_id) => {
    //             $.ajax({ url: awdajaxobject.url, data: {action: 'wdpDiscountedPrice', prodID: product_id, varID: variation_id} }).done(function(data) {
    //                 productPrice = data ? data : productPrice;
    //                 return productPrice;
    //             }).fail(function(data){
    //                 return productPrice;
    //             });
    //         }, 10);
    //         document.dispatchEvent(new Event("wcpaTrigger", {bubbles: true}));
    //     });
    //     $('form.cart').find('[name=quantity]').on('change input', function(){ 
    //         let qn = $(this).parents('form.cart').find('input[name="quantity"]').val();
    //         wp.hooks.addFilter('wcpa_product_price', 'wcpa', (productPrice, product_id, variation_id) => {
    //             $.ajax({ url: awdajaxobject.url, data: {action: 'wdpDiscountedPrice', prodID: product_id, varID: variation_id} }).done(function(data) {
    //                 productPrice = data ? qn * data : qn * productPrice;
    //                 return productPrice;
    //             }).fail(function(data){
    //                 return qn * productPrice;
    //             });
    //         }, 10);
    //         document.dispatchEvent(new Event("wcpaTrigger", {bubbles: true}));
    //     });

        $('form.cart').find('[name=quantity]').on('change input', function() { 
            let qnty = $(this).parents('form.cart').find('input[name="quantity"]').val();
            let product_id = $(this).parents('form.cart').find('button[name="add-to-cart"]').val();
            let variation_id = $(this).parents('form.cart').find('input[name="variation_id"]').val();
            $.ajax({
                dataType : "json",
                url : awdajaxobject.url,
                data : {action: 'wdpDynamicDiscount',
                prodID: product_id,
                varID: variation_id,
                proCount: qnty },
                success: function(response) {
                    function modifyProductPrice() {
                        return response; 
                    }
                    wp.hooks.addFilter('wcpa_product_price', 'wcpa', modifyProductPrice, 10);
                    document.dispatchEvent(new Event("wcpaTrigger", {bubbles: true}));
                }
            });

            $.ajax({
                dataType : "json",
                url : awdajaxobject.url,
                data : {action: 'wcpaQunantity_Discount',
                proCount: qnty },
                success: function(response) {
                    function modifyDiscountRule() {
                        return response; 
                    }
                    wp.hooks.addFilter('wcpa_discount_rule', 'wcpa', modifyDiscountRule, 10);
                }
            });
        });
    }

    if ( $wdpTable.length > 0 ) {
        var dataTable = $wdpTable.attr('data-table');
        if (isBase64($wdpTable.attr('data-table'))) {
            dataTable = atob($wdpTable.attr('data-table'));
        }
        if ( $variablePricing ) {
            $( ".single_variation_wrap" ).on( "show_variation", function ( event, variation ) {
                let attributes = variation.attributes;
                let price = variation.display_price;
                let price_html = variation.price_html;
                let variation_id = variation.variation_id;
                let loader = '<div class="wdpLoader"><span></span><span></span><span></span></div>';
                $wdpTable.find('tbody td').html(loader);
                $('.wdpHiddenPrice').html(price_html);
                let varPrice = $('.wdpHiddenPrice del').length ? $('.wdpHiddenPrice ins .amount').text() : $('.wdpHiddenPrice .amount').text();

                if(awdajaxobject.thousandSeparator == "," && awdajaxobject.decimalSeparator == ".") {
                    varPrice = varPrice ? varPrice.replace(/[^\d\.]/g, '') : price;
                } else {
                    varPrice = varPrice ? varPrice.replace(/^[^\d]*/, '').replace(/\./g, '').replace(',', '.') : price;
                }
                // varPrice = varPrice ? varPrice.replace(/[^\d\.]/g, '') : price;
                let data = {
                    'action': 'wdpAjax', 
                    'nonce': awdajaxobject.nonce, 
                    'type': 'change',
                    'attributes': attributes,
                    'price': varPrice,
                    'variation_id': variation_id,
                    // 'DisData': $wdpTable.attr('data-table'),
                    'DisData': dataTable,
                    'Rule': $wdpTable.attr('data-rule'),
                }
                $.post(awdajaxobject.url, data, function(response) { 
                    if ( response ) { 
                        $wdpTable.attr('data-price', varPrice);
                        $wdpTable.find('tbody').html(response); 
                    }
                });
            });
        }

        if ( $dynamicPricing ) {
            let loader = '<div class="wdpLoader"><span></span><span></span><span></span></div>';
            // $('form.cart').find('[name=quantity]').on('change input', function(){
            $('form.cart').find('[name=quantity]').on('input', function(){ 
                if ( $wdpTable.attr('data-product') === '' ) { return; } 
                let data = { 
                    'action': 'wdpAjax', 
                    'nonce': awdajaxobject.nonce, 
                    'type': 'update',
                    'ProdID': $wdpTable.attr('data-product'), 
                    // 'DisData': $wdpTable.attr('data-table'),
                    'DisData': dataTable,
                    'ProdPrice': $wdpTable.attr('data-price'), 
                    'ProdVarPrice': $wdpTable.attr('data-var-price'), 
                    'ProdQty': $(this).parents('form.cart').find('input[name="quantity"]').val() 
                };
                $(".wdpDynamicValue .wdpPrice").html(loader);
                $(".wdpDynamicValue .wdpTotal").html(loader);
                $.post(awdajaxobject.url, data, function(response) { 
                    if ( response ) { 
                        response = JSON.parse(response); 
                        // $(".wdpDynamicValue .wdpPrice").removeClass('wdpLoader');
                        // $(".wdpDynamicValue .wdpTotal").removeClass('wdpLoader');
                        
                        let yourPrice  = response.price;
                        let totalPrice = response.total;
                        if(awdajaxobject.thousandSeparator == "," && awdajaxobject.decimalSeparator == ".") {
                            yourPrice  = yourPrice ? yourPrice.toString().replace(",", ".") : yourPrice;
                            totalPrice = totalPrice ? totalPrice.toString().replace(",", ".") : totalPrice;
                        } else {
                            yourPrice = yourPrice ? yourPrice.toString().replace(".", ",") : yourPrice;
                            totalPrice = totalPrice ? totalPrice.toString().replace(".", ",") : totalPrice;
                        }
                        $(".wdpDynamicValue .wdpPrice").html(response.currency + yourPrice); 
                        $(".wdpDynamicValue .wdpTotal").html(response.currency + totalPrice); 
                        $(".wdpDynamicValue").show();
                    }
                });
            });
        }
    }
});
// source --> https://trottbike-experience.com/wp-content/plugins/woocommerce/assets/js/js-cookie/js.cookie.min.js?ver=2.1.4-wc.10.6.2 
/*! js-cookie v3.0.5 | MIT */
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self,function(){var n=e.Cookies,o=e.Cookies=t();o.noConflict=function(){return e.Cookies=n,o}}())}(this,function(){"use strict";function e(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)e[o]=n[o]}return e}return function t(n,o){function r(t,r,i){if("undefined"!=typeof document){"number"==typeof(i=e({},o,i)).expires&&(i.expires=new Date(Date.now()+864e5*i.expires)),i.expires&&(i.expires=i.expires.toUTCString()),t=encodeURIComponent(t).replace(/%(2[346B]|5E|60|7C)/g,decodeURIComponent).replace(/[()]/g,escape);var c="";for(var u in i)i[u]&&(c+="; "+u,!0!==i[u]&&(c+="="+i[u].split(";")[0]));return document.cookie=t+"="+n.write(r,t)+c}}return Object.create({set:r,get:function(e){if("undefined"!=typeof document&&(!arguments.length||e)){for(var t=document.cookie?document.cookie.split("; "):[],o={},r=0;r<t.length;r++){var i=t[r].split("="),c=i.slice(1).join("=");try{var u=decodeURIComponent(i[0]);if(o[u]=n.read(c,u),e===u)break}catch(f){}}return e?o[e]:o}},remove:function(t,n){r(t,"",e({},n,{expires:-1}))},withAttributes:function(n){return t(this.converter,e({},this.attributes,n))},withConverter:function(n){return t(e({},this.converter,n),this.attributes)}},{attributes:{value:Object.freeze(o)},converter:{value:Object.freeze(n)}})}({read:function(e){return'"'===e[0]&&(e=e.slice(1,-1)),e.replace(/(%[\dA-F]{2})+/gi,decodeURIComponent)},write:function(e){return encodeURIComponent(e).replace(/%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g,decodeURIComponent)}},{path:"/"})});