"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.moveToNextRowCommand = void 0;
const state_1 = require("../../../pm/state.js");
const specs_1 = require("../../base/specs.js");
const table_utils_1 = require("../../../table-utils/index.js");
const node_children_1 = require("../../../utils/node-children.js");
const helpers_1 = require("./helpers.js");
const moveToNextRow = ($pos, state, dispatch) => {
    const tableRes = (0, table_utils_1.findParentTableFromPos)($pos);
    if (!tableRes)
        return false;
    const tableHeadResult = (0, helpers_1.findParentHeadOnPos)($pos);
    const tableBodyResult = (0, helpers_1.findParentBodyOnPos)($pos);
    if (!tableHeadResult && !tableBodyResult)
        return false;
    const tableRowResult = (0, helpers_1.findParentRowOnPos)($pos);
    const tableCellResult = (0, helpers_1.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 = (0, node_children_1.getChildByIndex)(nextRow, cellIndex);
        if (cell) {
            const cellPos = nextRowPos + 1 + cell.offset;
            tr.setSelection(state_1.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(state_1.TextSelection.near($rowEnd, -1));
        }
    };
    const exitTable = (tr) => {
        const afterTablePos = tableRes.pos + tableRes.node.nodeSize;
        tr.insert(afterTablePos, (0, specs_1.pType)(state.schema).create());
        tr.setSelection(state_1.TextSelection.create(tr.doc, afterTablePos + 1));
    };
    const tr = state.tr;
    if (tableHeadResult) {
        // in table head
        const tableBody = (0, node_children_1.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 = (0, node_children_1.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;
};
const moveToNextRowCommand = (state, dispatch) => {
    return (moveToNextRow(state.selection.$head, state, dispatch) ||
        moveToNextRow(state.selection.$anchor, state, dispatch));
};
exports.moveToNextRowCommand = moveToNextRowCommand;
//# sourceMappingURL=commands.js.map
