const f = '.rte-content, .editora-content, [contenteditable="true"]', w = "[data-editora-editor], .rte-editor, .editora-editor, editora-editor", h = (t) => t ? t.nodeType === Node.ELEMENT_NODE ? t : t.parentElement : null, v = (t) => {
  const e = t?.contentElement;
  if (e instanceof HTMLElement) return e;
  const n = t?.editorElement;
  if (n instanceof HTMLElement) {
    const s = n.querySelector(f);
    if (s instanceof HTMLElement) return s;
    if (n.getAttribute("contenteditable") === "true") return n;
  }
  const r = window.__editoraCommandEditorRoot;
  if (r instanceof HTMLElement) {
    const s = r.querySelector(f);
    if (s instanceof HTMLElement) return s;
    if (r.getAttribute("contenteditable") === "true") return r;
  }
  const c = window.getSelection();
  if (c && c.rangeCount > 0) {
    const s = h(c.getRangeAt(0).startContainer), i = s?.closest(f);
    if (i instanceof HTMLElement) return i;
    const a = s?.closest(w)?.querySelector(f);
    if (a instanceof HTMLElement) return a;
  }
  const l = document.activeElement, d = l instanceof HTMLElement ? l.closest(f) : null;
  return d instanceof HTMLElement ? d : document.querySelector(f);
}, R = (t) => {
  const e = window.getSelection();
  if (!e || e.rangeCount === 0 || e.isCollapsed) return null;
  const n = e.getRangeAt(0);
  return !t.contains(n.startContainer) || !t.contains(n.endContainer) ? null : n;
}, S = (t, e) => {
  if (!e.data) return !1;
  if (typeof t.intersectsNode == "function")
    try {
      return t.intersectsNode(e);
    } catch {
      return !1;
    }
  const n = document.createRange();
  return n.selectNodeContents(e), t.compareBoundaryPoints(Range.END_TO_START, n) < 0 && t.compareBoundaryPoints(Range.START_TO_END, n) > 0;
}, L = (t, e) => {
  const n = [], r = document.createTreeWalker(t, NodeFilter.SHOW_TEXT);
  let c = r.nextNode();
  for (; c; ) {
    const l = c;
    if (S(e, l)) {
      const d = l === e.startContainer ? e.startOffset : 0, s = l === e.endContainer ? e.endOffset : l.data.length;
      d < s && n.push({
        node: l,
        start: d,
        end: s,
        text: l.data.slice(d, s)
      });
    }
    c = r.nextNode();
  }
  return n;
}, x = (t) => new RegExp("\\p{L}", "u").test(t), y = (t) => /[\p{M}'’-]/u.test(t), _ = () => {
  let t = !1;
  return (e) => {
    let n = "";
    return Array.from(e.text).forEach((r) => {
      if (x(r)) {
        n += t ? r.toLocaleLowerCase() : r.toLocaleUpperCase(), t = !0;
        return;
      }
      n += r, y(r) || (t = !1);
    }), n;
  };
}, M = (t) => {
  if (t.length === 0) return;
  const e = t[0], n = t[t.length - 1], r = window.getSelection();
  if (!r) return;
  const c = document.createRange();
  c.setStart(e.node, e.start), c.setEnd(n.node, n.end), r.removeAllRanges(), r.addRange(c);
}, H = (t, e, n) => {
  if (e !== n) {
    if (typeof window.execEditorCommand == "function") {
      window.execEditorCommand("recordDomTransaction", t, e, n);
      return;
    }
    if (typeof window.executeEditorCommand == "function")
      try {
        window.executeEditorCommand("recordDomTransaction", { editor: t, beforeHTML: e, afterHTML: n });
      } catch {
      }
  }
}, N = () => {
  const t = (i, o) => {
    try {
      const a = v(o);
      if (!a) return !1;
      const m = R(a);
      if (!m) return !1;
      const p = L(a, m);
      if (p.length === 0) return !1;
      const C = a.innerHTML;
      p.forEach((u, T) => {
        const E = i(u, T);
        u.node.replaceData(u.start, u.end - u.start, E), u.end = u.start + E.length;
      });
      const g = a.innerHTML;
      return C === g ? !1 : (H(a, C, g), a.dispatchEvent(new window.Event("input", { bubbles: !0 })), M(p), !0);
    } catch (a) {
      return console.error("Failed to transform capitalization:", a), !1;
    }
  }, e = (i, o) => t((a) => a.text.toLocaleUpperCase(), o), n = (i, o) => t((a) => a.text.toLocaleLowerCase(), o), r = (i, o) => t(_(), o), c = (i, o) => {
    if (!i) return !1;
    switch (i) {
      case "uppercase":
        return e(void 0, o);
      case "lowercase":
        return n(void 0, o);
      case "titlecase":
        return r(void 0, o);
      default:
        return !1;
    }
  }, l = (i, o) => typeof i == "string" ? { value: i, context: o } : typeof o == "string" ? { value: o } : {
    value: void 0,
    context: o
  }, d = (i, o) => {
    if (o && typeof o == "object" && !o.doc)
      return o;
  }, s = (i) => (o, a) => i(void 0, d(o, a));
  return {
    name: "capitalization",
    toolbar: [
      {
        label: "Capitalization",
        command: "setCapitalization",
        type: "inline-menu",
        options: [
          { label: "lowercase", value: "lowercase" },
          { label: "UPPERCASE", value: "uppercase" },
          { label: "Title Case", value: "titlecase" }
        ],
        icon: '<svg fill="#000000" width="24" height="24" viewBox="0 0 32.00 32.00" id="icon" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round" stroke="#CCCCCC" stroke-width="0.192"></g><g id="SVGRepo_iconCarrier"><defs><style>.cls-1{fill:none;}</style></defs><title>letter--Aa</title><path d="M23,13H18v2h5v2H19a2,2,0,0,0-2,2v2a2,2,0,0,0,2,2h6V15A2,2,0,0,0,23,13Zm0,8H19V19h4Z"></path><path d="M13,9H9a2,2,0,0,0-2,2V23H9V18h4v5h2V11A2,2,0,0,0,13,9ZM9,16V11h4v5Z"></path><rect id="_Transparent_Rectangle_" data-name="&lt;Transparent Rectangle&gt;" class="cls-1" width="32" height="32"></rect></g></svg>'
      }
    ],
    commands: {
      setCapitalization: (i, o) => {
        const { value: a, context: m } = l(i, o);
        return c(a, m);
      },
      toUpperCase: s(e),
      toLowerCase: s(n),
      toTitleCase: s(r)
    },
    keymap: {
      "Mod-Shift-u": "toUpperCase",
      "Mod-Shift-k": "toLowerCase",
      "Mod-Shift-t": "toTitleCase"
    }
  };
};
export {
  N as CapitalizationPlugin
};
