'use strict';

var useDataTable = require('./useDataTable.cjs');
var filters = require('../../../types/filters.cjs');
var filterOperations = require('../utils/filter-operations.cjs');

function useFilters() {
    const { updateTableQuery } = useDataTable.useDataTable();
    function onAddFilter(column) {
        const columnDef = column.columnDef;
        const id = columnDef.accessorKey || column.id;
        const options = columnDef.filterOptions || [];
        const filterType = columnDef.filterType || filters.FilterType.string;
        const dataType = filterOperations.getDataType({ filterType, dataType: columnDef.dataType });
        const defaultFilter = filters.filterOperators[filterType][0];
        const defaultValue = filterType === filters.FilterType.date
            ? new Date()
            : filterType === filters.FilterType.select
                ? options[0].value
                : "";
        updateTableQuery((query) => {
            return {
                ...query,
                filters: [
                    ...(query.filters || []),
                    // TODO: Add default filter value in column definition
                    {
                        _dataType: dataType,
                        _type: filterType,
                        name: id,
                        value: defaultValue,
                        operator: defaultFilter.value,
                    },
                ],
            };
        });
    }
    function handleRemoveFilter(columnId) {
        updateTableQuery((query) => {
            return {
                ...query,
                filters: query.filters?.filter((filter) => filter.name !== columnId),
            };
        });
    }
    function handleFilterValueChange(columnId, value) {
        updateTableQuery((query) => {
            return {
                ...query,
                filters: query.filters?.map((filter) => {
                    if (filter.name === columnId) {
                        return { ...filter, value };
                    }
                    return filter;
                }),
            };
        });
    }
    function handleFilterOperationChange(columnId, operator) {
        updateTableQuery((query) => {
            return {
                ...query,
                filters: query.filters?.map((filter) => {
                    if (filter.name === columnId) {
                        return { ...filter, operator };
                    }
                    return filter;
                }),
            };
        });
    }
    return {
        onAddFilter,
        handleRemoveFilter,
        handleFilterValueChange,
        handleFilterOperationChange,
    };
}

exports.useFilters = useFilters;
//# sourceMappingURL=useFilters.cjs.map
