import { hasParentNodeOfType } from "../../../pm/utils.js";
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, lineNumbersOptionsDefault } 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) => {
    const optsNormalized = {
        ...opts,
        lineWrapping: {
            enabled: opts.lineWrapping?.enabled || false,
        },
        lineNumbers: {
            enabled: typeof opts.lineNumbers?.enabled === 'boolean'
                ? opts.lineNumbers.enabled
                : lineNumbersOptionsDefault.enabled,
            showByDefault: typeof opts.lineNumbers?.showByDefault === 'boolean'
                ? opts.lineNumbers.showByDefault
                : lineNumbersOptionsDefault.showByDefault,
        },
    };
    builder.use(CodeBlockSpecs, optsNormalized);
    builder.addKeymap((deps) => {
        const { codeBlockKey } = optsNormalized;
        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(optsNormalized.langs)) {
        builder.use(optsNormalized.langs);
    }
    else {
        builder.use(CodeBlockHighlight, {
            langs: optsNormalized.langs,
            lineNumbers: optsNormalized.lineNumbers,
            lineWrapping: optsNormalized.lineWrapping,
        });
    }
};
// 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
