var Wick = window.Wick || {};

Wick.PLPFiltersMobile = {
    ROOT_WRAPPER:
        '.page-KitchenPLPPage, .products-list-page:not(.page-responsiveThematicProductListPage)',
    PRODUCTS_LIST: '.products-list-page',
    INPUTS: '.filter-card__checkbox, .swatch__checkbox, .checkbox__input',

    MODAL_SEL: '#filterModal',
    MODAL_VIEW_BTN: '#filterModal .btn.btn-primary',
    MODAL_CLEAR_BTN: '#filterModal .btn.btn-secondary',
    $ACCORDION: '.accordion',
    $ACCORDION_CONTENT: '.accordion__content',
    ACCORDION_COLLAPSED: 'accordion_collapsed',

    getPlural(value, one, many) {
        return value === 1 ? one : many;
    },

    isProductPage() {
        return $(Wick.PLPFiltersMobile.PRODUCTS_LIST).length > 0;
    },

    getMobileCounts() {
        const isProducts = Wick.PLPFiltersMobile.isProductPage();

        if (isProducts) {
            const total = (Wick.ProductCounter && Wick.ProductCounter.count) || 0;
            return { mode: 'products', total };
        }

        const total = Wick.PLPFilters.totalApplied();
        return { mode: 'filters', total };
    },

    updateMobileFilterInfo() {
        const info = $('.mobile-nav__item[data-target="#filterModal"] .mobile-nav__info');
        if (!info.length) return;

        const { mode, total } = Wick.PLPFiltersMobile.getMobileCounts();

        if (mode === 'products') {
            return;
        }

        if (total > 0) {
            const label = Wick.PLPFiltersMobile.getPlural(total, 'filter', 'filters');
            info.text(`${total} ${label} applied`);
        } else {
            info.text('No filters applied');
        }
    },

    updateViewResultsButton() {
        const { mode } = Wick.PLPFiltersMobile.getMobileCounts();

        if (mode === 'products') {
            return;
        }

        const $btn = $(Wick.PLPFiltersMobile.MODAL_VIEW_BTN);
        if (!$btn.length) return;

        const { total } = Wick.PLPFiltersMobile.getMobileCounts();
        $btn.text(total > 0 ? `View results (${total})` : 'View results');
    },

    init() {
        // two-way binding for input between desktop and modal
        $(document)
            .off('change.plpFiltersMobile', Wick.PLPFiltersMobile.INPUTS)
            .on('change.plpFiltersMobile', Wick.PLPFiltersMobile.INPUTS, function () {
                const inp = $(this);
                const name = inp.attr('name');
                if (!name) return;

                Wick.PLPUtils.syncTwinInputsByName(name, inp.prop('checked'), inp);
            });

        // when the modal is opened — synchronize its checkboxes with state
        $(document)
            .off('shown.bs.modal.plpFiltersMobile', Wick.PLPFiltersMobile.MODAL_SEL)
            .on('shown.bs.modal.plpFiltersMobile', Wick.PLPFiltersMobile.MODAL_SEL, function () {
                Wick.PLPFilters.setDOMFromState($(this), Wick.PLPFilters.draftState);
            });

        $(document)
            .off('hide.bs.modal.plpFiltersMobile', Wick.PLPFiltersMobile.MODAL_SEL)
            .on('hide.bs.modal.plpFiltersMobile', Wick.PLPFiltersMobile.MODAL_SEL, function () {
                const scope = $(Wick.PLPFiltersMobile.ROOT_WRAPPER).add($(this));
                Wick.PLPFilters.draftState = Wick.PLPFilters.collectStateFromDOM(scope);
            });

        // mobile "View results" button in modal
        $(document)
            .off('click.plpFiltersMobile', Wick.PLPFiltersMobile.MODAL_VIEW_BTN)
            .on('click.plpFiltersMobile', Wick.PLPFiltersMobile.MODAL_VIEW_BTN, function (e) {
                if ($(this).data('is-plp-srp')) {
                    e.stopImmediatePropagation();
                    e.preventDefault();
                    return;
                }

                e.preventDefault();

                const scope = $(Wick.PLPFiltersMobile.ROOT_WRAPPER).add(
                    $(Wick.PLPFiltersMobile.MODAL_SEL)
                );

                Wick.PLPFilters.applyFromScopes(scope, true);
            });

        // mobile "Clear all" button in modal
        $(document)
            .off('click.plpFiltersMobile', Wick.PLPFiltersMobile.MODAL_CLEAR_BTN)
            .on('click.plpFiltersMobile', Wick.PLPFiltersMobile.MODAL_CLEAR_BTN, function (e) {
                if ($(this).data('is-plp-srp')) {
                    e.stopImmediatePropagation();
                    e.preventDefault();
                    return;
                }

                e.preventDefault();

                Wick.PLPFilters.draftState = {};
                Wick.PLPFilters.appliedState = {};
                Wick.PLPFilters.writeStateToURL(Wick.PLPFilters.appliedState, false);

                $(document)
                    .find(Wick.PLPFiltersMobile.INPUTS + ':checked')
                    .prop('checked', false);

                Wick.PLPFilters.updateSelectedUI($(Wick.PLPFiltersMobile.ROOT_WRAPPER));
            });

        // emulation watcher (stub backend)
        $(document)
            .off('productCounter:change.plpFiltersMobile')
            .on('productCounter:change.plpFiltersMobile', function () {
                Wick.PLPFiltersMobile.updateMobileFilterInfo();
                Wick.PLPFiltersMobile.updateViewResultsButton();
            });

        Wick.PLPFiltersMobile.updateMobileFilterInfo();
        Wick.PLPFiltersMobile.updateViewResultsButton();
    },
};
