"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isSelectableNode = exports.isNodeEmpty = void 0;
exports.findFirstTextblockChild = findFirstTextblockChild;
exports.isCodeBlock = isCodeBlock;
exports.getChildrenOfNode = getChildrenOfNode;
exports.getLastChildOfNode = getLastChildOfNode;
const prosemirror_model_1 = require("prosemirror-model");
// @ts-ignore // TODO: fix cjs build
const prosemirror_utils_1 = require("prosemirror-utils");
function findFirstTextblockChild(parent) {
    if (parent instanceof prosemirror_model_1.Node && (!parent.isBlock || parent.isAtom))
        return null;
    for (const child of getChildrenOfNode(parent)) {
        if (child.node.isTextblock)
            return child;
        const nestedTextBlockChild = findFirstTextblockChild(child.node);
        if (nestedTextBlockChild) {
            return {
                node: nestedTextBlockChild.node,
                offset: child.offset + nestedTextBlockChild.offset,
            };
        }
    }
    return null;
}
const isNodeEmpty = (node) => {
    const emptyChildren = (0, prosemirror_utils_1.findChildren)(node, (n) => (!n.isText && !n.isAtom && n.content.size === 0 && n.type.name === 'paragraph') ||
        (n.isText && !n.textContent), true);
    let descendantsCount = 0;
    node.descendants(() => {
        descendantsCount++;
    });
    return emptyChildren.length === descendantsCount;
};
exports.isNodeEmpty = isNodeEmpty;
const isSelectableNode = (node) => {
    if (node.type.spec.selectable === false)
        return false;
    if (node.type.isText && node.type.spec.selectable !== true)
        return false;
    return true;
};
exports.isSelectableNode = isSelectableNode;
function isCodeBlock(node) {
    return node.isTextblock && Boolean(node.type.spec.code);
}
function getChildrenOfNode(node) {
    const children = [];
    node.forEach((node, offset, index) => children.push({ node, offset, index }));
    return children;
}
function getLastChildOfNode(node) {
    const children = getChildrenOfNode(node);
    return children[children.length - 1];
}
//# sourceMappingURL=nodes.js.map
