// @ts-ignore // TODO: fix cjs build
import { hasParentNodeOfType } from 'prosemirror-utils';
import { isFunction } from "../../../lodash.js";
import { textblockTypeInputRule } from "../../../utils/inputrules.js";
import { withLogAction } from "../../../utils/keymap.js";
import { CodeBlockHighlight } from "./CodeBlockHighlight/CodeBlockHighlight.js";
import { CodeBlockSpecs } from "./CodeBlockSpecs/index.js";
import { newlineInCode, resetCodeblock, setCodeBlockType } from "./commands.js";
import { cbAction, codeBlockType } from "./const.js";
import { codeBlockPastePlugin } from "./plugins/codeBlockPastePlugin.js";
export { resetCodeblock } from "./commands.js";
export { codeBlockNodeName, CodeBlockNodeAttr, codeBlockType } from "./CodeBlockSpecs/index.js";
export const CodeBlock = (builder, opts) => {
    builder.use(CodeBlockSpecs, opts);
    builder.addKeymap((deps) => {
        const { codeBlockKey } = opts;
        const bindings = { Enter: newlineInCode, Backspace: resetCodeblock };
        if (codeBlockKey) {
            bindings[codeBlockKey] = withLogAction('code_block', setCodeBlockType(deps));
        }
        return bindings;
    });
    builder.addInputRules(({ schema }) => ({ rules: [codeBlockRule(codeBlockType(schema))] }));
    builder.addAction(cbAction, (deps) => {
        const cb = codeBlockType(deps.schema);
        return {
            isActive: (state) => hasParentNodeOfType(cb)(state.selection),
            isEnable: setCodeBlockType(deps),
            run: setCodeBlockType(deps),
        };
    });
    builder.addPlugin(codeBlockPastePlugin, builder.Priority.High);
    if (isFunction(opts.langs)) {
        builder.use(opts.langs);
    }
    else {
        builder.use(CodeBlockHighlight, opts.langs ?? {});
    }
};
// Given a code block node type, returns an input rule that turns a
// textblock starting with three backticks into a code block.
function codeBlockRule(nodeType) {
    return textblockTypeInputRule(/^```$/, nodeType);
}
//# sourceMappingURL=index.js.map
