"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodeBlock = exports.codeBlockType = exports.CodeBlockNodeAttr = exports.codeBlockNodeName = exports.resetCodeblock = void 0;
// @ts-ignore // TODO: fix cjs build
const prosemirror_utils_1 = require("prosemirror-utils");
const lodash_1 = require("../../../lodash.js");
const inputrules_1 = require("../../../utils/inputrules.js");
const keymap_1 = require("../../../utils/keymap.js");
const CodeBlockHighlight_1 = require("./CodeBlockHighlight/CodeBlockHighlight.js");
const CodeBlockSpecs_1 = require("./CodeBlockSpecs/index.js");
const commands_1 = require("./commands.js");
const const_1 = require("./const.js");
const codeBlockPastePlugin_1 = require("./plugins/codeBlockPastePlugin.js");
var commands_2 = require("./commands.js");
Object.defineProperty(exports, "resetCodeblock", { enumerable: true, get: function () { return commands_2.resetCodeblock; } });
var CodeBlockSpecs_2 = require("./CodeBlockSpecs/index.js");
Object.defineProperty(exports, "codeBlockNodeName", { enumerable: true, get: function () { return CodeBlockSpecs_2.codeBlockNodeName; } });
Object.defineProperty(exports, "CodeBlockNodeAttr", { enumerable: true, get: function () { return CodeBlockSpecs_2.CodeBlockNodeAttr; } });
Object.defineProperty(exports, "codeBlockType", { enumerable: true, get: function () { return CodeBlockSpecs_2.codeBlockType; } });
const CodeBlock = (builder, opts) => {
    builder.use(CodeBlockSpecs_1.CodeBlockSpecs, opts);
    builder.addKeymap((deps) => {
        const { codeBlockKey } = opts;
        const bindings = { Enter: commands_1.newlineInCode, Backspace: commands_1.resetCodeblock };
        if (codeBlockKey) {
            bindings[codeBlockKey] = (0, keymap_1.withLogAction)('code_block', (0, commands_1.setCodeBlockType)(deps));
        }
        return bindings;
    });
    builder.addInputRules(({ schema }) => ({ rules: [codeBlockRule((0, const_1.codeBlockType)(schema))] }));
    builder.addAction(const_1.cbAction, (deps) => {
        const cb = (0, const_1.codeBlockType)(deps.schema);
        return {
            isActive: (state) => (0, prosemirror_utils_1.hasParentNodeOfType)(cb)(state.selection),
            isEnable: (0, commands_1.setCodeBlockType)(deps),
            run: (0, commands_1.setCodeBlockType)(deps),
        };
    });
    builder.addPlugin(codeBlockPastePlugin_1.codeBlockPastePlugin, builder.Priority.High);
    if ((0, lodash_1.isFunction)(opts.langs)) {
        builder.use(opts.langs);
    }
    else {
        builder.use(CodeBlockHighlight_1.CodeBlockHighlight, opts.langs ?? {});
    }
};
exports.CodeBlock = CodeBlock;
// 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 (0, inputrules_1.textblockTypeInputRule)(/^```$/, nodeType);
}
//# sourceMappingURL=index.js.map
