import { wrappingInputRule } from "../../../utils/inputrules.js";
import { ListsAttr } from "./ListsSpecs/index.js";
import { blType, olType } from "./utils.js";
export const ListsInputRulesExtension = (builder, options) => {
    builder.addInputRules(({ schema }) => {
        const rules = [orderedListRule(olType(schema))];
        if (options.bulletListInputRule !== false) {
            const blRule = bulletListRule(blType(schema), options.bulletListInputRule);
            if (blRule)
                rules.push(blRule);
        }
        return { rules };
    });
};
/**
 * 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.
 */
export function orderedListRule(nodeType) {
    return wrappingInputRule(/^(\d+)([.)])\s$/, nodeType, (match) => ({
        [ListsAttr.Order]: Number(match[1]),
        [ListsAttr.Markup]: match[2],
    }), (match, node) => node.childCount + node.attrs[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.
 */
export 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 wrappingInputRule(regexp, nodeType, (match) => ({ [ListsAttr.Markup]: match[1] }));
}
//# sourceMappingURL=inputrules.js.map
