"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeRow = void 0;
const prosemirror_model_1 = require("prosemirror-model");
// @ts-ignore // TODO: fix cjs build
const prosemirror_utils_1 = require("prosemirror-utils");
const core_1 = require("../../core/index.js");
const table_desc_1 = require("../table-desc.js");
const utils_1 = require("../utils.js");
const removeRow = (state, dispatch, _, attrs = {}) => {
    const { tablePos, rowNumber } = attrs;
    if (tablePos === undefined || rowNumber === undefined)
        return false;
    const tableNode = (0, prosemirror_utils_1.findParentNodeClosestToPos)(state.doc.resolve(tablePos + 1), utils_1.isTableNode)
        ?.node;
    if (!tableNode)
        return false;
    const tableDesc = table_desc_1.TableDesc.create(tableNode);
    if (!tableDesc || rowNumber >= tableDesc.rows)
        return false;
    if (!tableDesc.isSafeRow(rowNumber))
        return false;
    if (dispatch) {
        let { from, to } = tableDesc.getRelativePosForRow(rowNumber);
        from += tablePos;
        to += tablePos;
        dispatch((0, core_1.trackTransactionMetrics)(state.tr.replaceWith(from, to, prosemirror_model_1.Fragment.empty), 'removeRow', {
            rows: tableDesc.rows,
            cols: tableDesc.cols,
        }));
    }
    return true;
};
exports.removeRow = removeRow;
//# sourceMappingURL=removeRow.js.map
