import { InputRule } from 'prosemirror-inputrules';
import { escapeRegExp } from "../../../../utils/ecapeRegexp.js";
import { hasCodeMark } from "../../../../utils/inputrules.js";
import { EmojiConsts } from "../EmojiSpecs/index.js";
export const EmojiInput = (builder, opts) => {
    const { defs, shortcuts } = opts;
    if (defs && shortcuts) {
        const allPatternsForRegexp = [];
        const mapPatternToMarkup = {};
        for (const [markup, patterns] of Object.entries(shortcuts)) {
            if (!patterns)
                continue;
            const markupPatterns = [].concat(patterns);
            for (const pattern of markupPatterns) {
                allPatternsForRegexp.push(escapeRegExp(pattern));
                mapPatternToMarkup[pattern] = markup;
            }
        }
        if (allPatternsForRegexp.length) {
            const regex = new RegExp(`(?:^|\\s)(${allPatternsForRegexp.join('|')})\\s$`, 'g');
            builder.addInputRules(() => ({
                rules: [
                    new InputRule(regex, (state, match, start, end) => {
                        if (hasCodeMark(state, match, start, end))
                            return null;
                        const pattern = match[1];
                        const markup = mapPatternToMarkup[pattern];
                        const content = defs[markup];
                        if (content) {
                            const { tr, schema } = state;
                            const whitespaceAtStart = /^\s/.test(match[0]);
                            const from = start + (whitespaceAtStart ? 1 : 0);
                            const to = end;
                            return tr.replaceWith(from, to, [
                                EmojiConsts.nodeType(schema).create({ [EmojiConsts.NodeAttrs.Markup]: markup }, schema.text(content)),
                                schema.text(' '),
                            ]);
                        }
                        return null;
                    }),
                ],
            }));
        }
    }
};
//# sourceMappingURL=EmojiInput.js.map
