const T = ".rte-content, .editora-content", re = '[data-theme="dark"], .dark, .editora-theme-dark', ne = {
  title: "Insert Merge Tag",
  searchPlaceholder: "Search merge tags...",
  emptyStateText: "No merge tags found",
  cancelText: "Cancel",
  insertText: "Insert",
  showPreview: !0
};
function ae() {
  return [
    {
      id: "USER",
      name: "User",
      tags: [
        { key: "first_name", label: "First Name", category: "User", preview: "John" },
        { key: "last_name", label: "Last Name", category: "User", preview: "Doe" },
        { key: "email", label: "Email", category: "User", preview: "john@example.com" },
        { key: "phone", label: "Phone", category: "User", preview: "+1-555-1234" },
        { key: "full_name", label: "Full Name", category: "User", preview: "John Doe" },
        { key: "username", label: "Username", category: "User", preview: "johndoe" }
      ]
    },
    {
      id: "COMPANY",
      name: "Company",
      tags: [
        { key: "company_name", label: "Company Name", category: "Company", preview: "Acme Corp" },
        { key: "company_address", label: "Company Address", category: "Company", preview: "123 Main St" },
        { key: "company_phone", label: "Company Phone", category: "Company", preview: "+1-555-0000" },
        { key: "company_email", label: "Company Email", category: "Company", preview: "info@acme.com" }
      ]
    },
    {
      id: "DATE",
      name: "Date",
      tags: [
        { key: "today", label: "Today", category: "Date", preview: (/* @__PURE__ */ new Date()).toLocaleDateString() },
        { key: "tomorrow", label: "Tomorrow", category: "Date", preview: new Date(Date.now() + 864e5).toLocaleDateString() },
        { key: "next_week", label: "Next Week", category: "Date", preview: new Date(Date.now() + 6048e5).toLocaleDateString() }
      ]
    },
    {
      id: "CUSTOM",
      name: "Custom",
      tags: []
    }
  ];
}
function I(e, r) {
  return e.trim().toUpperCase().replace(/[^A-Z0-9]+/g, "_").replace(/^_+|_+$/g, "") || `CATEGORY_${r + 1}`;
}
function oe(e, r) {
  return (e.key || e.value || e.label).trim().toLowerCase().replace(/[^a-z0-9]+/g, "_").replace(/^_+|_+$/g, "") || `tag_${r + 1}`;
}
function le(e) {
  const r = (() => {
    if (Array.isArray(e == null ? void 0 : e.categories) && e.categories.length > 0)
      return e.categories;
    if (Array.isArray(e == null ? void 0 : e.tags) && e.tags.length > 0) {
      const c = /* @__PURE__ */ new Map();
      return e.tags.forEach((s) => {
        const g = (s.category || "Custom").trim() || "Custom", i = c.get(g);
        i ? i.push(s) : c.set(g, [s]);
      }), Array.from(c.entries()).map(([s, g], i) => ({
        id: I(s, i),
        name: s,
        tags: g
      }));
    }
    return ae();
  })(), t = {}, n = [];
  if (r.forEach((c, s) => {
    const g = I(c.id || c.name, s);
    n.push(g), t[g] = {
      name: c.name,
      tags: (Array.isArray(c.tags) ? c.tags : []).map((i, y) => {
        var h, x;
        const u = oe(i, y), p = (i.category || c.name).trim() || c.name;
        return {
          ...i,
          key: u,
          category: p,
          categoryKey: g,
          searchIndex: `${i.label} ${u} ${p} ${(h = i.description) != null ? h : ""} ${(x = i.value) != null ? x : ""}`.toLowerCase()
        };
      })
    };
  }), n.length === 0) {
    const c = "CUSTOM";
    n.push(c), t[c] = { name: "Custom", tags: [] };
  }
  const a = e != null && e.defaultCategory ? I(e.defaultCategory, 0) : null, o = a && n.includes(a) ? a : n[0];
  return {
    categoriesByKey: t,
    categoryKeys: n,
    defaultCategory: o
  };
}
function ce(e) {
  const r = e == null ? void 0 : e.tokenTemplate;
  return typeof r == "function" ? (t) => {
    var a;
    const n = r(t);
    return typeof n == "string" && n.trim() ? n : ((a = t.value) == null ? void 0 : a.trim()) || `{{ ${t.label} }}`;
  } : typeof r == "string" && r.trim() ? (t) => {
    var n;
    return r.replace(/\{key\}/gi, t.key).replace(/\{label\}/gi, t.label).replace(/\{category\}/gi, t.category).replace(/\{value\}/gi, (n = t.value) != null ? n : "");
  } : (t) => {
    var n;
    return ((n = t.value) == null ? void 0 : n.trim()) || `{{ ${t.label} }}`;
  };
}
function ie(e) {
  return {
    catalog: le(e),
    dialog: {
      ...ne,
      ...(e == null ? void 0 : e.dialog) || {}
    },
    formatToken: ce(e)
  };
}
let B = !1, q = !1, k = null, A = null, E = null, W = !1, b = null;
function Z(e, r) {
  if (r === e.innerHTML) return;
  const t = window.execEditorCommand || window.executeEditorCommand;
  if (typeof t == "function")
    try {
      t("recordDomTransaction", e, r, e.innerHTML);
    } catch (n) {
    }
}
function _() {
  if (B || typeof document == "undefined") return;
  B = !0;
  const e = document.createElement("style");
  e.id = "merge-tag-plugin-styles", e.textContent = `
    .rte-merge-tag-overlay {
      --rte-mt-overlay-bg: rgba(15, 23, 36, 0.56);
      --rte-mt-dialog-bg: #ffffff;
      --rte-mt-dialog-text: #101828;
      --rte-mt-border: #d6dbe4;
      --rte-mt-subtle-bg: #f7f9fc;
      --rte-mt-subtle-hover: #eef2f7;
      --rte-mt-muted-text: #5f6b7d;
      --rte-mt-accent: #1976d2;
      --rte-mt-accent-strong: #1565c0;
      position: fixed;
      inset: 0;
      background-color: var(--rte-mt-overlay-bg);
      backdrop-filter: blur(2px);
      display: flex;
      align-items: center;
      justify-content: center;
      z-index: 10000;
      padding: 16px;
      box-sizing: border-box;
    }

    .rte-merge-tag-overlay.rte-ui-theme-dark {
      --rte-mt-overlay-bg: rgba(2, 8, 20, 0.72);
      --rte-mt-dialog-bg: #202938;
      --rte-mt-dialog-text: #e8effc;
      --rte-mt-border: #49566c;
      --rte-mt-subtle-bg: #2a3444;
      --rte-mt-subtle-hover: #344256;
      --rte-mt-muted-text: #a5b1c5;
      --rte-mt-accent: #58a6ff;
      --rte-mt-accent-strong: #4598f4;
    }

    .rte-merge-tag-dialog {
      background: var(--rte-mt-dialog-bg);
      color: var(--rte-mt-dialog-text);
      border: 1px solid var(--rte-mt-border);
      border-radius: 12px;
      box-shadow: 0 24px 48px rgba(10, 15, 24, 0.28);
      width: 500px;
      max-width: 90vw;
      max-height: 80vh;
      display: flex;
      flex-direction: column;
      overflow: hidden;
    }

    .rte-merge-tag-header { padding: 16px; border-bottom: 1px solid var(--rte-mt-border); display:flex; justify-content:space-between; align-items:center; }
    .rte-merge-tag-body {
      padding: 16px;
      flex: 1;
      min-height: 0;
      display: flex;
      flex-direction: column;
      overflow: hidden;
    }
    .rte-merge-tag-input {
      width:100%;
      padding:11px 12px;
      border:1px solid var(--rte-mt-border);
      border-radius:6px;
      background:var(--rte-mt-subtle-bg);
      color:var(--rte-mt-dialog-text);
      font-size:14px;
      line-height:1.45;
      box-sizing:border-box;
    }
    .rte-merge-tag-tabs { display:flex; flex-wrap: wrap; gap:8px; margin: 12px 0; }
    .rte-merge-tag-tab { padding:8px 12px; background:none; border:none; cursor:pointer; color:var(--rte-mt-muted-text); border-bottom:3px solid transparent; }
    .rte-merge-tag-tab.active { color:var(--rte-mt-accent); border-bottom-color:var(--rte-mt-accent); }
    .rte-merge-tag-list {
      border:1px solid var(--rte-mt-border);
      border-radius:4px;
      flex: 1;
      min-height: 180px;
      max-height: 300px;
      overflow-y:auto;
      overflow-x:hidden;
      margin-bottom:12px;
      background:var(--rte-mt-subtle-bg);
    }
    .rte-merge-tag-item {
      padding:8px 12px;
      border-bottom:1px solid var(--rte-mt-border);
      cursor:pointer;
      transition:background-color 0.16s;
      color:var(--rte-mt-dialog-text);
      overflow-wrap:anywhere;
      word-break:break-word;
    }
    .rte-merge-tag-item:last-child { border-bottom: none; }
    .rte-merge-tag-item.selected, .rte-merge-tag-item:hover { background-color:var(--rte-mt-subtle-hover); }
    .rte-merge-tag-item-label { font-weight: 600; }
    .rte-merge-tag-item-preview { font-size: 12px; color: var(--rte-mt-muted-text); margin-top: 2px; overflow-wrap:anywhere; word-break:break-word; }
    .rte-merge-tag-empty { padding: 24px; text-align: center; color: var(--rte-mt-muted-text); }
    .rte-merge-tag-preview { padding:8px; background:var(--rte-mt-subtle-bg); border-radius:4px; font-family:monospace; font-size:12px; color:var(--rte-mt-dialog-text); overflow-wrap:anywhere; word-break:break-word; }
    .rte-merge-tag-footer { padding:12px 16px; border-top:1px solid var(--rte-mt-border); display:flex; gap:8px; justify-content:flex-end; background:var(--rte-mt-subtle-bg); }
    .rte-merge-tag-btn-primary { padding:8px 16px; border:none; border-radius:4px; background:var(--rte-mt-accent); color:#fff; cursor:pointer; }
    .rte-merge-tag-btn-primary:hover { background: var(--rte-mt-accent-strong); }
    .rte-merge-tag-btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
    .rte-merge-tag-btn-secondary { padding:8px 16px; border:1px solid var(--rte-mt-border); border-radius:4px; background:var(--rte-mt-subtle-bg); color:var(--rte-mt-dialog-text); cursor:pointer; }

    .rte-merge-tag {
      display: inline-flex;
      align-items: center;
      white-space: nowrap;
      user-select: none;
      background-color: #e3f2fd;
      border: 1px solid #bbdefb;
      border-radius: 3px;
      padding: 1px 6px;
      margin: 0 2px;
      color: #1976d2;
      font-weight: 600;
      line-height: 1.3;
    }

    .rte-merge-tag.rte-merge-tag-selected {
      outline: 2px solid #2563eb;
      outline-offset: 2px;
      box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.18);
    }

    :is([data-theme="dark"], .dark, .editora-theme-dark) .rte-merge-tag {
      background: #223247;
      border-color: #3f5f84;
      color: #8dc4ff;
    }
  `, document.head.appendChild(e);
}
function V() {
  q || typeof document == "undefined" || (q = !0, document.addEventListener("focusin", (e) => {
    const r = e.target, t = r == null ? void 0 : r.closest(T);
    t && (k = t);
  }), document.addEventListener("selectionchange", () => {
    const e = Q();
    e && (k = e);
  }));
}
function Q() {
  const e = window.getSelection();
  if (!e || e.rangeCount === 0) return null;
  const t = e.getRangeAt(0).startContainer, n = t.nodeType === Node.ELEMENT_NODE ? t : t.parentElement;
  return (n == null ? void 0 : n.closest(T)) || null;
}
function ee() {
  const e = Q();
  if (e) return e;
  const r = document.activeElement, t = r == null ? void 0 : r.closest(T);
  return t || (k != null && k.isConnected ? k : document.querySelector(T));
}
function O() {
  b && (b.classList.remove("rte-merge-tag-selected"), b.removeAttribute("data-rte-selected-object"), b = null);
}
function se(e) {
  b !== e && (O(), b = e, e.classList.add("rte-merge-tag-selected"), e.setAttribute("data-rte-selected-object", "true")), de(e);
}
function de(e) {
  const r = e.parentNode;
  if (!r) return;
  const t = window.getSelection();
  if (!t) return;
  const n = document.createRange(), o = Array.from(r.childNodes).indexOf(e);
  o < 0 || (n.setStart(r, o), n.setEnd(r, o + 1), t.removeAllRanges(), t.addRange(n));
}
function G(e, r) {
  var t;
  e instanceof Text && e.data.length !== 0 && (r ? (e.data.startsWith(" ") || e.data.startsWith(" ")) && e.deleteData(0, 1) : (e.data.endsWith(" ") || e.data.endsWith(" ")) && e.deleteData(e.data.length - 1, 1), e.data.length === 0 && ((t = e.parentNode) == null || t.removeChild(e)));
}
function J(e, r) {
  const t = window.getSelection();
  if (!t) return;
  const n = document.createRange(), a = Math.max(0, Math.min(r, e.childNodes.length));
  n.setStart(e, a), n.collapse(!0), t.removeAllRanges(), t.addRange(n);
}
function ge(e, r) {
  var i;
  const t = e.closest(T), n = (i = t == null ? void 0 : t.innerHTML) != null ? i : "", a = e.parentNode;
  if (!a) return !1;
  const c = Array.from(a.childNodes).indexOf(e);
  if (c < 0) return !1;
  const s = e.previousSibling, g = e.nextSibling;
  return a.removeChild(e), b === e && (b = null), r === "Backspace" ? (G(g, !0), J(a, c)) : (G(s, !1), J(a, c)), t && (Z(t, n), t.dispatchEvent(new Event("input", { bubbles: !0 }))), !0;
}
function me(e) {
  if (e.collapsed || !(e.startContainer instanceof HTMLElement || e.startContainer instanceof Text) || e.startContainer !== e.endContainer || e.endOffset !== e.startOffset + 1) return null;
  const r = e.startContainer;
  if (!(r instanceof Element || r instanceof DocumentFragment)) return null;
  const t = r.childNodes[e.startOffset];
  return t instanceof HTMLElement && t.classList.contains("rte-merge-tag") ? t : null;
}
function ue(e, r) {
  if (!e.collapsed) return null;
  const { startContainer: t, startOffset: n } = e, a = (o) => o instanceof HTMLElement && o.classList.contains("rte-merge-tag") ? o : null;
  if (t.nodeType === Node.ELEMENT_NODE) {
    const o = t;
    return r === "Backspace" && n > 0 ? a(o.childNodes[n - 1] || null) : r === "Delete" ? a(o.childNodes[n] || null) : null;
  }
  if (t.nodeType === Node.TEXT_NODE) {
    const o = t;
    return r === "Backspace" ? n === 0 ? a(o.previousSibling) : n === 1 && (o.data[0] === " " || o.data[0] === " ") && o.previousSibling instanceof HTMLElement && o.previousSibling.classList.contains("rte-merge-tag") ? o.previousSibling : null : n === o.data.length ? a(o.nextSibling) : null;
  }
  return null;
}
function Y() {
  W || typeof document == "undefined" || (W = !0, document.addEventListener("pointerdown", (e) => {
    var a;
    const r = e.target, t = r == null ? void 0 : r.closest(".rte-merge-tag");
    if (!t) {
      const o = e.target;
      (a = o == null ? void 0 : o.closest) != null && a.call(o, ".rte-merge-tag-overlay") || O();
      return;
    }
    const n = t.closest(T);
    n && (e.preventDefault(), e.stopPropagation(), n.focus({ preventScroll: !0 }), se(t));
  }, !0), document.addEventListener("keydown", (e) => {
    if (e.key === "Escape" && b) {
      O();
      return;
    }
    if (e.key !== "Backspace" && e.key !== "Delete") return;
    const r = window.getSelection();
    if (!r || r.rangeCount === 0) return;
    const t = r.getRangeAt(0), n = ee();
    if (!n || !n.contains(t.commonAncestorContainer)) return;
    let a = b != null && b.isConnected ? b : null;
    a && !n.contains(a) && (a = null), a || (a = me(t)), a || (a = ue(t, e.key)), a && (e.preventDefault(), e.stopPropagation(), ge(a, e.key));
  }));
}
function fe(e) {
  return e ? !!e.closest(re) : !1;
}
function pe() {
  E && (E(), E = null), A = null;
}
function X(e) {
  const r = document.createRange();
  return r.selectNodeContents(e), r.collapse(!1), r;
}
function be(e, r) {
  const t = window.getSelection(), n = r ? r.cloneRange() : X(e), o = n.startContainer.isConnected && n.endContainer.isConnected && e.contains(n.commonAncestorContainer) ? n : X(e);
  return t && (t.removeAllRanges(), t.addRange(o)), o;
}
function ye(e, r) {
  const t = document.createElement("span");
  t.className = "rte-merge-tag", t.setAttribute("contenteditable", "false"), t.setAttribute("data-key", e.key), t.setAttribute("data-category", e.category), t.setAttribute("data-label", e.label), e.value && t.setAttribute("data-value", e.value);
  const n = r(e);
  return t.setAttribute("data-token", n), t.setAttribute("aria-label", `Merge tag: ${e.label}`), t.textContent = n, t;
}
function xe(e, r, t, n) {
  const a = window.getSelection();
  if (!a) return !1;
  const o = e.innerHTML;
  e.focus({ preventScroll: !0 });
  const c = be(e, r), s = c.startContainer.nodeType === Node.ELEMENT_NODE ? c.startContainer : c.startContainer.parentElement, g = s == null ? void 0 : s.closest(".rte-merge-tag");
  g && e.contains(g) && (c.setStartAfter(g), c.setEndAfter(g));
  try {
    c.deleteContents();
    const i = ye(t, n), y = document.createTextNode(" "), u = document.createDocumentFragment();
    u.appendChild(i), u.appendChild(y), c.insertNode(u);
    const p = document.createRange();
    return p.setStartAfter(y), p.collapse(!0), a.removeAllRanges(), a.addRange(p), Z(e, o), e.dispatchEvent(new Event("input", { bubbles: !0 })), !0;
  } catch (i) {
    return console.error("Failed to insert merge tag:", i), !1;
  }
}
function ve(e, r, t) {
  var o;
  const n = ((o = e.catalog.categoriesByKey[r]) == null ? void 0 : o.tags) || [], a = t.trim().toLowerCase();
  return a ? n.filter((c) => c.searchIndex.includes(a)) : n;
}
function he(e, r) {
  var j;
  pe(), _();
  const t = {
    category: r.catalog.defaultCategory,
    searchTerm: "",
    filteredTags: ((j = r.catalog.categoriesByKey[r.catalog.defaultCategory]) == null ? void 0 : j.tags) || [],
    selectedIndex: 0,
    savedRange: (() => {
      const l = window.getSelection();
      if (!l || l.rangeCount === 0) return null;
      const d = l.getRangeAt(0);
      return e.contains(d.commonAncestorContainer) ? d.cloneRange() : null;
    })(),
    searchRaf: null
  }, n = document.createElement("div");
  n.className = "rte-merge-tag-overlay", n.setAttribute("role", "dialog"), n.setAttribute("aria-modal", "true"), fe(e) && n.classList.add("rte-ui-theme-dark");
  const a = document.createElement("div");
  a.className = "rte-merge-tag-dialog";
  const o = document.createElement("div");
  o.className = "rte-merge-tag-header";
  const c = document.createElement("h2");
  c.style.margin = "0", c.style.fontSize = "18px", c.style.fontWeight = "700", c.textContent = r.dialog.title;
  const s = document.createElement("button");
  s.className = "rte-merge-tag-close", s.setAttribute("aria-label", "Close"), s.style.background = "none", s.style.border = "none", s.style.color = "inherit", s.style.cursor = "pointer", s.style.fontSize = "20px", s.textContent = "✕", o.appendChild(c), o.appendChild(s);
  const g = document.createElement("div");
  g.className = "rte-merge-tag-body";
  const i = document.createElement("input");
  i.type = "text", i.className = "rte-merge-tag-input", i.placeholder = r.dialog.searchPlaceholder, i.setAttribute("aria-label", "Search merge tags");
  const y = document.createElement("div");
  y.className = "rte-merge-tag-tabs", r.catalog.categoryKeys.forEach((l) => {
    var f;
    const d = document.createElement("button");
    d.type = "button", d.className = "rte-merge-tag-tab", d.setAttribute("data-category", l), d.textContent = ((f = r.catalog.categoriesByKey[l]) == null ? void 0 : f.name) || l, y.appendChild(d);
  });
  const u = document.createElement("div");
  u.className = "rte-merge-tag-list";
  const p = document.createElement("div");
  p.className = "rte-merge-tag-preview", g.appendChild(i), g.appendChild(y), g.appendChild(u), g.appendChild(p);
  const h = document.createElement("div");
  h.className = "rte-merge-tag-footer";
  const x = document.createElement("button");
  x.type = "button", x.className = "rte-merge-tag-btn-secondary", x.textContent = r.dialog.cancelText;
  const v = document.createElement("button");
  v.type = "button", v.className = "rte-merge-tag-btn-primary", v.textContent = r.dialog.insertText, h.appendChild(x), h.appendChild(v), a.appendChild(o), a.appendChild(g), a.appendChild(h), n.appendChild(a), document.body.appendChild(n), A = n;
  const z = () => {
    y.querySelectorAll(".rte-merge-tag-tab").forEach((d) => {
      const f = d.dataset.category === t.category;
      d.classList.toggle("active", f);
    });
  }, S = () => {
    if (t.filteredTags.length === 0) {
      t.selectedIndex = -1;
      return;
    }
    t.selectedIndex < 0 && (t.selectedIndex = 0), t.selectedIndex >= t.filteredTags.length && (t.selectedIndex = t.filteredTags.length - 1);
  }, F = () => {
    if (!r.dialog.showPreview) {
      p.style.display = "none", v.disabled = t.filteredTags.length === 0;
      return;
    }
    S();
    const l = t.selectedIndex >= 0 ? t.filteredTags[t.selectedIndex] : null;
    if (!l) {
      p.style.display = "none", v.disabled = !0;
      return;
    }
    p.style.display = "block", p.textContent = `Preview: ${r.formatToken(l)}`, v.disabled = !1;
  }, te = () => {
    if (t.selectedIndex < 0) return;
    const l = u.querySelector(`.rte-merge-tag-item[data-index="${t.selectedIndex}"]`);
    l == null || l.scrollIntoView({ block: "nearest" });
  }, C = () => {
    const l = u.querySelector(".rte-merge-tag-item.selected");
    if (l == null || l.classList.remove("selected"), t.selectedIndex >= 0) {
      const d = u.querySelector(`.rte-merge-tag-item[data-index="${t.selectedIndex}"]`);
      d == null || d.classList.add("selected");
    }
    F(), te();
  }, D = () => {
    if (t.filteredTags = ve(r, t.category, t.searchTerm), t.filteredTags.length > 0 && t.selectedIndex < 0 && (t.selectedIndex = 0), S(), u.innerHTML = "", t.filteredTags.length === 0) {
      const d = document.createElement("div");
      d.className = "rte-merge-tag-empty", d.textContent = r.dialog.emptyStateText, u.appendChild(d), F();
      return;
    }
    const l = document.createDocumentFragment();
    t.filteredTags.forEach((d, f) => {
      const m = document.createElement("div");
      m.className = "rte-merge-tag-item", m.setAttribute("data-index", String(f)), m.classList.toggle("selected", f === t.selectedIndex);
      const M = document.createElement("div");
      if (M.className = "rte-merge-tag-item-label", M.textContent = d.label, m.appendChild(M), d.preview) {
        const R = document.createElement("div");
        R.className = "rte-merge-tag-item-preview", R.textContent = d.preview, m.appendChild(R);
      }
      l.appendChild(m);
    }), u.appendChild(l), C();
  }, U = () => {
    t.searchRaf !== null && cancelAnimationFrame(t.searchRaf), t.searchRaf = requestAnimationFrame(() => {
      t.searchRaf = null, t.searchTerm = i.value, t.selectedIndex = 0, D();
    });
  }, w = () => {
    t.searchRaf !== null && (cancelAnimationFrame(t.searchRaf), t.searchRaf = null), n.remove(), A === n && (A = null, E = null);
  }, N = () => {
    if (S(), t.selectedIndex < 0) return;
    const l = t.filteredTags[t.selectedIndex];
    xe(e, t.savedRange, l, r.formatToken) && w();
  }, $ = (l) => {
    const f = l.target.closest(".rte-merge-tag-tab");
    if (!f) return;
    const m = f.dataset.category;
    !m || !r.catalog.categoriesByKey[m] || (t.category = m, t.searchTerm = "", i.value = "", t.selectedIndex = 0, z(), D());
  }, P = (l) => {
    const f = l.target.closest(".rte-merge-tag-item");
    if (!f) return;
    const m = Number(f.dataset.index || "-1");
    Number.isNaN(m) || m < 0 || m >= t.filteredTags.length || (t.selectedIndex = m, C());
  }, H = (l) => {
    const f = l.target.closest(".rte-merge-tag-item");
    if (!f) return;
    const m = Number(f.dataset.index || "-1");
    Number.isNaN(m) || m < 0 || m >= t.filteredTags.length || (t.selectedIndex = m, N());
  }, L = (l) => {
    if (l.key === "Escape") {
      l.preventDefault(), w();
      return;
    }
    if (l.key === "ArrowDown") {
      if (l.preventDefault(), t.filteredTags.length === 0) return;
      t.selectedIndex = Math.min(t.filteredTags.length - 1, t.selectedIndex + 1), C();
      return;
    }
    if (l.key === "ArrowUp") {
      if (l.preventDefault(), t.filteredTags.length === 0) return;
      t.selectedIndex = Math.max(0, t.selectedIndex - 1), C();
      return;
    }
    l.key === "Enter" && (l.preventDefault(), N());
  }, K = (l) => {
    l.target === n && w();
  };
  y.addEventListener("click", $), u.addEventListener("click", P), u.addEventListener("dblclick", H), i.addEventListener("input", U), i.addEventListener("keydown", L), n.addEventListener("click", K), a.addEventListener("keydown", L), s == null || s.addEventListener("click", w), x.addEventListener("click", w), v.addEventListener("click", N), E = () => {
    y.removeEventListener("click", $), u.removeEventListener("click", P), u.removeEventListener("dblclick", H), i.removeEventListener("input", U), i.removeEventListener("keydown", L), n.removeEventListener("click", K), a.removeEventListener("keydown", L), s == null || s.removeEventListener("click", w), x.removeEventListener("click", w), v.removeEventListener("click", N), t.searchRaf !== null && (cancelAnimationFrame(t.searchRaf), t.searchRaf = null), n.remove();
  }, z(), D(), setTimeout(() => {
    i.focus();
  }, 0);
}
const we = (e) => ({
  name: "mergeTag",
  config: e,
  init: () => {
    _(), V(), Y();
  },
  toolbar: [
    {
      label: "Merge Tag",
      command: "insertMergeTag",
      icon: "{{ }}"
    }
  ],
  commands: {
    insertMergeTag: () => {
      _(), V(), Y();
      const r = ee();
      return r ? (he(r, ie(e)), !0) : !1;
    }
  }
});
export {
  we as MergeTagPlugin
};
//# sourceMappingURL=MergeTagPlugin.native-BzRVoD9-.mjs.map
