"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFacetedRowModel = getFacetedRowModel;
const utils_1 = require("../utils");
const filterRowsUtils_1 = require("./filterRowsUtils");
function getFacetedRowModel() {
    return (table, columnId) => (0, utils_1.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 (0, filterRowsUtils_1.filterRows)(preRowModel.rows, filterRowsImpl, table);
    }, (0, utils_1.getMemoOptions)(table.options, 'debugTable', 'getFacetedRowModel'));
}
