import { Plugin as T, PluginKey as U } from "../../../../../prosemirror-state@1.4.3/node_modules/prosemirror-state/dist/index.js";
import { DecorationSet as j, Decoration as L } from "../../../../../prosemirror-view@1.40.0/node_modules/prosemirror-view/dist/index.js";
import { escapeForRegEx as z } from "../../../../../@tiptap_core@2.14.0_@tiptap_pm@2.14.0/node_modules/@tiptap/core/dist/index.js";
function G(P) {
  var c;
  const { char: q, allowSpaces: B, allowToIncludeChar: _, allowedPrefixes: f, startOfLine: C, $position: d } = P, E = B && !_, g = z(q), D = new RegExp(`\\s${g}$`), v = C ? "^" : "", I = _ ? "" : g, M = E ? new RegExp(`${v}${g}.*?(?=\\s${I}|$)`, "gm") : new RegExp(`${v}(?:^)?${g}[^\\s${I}]*`, "gm"), r = ((c = d.nodeBefore) === null || c === void 0 ? void 0 : c.isText) && d.nodeBefore.text;
  if (!r)
    return null;
  const e = d.pos - r.length, o = Array.from(r.matchAll(M)).pop();
  if (!o || o.input === void 0 || o.index === void 0)
    return null;
  const n = o.input.slice(Math.max(0, o.index - 1), o.index), i = new RegExp(`^[${f?.join("")}\0]?$`).test(n);
  if (f !== null && !i)
    return null;
  const a = e + o.index;
  let l = a + o[0].length;
  return E && D.test(r.slice(l - 1, l + 1)) && (o[0] += " ", l += 1), a < d.pos && l >= d.pos ? {
    range: {
      from: a,
      to: l
    },
    query: o[0].slice(q.length),
    text: o[0]
  } : null;
}
const H = new U("suggestion");
function W({ pluginKey: P = H, editor: c, char: q = "@", allowSpaces: B = !1, allowToIncludeChar: _ = !1, allowedPrefixes: f = [" "], startOfLine: C = !1, decorationTag: d = "span", decorationClass: E = "suggestion", command: g = () => null, items: D = () => [], render: v = () => ({}), allow: I = () => !0, findSuggestionMatch: M = G }) {
  let r;
  const e = v?.(), o = new T({
    key: P,
    view() {
      return {
        update: async (n, i) => {
          var a, l, u, p, m, y, x;
          const t = (a = this.key) === null || a === void 0 ? void 0 : a.getState(i), s = (l = this.key) === null || l === void 0 ? void 0 : l.getState(n.state), S = t.active && s.active && t.range.from !== s.range.from, N = !t.active && s.active, b = t.active && !s.active, k = !N && !b && t.query !== s.query, $ = N || S && k, R = k || S, A = b || S && k;
          if (!$ && !R && !A)
            return;
          const h = A && !$ ? t : s, F = n.dom.querySelector(`[data-decoration-id="${h.decorationId}"]`);
          r = {
            editor: c,
            range: h.range,
            query: h.query,
            text: h.text,
            items: [],
            command: (w) => g({
              editor: c,
              range: h.range,
              props: w
            }),
            decorationNode: F,
            // virtual node for popper.js or tippy.js
            // this can be used for building popups without a DOM node
            clientRect: F ? () => {
              var w;
              const { decorationId: O } = (w = this.key) === null || w === void 0 ? void 0 : w.getState(c.state), K = n.dom.querySelector(`[data-decoration-id="${O}"]`);
              return K?.getBoundingClientRect() || null;
            } : null
          }, $ && ((u = e?.onBeforeStart) === null || u === void 0 || u.call(e, r)), R && ((p = e?.onBeforeUpdate) === null || p === void 0 || p.call(e, r)), (R || $) && (r.items = await D({
            editor: c,
            query: h.query
          })), A && ((m = e?.onExit) === null || m === void 0 || m.call(e, r)), R && ((y = e?.onUpdate) === null || y === void 0 || y.call(e, r)), $ && ((x = e?.onStart) === null || x === void 0 || x.call(e, r));
        },
        destroy: () => {
          var n;
          r && ((n = e?.onExit) === null || n === void 0 || n.call(e, r));
        }
      };
    },
    state: {
      // Initialize the plugin's internal state.
      init() {
        return {
          active: !1,
          range: {
            from: 0,
            to: 0
          },
          query: null,
          text: null,
          composing: !1
        };
      },
      // Apply changes to the plugin state from a view transaction.
      apply(n, i, a, l) {
        const { isEditable: u } = c, { composing: p } = c.view, { selection: m } = n, { empty: y, from: x } = m, t = { ...i };
        if (t.composing = p, u && (y || c.view.composing)) {
          (x < i.range.from || x > i.range.to) && !p && !i.composing && (t.active = !1);
          const s = M({
            char: q,
            allowSpaces: B,
            allowToIncludeChar: _,
            allowedPrefixes: f,
            startOfLine: C,
            $position: m.$from
          }), S = `id_${Math.floor(Math.random() * 4294967295)}`;
          s && I({
            editor: c,
            state: l,
            range: s.range,
            isActive: i.active
          }) ? (t.active = !0, t.decorationId = i.decorationId ? i.decorationId : S, t.range = s.range, t.query = s.query, t.text = s.text) : t.active = !1;
        } else
          t.active = !1;
        return t.active || (t.decorationId = null, t.range = { from: 0, to: 0 }, t.query = null, t.text = null), t;
      }
    },
    props: {
      // Call the keydown hook if suggestion is active.
      handleKeyDown(n, i) {
        var a;
        const { active: l, range: u } = o.getState(n.state);
        return l && ((a = e?.onKeyDown) === null || a === void 0 ? void 0 : a.call(e, { view: n, event: i, range: u })) || !1;
      },
      // Setup decorator on the currently active suggestion.
      decorations(n) {
        const { active: i, range: a, decorationId: l } = o.getState(n);
        return i ? j.create(n.doc, [
          L.inline(a.from, a.to, {
            nodeName: d,
            class: E,
            "data-decoration-id": l
          })
        ]) : null;
      }
    }
  });
  return o;
}
export {
  W as Suggestion,
  H as SuggestionPluginKey,
  W as default,
  G as findSuggestionMatch
};
//# sourceMappingURL=index.js.map
