"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTableDimensions = exports.findChildTableBody = exports.findChildTableCells = exports.findChildTableRows = exports.findParentTableCellFromPos = exports.findParentTableRowFromPos = exports.findParentTableBodyFromPos = exports.findParentTableFromPos = exports.findParentTableCell = exports.findParentTableRow = exports.findParentTableBody = exports.findParentTable = exports.isTableCellNode = exports.isTableRowNode = exports.isTableBodyNode = exports.isTableNode = void 0;
exports.atEndOfCell = atEndOfCell;
const tslib_1 = require("tslib");
const is_number_1 = tslib_1.__importDefault(require("is-number"));
const prosemirror_utils_1 = require("prosemirror-utils");
const selection_1 = require("../utils/selection.js");
const const_1 = require("./const.js");
const isTableNode = (node) => node.type.spec.tableRole === const_1.TableRole.Table;
exports.isTableNode = isTableNode;
const isTableBodyNode = (node) => node.type.spec.tableRole === const_1.TableRole.Body;
exports.isTableBodyNode = isTableBodyNode;
const isTableRowNode = (node) => node.type.spec.tableRole === const_1.TableRole.Row;
exports.isTableRowNode = isTableRowNode;
const isTableCellNode = (node) => node.type.spec.tableRole === const_1.TableRole.Cell;
exports.isTableCellNode = isTableCellNode;
exports.findParentTable = (0, prosemirror_utils_1.findParentNode)(exports.isTableNode);
exports.findParentTableBody = (0, prosemirror_utils_1.findParentNode)(exports.isTableBodyNode);
exports.findParentTableRow = (0, prosemirror_utils_1.findParentNode)(exports.isTableRowNode);
exports.findParentTableCell = (0, prosemirror_utils_1.findParentNode)(exports.isTableCellNode);
const findParentTableFromPos = ($pos) => (0, prosemirror_utils_1.findParentNodeClosestToPos)($pos, exports.isTableNode);
exports.findParentTableFromPos = findParentTableFromPos;
const findParentTableBodyFromPos = ($pos) => (0, prosemirror_utils_1.findParentNodeClosestToPos)($pos, exports.isTableBodyNode);
exports.findParentTableBodyFromPos = findParentTableBodyFromPos;
const findParentTableRowFromPos = ($pos) => (0, prosemirror_utils_1.findParentNodeClosestToPos)($pos, exports.isTableRowNode);
exports.findParentTableRowFromPos = findParentTableRowFromPos;
const findParentTableCellFromPos = ($pos) => (0, prosemirror_utils_1.findParentNodeClosestToPos)($pos, exports.isTableCellNode);
exports.findParentTableCellFromPos = findParentTableCellFromPos;
const findChildTableRows = (node) => (0, prosemirror_utils_1.findChildren)(node, exports.isTableRowNode);
exports.findChildTableRows = findChildTableRows;
const findChildTableCells = (node) => (0, prosemirror_utils_1.findChildren)(node, exports.isTableCellNode);
exports.findChildTableCells = findChildTableCells;
const findChildTableBody = (node) => (0, prosemirror_utils_1.findChildren)(node, exports.isTableBodyNode);
exports.findChildTableBody = findChildTableBody;
const getTableDimensions = (node) => {
    let rows = 0, cols = 0;
    const tbody = node.firstChild;
    tbody?.forEach((trow, _1, trowIndex) => {
        rows++;
        if (trowIndex === 0) {
            trow.forEach((tcell, _2) => {
                const cellAttrs = tcell.attrs;
                if ((0, is_number_1.default)(cellAttrs['colspan'])) {
                    cols += parseInt(cellAttrs['colspan'], 10);
                }
                else {
                    cols++;
                }
            });
        }
    });
    return { rows, cols };
};
exports.getTableDimensions = getTableDimensions;
function atEndOfCell(view, dir) {
    if (!(0, selection_1.isTextSelection)(view.state.selection))
        return null;
    const { $head } = view.state.selection;
    for (let d = $head.depth - 1; d >= 0; d--) {
        const parent = $head.node(d), index = dir < 0 ? $head.index(d) : $head.indexAfter(d);
        if (index !== (dir < 0 ? 0 : parent.childCount))
            return null;
        if (parent.type.spec.tableRole === const_1.TableRole.Cell) {
            const cellPos = $head.before(d);
            const dirStr = dir > 0 ? 'down' : 'up';
            return view.endOfTextblock(dirStr) ? cellPos : null;
        }
    }
    return null;
}
//# sourceMappingURL=utils.js.map
