import { Fragment, Slice } from 'prosemirror-model';
import { TableRole } from "../../../table-utils/const.js";
import { getChildrenOfNode } from "../../../utils/nodes.js";
import { yfmTableBodyType, yfmTableCellType, yfmTableRowType, yfmTableType } from "./utils.js";
export function fixPastedTableBodies(slice, schema) {
    if (slice.content.firstChild?.type === yfmTableBodyType(schema)) {
        const tRows = [];
        let bodiesSize = 0;
        for (const { node, offset } of getChildrenOfNode(slice.content)) {
            if (node.type !== yfmTableBodyType(schema))
                break;
            tRows.push(...getChildrenOfNode(node).map((item) => item.node));
            bodiesSize = offset + node.nodeSize;
        }
        const tableNode = yfmTableType(schema).create(null, yfmTableBodyType(schema).create(null, tRows));
        slice = new Slice(Fragment.from(tableNode).append(slice.content.cut(bodiesSize)), 0, 0);
    }
    return slice;
}
export function unpackSingleCellTable(slice) {
    if (slice.content.childCount !== 1)
        return slice;
    if (!isSingeCellTable(slice.content.child(0)))
        return slice;
    let node = slice.content.child(0);
    while (node && node.type.spec.tableRole !== TableRole.Cell) {
        node = node.lastChild;
    }
    // get content from tableCell
    const newFragment = Fragment.from(node?.content);
    const newSlice = new Slice(newFragment, 0, 0);
    return newSlice;
}
function isSingeCellTable(node) {
    const { schema } = node.type;
    if (node.type === yfmTableCellType(schema))
        return true;
    if (node.type === yfmTableRowType(schema) && node.childCount === 1)
        return isSingeCellTable(node.child(0));
    if (node.type === yfmTableBodyType(schema) && node.childCount === 1)
        return isSingeCellTable(node.child(0));
    if (node.type === yfmTableType(schema) && node.lastChild?.type === yfmTableBodyType(schema))
        return isSingeCellTable(node.lastChild);
    return false;
}
//# sourceMappingURL=paste.js.map
