import { getMemoOptions, memo } from '../utils';
import { filterRows } from './filterRowsUtils';
export function getFacetedRowModel() {
    return (table, columnId) => memo(() => [
        table.getPreFilteredRowModel(),
        table.getState().columnFilters,
        table.getState().globalFilter,
        table.getFilteredRowModel(),
    ], (preRowModel, columnFilters, globalFilter) => {
        if (!preRowModel.rows.length || (!(columnFilters === null || columnFilters === void 0 ? void 0 : columnFilters.length) && !globalFilter)) {
            return preRowModel;
        }
        const filterableIds = [
            ...columnFilters.map((d) => d.id).filter((d) => d !== columnId),
            globalFilter ? '__global__' : undefined,
        ].filter(Boolean);
        const filterRowsImpl = (row) => {
            // Horizontally filter rows through each column
            for (let i = 0; i < filterableIds.length; i++) {
                if (row.columnFilters[filterableIds[i]] === false) {
                    return false;
                }
            }
            return true;
        };
        return filterRows(preRowModel.rows, filterRowsImpl, table);
    }, getMemoOptions(table.options, 'debugTable', 'getFacetedRowModel'));
}
