"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createRow = void 0;
const utils_1 = require("../utils");
const cell_1 = require("./cell");
const createRow = (table, id, original, rowIndex, depth, subRows, parentId) => {
    var _a;
    const row = {
        id,
        index: rowIndex,
        original,
        depth,
        parentId,
        _valuesCache: {},
        _uniqueValuesCache: {},
        getValue: (columnId) => {
            if (Object.prototype.hasOwnProperty.call(row._valuesCache, columnId)) {
                return row._valuesCache[columnId];
            }
            const column = table.getColumn(columnId);
            if (!(column === null || column === void 0 ? void 0 : column.accessorFn)) {
                return undefined;
            }
            row._valuesCache[columnId] = column.accessorFn(row.original, rowIndex);
            return row._valuesCache[columnId];
        },
        getUniqueValues: (columnId) => {
            if (Object.prototype.hasOwnProperty.call(row._uniqueValuesCache, columnId)) {
                return row._uniqueValuesCache[columnId];
            }
            const column = table.getColumn(columnId);
            if (!(column === null || column === void 0 ? void 0 : column.accessorFn)) {
                return undefined;
            }
            if (!column.columnDef.getUniqueValues) {
                row._uniqueValuesCache[columnId] = [row.getValue(columnId)];
                return row._uniqueValuesCache[columnId];
            }
            row._uniqueValuesCache[columnId] = column.columnDef.getUniqueValues(row.original, rowIndex);
            return row._uniqueValuesCache[columnId];
        },
        renderValue: (columnId) => { var _a; return (_a = row.getValue(columnId)) !== null && _a !== void 0 ? _a : table.options.renderFallbackValue; },
        subRows: subRows !== null && subRows !== void 0 ? subRows : [],
        getLeafRows: () => (0, utils_1.flattenBy)(row.subRows, (d) => d.subRows),
        getParentRow: () => (row.parentId ? table.getRow(row.parentId, true) : undefined),
        getParentRows: () => {
            const parentRows = [];
            let currentRow = row;
            // eslint-disable-next-line no-constant-condition -- 向上遍历父行直到无父节点
            while (true) {
                const parentRow = currentRow.getParentRow();
                if (!parentRow)
                    break;
                parentRows.push(parentRow);
                currentRow = parentRow;
            }
            return parentRows.reverse();
        },
        getAllCells: (0, utils_1.memo)(() => [table.getAllLeafColumns()], (leafColumns) => {
            return leafColumns.map((column) => {
                return (0, cell_1.createCell)(table, row, column, column.id);
            });
        }, (0, utils_1.getMemoOptions)(table.options, 'debugRows', 'getAllCells')),
        _getAllCellsByColumnId: (0, utils_1.memo)(() => [row.getAllCells()], (allCells) => {
            const acc = {};
            allCells.forEach((cell) => {
                acc[cell.column.id] = cell;
            });
            return acc;
        }, (0, utils_1.getMemoOptions)(table.options, 'debugRows', 'getAllCellsByColumnId')),
    };
    for (let i = 0; i < table._features.length; i++) {
        const feature = table._features[i];
        (_a = feature === null || feature === void 0 ? void 0 : feature.createRow) === null || _a === void 0 ? void 0 : _a.call(feature, row, table);
    }
    return row;
};
exports.createRow = createRow;
