import { Decoration_Type } from "./../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import mentionDataDefaults from "./../../ricos-schema/legacy/statics/mention.defaults.json.js";
import { Fragment } from "./../../tiptap/pm/model.js";
import { createRicosMarkExtension } from "./../../plugin-commons/index.js";
import styles from "../statics/mentions-scss.js";
import { Suggestion } from "./suggestion.js";
const findMention = (editor, char) => {
  const mentionRegex = new RegExp(`(?:^)?${char}[^\\s${char}]*`, "gm");
  const { nodeBefore, pos } = editor.state.selection.$from;
  const { text, nodeSize: nodeBeforeSize } = nodeBefore || {};
  const mention = text?.match(mentionRegex)?.[0];
  const mentionIndex = text ? text.search(mentionRegex) : -1;
  if (nodeBeforeSize && mention && mentionIndex !== -1) {
    return { from: pos - nodeBeforeSize + mentionIndex, to: pos };
  }
};
const tiptapExtensions = [
  createRicosMarkExtension({
    type: "mark",
    groups: ["shortcuts-enabled"],
    name: Decoration_Type.MENTION,
    reconfigure: (config, _extensions, _props, settings) => {
      return {
        ...config,
        addOptions: () => ({
          settings,
          HTMLAttributes: {},
          renderLabel({ node }) {
            return `${settings.mentionTrigger}${node.attrs.label ?? node.attrs.id}`;
          }
        })
      };
    },
    createExtensionConfig() {
      return {
        priority: 110,
        name: this.name,
        group: "inline",
        inline: true,
        selectable: false,
        atom: true,
        addAttributes() {
          return mentionDataDefaults.mentionData;
        },
        renderHTML() {
          return ["span", { class: styles.mention }, 0];
        },
        addKeyboardShortcuts() {
          return {
            Backspace: () => this.editor.commands.command(({ dispatch, tr, state }) => {
              let isMention = false;
              const { selection } = state;
              const { empty, anchor } = selection;
              if (!empty) {
                return false;
              }
              state.doc.nodesBetween(anchor - 1, anchor, (node, pos) => {
                if (node.text === this.options.settings.mentionTrigger) {
                  isMention = true;
                  dispatch?.(tr.replaceWith(pos, pos + 1, Fragment.empty));
                }
              });
              return isMention;
            })
          };
        },
        addProseMirrorPlugins() {
          return [Suggestion(this.editor, this.options.settings)];
        },
        addCommands() {
          return {
            insertMention: (mention, pos) => ({ view, tr, editor }) => {
              const nodeAfter = view.state.selection.$to.nodeAfter;
              const overrideSpace = nodeAfter?.text?.startsWith(" ");
              const { mentionTrigger } = this.options.settings;
              const range = pos || mentionTrigger && findMention(editor, mentionTrigger) || {
                from: tr.selection.from,
                to: tr.selection.to
              };
              if (overrideSpace) {
                range.to += 1;
              }
              return editor.chain().focus().insertContentAt(range, [
                {
                  type: "text",
                  text: `${mentionTrigger}${mention.name}`,
                  marks: [
                    {
                      type: Decoration_Type.MENTION,
                      attrs: {
                        name: mention.name,
                        slug: mention.slug,
                        id: mention.id
                      }
                    }
                  ]
                },
                {
                  type: "text",
                  text: " "
                }
              ]).run();
            }
          };
        }
      };
    }
  })
];
export {
  tiptapExtensions
};
//# sourceMappingURL=tiptap.js.map
