import { pType } from "../../../base/BaseSchema/index.js";
import { findTableCells as findMdTableCells, findTableRows as findMdTableRows, findParentTable as findParentMdTable, } from "../../../markdown/Table/helpers.js";
import { yfmTableBodyType, yfmTableCellType, yfmTableRowType, yfmTableType } from "../utils.js";
export const convertToYfmTable = (state, dispatch) => {
    const mdTable = findParentMdTable(state);
    if (!mdTable)
        return false;
    if (dispatch) {
        const { schema } = state;
        const yfmTableRowNodes = [];
        const mdTableRows = findMdTableRows(mdTable.node, state.schema);
        for (const row of mdTableRows) {
            const yfmTableCellNodes = [];
            const mdRowCells = findMdTableCells(row.node, state.schema);
            for (const cell of mdRowCells) {
                yfmTableCellNodes.push(yfmTableCellType(schema).create(null, pType(schema).create(null, cell.node.content)));
            }
            yfmTableRowNodes.push(yfmTableRowType(schema).create(null, yfmTableCellNodes));
        }
        const yfmTableNode = yfmTableType(schema).create(null, yfmTableBodyType(schema).create(null, yfmTableRowNodes));
        dispatch(state.tr.replaceWith(mdTable.pos, mdTable.pos + mdTable.node.nodeSize, yfmTableNode));
    }
    return true;
};
//# sourceMappingURL=convert-table.js.map
