import { getMemoOptions, memo } from '../utils';
export function getExpandedRowModel() {
    return (table) => 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);
    }, getMemoOptions(table.options, 'debugTable', 'getExpandedRowModel'));
}
export 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,
    };
}
