"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.moveCursorLeftOfMathInline = exports.moveCursorRightOfMathInline = exports.removeEmptyMathInlineIfCursorIsAtBeginning = exports.ignoreIfCursorInsideMathInline = void 0;
const prosemirror_state_1 = require("prosemirror-state");
const selection_1 = require("../../../utils/selection.js");
const const_1 = require("./const.js");
const ignoreIfCursorInsideMathInline = ({ selection, schema }) => {
    const $cursor = (0, selection_1.get$Cursor)(selection);
    return $cursor?.parent.type === (0, const_1.mathIType)(schema);
};
exports.ignoreIfCursorInsideMathInline = ignoreIfCursorInsideMathInline;
/**
 * If cursor at the beginning of MathInline:
 * - if math is empty – remove anchor
 * - if math has content – select node with NodeSelection
 */
const removeEmptyMathInlineIfCursorIsAtBeginning = ({ tr, schema }, dispatch) => {
    const $cursor = (0, selection_1.get$Cursor)(tr.selection);
    if ($cursor?.parent.type === (0, const_1.mathIType)(schema) && $cursor.start() === $cursor.pos) {
        const isEmptyAnchor = $cursor.parent.nodeSize <= 2;
        if (isEmptyAnchor) {
            dispatch?.(tr.delete($cursor.before(), $cursor.after()));
        }
        else {
            dispatch?.(tr.setSelection(prosemirror_state_1.NodeSelection.create(tr.doc, $cursor.before())));
        }
        return true;
    }
    return false;
};
exports.removeEmptyMathInlineIfCursorIsAtBeginning = removeEmptyMathInlineIfCursorIsAtBeginning;
/**
 * Handle cursor movement to the right at the boundary of a MathInline block
 */
const moveCursorRightOfMathInline = ({ tr, schema }, dispatch) => {
    const $cursor = (0, selection_1.get$Cursor)(tr.selection);
    if ($cursor) {
        const mathType = (0, const_1.mathIType)(schema);
        const isOnBeforeOfMathInline = $cursor.nodeAfter?.type === mathType;
        const isOnEndOfMathInline = $cursor.parentOffset === $cursor.parent.content.size &&
            $cursor.parent.type === mathType;
        if (isOnBeforeOfMathInline || isOnEndOfMathInline) {
            const newPos = $cursor.pos + 1;
            dispatch?.(tr.setSelection(prosemirror_state_1.TextSelection.create(tr.doc, newPos)));
            return true;
        }
    }
    return false;
};
exports.moveCursorRightOfMathInline = moveCursorRightOfMathInline;
/**
 * Handle cursor movement to the left at the boundary of a MathInline block
 */
const moveCursorLeftOfMathInline = ({ tr, schema }, dispatch) => {
    const $cursor = (0, selection_1.get$Cursor)(tr.selection);
    if ($cursor) {
        const mathType = (0, const_1.mathIType)(schema);
        const isOnAfterOfMathInline = $cursor.nodeBefore?.type === mathType;
        const isOnStartOfMathInline = $cursor.parentOffset === 0 && $cursor.parent.type === mathType;
        if (isOnAfterOfMathInline || isOnStartOfMathInline) {
            const newPos = $cursor.pos - 1;
            dispatch?.(tr.setSelection(prosemirror_state_1.TextSelection.create(tr.doc, newPos)));
            return true;
        }
    }
    return false;
};
exports.moveCursorLeftOfMathInline = moveCursorLeftOfMathInline;
//# sourceMappingURL=commands.js.map
