import isNumber from 'is-number';
import { findChildren, findParentNode, findParentNodeClosestToPos,
// @ts-ignore // TODO: fix cjs build
 } from 'prosemirror-utils';
import { isTextSelection } from "../utils/selection.js";
import { TableRole } from "./const.js";
export const isTableNode = (node) => node.type.spec.tableRole === TableRole.Table;
export const isTableBodyNode = (node) => node.type.spec.tableRole === TableRole.Body;
export const isTableRowNode = (node) => node.type.spec.tableRole === TableRole.Row;
export const isTableCellNode = (node) => node.type.spec.tableRole === TableRole.Cell;
export const findParentTable = findParentNode(isTableNode);
export const findParentTableBody = findParentNode(isTableBodyNode);
export const findParentTableRow = findParentNode(isTableRowNode);
export const findParentTableCell = findParentNode(isTableCellNode);
export const findParentTableFromPos = ($pos) => findParentNodeClosestToPos($pos, isTableNode);
export const findParentTableBodyFromPos = ($pos) => findParentNodeClosestToPos($pos, isTableBodyNode);
export const findParentTableRowFromPos = ($pos) => findParentNodeClosestToPos($pos, isTableRowNode);
export const findParentTableCellFromPos = ($pos) => findParentNodeClosestToPos($pos, isTableCellNode);
export const findChildTableRows = (node) => findChildren(node, isTableRowNode);
export const findChildTableCells = (node) => findChildren(node, isTableCellNode);
export const findChildTableBody = (node) => findChildren(node, isTableBodyNode);
export 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 (isNumber(cellAttrs['colspan'])) {
                    cols += parseInt(cellAttrs['colspan'], 10);
                }
                else {
                    cols++;
                }
            });
        }
    });
    return { rows, cols };
};
export function atEndOfCell(view, dir) {
    if (!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 === 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
