(function (w, d) {
    var OVERLAY = d.getElementById('dk-loading-overlay');
    var TITLE_ID = 'dk-loading-title';
    function showOverlay(message) {

        if (message) {
            var title = OVERLAY.querySelector('#' + TITLE_ID);
            if (title) title.textContent = message;
        }
        OVERLAY.style.display = 'flex';
        d.body.style.overflow = 'hidden';
    }

    function hideOverlay() {

        if (!OVERLAY) return;
        OVERLAY.style.display = 'none';
        d.body.style.overflow = '';
    }

    w.showOverlay = showOverlay;
    w.hideOverlay = hideOverlay;




})(window, document);



jQuery(document).ready(function ($) {

    jQuery('.wrap-bwdk-error span , .dk-mask').on('click', function () {
        jQuery('.wrap-bwdk-error , .dk-mask').fadeOut();
    });

    jQuery('.digikala-box-after-addToCart ,.digikala-box-before-header').on('click', function () {
        jQuery('.digikala-modal-sp , .dk-mask').fadeIn();
    });

    jQuery('.digikala-modal-sp button, .close-modal-sp, .dk-mask').on('click', function () {
        jQuery('.digikala-modal-sp , .dk-mask').fadeOut();
    });
});
jQuery(document).ready(function () {
    bwdk_send_request();
});
jQuery(document.body).on(
    'updated_wc_div ',
    function () {
        bwdk_send_request();
    }
);


function bwdk_send_request(){
    jQuery('.bwdk-button').on('click', function (e) {
        e.preventDefault();
        showOverlay();
        jQuery.post({
            url: bwdk_ajax.ajax_url,
            dataType: 'json',
            data: {
                action: 'bwdk_get_cart',
                _ajax_nonce: bwdk_ajax.nonce,
                callback_url:window.location.href
            },
            success: function (response) {
                if (response.success) {
                    window.location.href = response.data.order_start_url;

                } else {
                    hideOverlay();
                    jQuery('#bwdk-error')
                        .html(response.data?.message || 'خطای ناشناخته');
                    jQuery('.wrap-bwdk-error , .dk-mask').fadeIn();
                }
            },
            error: function (xhr) {
                let msg = '';
                try {
                    let res = JSON.parse(xhr.responseText);
                    msg = res.data?.message || 'خطای ناشناخته';
                } catch (e) {
                    msg = 'خطای ناشناخته';
                }
                hideOverlay();
                jQuery('#bwdk-error').html(msg);
                jQuery('.wrap-bwdk-error , .dk-mask').fadeIn();
            }
        });
    });





}

jQuery(function ($) {
    var $box = $('.wrap-anim-text .anim-text');
    var $items = $box.children('span');

    if (!$box.length || $items.length < 2) return;

    var step = $items.first().outerHeight();
    var dur = 300;
    var wait = 1000;
    var total = $items.length - 1;
    var i = 0;

    $('.wrap-anim-text').css({overflow: 'hidden', height: step});

    function tick() {
        i++;
        $box.css({
            transition: 'transform ' + dur + 'ms',
            transform: 'translateY(' + (-i * step) + 'px)'
        });

        if (i === total) {
            setTimeout(function () {
                $box.css({transition: 'none', transform: 'translateY(0px)'});
                i = 0;
            }, dur + 20);
        }

        setTimeout(tick, dur + wait);
    }

    tick();
});

jQuery(function ($) {
    var $wrap = $('.pdp-anim-text');
    var $box = $wrap.find('.anim-text');
    var $items = $box.children('span');

    if (!$wrap.length || !$box.length || $items.length < 2) return;

    if ($items.last().text().trim() !== $items.first().text().trim()) {
        $box.append($items.first().clone());
        $items = $box.children('span');
    }

    var step = Math.round($wrap.height()) || $items.first().outerHeight();
    var dur = 300;
    var wait = 1000;
    var total = $items.length - 1;
    var i = 0;

    $wrap.css({overflow: 'hidden', height: step});
    $box.css({willChange: 'transform'});

    function tick() {
        i++;
        $box.css({
            transition: 'transform ' + dur + 'ms',
            transform: 'translateY(' + (-i * step) + 'px)'
        });

        if (i === total) {
            setTimeout(function () {
                $box.css({transition: 'none', transform: 'translateY(0px)'});
                i = 0;
            }, dur + 20);
        }

        setTimeout(tick, dur + wait);
    }

    tick();
});

