import { TextSelection } from "../../../pm/state.js";
import { pType } from "../../base/specs.js";
import { findParentTableFromPos } from "../../../table-utils/index.js";
import { getChildByIndex } from "../../../utils/node-children.js";
import { findParentBodyOnPos, findParentCellOnPos, findParentHeadOnPos, findParentRowOnPos, } from "./helpers.js";
const moveToNextRow = ($pos, state, dispatch) => {
    const tableRes = findParentTableFromPos($pos);
    if (!tableRes)
        return false;
    const tableHeadResult = findParentHeadOnPos($pos);
    const tableBodyResult = findParentBodyOnPos($pos);
    if (!tableHeadResult && !tableBodyResult)
        return false;
    const tableRowResult = findParentRowOnPos($pos);
    const tableCellResult = findParentCellOnPos($pos);
    if (!tableRowResult || !tableCellResult)
        return false;
    const cellIndex = $pos.index(tableCellResult.depth - 1);
    const rowIndex = $pos.index(tableRowResult.depth - 1);
    if (!dispatch)
        return true;
    const goToNextRow = (tr, nextRow, nextRowPos) => {
        const cell = getChildByIndex(nextRow, cellIndex);
        if (cell) {
            const cellPos = nextRowPos + 1 + cell.offset;
            tr.setSelection(TextSelection.create(tr.doc, cellPos + cell.node.nodeSize - 1));
        }
        else {
            // fallback if cell not found
            const $rowEnd = tr.doc.resolve(nextRowPos + nextRow.nodeSize - 1);
            tr.setSelection(TextSelection.near($rowEnd, -1));
        }
    };
    const exitTable = (tr) => {
        const afterTablePos = tableRes.pos + tableRes.node.nodeSize;
        tr.insert(afterTablePos, pType(state.schema).create());
        tr.setSelection(TextSelection.create(tr.doc, afterTablePos + 1));
    };
    const tr = state.tr;
    if (tableHeadResult) {
        // in table head
        const tableBody = getChildByIndex(tableRes.node, tableRes.node.childCount - 1);
        if (tableBody && tableBody.node !== tableHeadResult.node && tableBody.node.firstChild) {
            goToNextRow(tr, tableBody.node.firstChild, tableRes.start + tableBody.offset + 1);
        }
        else {
            // no table body or it is empty
            exitTable(tr);
        }
    }
    else {
        // in table body
        const nextRow = getChildByIndex(tableBodyResult.node, rowIndex + 1);
        if (nextRow) {
            goToNextRow(tr, nextRow.node, tableBodyResult.start + nextRow.offset);
        }
        else {
            // pos in last row
            exitTable(tr);
        }
    }
    dispatch(tr.scrollIntoView());
    return true;
};
export const moveToNextRowCommand = (state, dispatch) => {
    return (moveToNextRow(state.selection.$head, state, dispatch) ||
        moveToNextRow(state.selection.$anchor, state, dispatch));
};
//# sourceMappingURL=commands.js.map
