"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Math = exports.MathInlineNodeView = exports.MathBlockNodeView = exports.mathIType = exports.mathBType = exports.MathNode = void 0;
const prosemirror_commands_1 = require("prosemirror-commands");
const prosemirror_state_1 = require("prosemirror-state");
// @ts-ignore // TODO: fix cjs build
const prosemirror_utils_1 = require("prosemirror-utils");
const inputrules_1 = require("../../../utils/inputrules.js");
const selection_1 = require("../../../utils/selection.js");
const MathSpecs_1 = require("./MathSpecs/index.js");
const commands_1 = require("./commands.js");
const const_1 = require("./const.js");
const view_and_edit_1 = require("./view-and-edit.js");
require("./index.css");
var MathSpecs_2 = require("./MathSpecs/index.js");
Object.defineProperty(exports, "MathNode", { enumerable: true, get: function () { return MathSpecs_2.MathNode; } });
Object.defineProperty(exports, "mathBType", { enumerable: true, get: function () { return MathSpecs_2.mathBType; } });
Object.defineProperty(exports, "mathIType", { enumerable: true, get: function () { return MathSpecs_2.mathIType; } });
var view_and_edit_2 = require("./view-and-edit.js");
Object.defineProperty(exports, "MathBlockNodeView", { enumerable: true, get: function () { return view_and_edit_2.MathBlockNodeView; } });
Object.defineProperty(exports, "MathInlineNodeView", { enumerable: true, get: function () { return view_and_edit_2.MathInlineNodeView; } });
const mathIAction = 'addMathInline';
const mathBAction = 'toMathBlock';
const mathITemplate = 'f(x)=';
const Math = (builder, opts) => {
    builder.use(MathSpecs_1.MathSpecs);
    builder.addKeymap(() => ({
        Enter: commands_1.ignoreIfCursorInsideMathInline, // ignore breaks in math inline
        Backspace: (0, prosemirror_commands_1.chainCommands)(commands_1.removeEmptyMathInlineIfCursorIsAtBeginning, commands_1.moveCursorLeftOfMathInline),
    }));
    builder
        .addPlugin(() => (0, view_and_edit_1.mathViewAndEditPlugin)({
        ...opts,
        reactRenderer: builder.context.get('reactrenderer'),
    }))
        .addInputRules((deps) => ({
        rules: [
            (0, inputrules_1.textblockTypeInputRule)(/^\$\$\s$/, (0, const_1.mathBType)(deps.schema)),
            (0, inputrules_1.inlineNodeInputRule)(/\$[^$\s]+\$$/, (match) => (0, const_1.mathIType)(deps.schema).create(null, deps.schema.text(match.replace(/\$/g, '')))),
        ],
    }));
    builder
        .addAction(mathIAction, (deps) => {
        const type = (0, const_1.mathIType)(deps.schema);
        const cmd = (state, dispatch) => {
            const { selection } = state;
            if ((0, selection_1.isTextSelection)(selection) && selection.$from.sameParent(selection.$to)) {
                if (dispatch) {
                    const fragment = selection.content().content;
                    const selectedText = fragment.textBetween(0, fragment.size) || mathITemplate;
                    const tr = state.tr;
                    selection.replaceWith(tr, type.create(null, deps.schema.text(selectedText)));
                    tr.setSelection(prosemirror_state_1.TextSelection.create(tr.doc, selection.from + selectedText.length + 1));
                    dispatch(tr);
                }
                return true;
            }
            return false;
        };
        return {
            isActive: (state) => (0, prosemirror_utils_1.hasParentNodeOfType)(type)(state.selection),
            isEnable: cmd,
            run: cmd,
        };
    })
        .addAction(mathBAction, (deps) => {
        const type = (0, const_1.mathBType)(deps.schema);
        const cmd = (0, prosemirror_commands_1.setBlockType)(type);
        return {
            isActive: (state) => (0, prosemirror_utils_1.hasParentNodeOfType)(type)(state.selection),
            isEnable: cmd,
            run: cmd,
        };
    });
};
exports.Math = Math;
//# sourceMappingURL=index.js.map
