import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { FilterProvider, FilterBtn, FilterBar, FilterClear } from '@vertesia/ui/core';
import { useState } from 'react';
// Hook to create filter groups for prompts
export function usePromptsFilterGroups(facets) {
    void facets;
    const customFilterGroups = [];
    // Add name filter as text type
    const nameFilterGroup = {
        name: 'name',
        placeholder: 'Name',
        type: 'text',
        multiple: false
    };
    customFilterGroups.push(nameFilterGroup);
    // Add role filter as select type if role facets are available
    if (facets.role && facets.role.length > 0) {
        const rolesFilterGroup = {
            name: 'role',
            placeholder: 'Role',
            type: 'select',
            options: facets.role.map((facet) => ({
                label: facet._id,
                value: facet._id,
                count: facet.count
            })),
        };
        customFilterGroups.push(rolesFilterGroup);
    }
    return customFilterGroups;
}
// Hook to create filter change handler for prompts
export function usePromptsFilterHandler(search) {
    return (newFilters) => {
        if (newFilters.length === 0) {
            // Clear filters without applying defaults - user wants to remove all filters
            search.clearFilters(true, false);
            return;
        }
        // Clear all filters first without defaults, then apply new ones
        search.clearFilters(false, false);
        newFilters.forEach(filter => {
            if (filter.value && filter.value.length > 0) {
                const filterName = filter.name;
                let filterValue;
                if (filter.type === 'stringList') {
                    filterValue = filter.value.map(v => typeof v === 'string' ? v : v.value);
                }
                else if (filter.multiple) {
                    filterValue = Array.isArray(filter.value)
                        ? filter.value.map((v) => typeof v === 'object' && v.value ? v.value : v)
                        : [typeof filter.value === 'object' && filter.value.value ? filter.value.value : filter.value];
                }
                else {
                    // Single value - don't wrap in array
                    filterValue = Array.isArray(filter.value) && filter.value[0] && typeof filter.value[0] === 'object'
                        ? filter.value[0].value
                        : Array.isArray(filter.value) && filter.value[0]
                            ? filter.value[0]
                            : filter.value;
                }
                search.query[filterName] = filterValue;
            }
        });
        search.search();
    };
}
// Component for prompts filtering
export function PromptsFacetsNav({ facets, search }) {
    const [filters, setFilters] = useState([]);
    const filterGroups = usePromptsFilterGroups(facets);
    const handleFilterLogic = usePromptsFilterHandler(search);
    const handleFilterChange = (value) => {
        const newFilters = typeof value === 'function' ? value(filters) : value;
        setFilters(newFilters);
        handleFilterLogic(newFilters);
    };
    return (_jsx(FilterProvider, { filterGroups: filterGroups, filters: filters, setFilters: handleFilterChange, children: _jsxs("div", { className: "flex gap-2 items-center", children: [_jsx(FilterBtn, {}), _jsx(FilterBar, {}), _jsx(FilterClear, {})] }) }));
}
//# sourceMappingURL=PromptsFacetsNav.js.map