"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCoreRowModel = getCoreRowModel;
/* eslint-disable import/no-cycle */
const row_1 = require("../core/row");
const utils_1 = require("../utils");
function getCoreRowModel() {
    return (table) => (0, utils_1.memo)(() => [table.options.data], (data) => {
        const rowModel = {
            rows: [],
            flatRows: [],
            rowsById: {},
        };
        const accessRows = (originalRows, depth = 0, parentRow) => {
            var _a;
            const rows = [];
            for (let i = 0; i < (originalRows === null || originalRows === void 0 ? void 0 : originalRows.length); i++) {
                // This could be an expensive check at scale, so we should move it somewhere else, but where?
                // if (!id) {
                //   if (process.env.NODE_ENV !== 'production') {
                //     throw new Error(`getRowId expected an ID, but got ${id}`)
                //   }
                // }
                // Make the row
                const row = (0, row_1.createRow)(table, table._getRowId(originalRows[i], i, parentRow), originalRows[i], i, depth, undefined, parentRow === null || parentRow === void 0 ? void 0 : parentRow.id);
                // Keep track of every row in a flat array
                rowModel.flatRows.push(row);
                // Also keep track of every row by its ID
                rowModel.rowsById[row.id] = row;
                // Push table row into parent
                rows.push(row);
                // Get the original subrows
                if (table.options.getSubRows) {
                    row.originalSubRows = table.options.getSubRows(originalRows[i], i);
                    // Then recursively access them
                    if ((_a = row.originalSubRows) === null || _a === void 0 ? void 0 : _a.length) {
                        row.subRows = accessRows(row.originalSubRows, depth + 1, row);
                    }
                }
            }
            return rows;
        };
        rowModel.rows = accessRows(data);
        return rowModel;
    }, (0, utils_1.getMemoOptions)(table.options, 'debugTable', 'getRowModel', () => table._autoResetPageIndex()));
}
