import { create } from 'zustand';
import { devtools } from 'zustand/middleware';
/**
 * Creates a filter store with the given configuration and initial state.
 *
 * @template Names - The type of filter names.
 * @template S - The type of the filter state.
 * @template Options - The type of options for the filters.
 *
 * @param {Record<Names, TConfigFilter>} configFilters - The configuration for the filters.
 * @param {S} filterState - The initial state of the filters.
 */
export const setFilterStore = (configFilters, filterState) => create()(devtools((setState, getState) => ({
    setFilter: (name, data) => {
        const state = getState();
        const config = structuredClone(state[name].config);
        Object.keys(config).map((key) => {
            config[key].config.map((item) => {
                delete item.value;
            });
        });
        Object.entries(data).map(([key, value]) => {
            const [name, index] = key.split('__');
            const hasConfig = config[name].config.length;
            if (!hasConfig) {
                delete data[key];
                return;
            }
            config[name].config[Number(index) || 0].value = value;
        });
        setState((state) => ({
            ...state,
            [name]: {
                values: data,
                config,
            },
        }), false, { type: `setFilter/${name}` });
    },
    restFilter: (name) => {
        const state = getState();
        const config = structuredClone(state[name].config);
        Object.keys(config).map((key) => {
            config[key].config.map((item) => {
                delete item.value;
            });
        });
        setState((state) => ({
            ...state,
            [name]: {
                values: {},
                config,
            },
        }), false, { type: `restFilter/${name}` });
    },
    setOptions: (name, options) => {
        const state = getState();
        const config = structuredClone(state[name].config);
        Object.entries(options).map(([key, value]) => {
            if (Object.hasOwn(config, key)) {
                config[key].config = value;
            }
        });
        setState((state) => ({
            ...state,
            [name]: {
                values: state[name].values,
                config,
            },
        }), false, { type: `setOptions/${name}` });
    },
    ...filterState,
}), { name: 'filter-store' }));
//# sourceMappingURL=useFilterStore.js.map