"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.foldingHeadingRule = foldingHeadingRule;
const prosemirror_inputrules_1 = require("prosemirror-inputrules");
const inputrules_1 = require("../../../utils/inputrules.js");
const specs_1 = require("../../base/specs.js");
const const_1 = require("./const.js");
const utils_1 = require("./utils.js");
function foldingHeadingRule(nodeType, maxLevel) {
    return new prosemirror_inputrules_1.InputRule(new RegExp('^(#{1,' + maxLevel + '})\\+\\s$'), (state, match, start, end) => {
        if ((0, inputrules_1.hasCodeMark)(state, match, start, end))
            return null;
        const $start = state.doc.resolve(start);
        if (!$start.node(-1).canReplaceWith($start.index(-1), $start.indexAfter(-1), nodeType))
            return null;
        const level = match[1].length;
        const pos = $start.before();
        const tr = state.tr.delete(start, end);
        if (!(0, utils_1.isHeading)($start.parent))
            tr.setNodeMarkup(pos, nodeType);
        tr.setNodeAttribute(pos, const_1.YfmHeadingAttr.Level, level);
        tr.setNodeAttribute(pos, const_1.YfmHeadingAttr.Folding, false);
        // insert empty paragraph if content of new folding heading is empty
        const nextNode = $start.node(-1).maybeChild($start.indexAfter(-1));
        if (!nextNode || ((0, utils_1.isFoldingHeading)(nextNode) && (0, utils_1.parseLevel)(nextNode) <= level)) {
            tr.insert(tr.mapping.map($start.after()), (0, specs_1.pType)(state.schema).create());
        }
        return tr;
    });
}
//# sourceMappingURL=input-rules.js.map
