"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.filterRows = filterRows;
/* eslint-disable import/no-cycle */
const row_1 = require("../core/row");
function filterRows(rows, filterRowImpl, table) {
    if (table.options.filterFromLeafRows) {
        return filterRowModelFromLeafs(rows, filterRowImpl, table);
    }
    return filterRowModelFromRoot(rows, filterRowImpl, table);
}
function filterRowModelFromLeafs(rowsToFilter, filterRow, table) {
    var _a;
    const newFilteredFlatRows = [];
    const newFilteredRowsById = {};
    const maxDepth = (_a = table.options.maxLeafRowFilterDepth) !== null && _a !== void 0 ? _a : 100;
    const recurseFilterRows = (inputRows, depth = 0) => {
        var _a;
        const rows = [];
        // Filter from children up first
        for (let i = 0; i < inputRows.length; i++) {
            let row = inputRows[i];
            const newRow = (0, row_1.createRow)(table, row.id, row.original, row.index, row.depth, undefined, row.parentId);
            newRow.columnFilters = row.columnFilters;
            if (((_a = row.subRows) === null || _a === void 0 ? void 0 : _a.length) && depth < maxDepth) {
                newRow.subRows = recurseFilterRows(row.subRows, depth + 1);
                row = newRow;
                if (filterRow(row) && !newRow.subRows.length) {
                    rows.push(row);
                    newFilteredRowsById[row.id] = row;
                    newFilteredFlatRows.push(row);
                    continue;
                }
                if (filterRow(row) || newRow.subRows.length) {
                    rows.push(row);
                    newFilteredRowsById[row.id] = row;
                    newFilteredFlatRows.push(row);
                    continue;
                }
            }
            else {
                row = newRow;
                if (filterRow(row)) {
                    rows.push(row);
                    newFilteredRowsById[row.id] = row;
                    newFilteredFlatRows.push(row);
                }
            }
        }
        return rows;
    };
    return {
        rows: recurseFilterRows(rowsToFilter),
        flatRows: newFilteredFlatRows,
        rowsById: newFilteredRowsById,
    };
}
function filterRowModelFromRoot(rowsToFilter, filterRow, table) {
    var _a;
    const newFilteredFlatRows = [];
    const newFilteredRowsById = {};
    const maxDepth = (_a = table.options.maxLeafRowFilterDepth) !== null && _a !== void 0 ? _a : 100;
    // Filters top level and nested rows
    const recurseFilterRows = (inputRows, depth = 0) => {
        // Filter from parents downward first
        var _a;
        const rows = [];
        // Apply the filter to any subRows
        for (let i = 0; i < inputRows.length; i++) {
            let row = inputRows[i];
            const pass = filterRow(row);
            if (pass) {
                if (((_a = row.subRows) === null || _a === void 0 ? void 0 : _a.length) && depth < maxDepth) {
                    const newRow = (0, row_1.createRow)(table, row.id, row.original, row.index, row.depth, undefined, row.parentId);
                    newRow.subRows = recurseFilterRows(row.subRows, depth + 1);
                    row = newRow;
                }
                rows.push(row);
                newFilteredFlatRows.push(row);
                newFilteredRowsById[row.id] = row;
            }
        }
        return rows;
    };
    return {
        rows: recurseFilterRows(rowsToFilter),
        flatRows: newFilteredFlatRows,
        rowsById: newFilteredRowsById,
    };
}
