const S = ".rte-content, .editora-content", P = '.rte-data-binding[data-binding="true"]', ot = "rte-data-binding-styles", p = "rte-data-binding-dialog-overlay", b = ':is([data-theme="dark"], .dark, .editora-theme-dark, .rte-theme-dark)', yt = {
  dialogTitleInsert: "Insert Data Binding",
  dialogTitleEdit: "Edit Data Binding",
  keyLabel: "Data Key",
  keyPlaceholder: "user.firstName",
  fallbackLabel: "Fallback Text",
  fallbackPlaceholder: "Guest",
  formatLabel: "Format",
  currencyLabel: "Currency",
  currencyPlaceholder: "USD",
  saveText: "Save",
  cancelText: "Cancel",
  previewOnText: "Preview On",
  previewOffText: "Preview Off",
  tokenAriaPrefix: "Data binding token"
}, v = /* @__PURE__ */ new WeakMap(), J = /* @__PURE__ */ new WeakMap(), g = /* @__PURE__ */ new WeakMap(), N = /* @__PURE__ */ new WeakMap(), I = /* @__PURE__ */ new WeakMap();
let R = null, k = null, z = 0, O = null, M = null, B = null, V = null;
function ht(t) {
  return {
    ...yt,
    ...t || {}
  };
}
function dt(t = {}) {
  var e;
  const a = (t.locale || (typeof navigator != "undefined" ? navigator.language : "en-US")).trim() || "en-US";
  return {
    data: t.data,
    getData: t.getData,
    api: t.api,
    cacheTtlMs: Math.max(0, Number((e = t.cacheTtlMs) != null ? e : 3e4)),
    labels: ht(t.labels),
    defaultFormat: t.defaultFormat || "text",
    defaultFallback: t.defaultFallback || "",
    locale: a,
    numberFormatOptions: t.numberFormatOptions || {},
    dateFormatOptions: t.dateFormatOptions || { year: "numeric", month: "short", day: "2-digit" }
  };
}
function h(t) {
  return t.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
}
function lt(t) {
  return t ? t.nodeType === Node.ELEMENT_NODE ? t : t.parentElement : null;
}
function q(t) {
  return t.closest("[data-editora-editor], .rte-editor, .editora-editor, editora-editor") || t;
}
function _(t) {
  return t ? (t.getAttribute("data-theme") || t.getAttribute("theme") || "").toLowerCase() === "dark" ? !0 : t.classList.contains("dark") || t.classList.contains("editora-theme-dark") || t.classList.contains("rte-theme-dark") : !1;
}
function kt(t) {
  const a = q(t);
  if (_(a)) return !0;
  const e = a.closest("[data-theme], [theme], .dark, .editora-theme-dark, .rte-theme-dark");
  return _(e) ? !0 : _(document.documentElement) || _(document.body);
}
function A(t, a = !0) {
  if ((t == null ? void 0 : t.contentElement) instanceof HTMLElement) return t.contentElement;
  if ((t == null ? void 0 : t.editorElement) instanceof HTMLElement) {
    const n = t.editorElement;
    if (n.matches(S)) return n;
    const i = n.querySelector(S);
    if (i instanceof HTMLElement) return i;
  }
  const e = window.getSelection();
  if (e && e.rangeCount > 0) {
    const n = e.getRangeAt(0).startContainer, i = lt(n), o = i == null ? void 0 : i.closest(S);
    if (o) return o;
  }
  const r = document.activeElement;
  if (r) {
    if (r.matches(S)) return r;
    const n = r.closest(S);
    if (n) return n;
  }
  return k && k.isConnected ? k : a ? document.querySelector(S) : null;
}
function H(t) {
  return t.getAttribute("contenteditable") === "false" || t.getAttribute("data-readonly") === "true";
}
function st(t) {
  const a = window.getSelection();
  if (!a || a.rangeCount === 0) return null;
  const e = a.getRangeAt(0);
  return t.contains(e.commonAncestorContainer) ? e.cloneRange() : null;
}
function ct(t, a) {
  const e = window.getSelection();
  e && (e.removeAllRanges(), e.addRange(a), t.focus({ preventScroll: !0 }));
}
function vt(t, a) {
  const e = (a || "").trim();
  if (e)
    return e.split(".").filter(Boolean).reduce((r, n) => {
      if (!(r == null || typeof r != "object"))
        return r[n];
    }, t);
}
function xt() {
  if (typeof document == "undefined" || document.getElementById(ot)) return;
  const t = document.createElement("style");
  t.id = ot, t.textContent = `
    .rte-data-binding {
      display: inline-flex;
      align-items: center;
      max-width: 100%;
      gap: 5px;
      padding: 2px 8px 2px 7px;
      border-radius: 999px;
      border: 1px dashed #8b5cf6;
      background: linear-gradient(180deg, #f9f7ff 0%, #f1edff 100%);
      color: #4c1d95;
      font-size: 0.88em;
      font-weight: 600;
      line-height: 1.3;
      vertical-align: baseline;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      user-select: all;
      font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
      cursor: pointer;
    }

    .rte-data-binding::before {
      content: '{}';
      display: inline-flex;
      align-items: center;
      justify-content: center;
      min-width: 14px;
      height: 14px;
      border-radius: 999px;
      background: rgba(139, 92, 246, 0.16);
      color: #5b21b6;
      font-size: 10px;
      font-weight: 700;
      line-height: 1;
      letter-spacing: -0.2px;
      flex: 0 0 auto;
    }

    .rte-data-binding.rte-data-binding-preview {
      border-style: solid;
      background: #ecfdf5;
      border-color: #34d399;
      color: #065f46;
      user-select: text;
    }

    .rte-data-binding.rte-data-binding-preview::before {
      content: '=';
      background: rgba(16, 185, 129, 0.15);
      color: #047857;
      letter-spacing: 0;
    }

    .rte-data-binding.rte-data-binding-missing {
      border-style: dashed;
      border-color: #f87171;
      background: #fef2f2;
      color: #991b1b;
    }

    .rte-data-binding-dialog-overlay {
      position: fixed;
      inset: 0;
      z-index: 2147483646;
      background: rgba(15, 23, 42, 0.54);
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 20px;
    }

    .rte-data-binding-dialog {
      width: min(560px, 96vw);
      max-height: min(86vh, 720px);
      border: 1px solid #dbe3ec;
      border-radius: 8px;
      background: #ffffff;
      box-shadow: 0 24px 50px rgba(15, 23, 42, 0.26);
      display: flex;
      flex-direction: column;
      overflow: hidden;
    }

    .rte-data-binding-header,
    .rte-data-binding-footer {
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: 10px;
      padding: 12px 14px;
      background: #f8fafc;
      border-bottom: 1px solid #e2e8f0;
    }

    .rte-data-binding-footer {
      justify-content: flex-end;
      border-bottom: 0;
      border-top: 1px solid #e2e8f0;
    }

    .rte-data-binding-title {
      margin: 0;
      font-size: 16px;
      font-weight: 700;
      color: #0f172a;
    }

    .rte-data-binding-body {
      padding: 14px;
      overflow: auto;
      display: grid;
      gap: 12px;
      grid-template-columns: 1fr;
    }

    .rte-data-binding-field {
      display: grid;
      gap: 6px;
    }

    .rte-data-binding-field label {
      font-size: 12px;
      font-weight: 600;
      color: #334155;
    }

    .rte-data-binding-field input,
    .rte-data-binding-field select {
      width: 100%;
      min-height: 36px;
      border: 1px solid #cbd5e1;
      border-radius: 6px;
      padding: 8px 10px;
      font-size: 13px;
      line-height: 1.4;
      color: #0f172a;
      background: #ffffff;
      box-sizing: border-box;
    }

    .rte-data-binding-help {
      margin: 0;
      font-size: 12px;
      color: #64748b;
    }

    .rte-data-binding-btn {
      border: 1px solid #cbd5e1;
      border-radius: 6px;
      min-height: 34px;
      padding: 6px 12px;
      background: #ffffff;
      color: #0f172a;
      font-size: 13px;
      cursor: pointer;
    }

    .rte-data-binding-btn-primary {
      background: #2563eb;
      border-color: #2563eb;
      color: #ffffff;
    }

    .rte-data-binding-btn-primary:hover {
      background: #1d4ed8;
    }

    .rte-toolbar-group-items.data-binding,
    .editora-toolbar-group-items.data-binding {
      display: flex;
      border: 1px solid #cbd5e1;
      border-radius: 6px;
      overflow: hidden;
      background: #ffffff;
    }

    .rte-toolbar-group-items.data-binding .rte-toolbar-item,
    .editora-toolbar-group-items.data-binding .editora-toolbar-item {
      display: flex;
    }

    .rte-toolbar-group-items.data-binding .rte-toolbar-button,
    .editora-toolbar-group-items.data-binding .editora-toolbar-button {
      border: 0;
      border-radius: 0;
      border-right: 1px solid #cbd5e1;
    }

    .rte-toolbar-group-items.data-binding .rte-toolbar-item:last-child .rte-toolbar-button,
    .editora-toolbar-group-items.data-binding .editora-toolbar-item:last-child .editora-toolbar-button {
      border-right: 0;
    }

    .rte-data-binding-btn:focus-visible,
    .rte-data-binding-field input:focus-visible,
    .rte-data-binding-field select:focus-visible {
      outline: none;
      border-color: #3b82f6;
      box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
    }

    ${b} .rte-data-binding,
    .${p}.rte-data-binding-theme-dark .rte-data-binding {
      background: linear-gradient(180deg, #3b0764 0%, #2e1065 100%);
      border-color: #a78bfa;
      color: #ede9fe;
    }

    ${b} .rte-data-binding::before,
    .${p}.rte-data-binding-theme-dark .rte-data-binding::before {
      background: rgba(167, 139, 250, 0.22);
      color: #ddd6fe;
    }

    ${b} .rte-data-binding.rte-data-binding-preview,
    .${p}.rte-data-binding-theme-dark .rte-data-binding.rte-data-binding-preview {
      background: #064e3b;
      border-color: #10b981;
      color: #d1fae5;
    }

    ${b} .rte-data-binding.rte-data-binding-missing,
    .${p}.rte-data-binding-theme-dark .rte-data-binding.rte-data-binding-missing {
      background: #7f1d1d;
      border-color: #ef4444;
      color: #fee2e2;
    }

    ${b} .rte-toolbar-group-items.data-binding,
    ${b} .editora-toolbar-group-items.data-binding {
      display: flex;
      border: 1px solid #566275;
      border-radius: 6px;
      overflow: hidden;
    }

    ${b} .rte-toolbar-group-items.data-binding .rte-toolbar-button,
    ${b} .editora-toolbar-group-items.data-binding .editora-toolbar-button {
      border-right-color: #566275;
    }

    ${b} .rte-data-binding-dialog,
    .${p}.rte-data-binding-theme-dark .rte-data-binding-dialog {
      background: #1f2937;
      border-color: #334155;
    }

    ${b} .rte-data-binding-header,
    ${b} .rte-data-binding-footer,
    .${p}.rte-data-binding-theme-dark .rte-data-binding-header,
    .${p}.rte-data-binding-theme-dark .rte-data-binding-footer {
      background: #111827;
      border-color: #334155;
    }

    ${b} .rte-data-binding-title,
    ${b} .rte-data-binding-field label,
    .${p}.rte-data-binding-theme-dark .rte-data-binding-title,
    .${p}.rte-data-binding-theme-dark .rte-data-binding-field label {
      color: #e2e8f0;
    }

    ${b} .rte-data-binding-help,
    .${p}.rte-data-binding-theme-dark .rte-data-binding-help {
      color: #94a3b8;
    }

    ${b} .rte-data-binding-field input,
    ${b} .rte-data-binding-field select,
    ${b} .rte-data-binding-btn,
    .${p}.rte-data-binding-theme-dark .rte-data-binding-field input,
    .${p}.rte-data-binding-theme-dark .rte-data-binding-field select,
    .${p}.rte-data-binding-theme-dark .rte-data-binding-btn {
      background: #0f172a;
      border-color: #475569;
      color: #e2e8f0;
    }

    ${b} .rte-data-binding-btn-primary,
    .${p}.rte-data-binding-theme-dark .rte-data-binding-btn-primary {
      background: #2563eb;
      border-color: #2563eb;
      color: #ffffff;
    }
  `, document.head.appendChild(t);
}
function Y(t) {
  t.dispatchEvent(new Event("input", { bubbles: !0 }));
}
function Q(t, a) {
  if (a === t.innerHTML) return;
  const e = window.execEditorCommand || window.executeEditorCommand;
  if (typeof e == "function")
    try {
      e("recordDomTransaction", t, a, t.innerHTML);
    } catch (r) {
    }
}
function j(t, a) {
  var e, r;
  return {
    key: String(t.key || "").trim(),
    fallback: String((r = (e = t.fallback) != null ? e : a.defaultFallback) != null ? r : ""),
    format: t.format || a.defaultFormat || "text",
    currency: String(t.currency || "USD").trim().toUpperCase() || "USD"
  };
}
function ut(t) {
  return `{{${t.key}}}`;
}
function ft(t, a) {
  const e = document.createElement("span");
  return e.className = "rte-data-binding", e.setAttribute("data-binding", "true"), e.setAttribute("data-binding-key", t.key), e.setAttribute("data-binding-fallback", t.fallback || ""), e.setAttribute("data-binding-format", t.format || "text"), e.setAttribute("data-binding-currency", t.currency || "USD"), e.setAttribute("contenteditable", "false"), e.setAttribute("spellcheck", "false"), e.setAttribute("draggable", "false"), e.setAttribute("tabindex", "0"), e.setAttribute("role", "button"), e.setAttribute("aria-label", `${a.tokenAriaPrefix}: ${t.key}. Press Enter to edit.`), e.textContent = ut(t), e;
}
function W(t, a) {
  return j(
    {
      key: t.getAttribute("data-binding-key") || "",
      fallback: t.getAttribute("data-binding-fallback") || a.defaultFallback,
      format: t.getAttribute("data-binding-format") || a.defaultFormat,
      currency: t.getAttribute("data-binding-currency") || "USD"
    },
    a
  );
}
function tt(t, a, e) {
  t.classList.add("rte-data-binding"), t.setAttribute("data-binding", "true"), t.setAttribute("data-binding-key", a.key), t.setAttribute("data-binding-fallback", a.fallback || ""), t.setAttribute("data-binding-format", a.format || "text"), t.setAttribute("data-binding-currency", a.currency || "USD"), t.setAttribute("contenteditable", "false"), t.setAttribute("spellcheck", "false"), t.setAttribute("draggable", "false"), t.setAttribute("tabindex", "0"), t.setAttribute("role", "button"), t.setAttribute("aria-label", `${e.tokenAriaPrefix}: ${a.key}. Press Enter to edit.`);
}
function G(t, a) {
  const e = Array.from(t.querySelectorAll(P));
  return e.forEach((r) => {
    const n = W(r, a);
    tt(r, n, a.labels);
  }), e;
}
function X(t) {
  const a = window.getSelection();
  if (a && a.rangeCount > 0) {
    const n = a.getRangeAt(0).startContainer, i = lt(n), o = i == null ? void 0 : i.closest(P);
    if (o && t.contains(o)) return o;
  }
  const e = document.activeElement, r = e == null ? void 0 : e.closest(P);
  return r && t.contains(r) ? r : null;
}
function bt() {
  R && (R.cleanup(), R = null);
}
function et(t, a, e) {
  const r = q(t);
  Array.from(r.querySelectorAll('[data-command="toggleDataBindingPreview"]')).forEach((i) => {
    if (i.setAttribute("data-active", a ? "true" : "false"), i.classList.toggle("active", a), i.setAttribute("aria-pressed", a ? "true" : "false"), e) {
      const o = a ? e.labels.previewOnText : e.labels.previewOffText;
      i.setAttribute("title", o), i.setAttribute("aria-label", o);
    }
  });
}
function wt(t, a, e) {
  const r = a.format || "text";
  if (t == null) return a.fallback || "";
  if (r === "json") {
    if (typeof t == "string") return t;
    try {
      return JSON.stringify(t);
    } catch (n) {
      return String(t);
    }
  }
  if (r === "date") {
    const n = t instanceof Date ? t : new Date(String(t));
    if (Number.isNaN(n.getTime())) return String(t);
    try {
      return new Intl.DateTimeFormat(e.locale, e.dateFormatOptions).format(n);
    } catch (i) {
      return n.toISOString();
    }
  }
  if (r === "number" || r === "currency") {
    const n = typeof t == "number" ? t : Number(t);
    if (!Number.isFinite(n)) return String(t);
    const i = { ...e.numberFormatOptions };
    if (r === "currency") {
      const o = (a.currency || "USD").toUpperCase();
      Object.assign(i, { style: "currency", currency: o });
    }
    try {
      return new Intl.NumberFormat(e.locale, i).format(n);
    } catch (o) {
      return String(n);
    }
  }
  return String(t);
}
function C(t, a, e, r) {
  const n = W(t, a), i = n.key;
  if (t.classList.remove("rte-data-binding-preview", "rte-data-binding-missing"), !e) {
    t.textContent = ut(n), t.setAttribute("aria-label", `${a.labels.tokenAriaPrefix}: ${i}. Press Enter to edit.`);
    return;
  }
  const o = vt(r, i), l = o == null, d = l ? n.fallback || "" : wt(o, n, a);
  t.textContent = d || n.fallback || "", t.classList.add("rte-data-binding-preview"), l && !(n.fallback || "").trim() && t.classList.add("rte-data-binding-missing"), t.setAttribute("aria-label", `${a.labels.tokenAriaPrefix}: ${i} = ${t.textContent || ""}. Press Enter to edit.`);
}
function w(t) {
  return typeof t == "object" && t !== null && !Array.isArray(t);
}
function Et(t, a) {
  return a ? a.split(".").filter(Boolean).reduce((e, r) => {
    if (!(!w(e) && !Array.isArray(e)))
      return e[r];
  }, t) : t;
}
function Dt(t, a, e) {
  const r = a.api, n = q(t), i = { editor: t, editorRoot: n, signal: e };
  if (r.buildRequest) {
    const m = r.buildRequest(i);
    return { url: m.url, init: { ...m.init || {}, signal: e } };
  }
  const o = (r.method || "GET").toUpperCase(), l = typeof r.headers == "function" ? r.headers(i) : r.headers || {}, d = typeof r.params == "function" ? r.params(i) : r.params, u = new URL(r.url, window.location.origin);
  d && Object.entries(d).forEach(([m, f]) => {
    f != null && u.searchParams.set(m, String(f));
  });
  const s = {
    method: o,
    headers: { ...l },
    credentials: r.credentials,
    mode: r.mode,
    cache: r.cache,
    signal: e
  };
  if (o !== "GET" && o !== "HEAD") {
    const m = typeof r.body == "function" ? r.body(i) : r.body;
    if (m != null)
      if (w(m)) {
        s.body = JSON.stringify(m);
        const f = s.headers;
        !f["Content-Type"] && !f["content-type"] && (f["Content-Type"] = "application/json");
      } else
        s.body = m;
  }
  return { url: u.toString(), init: s };
}
async function At(t, a) {
  var d, u;
  const e = a.api;
  if (!e) return {};
  const r = new AbortController(), n = q(t), i = { editor: t, editorRoot: n, signal: r.signal }, o = Math.max(0, Number((d = e.timeoutMs) != null ? d : 1e4));
  let l = null;
  o > 0 && (l = window.setTimeout(() => r.abort(), o));
  try {
    const { url: s, init: m } = Dt(t, a, r.signal), f = await fetch(s, { ...m, signal: r.signal });
    if (!f.ok)
      throw new Error(`Data binding API request failed: ${f.status}`);
    const $ = (e.responseType || "json") === "text" ? await f.text() : await f.json();
    if (e.transformResponse) {
      const F = e.transformResponse($, i);
      return w(F) ? F : {};
    }
    const x = Et($, e.responsePath);
    return w(x) ? x : w($) ? $ : { value: x };
  } catch (s) {
    return (s == null ? void 0 : s.name) !== "AbortError" && ((u = e.onError) == null || u.call(e, s, i)), {};
  } finally {
    l !== null && window.clearTimeout(l);
  }
}
async function U(t, a) {
  const e = J.get(t);
  if (e) return e;
  const r = N.get(t), n = Date.now();
  if (r && n - r.timestamp <= a.cacheTtlMs)
    return r.data;
  const i = I.get(t);
  if (i) return i;
  const o = (async () => {
    try {
      if (typeof a.getData == "function") {
        const d = await Promise.resolve(a.getData({ editor: t, editorRoot: q(t) }));
        if (w(d)) return d;
      }
      if (a.api) {
        const d = await At(t, a);
        if (w(d)) return d;
      }
      if (typeof a.data == "function") {
        const d = await Promise.resolve(a.data());
        if (w(d)) return d;
      }
      return w(a.data) ? a.data : {};
    } finally {
      I.delete(t);
    }
  })();
  I.set(t, o);
  const l = await o;
  return N.set(t, { timestamp: n, data: l }), l;
}
async function Z(t, a, e) {
  const r = G(t, a);
  if (v.set(t, e), et(t, e, a), !e) {
    r.forEach((i) => C(i, a, !1));
    return;
  }
  const n = await U(t, a);
  r.forEach((i) => C(i, a, !0, n));
}
function gt(t, a) {
  let e = st(t);
  e || (e = document.createRange(), e.selectNodeContents(t), e.collapse(!1)), e.collapsed || e.deleteContents(), e.insertNode(a);
  const r = document.createTextNode(" ");
  a.after(r);
  const n = document.createRange();
  n.setStart(r, 1), n.collapse(!0), ct(t, n), t.normalize();
}
function mt(t) {
  return typeof t.key == "string" ? t.key.toLowerCase() : "";
}
function St(t) {
  const a = t.metaKey || t.ctrlKey, e = mt(t), r = a && t.altKey && t.shiftKey && e === "d", n = !t.metaKey && !t.ctrlKey && !t.altKey && !t.shiftKey && e === "f7";
  return r || n;
}
function Tt(t) {
  const a = t.metaKey || t.ctrlKey, e = mt(t), r = a && t.altKey && t.shiftKey && e === "b", n = !t.metaKey && !t.ctrlKey && !t.altKey && !t.shiftKey && e === "f8";
  return r || n;
}
function L(t, a, e, r, n) {
  bt();
  const i = e === "insert" ? st(t) : null, o = a.labels, l = n ? W(n, a) : j(r || {}, a), d = document.createElement("div");
  d.className = p, kt(t) && d.classList.add("rte-data-binding-theme-dark");
  const u = document.createElement("section");
  u.className = "rte-data-binding-dialog", u.setAttribute("role", "dialog"), u.setAttribute("aria-modal", "true"), u.setAttribute("aria-labelledby", "rte-data-binding-dialog-title"), u.innerHTML = `
    <header class="rte-data-binding-header">
      <h2 id="rte-data-binding-dialog-title" class="rte-data-binding-title">${h(
    e === "edit" ? o.dialogTitleEdit : o.dialogTitleInsert
  )}</h2>
      <button type="button" class="rte-data-binding-btn" data-action="cancel" aria-label="${h(o.cancelText)}">✕</button>
    </header>
    <div class="rte-data-binding-body">
      <div class="rte-data-binding-field">
        <label for="rte-data-binding-key">${h(o.keyLabel)}</label>
        <input id="rte-data-binding-key" class="rte-data-binding-key" type="text" value="${h(l.key)}" placeholder="${h(
    o.keyPlaceholder
  )}" />
      </div>
      <div class="rte-data-binding-field">
        <label for="rte-data-binding-fallback">${h(o.fallbackLabel)}</label>
        <input id="rte-data-binding-fallback" class="rte-data-binding-fallback" type="text" value="${h(
    l.fallback || ""
  )}" placeholder="${h(o.fallbackPlaceholder)}" />
      </div>
      <div class="rte-data-binding-field">
        <label for="rte-data-binding-format">${h(o.formatLabel)}</label>
        <select id="rte-data-binding-format" class="rte-data-binding-format">
          <option value="text" ${l.format === "text" ? "selected" : ""}>Text</option>
          <option value="number" ${l.format === "number" ? "selected" : ""}>Number</option>
          <option value="currency" ${l.format === "currency" ? "selected" : ""}>Currency</option>
          <option value="date" ${l.format === "date" ? "selected" : ""}>Date</option>
          <option value="json" ${l.format === "json" ? "selected" : ""}>JSON</option>
        </select>
      </div>
      <div class="rte-data-binding-field">
        <label for="rte-data-binding-currency">${h(o.currencyLabel)}</label>
        <input id="rte-data-binding-currency" class="rte-data-binding-currency" type="text" maxlength="3" value="${h(
    l.currency || "USD"
  )}" placeholder="${h(o.currencyPlaceholder)}" />
      </div>
      <p class="rte-data-binding-help">Use dot paths like <code>user.name</code> or <code>order.total</code>.</p>
    </div>
    <footer class="rte-data-binding-footer">
      <button type="button" class="rte-data-binding-btn" data-action="cancel">${h(o.cancelText)}</button>
      <button type="button" class="rte-data-binding-btn rte-data-binding-btn-primary" data-action="save">${h(o.saveText)}</button>
    </footer>
  `, d.appendChild(u), document.body.appendChild(d);
  const s = u.querySelector(".rte-data-binding-key"), m = u.querySelector(".rte-data-binding-fallback"), f = u.querySelector(".rte-data-binding-format"), T = u.querySelector(".rte-data-binding-currency"), $ = () => {
    const c = (f == null ? void 0 : f.value) === "currency", y = T == null ? void 0 : T.closest(".rte-data-binding-field");
    y && (y.style.display = c ? "grid" : "none");
  };
  $(), f == null || f.addEventListener("change", $);
  const x = () => {
    d.removeEventListener("click", nt), d.removeEventListener("keydown", rt, !0), document.removeEventListener("keydown", F, !0), d.parentNode && d.parentNode.removeChild(d), R = null, t.focus({ preventScroll: !0 });
  }, F = (c) => {
    c.key === "Escape" && (c.preventDefault(), c.stopPropagation(), x());
  }, nt = (c) => {
    c.target === d && x();
  }, pt = (c) => {
    if (c.key !== "Tab") return;
    const y = Array.from(
      u.querySelectorAll('button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])')
    );
    if (y.length === 0) return;
    const E = y[0], D = y[y.length - 1], K = document.activeElement;
    if (c.shiftKey && K === E) {
      c.preventDefault(), D.focus();
      return;
    }
    !c.shiftKey && K === D && (c.preventDefault(), E.focus());
  }, at = async () => {
    const c = ((s == null ? void 0 : s.value) || "").trim();
    if (!c) {
      s == null || s.focus();
      return;
    }
    const y = j(
      {
        key: c,
        fallback: ((m == null ? void 0 : m.value) || "").trim(),
        format: (f == null ? void 0 : f.value) || a.defaultFormat,
        currency: ((T == null ? void 0 : T.value) || "USD").trim().toUpperCase()
      },
      a
    ), E = t.innerHTML;
    if (n) {
      tt(n, y, o);
      const D = v.get(t) === !0, K = D ? await U(t, a) : void 0;
      C(n, a, D, K);
    } else {
      if (i)
        try {
          ct(t, i);
        } catch (it) {
        }
      const D = ft(y, o);
      if (gt(t, D), v.get(t) === !0) {
        const it = await U(t, a);
        C(D, a, !0, it);
      }
    }
    Y(t), Q(t, E), x();
  }, rt = (c) => {
    if (c.key === "Escape") {
      c.preventDefault(), x();
      return;
    }
    pt(c), c.key === "Enter" && !c.shiftKey && c.target instanceof HTMLInputElement && (c.preventDefault(), at());
  };
  u.addEventListener("click", (c) => {
    const y = c.target, E = y == null ? void 0 : y.getAttribute("data-action");
    if (E) {
      if (E === "cancel") {
        x();
        return;
      }
      E === "save" && at();
    }
  }), d.addEventListener("click", nt), d.addEventListener("keydown", rt, !0), document.addEventListener("keydown", F, !0), R = { cleanup: x }, s == null || s.focus();
}
function $t(t) {
  V = t, M || (M = (a) => {
    const e = a.target, r = e == null ? void 0 : e.closest(S);
    if (!r) return;
    k = r, g.has(r) || g.set(r, t);
    const n = g.get(r) || t;
    G(r, n);
    const i = v.get(r) === !0;
    et(r, i, n), i || Array.from(r.querySelectorAll(P)).forEach((l) => C(l, n, !1));
  }, document.addEventListener("focusin", M, !0)), O || (O = (a) => {
    if (document.querySelector(`.${p}`)) return;
    const e = a.target;
    if (e != null && e.closest("input, textarea, select")) return;
    const r = A(void 0, !1);
    if (!r || H(r)) return;
    const n = g.get(r) || V || t, i = e == null ? void 0 : e.closest(P);
    if (i && (a.key === "Enter" || a.key === " ")) {
      a.preventDefault(), a.stopPropagation(), k = r, L(r, n, "edit", void 0, i);
      return;
    }
    if (St(a)) {
      a.preventDefault(), a.stopPropagation();
      const o = X(r);
      o ? L(r, n, "edit", void 0, o) : L(r, n, "insert");
      return;
    }
    if (Tt(a)) {
      a.preventDefault(), a.stopPropagation();
      const o = v.get(r) !== !0;
      Z(r, n, o);
    }
  }, document.addEventListener("keydown", O, !0)), B || (B = (a) => {
    if (document.querySelector(`.${p}`) || a.defaultPrevented || a.button !== 0 || a.metaKey || a.ctrlKey || a.altKey || a.shiftKey) return;
    const e = a.target, r = e == null ? void 0 : e.closest(P);
    if (!r) return;
    const n = r.closest(S);
    if (!n || H(n)) return;
    const i = g.get(n) || V || t;
    g.set(n, i), k = n, a.preventDefault(), a.stopPropagation(), r.focus({ preventScroll: !0 }), L(n, i, "edit", void 0, r);
  }, document.addEventListener("click", B, !0));
}
function Lt() {
  M && (document.removeEventListener("focusin", M, !0), M = null), O && (document.removeEventListener("keydown", O, !0), O = null), B && (document.removeEventListener("click", B, !0), B = null), V = null, k = null;
}
const Ct = (t = {}) => {
  const a = dt(t);
  return xt(), {
    name: "dataBinding",
    toolbar: [
      {
        id: "dataBindingTools",
        label: "Data Binding",
        type: "group",
        command: "openDataBindingDialog",
        items: [
          {
            id: "dataBinding",
            label: "Data Binding",
            command: "openDataBindingDialog",
            icon: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" focusable="false" aria-hidden="true"><path d="M7 4a3 3 0 0 0-3 3v3h2V7a1 1 0 0 1 1-1h3V4H7Zm10 0h-3v2h3a1 1 0 0 1 1 1v3h2V7a3 3 0 0 0-3-3ZM4 14v3a3 3 0 0 0 3 3h3v-2H7a1 1 0 0 1-1-1v-3H4Zm14 0v3a1 1 0 0 1-1 1h-3v2h3a3 3 0 0 0 3-3v-3h-2ZM8.5 12a1.5 1.5 0 1 1 0-3h7a1.5 1.5 0 0 1 0 3h-7Zm0 4a1.5 1.5 0 1 1 0-3h4a1.5 1.5 0 0 1 0 3h-4Z" fill="currentColor"></path></svg>'
          },
          {
            id: "dataBindingPreview",
            label: "Data Preview",
            command: "toggleDataBindingPreview",
            icon: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" focusable="false" aria-hidden="true"><path d="M12 3c-4.4 0-8 1.3-8 3v12c0 1.7 3.6 3 8 3s8-1.3 8-3V6c0-1.7-3.6-3-8-3Zm0 2c3.9 0 6 .9 6 1s-2.1 1-6 1-6-.9-6-1 2.1-1 6-1Zm0 4c3 0 5.6-.5 7-1.3V11c0 .9-2.7 2-7 2s-7-1.1-7-2V7.7C6.4 8.5 9 9 12 9Zm0 6c-4.3 0-7-1.1-7-2v3c0 .9 2.7 2 7 2s7-1.1 7-2v-3c0 .9-2.7 2-7 2Z" fill="currentColor"></path><path d="M16.5 9.4a1 1 0 0 1 1.4 0l1.1 1.1 1.8-1.8a1 1 0 1 1 1.4 1.4l-2.5 2.5a1 1 0 0 1-1.4 0l-1.8-1.8a1 1 0 0 1 0-1.4Z" fill="currentColor"></path></svg>'
          }
        ]
      }
    ],
    commands: {
      openDataBindingDialog: (e, r) => {
        const n = A(r);
        if (!n || H(n)) return !1;
        k = n;
        const i = g.get(n) || a;
        g.set(n, i), G(n, i);
        const o = e == null ? void 0 : e.target;
        if (o === "insert")
          return L(n, i, "insert", e), !0;
        const l = X(n);
        return o === "edit" || l ? !l && o === "edit" ? !1 : (L(n, i, "edit", e, l || void 0), !0) : (L(n, i, "insert", e), !0);
      },
      insertDataBindingToken: async (e, r) => {
        const n = A(r);
        if (!n || H(n)) return !1;
        k = n;
        const i = g.get(n) || a;
        g.set(n, i);
        const o = j(e || {}, i);
        if (!o.key) return !1;
        const l = n.innerHTML, d = ft(o, i.labels);
        if (gt(n, d), v.get(n) === !0) {
          const s = await U(n, i);
          C(d, i, !0, s);
        }
        return Y(n), Q(n, l), !0;
      },
      editDataBindingToken: async (e, r) => {
        const n = A(r);
        if (!n || H(n)) return !1;
        k = n;
        const i = g.get(n) || a;
        g.set(n, i);
        const o = X(n);
        if (!o) return !1;
        const l = W(o, i), d = j({ ...l, ...e || {} }, i);
        if (!d.key) return !1;
        const u = n.innerHTML;
        tt(o, d, i.labels);
        const s = v.get(n) === !0, m = s ? await U(n, i) : void 0;
        return C(o, i, s, m), Y(n), Q(n, u), !0;
      },
      toggleDataBindingPreview: async (e, r) => {
        const n = A(r);
        if (!n) return !1;
        k = n;
        const i = g.get(n) || a;
        g.set(n, i);
        const o = typeof e == "boolean" ? e : v.get(n) !== !0;
        return await Z(n, i, o), !0;
      },
      setDataBindingData: async (e, r) => {
        const n = A(r);
        if (!n) return !1;
        if (k = n, e && typeof e == "object" ? (J.set(n, e), N.set(n, { timestamp: Date.now(), data: e })) : (J.delete(n), N.delete(n)), v.get(n) === !0) {
          const i = g.get(n) || a;
          await Z(n, i, !0);
        }
        return !0;
      },
      refreshDataBindings: async (e, r) => {
        const n = A(r);
        if (!n) return !1;
        k = n, N.delete(n);
        const i = g.get(n) || a;
        g.set(n, i);
        const o = v.get(n) === !0;
        return await Z(n, i, o), !0;
      }
    },
    keymap: {
      "Mod-Alt-Shift-d": "openDataBindingDialog",
      "Mod-Alt-Shift-D": "openDataBindingDialog",
      "Mod-Alt-Shift-b": "toggleDataBindingPreview",
      "Mod-Alt-Shift-B": "toggleDataBindingPreview",
      F7: "openDataBindingDialog",
      F8: "toggleDataBindingPreview"
    },
    init: function(r) {
      z += 1;
      const n = this && typeof this.__pluginConfig == "object" ? dt({ ...a, ...this.__pluginConfig }) : a;
      $t(n);
      const i = A(
        r && r.editorElement ? { editorElement: r.editorElement } : void 0,
        !1
      );
      if (i) {
        k = i, g.set(i, n), G(i, n);
        const o = v.get(i) === !0;
        et(i, o, n);
      }
    },
    destroy: () => {
      z = Math.max(0, z - 1), z === 0 && (bt(), Lt());
    }
  };
};
export {
  Ct as DataBindingPlugin
};
//# sourceMappingURL=DataBindingPlugin.native-BoXwghTz.mjs.map
