import codemark from 'prosemirror-codemark';
import { toggleMark } from 'prosemirror-commands';
import { Plugin } from 'prosemirror-state';
import { createToggleMarkAction } from "../../../utils/actions.js";
import { withLogAction } from "../../../utils/keymap.js";
import { CodeSpecs, codeType } from "./CodeSpecs/index.js";
import "./code.css";
export { codeMarkName, codeType } from "./CodeSpecs/index.js";
const codeAction = 'code';
export const Code = (builder, opts) => {
    builder.use(CodeSpecs);
    builder.addAction(codeAction, ({ schema }) => createToggleMarkAction(codeType(schema)));
    if (opts?.codeKey) {
        const { codeKey } = opts;
        builder.addKeymap(({ schema }) => ({
            [codeKey]: withLogAction('code_inline', toggleMark(codeType(schema))),
        }));
    }
    builder
        // codemark adds inputRule for `code`,
        // adds fake cursor behaviour when crossing inline_code boundaries,
        // wrap current selected text to inline_code when '`' was pressed.
        // See demo: https://curvenote.github.io/prosemirror-codemark/
        .addPlugin(({ schema }) => codemark({ markType: codeType(schema) }));
    builder.addPlugin(() => 
    // apply codemark when typing text between ``
    new Plugin({
        props: {
            handleTextInput: (view, from, to, text) => {
                const { selection: { $anchor }, tr, schema, } = view.state;
                if ($anchor.nodeBefore?.text?.endsWith('`') &&
                    $anchor.nodeAfter?.text?.startsWith('`')) {
                    view.dispatch(tr.replaceRangeWith(from - 1, to + 1, view.state.schema.text(text, [codeType(schema).create()])));
                    return true;
                }
                return false;
            },
        },
    }));
};
//# sourceMappingURL=index.js.map
