import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { FilterProvider, FilterBtn, FilterBar, FilterClear } from '@vertesia/ui/core';
import { useState } from 'react';
import { VStringFacet } from './utils/VStringFacet';
import { VUserFacet } from './utils/VUserFacet';
// Hook to create filter groups for workflow executions
export function useWorkflowExecutionsFilterGroups(facets) {
    const customFilterGroups = [];
    customFilterGroups.push({
        placeholder: 'Workflow or Run ID',
        name: 'name',
        type: 'text',
        options: [],
    });
    if (facets.status) {
        const statusFilterGroup = VStringFacet({
            search: null, // This will be provided by the search context
            buckets: facets.status || [],
            name: 'status',
            placeholder: 'Status'
        });
        customFilterGroups.push(statusFilterGroup);
    }
    if (facets.initiated_by) {
        const initiatedByFilterGroup = VUserFacet({
            buckets: facets.initiated_by || [],
            name: 'initiated_by',
            placeholder: 'Initiated By'
        });
        customFilterGroups.push(initiatedByFilterGroup);
    }
    const dateAfterFilterGroup = {
        name: 'start',
        placeholder: 'Date After',
        type: 'date',
        multiple: false
    };
    customFilterGroups.push(dateAfterFilterGroup);
    const dateBeforeFilterGroup = {
        name: 'end',
        placeholder: 'Date Before',
        type: 'date',
        multiple: false
    };
    customFilterGroups.push(dateBeforeFilterGroup);
    return customFilterGroups;
}
// Hook to create filter change handler for workflow executions
export function useWorkflowExecutionsFilterHandler(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 === '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;
                }
                if (filterName === 'name') {
                    search.query.search_term = filterValue;
                    search.query.name = filterValue;
                }
                else {
                    search.query[filterName] = filterValue;
                }
            }
        });
        search.search();
    };
}
// Legacy component for backward compatibility
export function WorkflowExecutionsFacetsNav({ facets, search, }) {
    const [filters, setFilters] = useState([]);
    const filterGroups = useWorkflowExecutionsFilterGroups(facets);
    const handleFilterLogic = useWorkflowExecutionsFilterHandler(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=WorkflowExecutionsFacetsNav.js.map