import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { FilterProvider, FilterBtn, FilterBar, FilterClear } from '@vertesia/ui/core';
import { useState } from 'react';
export function VFacetsNav({ search, textSearch = '' }) {
    const [filters, setFilters] = useState([]);
    const customFilterGroups = [];
    if (textSearch) {
        customFilterGroups.push({
            placeholder: textSearch,
            name: 'name',
            type: 'text',
            options: [],
        });
    }
    const handleFilterChange = (value) => {
        const newFilters = typeof value === 'function' ? value(filters) : value;
        if (newFilters.length === 0) {
            search.clearFilters();
            setFilters([]);
            return;
        }
        setFilters(newFilters);
        // Reset the actual query before reapplying filters. Otherwise the removed filters remain.
        search.clearFilters(false);
        newFilters.forEach(filter => {
            if (filter.value && filter.value.length > 0) {
                const filterName = filter.name.toLowerCase();
                const filterValue = filter.type === 'stringList'
                    ? filter.value.map(v => typeof v === 'string' ? v : v.value)
                    : Array.isArray(filter.value) && filter.value[0] && typeof filter.value[0] === 'object'
                        ? filter.value[0].value
                        : filter.value;
                switch (filterName) {
                    case 'name':
                        search.query.search_term = filterValue;
                        search.query.name = filterValue;
                        break;
                    default:
                        search.query[filterName] = filterValue;
                        break;
                }
            }
        });
        search.search();
    };
    return (_jsx(FilterProvider, { filterGroups: customFilterGroups, filters: filters, setFilters: handleFilterChange, children: _jsxs("div", { className: "flex gap-2 items-center", children: [_jsx(FilterBtn, {}), _jsx(FilterBar, {}), _jsx(FilterClear, {})] }) }));
}
//# sourceMappingURL=VFacetsNav.js.map