import { TextSelection } from "../../../../../../pm/state.js";
import { isEqual, uniqWith } from "../../../../../../lodash.js";
import { TableDesc } from "../../../../../../table-utils/table-desc.js";
import { yfmTableCellType, yfmTableRowType } from "../../../YfmTableSpecs/index.js";
import { YfmTableAttr } from "../../../YfmTableSpecs/const.js";
import { getCellBg } from "../utils.js";
export const insertEmptyRow = (params) => {
    return (state, dispatch) => {
        const table = state.doc.nodeAt(params.tablePos);
        const tableDesc = table && 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 uniqWith(incrementRowspan, 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(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, YfmTableAttr.HeaderRows, rowIdx);
            }
            dispatch(tr.scrollIntoView());
        }
        return true;
    };
};
function getBg(base, sourceRowIndex, colIdx) {
    if (sourceRowIndex === undefined)
        return null;
    return getCellBg(base, sourceRowIndex, colIdx);
}
const createSimpleRow = (schema, cellBgs) => {
    const tr = yfmTableRowType(schema);
    const td = yfmTableCellType(schema);
    return tr.create(null, 
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    cellBgs.map((bg) => td.createAndFill(bg ? { [YfmTableAttr.CellBg]: bg } : undefined)));
};
//# sourceMappingURL=insert-empty-row.js.map
