"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLogic = void 0;
const react_1 = require("react");
const react_hook_form_1 = require("react-hook-form");
const useViewportType_1 = require("../../../../components/useViewportType");
const classNames_1 = require("../../../../components/utils/classNames");
const constants_1 = require("../../../FormFiltersSearchField/constants");
const constants_2 = require("../../../FormQuickFilters/constants");
const constants_3 = require("../../constants");
const utils_1 = require("../../utils");
const useLogic = ({ form, onOpenModal, hasMainFilters, hasSecondaryFilters, hasSearch, hasQuickFilters, }) => {
    const { isMobile } = (0, useViewportType_1.useViewportType)();
    const formValues = (0, react_hook_form_1.useWatch)({
        control: form.control,
    });
    const [searchName, setSearchName] = (0, react_1.useState)('');
    const [quickFiltersName, setQuickFiltersName] = (0, react_1.useState)('');
    (0, react_1.useEffect)(() => {
        const searchFieldName = document
            .getElementById(constants_1.FORM_FILTERS_SEARCH_FIELD_ID)
            ?.getAttribute('name') || '';
        setSearchName(searchFieldName);
    }, [hasSearch]);
    (0, react_1.useEffect)(() => {
        const quickFiltersFieldName = document
            .getElementById(constants_2.FORM_FILTERS_QUICK_FILTERS_ID)
            ?.getAttribute('name') || '';
        setQuickFiltersName(quickFiltersFieldName);
    }, [hasQuickFilters]);
    const getCounter = () => {
        const filledValuesCount = (0, utils_1.getFilledCount)(formValues);
        // Исключаем поиск и quickFilters из счётчика — они видны на странице
        // quickFilters учитывается только на мобильных устройствах
        const searchValue = formValues?.[searchName];
        const quickFiltersValue = !isMobile && (0, utils_1.checkIsFilledValue)(formValues?.[quickFiltersName]);
        return (filledValuesCount - (searchValue ? 1 : 0) - (quickFiltersValue ? 1 : 0));
    };
    const allFiltersButtonClassName = (0, react_1.useMemo)(() => {
        return (0, classNames_1.classNames)({
            [constants_3.formFiltersClassnames.showAllFiltersButtonOnDesktop]: hasSecondaryFilters,
            [constants_3.formFiltersClassnames.showAllFiltersButtonOnLaptop]: hasMainFilters || hasSecondaryFilters,
            [constants_3.formFiltersClassnames.showAllFiltersButtonOnMobile]: hasMainFilters || hasSecondaryFilters,
            [constants_3.formFiltersClassnames.withSearch]: hasSearch,
            [constants_3.formFiltersClassnames.withoutSearch]: !hasSearch,
        });
    }, [hasMainFilters, hasSecondaryFilters, hasSearch]);
    return {
        allFiltersButtonClassName,
        counter: getCounter(),
        onOpenModal,
    };
};
exports.useLogic = useLogic;
