"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getExpandedRowModel = getExpandedRowModel;
exports.expandRows = expandRows;
const utils_1 = require("../utils");
function getExpandedRowModel() {
    return (table) => (0, utils_1.memo)(() => [
        table.getState().expanded,
        table.getPreExpandedRowModel(),
        table.options.paginateExpandedRows,
    ], (expanded, rowModel, paginateExpandedRows) => {
        if (!rowModel.rows.length || (expanded !== true && !Object.keys(expanded !== null && expanded !== void 0 ? expanded : {}).length)) {
            return rowModel;
        }
        if (!paginateExpandedRows) {
            // Only expand rows at this point if they are being paginated
            return rowModel;
        }
        return expandRows(rowModel);
    }, (0, utils_1.getMemoOptions)(table.options, 'debugTable', 'getExpandedRowModel'));
}
function expandRows(rowModel) {
    const expandedRows = [];
    const handleRow = (row) => {
        var _a;
        expandedRows.push(row);
        if (((_a = row.subRows) === null || _a === void 0 ? void 0 : _a.length) && row.getIsExpanded()) {
            row.subRows.forEach(handleRow);
        }
    };
    rowModel.rows.forEach(handleRow);
    return {
        rows: expandedRows,
        flatRows: rowModel.flatRows,
        rowsById: rowModel.rowsById,
    };
}
