"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ListsInputRulesExtension = void 0;
exports.orderedListRule = orderedListRule;
exports.bulletListRule = bulletListRule;
const inputrules_1 = require("../../../utils/inputrules.js");
const ListsSpecs_1 = require("./ListsSpecs/index.js");
const utils_1 = require("./utils.js");
const ListsInputRulesExtension = (builder, options) => {
    builder.addInputRules(({ schema }) => {
        const rules = [orderedListRule((0, utils_1.olType)(schema))];
        if (options.bulletListInputRule !== false) {
            const blRule = bulletListRule((0, utils_1.blType)(schema), options.bulletListInputRule);
            if (blRule)
                rules.push(blRule);
        }
        return { rules };
    });
};
exports.ListsInputRulesExtension = ListsInputRulesExtension;
/**
 * Given a list node type, returns an input rule that turns a number
 * followed by a dot or parenthesis at the start of a textblock into an ordered list.
 */
function orderedListRule(nodeType) {
    return (0, inputrules_1.wrappingInputRule)(/^(\d+)([.)])\s$/, nodeType, (match) => ({
        [ListsSpecs_1.ListsAttr.Order]: Number(match[1]),
        [ListsSpecs_1.ListsAttr.Markup]: match[2],
    }), (match, node) => node.childCount + node.attrs[ListsSpecs_1.ListsAttr.Order] === Number(match[1]));
}
/**
 * Given a list node type, returns an input rule that turns a bullet
 * (dash, plus, or asterisk) at the start of a textblock into a
 * bullet list.
 */
function bulletListRule(nodeType, config) {
    const bullets = [];
    if (config?.dash !== false)
        bullets.push('-');
    if (config?.plus !== false)
        bullets.push('+');
    if (config?.asterisk !== false)
        bullets.push('*');
    if (bullets.length === 0)
        return null;
    const regexp = new RegExp(`^\\s*([${bullets.join('')}])\\s$`); // same as /^\s*([-+*])\s$/
    return (0, inputrules_1.wrappingInputRule)(regexp, nodeType, (match) => ({ [ListsSpecs_1.ListsAttr.Markup]: match[1] }));
}
//# sourceMappingURL=inputrules.js.map
