"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useFilters = void 0;
const react_1 = __importDefault(require("react"));
// @ts-expect-error missing types
const utils_1 = require("@usereactify/search-internals/lib/utils");
// @ts-expect-error missing types
const actions_1 = require("@appbaseio/reactivecore/lib/actions");
const hooks_1 = require("../hooks");
const utility_1 = require("../utility");
const useFilters = () => {
    var _a;
    const { options, config } = (0, hooks_1.useReactifySearchContext)();
    const reactivesearchContext = react_1.default.useContext(utils_1.ReactReduxContext);
    const filterStack = react_1.default.useMemo(() => {
        // select filters by type
        const matchingFilterss = config.filters.filter((filter) => filter.type === options.mode);
        // select filter stack by handle if provided
        if (options.filtersHandle) {
            const matchingFilters = matchingFilterss.find((filterStack) => options.filtersHandle === filterStack.handle);
            if (matchingFilters) {
                return matchingFilters;
            }
        }
        // select filter stack by collection if provided
        if (options.mode === "collection" && options.collectionHandle) {
            const matchingFilters = matchingFilterss.find((filterStack) => { var _a; return (_a = filterStack.collections) === null || _a === void 0 ? void 0 : _a.includes(options.collectionHandle); });
            if (matchingFilters) {
                return matchingFilters;
            }
        }
        // select filter with "default" handle
        let matchingFilters = matchingFilterss.find((filterStack) => filterStack.handle === "default");
        if (matchingFilters) {
            return matchingFilters;
        }
        // select any filter with "default" handle
        matchingFilters = config.filters.find((filterStack) => filterStack.handle === "default");
        if (matchingFilters) {
            return matchingFilters;
        }
        // select any available filter as a last resort
        matchingFilters = config.filters[0];
        return matchingFilters;
    }, [
        config,
        options.mode === "collection" ? options.collectionHandle : undefined,
    ]);
    const filters = react_1.default.useMemo(() => {
        const sortedFilters = filterStack === null || filterStack === void 0 ? void 0 : filterStack.options.sort((a, b) => a.position > b.position ? 1 : -1);
        return sortedFilters;
    }, [filterStack]);
    const selected = react_1.default.useMemo(() => {
        return Object.keys(reactivesearchContext.storeState.selectedValues)
            .filter((item) => filters.find((filter) => filter.handle === item))
            .map((item) => ({
            handle: item,
            value: reactivesearchContext.storeState.selectedValues[item].value,
        }));
    }, [filters, (_a = reactivesearchContext === null || reactivesearchContext === void 0 ? void 0 : reactivesearchContext.storeState) === null || _a === void 0 ? void 0 : _a.selectedValues]);
    const setFilterValue = react_1.default.useCallback((handle, value) => {
        utility_1.debug.log("useFilters", "setFilterValue[handle,value]", handle, value);
        reactivesearchContext.store.dispatch((0, actions_1.setValue)(handle, value));
    }, [reactivesearchContext]);
    utility_1.debug.hook("useFilters", "filterStack", filterStack);
    utility_1.debug.hook("useFilters", "filters", filters);
    utility_1.debug.hook("useFilters", "selected", selected);
    return {
        filterStack: filterStack,
        filters: filters,
        selected: selected,
        setFilterValue: setFilterValue,
    };
};
exports.useFilters = useFilters;
//# sourceMappingURL=useFilters.js.map