import { Node as l, mergeAttributes as a } from "../../../../../@tiptap_core@2.14.0_@tiptap_pm@2.14.0/node_modules/@tiptap/core/dist/index.js";
import { PluginKey as u } from "../../../../../prosemirror-state@1.4.3/node_modules/prosemirror-state/dist/index.js";
import { Suggestion as p } from "../../../../../@tiptap_suggestion@2.14.0_@tiptap_core@2.14.0_@tiptap_pm@2.14.0__@tiptap_pm@2.14.0/node_modules/@tiptap/suggestion/dist/index.js";
const c = new u("mention"), g = l.create({
  name: "mention",
  priority: 101,
  addOptions() {
    return {
      HTMLAttributes: {},
      renderText({ options: e, node: t }) {
        var n;
        return `${e.suggestion.char}${(n = t.attrs.label) !== null && n !== void 0 ? n : t.attrs.id}`;
      },
      deleteTriggerWithBackspace: !1,
      renderHTML({ options: e, node: t }) {
        var n;
        return [
          "span",
          a(this.HTMLAttributes, e.HTMLAttributes),
          `${e.suggestion.char}${(n = t.attrs.label) !== null && n !== void 0 ? n : t.attrs.id}`
        ];
      },
      suggestion: {
        char: "@",
        pluginKey: c,
        command: ({ editor: e, range: t, props: n }) => {
          var r, i, s;
          const o = e.view.state.selection.$to.nodeAfter;
          ((r = o?.text) === null || r === void 0 ? void 0 : r.startsWith(" ")) && (t.to += 1), e.chain().focus().insertContentAt(t, [
            {
              type: this.name,
              attrs: n
            },
            {
              type: "text",
              text: " "
            }
          ]).run(), (s = (i = e.view.dom.ownerDocument.defaultView) === null || i === void 0 ? void 0 : i.getSelection()) === null || s === void 0 || s.collapseToEnd();
        },
        allow: ({ state: e, range: t }) => {
          const n = e.doc.resolve(t.from), r = e.schema.nodes[this.name];
          return !!n.parent.type.contentMatch.matchType(r);
        }
      }
    };
  },
  group: "inline",
  inline: !0,
  selectable: !1,
  atom: !0,
  addAttributes() {
    return {
      id: {
        default: null,
        parseHTML: (e) => e.getAttribute("data-id"),
        renderHTML: (e) => e.id ? {
          "data-id": e.id
        } : {}
      },
      label: {
        default: null,
        parseHTML: (e) => e.getAttribute("data-label"),
        renderHTML: (e) => e.label ? {
          "data-label": e.label
        } : {}
      }
    };
  },
  parseHTML() {
    return [
      {
        tag: `span[data-type="${this.name}"]`
      }
    ];
  },
  renderHTML({ node: e, HTMLAttributes: t }) {
    if (this.options.renderLabel !== void 0)
      return console.warn("renderLabel is deprecated use renderText and renderHTML instead"), [
        "span",
        a({ "data-type": this.name }, this.options.HTMLAttributes, t),
        this.options.renderLabel({
          options: this.options,
          node: e
        })
      ];
    const n = { ...this.options };
    n.HTMLAttributes = a({ "data-type": this.name }, this.options.HTMLAttributes, t);
    const r = this.options.renderHTML({
      options: n,
      node: e
    });
    return typeof r == "string" ? [
      "span",
      a({ "data-type": this.name }, this.options.HTMLAttributes, t),
      r
    ] : r;
  },
  renderText({ node: e }) {
    return this.options.renderLabel !== void 0 ? (console.warn("renderLabel is deprecated use renderText and renderHTML instead"), this.options.renderLabel({
      options: this.options,
      node: e
    })) : this.options.renderText({
      options: this.options,
      node: e
    });
  },
  addKeyboardShortcuts() {
    return {
      Backspace: () => this.editor.commands.command(({ tr: e, state: t }) => {
        let n = !1;
        const { selection: r } = t, { empty: i, anchor: s } = r;
        return i ? (t.doc.nodesBetween(s - 1, s, (o, d) => {
          if (o.type.name === this.name)
            return n = !0, e.insertText(this.options.deleteTriggerWithBackspace ? "" : this.options.suggestion.char || "", d, d + o.nodeSize), !1;
        }), n) : !1;
      })
    };
  },
  addProseMirrorPlugins() {
    return [
      p({
        editor: this.editor,
        ...this.options.suggestion
      })
    ];
  }
});
export {
  g as Mention,
  c as MentionPluginKey,
  g as default
};
//# sourceMappingURL=index.js.map