jQuery(document.body).on('wc_fragments_loaded', function(){
    var wrap = jQuery('.wrap-anim-text-addToCart-pop');
    var box = jQuery('#anim-text');

    var items = box.children('span');

    if (!wrap.length || !box.length || items.length < 2) return;

    if (items.last().text().trim() !== items.first().text().trim()) {
        box.append(items.first().clone());
        items = box.children('span');
    }

    var step = Math.round(wrap.height()) || items.first().outerHeight();
    var dur = 300;
    var wait = 1000;
    var total = items.length - 1;
    var i = 0;

    wrap.css({overflow: 'hidden', height: step});
    box.css({willChange: 'transform'});

    function tick() {
        i++;
        box.css({
            transition: 'transform ' + dur + 'ms',
            transform: 'translateY(' + (-i * step) + 'px)'
        });

        if (i === total) {
            setTimeout(function () {
                box.css({transition: 'none', transform: 'translateY(0px)'});
                i = 0;
            }, dur + 20);
        }

        setTimeout(tick, dur + wait);
    }
    tick();

});

jQuery(function ($) {
    function activateBox() {
        var $box = $('.digikala-box-before-ct');
        if (!$box.length) return;
        $box.removeClass('active');
        setTimeout(function () {
            $box.addClass('active');
        }, 10);
    }

    setTimeout(activateBox, 1000);

    $(document.body).on(
        'updated_wc_div updated_cart_totals cart_totals_refreshed wc_fragments_loaded wc_fragments_refreshed removed_from_cart added_to_cart',
        function ($) {
            setTimeout(activateBox, 100);
        }
    );

    var observer = new MutationObserver(function (mutations) {
        for (var i = 0; i < mutations.length; i++) {
            var m = mutations[i];
            if ($(m.addedNodes).find('.digikala-box-before-ct').length) {
                setTimeout(activateBox, 50);
                break;
            }
        }
    });
    observer.observe(document.body, { childList: true, subtree: true });
});






jQuery(function ($) {
    var COOKIE = 'bwdk_error';

    function getCookie(name) {
        var m = document.cookie.match(new RegExp('(?:^|; )' + name.replace(/([.$?*|{}()\[\]\\\/\+^])/g,'\\$1') + '=([^;]*)'));
        return m ? m[1] : null;
    }



    function eraseCookieAllScopes(name) {
        var host = location.hostname;
        var domains = ['', host, '.' + host];

        if (host.indexOf('www.') === 0) {
            var noWWW = host.replace(/^www\./, '');
            domains.push(noWWW, '.' + noWWW);
        }
        domains.push(host);



        var paths = ['/'];
        var parts = location.pathname.split('/').filter(Boolean);
        var cur = '';
        for (var i = 0; i < parts.length; i++) {
            cur += '/' + parts[i];
            paths.push(cur);
        }

        var base = '; expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0';
        $.each(domains, function (_, d) {
            $.each(paths, function (_, p) {
                document.cookie = name + '=' + base + '; path=/'+ (d ? '; domain=' + d : '');
            });
        });
    }

    var raw = getCookie(COOKIE);
    if (!raw) return;

    try { raw = decodeURIComponent(raw.replace(/\+/g, ' ')); } catch (e) {}


    $('#bwdk-error')
        .html(raw || 'خطای ناشناخته');
    $('.wrap-bwdk-error , .dk-mask').fadeIn();


    eraseCookieAllScopes(COOKIE);
});

