"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.insertEmptyRow = void 0;
const state_1 = require("../../../../../../pm/state.js");
const lodash_1 = require("../../../../../../lodash.js");
const table_desc_1 = require("../../../../../../table-utils/table-desc.js");
const YfmTableSpecs_1 = require("../../../YfmTableSpecs/index.js");
const const_1 = require("../../../YfmTableSpecs/const.js");
const utils_1 = require("../utils.js");
const insertEmptyRow = (params) => {
    return (state, dispatch) => {
        const table = state.doc.nodeAt(params.tablePos);
        const tableDesc = table && table_desc_1.TableDesc.create(table)?.bind(params.tablePos);
        if (!tableDesc)
            return false;
        const rowIdx = Math.min(Math.max(params.rowIndex, 0), tableDesc.rows);
        if (dispatch) {
            let posToInsert;
            const newCellBgs = [];
            const incrementRowspan = [];
            const { sourceRowIndex } = params;
            if (rowIdx === 0 || rowIdx === tableDesc.rows) {
                posToInsert =
                    rowIdx === 0
                        ? tableDesc.getPosForRow(0).from
                        : tableDesc.getPosForRow(tableDesc.rows - 1).to;
                for (let colIdx = 0; colIdx < tableDesc.cols; colIdx++) {
                    newCellBgs.push(getBg(tableDesc.base, sourceRowIndex, colIdx));
                }
            }
            else {
                posToInsert = tableDesc.getPosForRow(rowIdx).from;
                for (let colIdx = 0; colIdx < tableDesc.cols; colIdx++) {
                    const cell = tableDesc.base.rowsDesc[rowIdx].cells[colIdx];
                    if (cell.type === 'virtual' && cell.rowspan) {
                        incrementRowspan.push(cell.rowspan);
                    }
                    else {
                        newCellBgs.push(getBg(tableDesc.base, sourceRowIndex, colIdx));
                    }
                }
            }
            const { tr } = state;
            for (const [rowIdx, colIdx] of (0, lodash_1.uniqWith)(incrementRowspan, lodash_1.isEqual)) {
                const cell = tableDesc.base.rowsDesc[rowIdx].cells[colIdx];
                const cellPos = tableDesc.getPosForCell(rowIdx, colIdx);
                tr.setNodeAttribute(cellPos.from, 'rowspan', cell.rowspan + 1);
            }
            tr.insert(posToInsert, createSimpleRow(state.schema, newCellBgs));
            tr.setSelection(state_1.TextSelection.near(tr.doc.resolve(posToInsert), 1));
            // If the new row is inserted inside the header-rows block, shrink the block
            // so the new row and everything below it stop being header rows.
            if (tableDesc.base.isHeaderRow(rowIdx)) {
                tr.setNodeAttribute(params.tablePos, const_1.YfmTableAttr.HeaderRows, rowIdx);
            }
            dispatch(tr.scrollIntoView());
        }
        return true;
    };
};
exports.insertEmptyRow = insertEmptyRow;
function getBg(base, sourceRowIndex, colIdx) {
    if (sourceRowIndex === undefined)
        return null;
    return (0, utils_1.getCellBg)(base, sourceRowIndex, colIdx);
}
const createSimpleRow = (schema, cellBgs) => {
    const tr = (0, YfmTableSpecs_1.yfmTableRowType)(schema);
    const td = (0, YfmTableSpecs_1.yfmTableCellType)(schema);
    return tr.create(null, 
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    cellBgs.map((bg) => td.createAndFill(bg ? { [const_1.YfmTableAttr.CellBg]: bg } : undefined)));
};
//# sourceMappingURL=insert-empty-row.js.map
