(function ($) {
    "use strict";

    $('body').on('click', '.xs-categories-toggle', function (e) {
        if (e.target !== this) return;

        if ($(this).hasClass('show-items')) {
            $(this).removeClass('show-items');
        } else {
            $(this).addClass('show-items');
        }
    });

    $('body').on('click', this, function (event) {
        if (!$(event.target).closest('.menu-category').length) {
            $(".xs-categories-toggle").removeClass('show-items');
        }
    });

    $('body').on('click', '.cat-dropdown-menu > li', function (e) {
        const $this = $(this);

        const hasOpen = $this.hasClass('show-sub-menu');

        $('.cat-dropdown-menu > li').each((key, item) => {
            $(item).removeClass('show-sub-menu')
        });

        if (hasOpen) {
            $this.removeClass('show-sub-menu');
        } else {
            $this.addClass('show-sub-menu');
        }
    });

    $('body').on('click', '#navbarClose', function (e) {
        $('#navbarContent').removeClass('show');
    });

    $('body').on('click', '#navbarToggle', function (e) {
        $('#navbarContent').addClass('show');
    });

    function isMobileNavDropdownViewport() {
        return window.innerWidth <= 767;
    }

    function forceCloseMobileNavDropdown($dropdown) {
        const $toggle = $dropdown.find('[data-toggle="dropdown"]');
        const $menu = $dropdown.find('.dropdown-menu');

        $menu.removeClass('show').removeAttr('style');
        $toggle.attr('aria-expanded', 'false');
        $dropdown.removeClass('show');

        if ($toggle.length && typeof $toggle.dropdown === 'function') {
            try {
                $toggle.dropdown('hide');
            } catch (err) {
                // Bootstrap dropdown may not be initialized yet.
            }
        }
    }

    function closeAllMobileNavDropdowns() {
        if (!isMobileNavDropdownViewport()) {
            return;
        }

        $('.nav-notify-cart-dropdown .dropdown').each(function () {
            forceCloseMobileNavDropdown($(this));
        });
    }

    $('body').on('click', '.close-dropdown', function (e) {
        e.preventDefault();
        e.stopPropagation();
        forceCloseMobileNavDropdown($(this).closest('.dropdown'));
    });

    // On mobile, handle cart/notification panels manually so Popper inline styles
    // cannot leave the "all notifications" action bar stuck on screen.
    $('body').on('click', '.nav-notify-cart-dropdown [data-toggle="dropdown"]', function (e) {
        if (!isMobileNavDropdownViewport()) {
            return;
        }

        e.preventDefault();
        e.stopImmediatePropagation();

        const $toggle = $(this);
        const $dropdown = $toggle.closest('.dropdown');
        const $menu = $dropdown.find('.dropdown-menu');
        const willOpen = !$menu.hasClass('show');

        closeAllMobileNavDropdowns();

        if (willOpen) {
            $menu.addClass('show');
            $toggle.attr('aria-expanded', 'true');
            $dropdown.addClass('show');
        }
    });

    $(document).on('click', function (e) {
        if (!isMobileNavDropdownViewport()) {
            return;
        }

        if (!$(e.target).closest('.nav-notify-cart-dropdown .dropdown').length) {
            closeAllMobileNavDropdowns();
        }
    });

    $(window).on('scroll', closeAllMobileNavDropdowns);

    $(document).ready(closeAllMobileNavDropdowns);
    $(window).on('load', closeAllMobileNavDropdowns);

    $('body').on('hidden.bs.dropdown', '.nav-notify-cart-dropdown .dropdown', function () {
        forceCloseMobileNavDropdown($(this));
    });

    $(window).on('resize', closeAllMobileNavDropdowns);

    var navbar = document.getElementById("navbar");
    var navbarVacuum = document.getElementById("navbarVacuum");

    function isMobileStickyDisabledViewport() {
        return window.innerWidth <= 767;
    }

    function resetMobileNavbarStickyState() {
        if (!navbar || !navbarVacuum) {
            return;
        }

        navbarVacuum.style.height = '0';
        navbar.classList.remove("sticky");
    }

    var sticky = navbar ? navbar.offsetTop : 0;

    function myFunction() {
        if (!navbar || !navbarVacuum) {
            return;
        }

        if (isMobileStickyDisabledViewport()) {
            resetMobileNavbarStickyState();
            return;
        }

        if (window.pageYOffset >= sticky) {
            navbarVacuum.style.height = navbar.offsetHeight + 'px';
            navbar.classList.add("sticky")
        } else {
            navbarVacuum.style.height = 0;
            navbar.classList.remove("sticky");
        }
    }

    window.onscroll = function () {
        myFunction();
    };

    $(window).on('resize', function () {
        if (isMobileStickyDisabledViewport()) {
            resetMobileNavbarStickyState();
            return;
        }

        sticky = navbar.offsetTop;
        myFunction();
    });

    resetMobileNavbarStickyState();
    myFunction();

})(jQuery);
