import { chainCommands, setBlockType } from 'prosemirror-commands';
import { TextSelection } from 'prosemirror-state';
// @ts-ignore // TODO: fix cjs build
import { hasParentNodeOfType } from 'prosemirror-utils';
import { inlineNodeInputRule, textblockTypeInputRule } from "../../../utils/inputrules.js";
import { isTextSelection } from "../../../utils/selection.js";
import { MathSpecs } from "./MathSpecs/index.js";
import { ignoreIfCursorInsideMathInline, moveCursorLeftOfMathInline, removeEmptyMathInlineIfCursorIsAtBeginning, } from "./commands.js";
import { mathBType, mathIType } from "./const.js";
import { mathViewAndEditPlugin } from "./view-and-edit.js";
import "./index.css";
export { MathNode, mathBType, mathIType } from "./MathSpecs/index.js";
export { MathBlockNodeView, MathInlineNodeView } from "./view-and-edit.js";
const mathIAction = 'addMathInline';
const mathBAction = 'toMathBlock';
const mathITemplate = 'f(x)=';
export const Math = (builder, opts) => {
    builder.use(MathSpecs);
    builder.addKeymap(() => ({
        Enter: ignoreIfCursorInsideMathInline, // ignore breaks in math inline
        Backspace: chainCommands(removeEmptyMathInlineIfCursorIsAtBeginning, moveCursorLeftOfMathInline),
    }));
    builder
        .addPlugin(() => mathViewAndEditPlugin({
        ...opts,
        reactRenderer: builder.context.get('reactrenderer'),
    }))
        .addInputRules((deps) => ({
        rules: [
            textblockTypeInputRule(/^\$\$\s$/, mathBType(deps.schema)),
            inlineNodeInputRule(/\$[^$\s]+\$$/, (match) => mathIType(deps.schema).create(null, deps.schema.text(match.replace(/\$/g, '')))),
        ],
    }));
    builder
        .addAction(mathIAction, (deps) => {
        const type = mathIType(deps.schema);
        const cmd = (state, dispatch) => {
            const { selection } = state;
            if (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(TextSelection.create(tr.doc, selection.from + selectedText.length + 1));
                    dispatch(tr);
                }
                return true;
            }
            return false;
        };
        return {
            isActive: (state) => hasParentNodeOfType(type)(state.selection),
            isEnable: cmd,
            run: cmd,
        };
    })
        .addAction(mathBAction, (deps) => {
        const type = mathBType(deps.schema);
        const cmd = setBlockType(type);
        return {
            isActive: (state) => hasParentNodeOfType(type)(state.selection),
            isEnable: cmd,
            run: cmd,
        };
    });
};
//# sourceMappingURL=index.js.map
