import { useState, useCallback, useMemo } from 'react';
import { filterData } from '../Table.filterUtils.js';

function useTableFilter({ filterConfig = [], onFilterChange, filterLogic: controlledLogic, onFilterLogicChange, data, columns, enableBuiltInFilter = false, }) {
    // Internal logic state (used when not controlled)
    const [internalLogic, setInternalLogic] = useState('and');
    const filterLogic = controlledLogic !== null && controlledLogic !== void 0 ? controlledLogic : internalLogic;
    const handleSetLogic = useCallback((logic) => {
        if (onFilterLogicChange) {
            onFilterLogicChange(logic);
        }
        else {
            setInternalLogic(logic);
        }
    }, [onFilterLogicChange]);
    const handleToggleLogic = useCallback(() => {
        handleSetLogic(filterLogic === 'and' ? 'or' : 'and');
    }, [filterLogic, handleSetLogic]);
    const generateFilterId = useCallback(() => {
        return `filter-${Date.now()}-${Math.random().toString(36).substring(2, 7)}`;
    }, []);
    const handleAddFilter = useCallback((columnId, value, operator) => {
        if (!onFilterChange)
            return;
        const existing = filterConfig.find(f => f.columnId === columnId);
        if (existing) {
            const updated = filterConfig.map(f => f.columnId === columnId ? { ...f, value, operator } : f);
            onFilterChange(updated);
        }
        else {
            onFilterChange([...filterConfig, { id: generateFilterId(), columnId, value, operator }]);
        }
    }, [filterConfig, onFilterChange, generateFilterId]);
    const handleRemoveFilter = useCallback((columnId) => {
        if (!onFilterChange)
            return;
        onFilterChange(filterConfig.filter(f => f.columnId !== columnId));
    }, [filterConfig, onFilterChange]);
    const handleUpdateFilter = useCallback((columnId, value, operator) => {
        if (!onFilterChange)
            return;
        onFilterChange(filterConfig.map(f => f.columnId === columnId ? { ...f, value, operator } : f));
    }, [filterConfig, onFilterChange]);
    const handleUpdateFilterById = useCallback((filterId, updates) => {
        if (!onFilterChange)
            return;
        onFilterChange(filterConfig.map(f => f.id === filterId ? { ...f, ...updates } : f));
    }, [filterConfig, onFilterChange]);
    const handleClearFilters = useCallback(() => {
        onFilterChange === null || onFilterChange === void 0 ? void 0 : onFilterChange([]);
    }, [onFilterChange]);
    // Compute filtered data when built-in filtering is enabled
    const filteredData = useMemo(() => {
        if (!enableBuiltInFilter || !data || !columns)
            return data || [];
        return filterData(data, columns, filterConfig, filterLogic);
    }, [enableBuiltInFilter, data, columns, filterConfig, filterLogic]);
    return {
        handleAddFilter,
        handleRemoveFilter,
        handleUpdateFilter,
        handleUpdateFilterById,
        handleClearFilters,
        filterLogic,
        handleToggleLogic,
        handleSetLogic,
        generateFilterId,
        filteredData,
    };
}

export { useTableFilter };
