import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { FilterProvider, FilterBtn, FilterBar, FilterClear } from '@vertesia/ui/core';
import { useUserSession } from '@vertesia/ui/session';
import { useState } from 'react';
import { VStringFacet } from './utils/VStringFacet';
import { VTypeFacet } from './utils/VTypeFacet';
// Hook to create filter groups for documents
export function useDocumentFilterGroups(facets) {
    const { typeRegistry } = useUserSession();
    const customFilterGroups = [];
    customFilterGroups.push({
        placeholder: 'ID',
        name: 'id',
        type: 'text',
        options: [],
    });
    customFilterGroups.push({
        placeholder: 'Name',
        name: 'name',
        type: 'text',
        options: [],
    });
    if (facets.type) {
        const typeFilterGroup = VTypeFacet({
            buckets: facets.type || [],
            typeRegistry: typeRegistry,
            type: 'select',
            multiple: true
        });
        customFilterGroups.push(typeFilterGroup);
    }
    if (facets.status) {
        const statusFilterGroup = VStringFacet({
            search: null, // This will be provided by the search context
            buckets: facets.status || [],
            name: 'status',
            placeholder: 'Status',
            type: 'select',
            multiple: true
        });
        customFilterGroups.push(statusFilterGroup);
    }
    if (facets.tags) {
        customFilterGroups.push({
            name: 'tags',
            placeholder: 'Tags',
            type: 'stringList',
            options: facets.tags.map((tag) => ({
                label: tag,
                value: tag
            }))
        });
    }
    customFilterGroups.push({
        name: 'created_at',
        placeholder: 'Created Date',
        type: 'date',
        multiple: true,
        options: []
    });
    customFilterGroups.push({
        name: 'updated_at',
        placeholder: 'Updated Date',
        type: 'date',
        multiple: true,
        options: []
    });
    return customFilterGroups;
}
// Hook to create filter change handler for documents
export function useDocumentFilterHandler(search) {
    return (newFilters) => {
        if (newFilters.length === 0) {
            search.clearFilters();
            return;
        }
        search.clearFilters(false);
        newFilters.forEach(filter => {
            if (filter.value && filter.value.length > 0) {
                const filterName = filter.name;
                let filterValue;
                if (filter.type === 'date' && filter.multiple) {
                    // Handle date range filters
                    if (Array.isArray(filter.value) && filter.value.length > 0) {
                        if (filter.value.length === 1) {
                            // Single date - use as both start and end
                            const dateValue = typeof filter.value[0] === 'object' ? filter.value[0].value : filter.value[0];
                            filterValue = {
                                gte: dateValue,
                                lte: dateValue
                            };
                        }
                        else if (filter.value.length === 2) {
                            // Date range - start and end dates
                            const startDate = typeof filter.value[0] === 'object' ? filter.value[0].value : filter.value[0];
                            const endDate = typeof filter.value[1] === 'object' ? filter.value[1].value : filter.value[1];
                            filterValue = {
                                gte: startDate,
                                lte: endDate
                            };
                        }
                    }
                }
                else if (filter.multiple) {
                    if (Array.isArray(filter.value)) {
                        filterValue = filter.value.map((v) => typeof v === 'object' && v.value ? v.value : v);
                    }
                    else {
                        const singleValue = typeof filter.value === 'object' && filter.value.value ? filter.value.value : filter.value;
                        filterValue = [singleValue];
                    }
                }
                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;
                }
                if (filterName === 'name') {
                    search.query.name = filterValue;
                }
                else if (filterName === 'id') {
                    search.query.id = filterValue;
                }
                else {
                    search.query[filterName] = filterValue;
                }
            }
        });
        search.search();
    };
}
// Legacy component for backward compatibility
export function DocumentsFacetsNav({ facets, search, }) {
    const [filters, setFilters] = useState([]);
    const filterGroups = useDocumentFilterGroups(facets);
    const handleFilterLogic = useDocumentFilterHandler(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=DocumentsFacetsNav.js.map