(function ($) {
    'use strict';

    const ariaManager = {
        setAriaHidden: function ($el, isHidden) {
            if ($el && $el.length) {
                $el.attr('aria-hidden', isHidden ? 'true' : 'false');
            }
        },
        setAriaExpanded: function ($el, isExpanded) {
            if ($el && $el.length) {
                $el.attr('aria-expanded', isExpanded ? 'true' : 'false');
            }
        },
        updateSidebarAria: function ($sidebar, $sidebarWrapper, $toggleButton, isMobile) {
            const isOpen = $sidebar.hasClass('prodigy-shop-sidebar--open');
            const shouldBeHidden = isMobile && !isOpen;

            this.setAriaHidden($sidebarWrapper, shouldBeHidden ? true : false);
            this.setAriaExpanded($toggleButton, isOpen);
        },
        resizeWindow: function ($sidebar, $sidebarWrapper) {
            $(window).on('resize', function () {
               const isMobileNow = window.innerWidth < 1024;
               const isOpenNow = $sidebar.hasClass('prodigy-shop-sidebar--open');
               $sidebarWrapper.attr('aria-hidden', (isMobileNow && !isOpenNow) ? 'true' : 'false');
            }).trigger('resize');
        },
        checkClassName: function ($elem, $className) {
            return $elem.hasClass($className);
        },
        moveFocus: function ($focusTarget) {
            if ($focusTarget && $focusTarget.length) {
                $focusTarget.focus();
            } else {
                $('body').focus();
            }
        },
    }

    $(document).ready(
        function () {
            // left column sidebar on lg screens accessibility
            const $sidebarWrapper = $('#left-sidebar');
            var $sidebarAsideClass = 'prodigy-shop-sidebar--lg-aside';
            var $sidebar = $('#filter');
            const $toggleButton = $('.prodigy-filter__sm-btn-lg-none');

            var isMobile = window.innerWidth < 1024;

            ariaManager.updateSidebarAria($sidebar, $sidebarWrapper, $toggleButton, isMobile);


            const filterToggleBtnHandler = () => {
                if(ariaManager.checkClassName($sidebar, $sidebarAsideClass) === 'false') {
                    $sidebar.toggleClass('prodigy-shop-sidebar--open');
                }

                const nowOpen = ariaManager.checkClassName($sidebar,'prodigy-shop-sidebar--open');

                if (!nowOpen) {
                    ariaManager.moveFocus($toggleButton);
                }

                ariaManager.setAriaHidden($sidebarWrapper, !nowOpen);
                ariaManager.setAriaExpanded($toggleButton, nowOpen);

                $('html,body').toggleClass('prodigy-overflow-y-hidden', nowOpen);
            };

            $('body').on('click', '#filter-toggle-btn-close, .prodigy-filter-toggle-btn-2, #filter-toggle-btn-close, #shop-sidebar-backdrop-js', filterToggleBtnHandler);

            ariaManager.resizeWindow($sidebar, $sidebarWrapper);

            var active_id = $('.active-category-js').data('category-id');
            var parent_id = $('.parent-category-js').data('parent-id');

            if (typeof active_id !== 'undefined' && typeof parent_id !== 'undefined') {
                control_menu(active_id, parent_id);
            }

            function control_menu(category_active_id, parent_id) {

                const card_element = document.querySelector('#cat_' + parent_id);
                const collapse_element = document.querySelector('#collapse_' + parent_id);
                const collapse_heading_element_link = document.querySelector('#headingCollapse_' + parent_id + ' .filter__card-link');
                const collapse_button = document.querySelector('#headingCollapse_' + parent_id + ' .prodigy-filter__card-btn');

                if (category_active_id !== parent_id) {
                    const sub_cut_element = document.querySelector('#sub_cat_' + category_active_id);
                    if (sub_cut_element) {
                        sub_cut_element.classList.toggle('filter__card-list-item--active');
                        sub_cut_element.classList.add('font-800');
                    }
                    if (collapse_element) {
                        collapse_element.classList.toggle('show');
                    }

                }

                if (collapse_button) {
                    collapse_button.setAttribute('aria-expanded', 'true');
                }

                if (card_element) {
                    collapse_heading_element_link.classList.replace('filter__card-link', 'filter__card-link--active');
                    collapse_heading_element_link.classList.add('font-800');
                    collapse_button.setAttribute('aria-expanded', 'false');
                }
            }

        }
    );

})(jQuery);
