// -----------------------------------------------------------------------------
// Sidebar

booyah.$.$pageSidebar = $('.page-sidebar');

if (booyah.$.$pageSidebar.length) {

    booyah.$.$pageSidebar.$nav = booyah.$.$pageSidebar.find('.sidebar-nav');
    booyah.$.$pageSidebar.$nav.$subnavs = booyah.$.$pageSidebar.$nav.find('.sidebar-subnav');
    booyah.$.$pageSidebar.$nav.$subnavToggleButtons = booyah.$.$pageSidebar.$nav.find('button');

    booyah.$.$pageSidebar.$nav.$subnavToggleButtons.on('click', function () {
        let $button = $(this);
        let $parentNav_item = $button.closest('li');
        let $subnavs = booyah.$.$pageSidebar.$nav.$subnavs;
        let $subnav_affected = $button.next($subnavs);
        let $subnavs_other = $subnavs.not($subnav_affected);
        let toggleMode = $parentNav_item.is('.expanded') ? 'hide' : 'show';
        booyah.$.$pageSidebar.$nav.toggleSubnavs($subnav_affected, toggleMode);
        booyah.$.$pageSidebar.$nav.toggleSubnavs($subnavs_other, 'hide');
    });

    booyah.$.$pageSidebar.$nav.toggleSubnavs = function ($subnavs, mode) {
        let animation_done_css = {
            'height': '',
            'padding': '',
            'margin': ''
        };
        let $subnav, $subnav_list, $parentNav_item;
        $subnavs.each(function (i, subnav) {
            $subnav = $(subnav);
            $subnav_list = $subnav.find('> ul');
            $parentNav_item = $subnav.closest('li');
            $subnav_list.stop();
            if (mode === 'show') {
                $parentNav_item.addClass('expanded');
                $subnav_list.slideDown(function () {
                    $(this).css(animation_done_css);
                });
            } else {
                $parentNav_item.removeClass('expanded');
                $subnav_list.slideUp(function () {
                    $(this).css(animation_done_css);
                });
            }
        });
    };

    // Mobil - Toggel

    booyah.$.$pageSidebar.toggleButton = $('.btn--toggle-sidebar');
    booyah.$.$pageSidebar.toggleButton.on('click', function () {
        booyah.$.$pageSidebar.toggleVisibility();
    });

    booyah.$.$pageSidebar.toggleVisibility = function () {
        if ($('body').hasClass('sidebar-hidden')) {
            $('body').removeClass('sidebar-hidden');
        } else {
            $('body').addClass('sidebar-hidden');
        }
    };

    booyah.$.$pageSidebar.navToggelButton = $('#toggel-navbar');

    booyah.$.$pageSidebar.navToggelButton.on('click', function () {
        booyah.$.$pageSidebar.toggleClass('mobile-nav');
    });

    booyah.$.$pageSidebar.toggleMobile = function () {
        if ($.inArray(Booyah.currentBreakpoint, ['md', 'sm', 'xs']) < 0) {
            $('body').removeClass('sidebar-hidden');
        } else {
            $('body').addClass('sidebar-hidden')
        }
    };

    $(window).on('ready load', function () {
        booyah.$.$pageSidebar.toggleMobile();
    });

    $('.nav-active-line .nav-item a').on('click', function () {
        let li = $(this).parents('li');
        if (! li.hasClass('active')) {
            $(this).parents('ul').find('li.active').removeClass('active');
            li.addClass('active');
        }
    });
}
