"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.appendRow = void 0;
// @ts-ignore // TODO: fix cjs build
const prosemirror_utils_1 = require("prosemirror-utils");
const table_desc_1 = require("../table-desc.js");
const utils_1 = require("../utils.js");
const appendRow = (state, dispatch, _, attrs) => {
    if (!attrs)
        return false;
    const { tablePos, rowNumber, direction = 'after' } = attrs;
    const res = (0, prosemirror_utils_1.findParentNodeClosestToPos)(state.doc.resolve(tablePos + 1), utils_1.isTableNode);
    if (!res)
        return false;
    const tableNode = res.node;
    const tableDesc = table_desc_1.TableDesc.create(tableNode);
    if (!tableDesc)
        return false;
    const rowIndex = rowNumber ?? tableDesc.rows - 1; // if rowNumber is not defined, that means last row
    const isFirstRow = rowIndex === 0;
    const isLastRow = rowIndex === tableDesc.rows - 1;
    let pos = -1;
    if (isFirstRow && direction === 'before')
        pos = tableDesc.getRelativePosForRow(0).from;
    if (isLastRow && direction === 'after')
        pos = tableDesc.getRelativePosForRow(tableDesc.rows - 1).to;
    if (pos === -1) {
        if (tableDesc.rows <= rowIndex)
            return false;
        if (tableDesc.isSafeRow(rowIndex)) {
            const rowPos = tableDesc.getRelativePosForRow(rowIndex);
            if (direction === 'before')
                pos = rowPos.from;
            if (direction === 'after')
                pos = rowPos.to;
        }
        else {
            if (direction === 'before' && tableDesc.isSafeRow(rowIndex - 1))
                pos = tableDesc.getRelativePosForRow(rowIndex - 1).to;
            if (direction === 'after' && tableDesc.isSafeRow(rowIndex + 1))
                pos = tableDesc.getRelativePosForRow(rowIndex + 1).from;
        }
    }
    if (pos === -1)
        return false;
    if (dispatch) {
        const cells = getNodes(tableDesc.getCellNodeType(), tableDesc.cols);
        dispatch(state.tr.insert(res.pos + pos, tableDesc.getRowNodeType().create(null, cells)));
    }
    return true;
};
exports.appendRow = appendRow;
function getNodes(type, count) {
    const nodes = [];
    for (let i = 0; i < count; i++)
        nodes.push(type.createAndFill());
    return nodes;
}
//# sourceMappingURL=appendRow.js.map
