'use strict';

var jsxRuntime = require('../../../node_modules/.pnpm/react@18.2.0/node_modules/react/jsx-runtime.cjs');
var filterChip = require('../../filter-chip/filter-chip.cjs');
var iconButton = require('../../icon-button/icon-button.cjs');
var button = require('../../button/button.cjs');
var filter = require('../../icons/assets/filter.svg.cjs');
var dropdownMenu = require('../../dropdown-menu/dropdown-menu.cjs');
var useDataTable = require('../hooks/useDataTable.cjs');
var flex = require('../../flex/flex.cjs');
var useFilters = require('../hooks/useFilters.cjs');

function AddFilter({ columnList = [], appliedFiltersSet, onAddFilter, }) {
    const availableFilters = columnList?.filter((col) => !appliedFiltersSet.has(col.id));
    return availableFilters.length > 0 ? (jsxRuntime.jsxRuntimeExports.jsxs(dropdownMenu.DropdownMenu, { children: [jsxRuntime.jsxRuntimeExports.jsx(dropdownMenu.DropdownMenu.Trigger, { asChild: true, children: appliedFiltersSet.size > 0 ? (jsxRuntime.jsxRuntimeExports.jsx(iconButton.IconButton, { size: 4, children: jsxRuntime.jsxRuntimeExports.jsx(filter.ReactComponent, {}) })) : (jsxRuntime.jsxRuntimeExports.jsx(button.Button, { variant: "text", size: "small", leadingIcon: jsxRuntime.jsxRuntimeExports.jsx(filter.ReactComponent, {}), children: "Filter" })) }), jsxRuntime.jsxRuntimeExports.jsx(dropdownMenu.DropdownMenu.Content, { align: "start", children: availableFilters?.map((column) => {
                    const columnDef = column.columnDef;
                    const id = columnDef.accessorKey || column.id;
                    return (jsxRuntime.jsxRuntimeExports.jsx(dropdownMenu.DropdownMenu.Item, { onSelect: () => onAddFilter(column), children: columnDef.header || id }, id));
                }) })] })) : null;
}
function Filters() {
    const { table, tableQuery } = useDataTable.useDataTable();
    const columns = table?.getAllColumns();
    const { onAddFilter, handleRemoveFilter, handleFilterValueChange, handleFilterOperationChange, } = useFilters.useFilters();
    const columnList = columns?.filter((column) => column.columnDef.enableColumnFilter);
    const appliedFiltersSet = new Set(tableQuery?.filters?.map((filter) => filter.name));
    const appliedFilters = tableQuery?.filters?.map((filter) => {
        const columnDef = columns?.find((col) => {
            const columnDef = col.columnDef;
            const id = columnDef.accessorKey || col.id;
            return id === filter.name;
        })?.columnDef;
        return {
            columnType: columnDef?.columnType || "text",
            label: columnDef?.header || "",
            options: columnDef?.filterOptions || [],
            ...filter,
        };
    }) || [];
    return (jsxRuntime.jsxRuntimeExports.jsxs(flex.Flex, { gap: 3, children: [jsxRuntime.jsxRuntimeExports.jsx(flex.Flex, { gap: 3, children: appliedFilters.map((filter) => (jsxRuntime.jsxRuntimeExports.jsx(filterChip.FilterChip, { label: filter.label, value: filter.value, onRemove: () => handleRemoveFilter(filter.name), onValueChange: (value) => handleFilterValueChange(filter.name, value), onOperationChange: (operator) => handleFilterOperationChange(filter.name, operator), columnType: filter.columnType, options: filter.options }, filter.name))) }), jsxRuntime.jsxRuntimeExports.jsx(AddFilter, { columnList: columnList, appliedFiltersSet: appliedFiltersSet, onAddFilter: onAddFilter })] }));
}

exports.Filters = Filters;
//# sourceMappingURL=filters.cjs.map
