const l = '.rte-content, .editora-content, [contenteditable="true"]', T = "[data-editora-editor], .rte-editor, .editora-editor, editora-editor", C = /* @__PURE__ */ new Set([
  "P",
  "DIV",
  "H1",
  "H2",
  "H3",
  "H4",
  "H5",
  "H6",
  "LI",
  "BLOCKQUOTE",
  "PRE",
  "TABLE",
  "FIGURE",
  "UL",
  "OL"
]), d = (e) => C.has(e.tagName) && e.getAttribute("contenteditable") !== "true", m = (e) => e ? e.nodeType === Node.ELEMENT_NODE ? e : e.parentElement : null, g = (e) => {
  const n = e?.contentElement;
  if (n instanceof HTMLElement) return n;
  const t = e?.editorElement;
  if (t instanceof HTMLElement) {
    const o = t.querySelector(l);
    if (o instanceof HTMLElement) return o;
    if (t.getAttribute("contenteditable") === "true") return t;
  }
  const r = window.__editoraCommandEditorRoot;
  if (r instanceof HTMLElement) {
    const o = r.querySelector(l);
    if (o instanceof HTMLElement) return o;
    if (r.getAttribute("contenteditable") === "true") return r;
  }
  const i = window.getSelection();
  if (i && i.rangeCount > 0) {
    const o = m(i.getRangeAt(0).startContainer), c = o?.closest(l);
    if (c instanceof HTMLElement) return c;
    const f = o?.closest(T)?.querySelector(l);
    if (f instanceof HTMLElement) return f;
  }
  const a = document.activeElement, s = a instanceof HTMLElement ? a.closest(l) : null;
  return s instanceof HTMLElement ? s : document.querySelector(l);
}, L = (e) => {
  const n = window.getSelection();
  if (!n || n.rangeCount === 0) return null;
  const t = n.getRangeAt(0);
  return !e.contains(t.startContainer) || !e.contains(t.endContainer) ? null : t;
}, R = (e, n) => {
  let t = m(n);
  for (; t && t !== e && e.contains(t); ) {
    if (d(t)) return t;
    t = t.parentElement;
  }
  return d(e) ? e : null;
}, u = (e, n) => {
  const t = R(e, n);
  if (!t || t === e) return null;
  const r = t.closest("li");
  return r instanceof HTMLElement && e.contains(r) ? r : t;
}, h = (e, n) => {
  if (typeof e.intersectsNode == "function")
    try {
      return e.intersectsNode(n);
    } catch {
      return !1;
    }
  const t = document.createRange();
  return t.selectNode(n), e.compareBoundaryPoints(Range.END_TO_START, t) < 0 && e.compareBoundaryPoints(Range.START_TO_END, t) > 0;
}, w = (e, n) => {
  const t = [], r = /* @__PURE__ */ new Set(), i = (o) => {
    !o || r.has(o) || o === n || !n.contains(o) || d(o) && (r.add(o), t.push(o));
  };
  if (e.collapsed)
    return i(u(n, e.startContainer)), t;
  i(u(n, e.startContainer)), i(u(n, e.endContainer));
  const a = document.createTreeWalker(
    n,
    NodeFilter.SHOW_ELEMENT,
    {
      acceptNode: (o) => {
        const c = o;
        return !d(c) || c.closest("li") && c.tagName !== "LI" ? NodeFilter.FILTER_SKIP : h(e, c) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
      }
    }
  );
  let s = a.nextNode();
  for (; s; )
    i(s), s = a.nextNode();
  return t.length === 0 && i(u(n, e.commonAncestorContainer)), t.filter((o) => !t.some((c) => c !== o && o.contains(c)));
}, p = (e, n, t) => {
  if (n !== t) {
    if (typeof window.execEditorCommand == "function") {
      window.execEditorCommand("recordDomTransaction", e, n, t);
      return;
    }
    if (typeof window.executeEditorCommand == "function")
      try {
        window.executeEditorCommand("recordDomTransaction", { editor: e, beforeHTML: n, afterHTML: t });
      } catch {
      }
  }
}, E = (e, n) => {
  const t = g(n);
  if (!t) return !1;
  const r = L(t);
  if (!r) return !1;
  const i = w(r, t);
  if (i.length === 0) return !1;
  const a = t.innerHTML;
  i.forEach((o) => {
    o.setAttribute("dir", e);
  });
  const s = t.innerHTML;
  return a === s ? !1 : (p(t, a, s), t.dispatchEvent(new window.Event("input", { bubbles: !0 })), !0);
}, M = () => ({
  name: "direction",
  toolbar: [
    {
      label: "Left to Right",
      command: "setDirectionLTR",
      icon: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" 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"></g><g id="SVGRepo_iconCarrier"> <path d="M21 18H3M21 18L18 21M21 18L18 15M13 3V12M13 3H7M13 3C13.4596 3 13.9148 3.0776 14.3394 3.22836C14.764 3.37913 15.1499 3.6001 15.4749 3.87868C15.7999 4.15726 16.0577 4.48797 16.2336 4.85195C16.4095 5.21593 16.5 5.60603 16.5 6C16.5 6.39397 16.4095 6.78407 16.2336 7.14805C16.0577 7.51203 15.7999 7.84274 15.4749 8.12132C15.1499 8.3999 14.764 8.62087 14.3394 8.77164C13.9148 8.9224 13.4596 9 13 9V3ZM9 3V12" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path> </g></svg>',
      shortcut: "Mod-Shift-l"
    },
    {
      label: "Right to Left",
      command: "setDirectionRTL",
      icon: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" 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"></g><g id="SVGRepo_iconCarrier"> <path d="M3 18H21M3 18L6 21M3 18L6 15M11 12V3H17M15 3V12M10.5 3C10.0404 3 9.58525 3.0776 9.16061 3.22836C8.73597 3.37913 8.35013 3.6001 8.02513 3.87868C7.70012 4.15726 7.44231 4.48797 7.26642 4.85195C7.09053 5.21593 7 5.60603 7 6C7 6.39397 7.09053 6.78407 7.26642 7.14805C7.44231 7.51203 7.70012 7.84274 8.02513 8.12132C8.35013 8.3999 8.73597 8.62087 9.16061 8.77164C9.58525 8.9224 10.0404 9 10.5 9L10.5 3Z" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path> </g></svg>',
      shortcut: "Mod-Shift-r"
    }
  ],
  commands: {
    setDirectionLTR: (e, n) => {
      try {
        return E("ltr", n);
      } catch (t) {
        return console.error("Failed to set LTR direction:", t), !1;
      }
    },
    setDirectionRTL: (e, n) => {
      try {
        return E("rtl", n);
      } catch (t) {
        return console.error("Failed to set RTL direction:", t), !1;
      }
    }
  },
  keymap: {
    "Mod-Shift-l": "setDirectionLTR",
    "Mod-Shift-r": "setDirectionRTL"
  }
});
export {
  M as DirectionPlugin
};
