"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeColumn = void 0;
const table_desc_1 = require("../table-desc.js");
const utils_1 = require("../utils.js");
const removeColumn = (state, dispatch, _, attrs = {}) => {
    const { tablePos, columnNumber } = attrs;
    if (typeof tablePos !== 'number' || typeof columnNumber !== 'number')
        return false;
    if (Number.isNaN(tablePos) || Number.isNaN(columnNumber))
        return false;
    if (tablePos < 0 || columnNumber < 0)
        return false;
    const tableNode = state.doc.nodeAt(tablePos);
    if (!tableNode || tableNode.nodeSize <= 2 || !(0, utils_1.isTableNode)(tableNode))
        return false;
    const tableDesc = table_desc_1.TableDesc.create(tableNode);
    if (!tableDesc)
        return false;
    if (!tableDesc ||
        // there is one column left
        tableDesc.cols < 2 ||
        tableDesc.cols <= columnNumber ||
        !tableDesc.isSafeColumn(columnNumber))
        return false;
    if (dispatch) {
        const { tr } = state;
        const pos = tableDesc.getRelativePosForColumn(columnNumber);
        for (const item of pos) {
            if (item.type === 'real') {
                let { from, to } = item;
                from += tablePos;
                to += tablePos;
                tr.delete(tr.mapping.map(from), tr.mapping.map(to));
            }
        }
        dispatch(tr);
    }
    return true;
};
exports.removeColumn = removeColumn;
//# sourceMappingURL=removeColumn.js.map
