"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.backspaceCommand = exports.clearSelectedCells = void 0;
const prosemirror_commands_1 = require("prosemirror-commands");
const prosemirror_state_1 = require("prosemirror-state");
const table_utils_1 = require("../../../../table-utils/index.js");
const selection_1 = require("../../../../utils/selection.js");
const removeCellNodeContent = (state, dispatch) => {
    const sel = state.selection;
    if (!(0, selection_1.isNodeSelection)(sel))
        return false;
    const selNode = sel.node;
    if ((0, table_utils_1.isTableRowNode)(selNode)) {
        // remove the table row
        dispatch?.(state.tr.deleteSelection());
        return true;
    }
    if ((0, table_utils_1.isTableCellNode)(selNode)) {
        if (dispatch) {
            // clearing the table cell
            const from = sel.from + 1;
            const to = sel.to - 1;
            let tr = state.tr.delete(from, to);
            tr = tr.setSelection(prosemirror_state_1.TextSelection.create(tr.doc, from));
            dispatch(tr);
        }
        return true;
    }
    return false;
};
// eslint-disable-next-line complexity
const clearSelectedCells = (state, dispatch) => {
    const sel = state.selection;
    if (!(0, selection_1.isTextSelection)(sel))
        return false;
    const { $from, $to } = sel;
    const fromCell = (0, table_utils_1.findParentTableCellFromPos)($from);
    const toCell = (0, table_utils_1.findParentTableCellFromPos)($to);
    const fromTBody = (0, table_utils_1.findParentTableBodyFromPos)($from);
    const toTBody = (0, table_utils_1.findParentTableBodyFromPos)($to);
    const fromTable = (0, table_utils_1.findParentTableFromPos)($to);
    if (!fromCell || !toCell || !fromTBody || !toTBody || !fromTable)
        return false;
    if (fromCell.node === toCell.node) {
        // selection inside table cell
        return false; // should executes default command
    }
    if (fromTBody && toTBody && fromTBody.pos === toTBody.pos) {
        if (dispatch) {
            const table = fromTable;
            const tBody = fromTBody;
            const fromCellIndexInRow = $from.index(fromCell.depth - 1);
            const toCellIndexInRow = $to.index(toCell.depth - 1);
            const fromRowIndexInBody = $from.index(fromCell.depth - 2);
            const toRowIndexInBody = $to.index(toCell.depth - 2);
            const bodyRows = (0, table_utils_1.findChildTableRows)(tBody.node);
            let tr = state.tr;
            tr = tr.delete(toCell.start, $to.pos);
            let rowIndex = toRowIndexInBody;
            while (rowIndex >= fromRowIndexInBody) {
                const row = bodyRows[rowIndex];
                const rowCells = (0, table_utils_1.findChildTableCells)(row.node);
                let cellIndex = rowIndex === toRowIndexInBody ? toCellIndexInRow - 1 : rowCells.length - 1;
                while (cellIndex > (rowIndex === fromRowIndexInBody ? fromCellIndexInRow : -1)) {
                    const cell = rowCells[cellIndex];
                    const from = tBody.pos + row.pos + cell.pos + 3;
                    const to = from + cell.node.nodeSize - 2;
                    tr = tr.delete(from, to);
                    cellIndex--;
                }
                if (rowIndex !== fromRowIndexInBody) {
                    const rowPos = tBody.pos + row.pos + 1;
                    const trRow = tr.doc.nodeAt(rowPos);
                    if (trRow && isEmptyTableRow(trRow)) {
                        tr = tr.delete(rowPos, rowPos + trRow.nodeSize);
                    }
                }
                rowIndex--;
            }
            tr = tr.delete($from.pos, fromCell.pos + fromCell.node.nodeSize);
            const fromRowPos = tBody.pos + bodyRows[fromRowIndexInBody].pos + 1;
            const trFromRow = tr.doc.nodeAt(fromRowPos);
            if (fromCellIndexInRow === 0 && trFromRow && isEmptyTableRow(trFromRow)) {
                const rowsCountBeforeDelete = tr.doc.nodeAt(tBody.pos).childCount;
                tr = tr.delete(fromRowPos, fromRowPos + tr.doc.nodeAt(fromRowPos).nodeSize);
                const trTable = tr.doc.nodeAt(table.pos);
                if (rowsCountBeforeDelete <= 1) {
                    if (trTable) {
                        if (trTable.childCount <= 1) {
                            tr = tr.delete(table.pos, trTable.nodeSize);
                        }
                    }
                    else {
                        tr = tr.delete(tBody.pos, tBody.pos + tr.doc.nodeAt(tBody.pos).nodeSize);
                    }
                }
            }
            tr = tr.setSelection(prosemirror_state_1.TextSelection.create(tr.doc, tr.mapping.map(sel.head)));
            dispatch(tr);
        }
        return true;
    }
    return false;
};
exports.clearSelectedCells = clearSelectedCells;
exports.backspaceCommand = (0, prosemirror_commands_1.chainCommands)(removeCellNodeContent, exports.clearSelectedCells);
function isEmptyTableRow(node) {
    if (!(0, table_utils_1.isTableRowNode)(node))
        return false;
    if (node.childCount === 0)
        return true;
    let isRowEmpty = true;
    node.forEach((cellNode) => {
        isRowEmpty = isEmptyTableCell(cellNode);
    });
    return isRowEmpty;
}
function isEmptyTableCell(node) {
    if (!(0, table_utils_1.isTableCellNode)(node))
        return false;
    if (node.childCount === 0)
        return true;
    return node.childCount === 1 && node.child(0).isTextblock && node.child(0).childCount === 0;
}
//# sourceMappingURL=backspace.js.map
