"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.findRowGroupByPrimaryKey = findRowGroupByPrimaryKey;
const primary_key_1 = require("../../../../../../../../cn-ai-table/view/utils/primary-key");
/**
 * 在分组树中按主键查找数据行及其所属分组。
 * 折叠时 getRowModel() 扁平行可能不含子行，故优先走 getGroupedRowModel() 的完整树。
 */
function findRowGroupByPrimaryKey(table, primaryKey, rowId) {
    var _a, _b;
    const groupedModel = (_a = table.getGroupedRowModel) === null || _a === void 0 ? void 0 : _a.call(table);
    const allRows = (_b = groupedModel === null || groupedModel === void 0 ? void 0 : groupedModel.rows) !== null && _b !== void 0 ? _b : table.getRowModel().rows;
    const findInRows = (rows, parentGroupRow) => {
        var _a;
        for (let i = 0; i < rows.length; i++) {
            const row = rows[i];
            if (row.getIsGrouped()) {
                if (((_a = row.subRows) === null || _a === void 0 ? void 0 : _a.length) > 0) {
                    const found = findInRows(row.subRows, row);
                    if (found)
                        return found;
                }
            }
            else {
                const rowPk = (0, primary_key_1.safeGetRowKey)(row.original, primaryKey, i);
                if (String(rowPk) === String(rowId)) {
                    return { row, groupRow: parentGroupRow };
                }
            }
        }
        return null;
    };
    return findInRows(allRows, null);
}
