import { a as P, b as R } from "./anchoredPopover-Dts0IrgU.mjs";
let a = null, N = null, g = null, L = null, i = "#ffff00";
const k = '[data-theme="dark"], .dark, .editora-theme-dark', I = [
  "#000000",
  "#ffffff",
  "#808080",
  "#ff0000",
  "#00ff00",
  "#0000ff",
  "#ffff00",
  "#ff00ff",
  "#00ffff",
  "#ffa500",
  "#800080",
  "#ffc0cb"
];
function A() {
  const e = window.getSelection();
  if (e && e.rangeCount > 0) {
    const r = e.getRangeAt(0).startContainer, n = r.nodeType === Node.ELEMENT_NODE ? r : r.parentElement;
    if (n) {
      const o = n.closest(
        '[data-editora-editor="true"], .rte-editor, .editora-editor'
      );
      if (o) return o;
    }
  }
  const t = document.activeElement;
  return t ? t.closest(
    '[data-editora-editor="true"], .rte-editor, .editora-editor'
  ) : null;
}
function M(e) {
  const t = window.__editoraLastCommand, c = window.__editoraLastCommandButton;
  if (t === e && c && c.isConnected) {
    const l = window.getComputedStyle(c), s = c.getBoundingClientRect();
    if (l.display !== "none" && l.visibility !== "hidden" && l.pointerEvents !== "none" && !(s.width === 0 && s.height === 0))
      return c;
  }
  const r = (l) => {
    for (const s of l) {
      const d = window.getComputedStyle(s), p = s.getBoundingClientRect();
      if (!(d.display === "none" || d.visibility === "hidden" || d.pointerEvents === "none") && !(p.width === 0 && p.height === 0))
        return s;
    }
    return null;
  }, n = A();
  if (n) {
    const l = Array.from(
      n.querySelectorAll(`[data-command="${e}"]`)
    ), s = r(l);
    if (s) return s;
  }
  const o = Array.from(
    document.querySelectorAll(`[data-command="${e}"]`)
  );
  return r(o);
}
function T(e) {
  if (e?.closest(k)) return !0;
  const t = window.getSelection();
  if (t && t.rangeCount > 0) {
    const r = t.getRangeAt(0).startContainer;
    if ((r.nodeType === Node.ELEMENT_NODE ? r : r.parentElement)?.closest(k)) return !0;
  }
  return document.activeElement?.closest(k) ? !0 : document.body.matches(k) || document.documentElement.matches(k);
}
function U() {
  if (document.getElementById("rte-bg-color-picker-styles"))
    return;
  const e = document.createElement("style");
  e.id = "rte-bg-color-picker-styles", e.textContent = `
    .rte-bg-color-picker {
      position: fixed;
      background: white;
      border-radius: 8px;
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
      width: 220px;
      z-index: 10000;
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
      overflow: hidden;
    }

    .rte-bg-color-picker-header {
      padding: 12px 16px;
      border-bottom: 1px solid #eee;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    .rte-bg-color-picker-title {
      font-size: 14px;
      font-weight: 600;
      color: #333;
    }

    .rte-bg-color-picker-close {
      background: none;
      border: none;
      font-size: 24px;
      cursor: pointer;
      color: #999;
      padding: 0;
      width: 24px;
      height: 24px;
      display: flex;
      align-items: center;
      justify-content: center;
      line-height: 1;
    }

    .rte-bg-color-picker-close:hover {
      color: #333;
    }

    .rte-bg-color-picker-body {
      padding: 8px;
    }

    .rte-bg-color-section {
      margin-bottom: 16px;
    }

    .rte-bg-color-section:last-child {
      margin-bottom: 0;
    }

    .rte-bg-color-section-label {
      display: block;
      font-size: 12px;
      font-weight: 600;
      color: #555;
      margin-bottom: 8px;
      text-transform: uppercase;
      letter-spacing: 0.5px;
    }

    .rte-bg-color-preview {
      display: flex;
      align-items: center;
      gap: 12px;
      margin-bottom: 8px;
      padding: 6px;
      background-color: #f8f9fa;
      border-radius: 6px;
      border: 1px solid #e0e0e0;
    }

    .rte-bg-color-grid {
      display: grid;
      grid-template-columns: repeat(7, 1fr);
      gap: 6px;
      max-width: 180px;
    }

    .rte-bg-color-swatch {
      width: 100%;
      aspect-ratio: 1;
      border: 1px solid #e0e0e0;
      border-radius: 3px;
      cursor: pointer;
      transition: all 0.15s;
      padding: 0;
      background: none;
      min-height: 20px;
    }

    .rte-bg-color-swatch:hover {
      transform: scale(1.05);
      border-color: #ccc;
      box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
    }

    .rte-bg-color-swatch.selected {
      border-color: #1976d2;
      box-shadow: 0 0 0 1px rgba(25, 118, 210, 0.3);
    }

    .rte-bg-color-preview-swatch {
      width: 24px;
      height: 24px;
      border-radius: 4px;
      border: 1px solid #ddd;
      flex-shrink: 0;
    }

    .rte-bg-color-preview-hex {
      font-size: 13px;
      font-weight: 500;
      color: #666;
      font-family: monospace;
    }

    .rte-bg-color-input {
      width: 50px;
      height: 26px;
      border: 1px solid #ddd;
      border-radius: 4px;
      cursor: pointer;
      padding: 2px;
    }

    .rte-bg-color-text-input {
      flex: 1;
      height: 26px;
      width: 50px;
      border: 1px solid #ddd;
      border-radius: 4px;
      padding: 0 12px;
      font-size: 13px;
      font-family: monospace;
    }

    .rte-bg-color-text-input:focus {
      outline: none;
      border-color: #1976d2;
    }

    .rte-bg-color-custom {
      display: flex;
      gap: 8px;
    }

    .rte-bg-color-picker.rte-theme-dark {
      background: #1f2937;
      border: 1px solid #4b5563;
      box-shadow: 0 14px 30px rgba(0, 0, 0, 0.5);
    }

    .rte-bg-color-picker.rte-theme-dark .rte-bg-color-picker-header {
      border-bottom-color: #3b4657;
    }

    .rte-bg-color-picker.rte-theme-dark .rte-bg-color-picker-title {
      color: #e2e8f0;
    }

    .rte-bg-color-picker.rte-theme-dark .rte-bg-color-picker-close {
      color: #94a3b8;
    }

    .rte-bg-color-picker.rte-theme-dark .rte-bg-color-picker-close:hover {
      color: #f8fafc;
    }

    .rte-bg-color-picker.rte-theme-dark .rte-bg-color-section-label {
      color: #9fb0c6;
    }

    .rte-bg-color-picker.rte-theme-dark .rte-bg-color-preview {
      background-color: #111827;
      border-color: #4b5563;
    }

    .rte-bg-color-picker.rte-theme-dark .rte-bg-color-preview-hex {
      color: #cbd5e1;
    }

    .rte-bg-color-picker.rte-theme-dark .rte-bg-color-preview-swatch,
    .rte-bg-color-picker.rte-theme-dark .rte-bg-color-swatch {
      border-color: #4b5563;
    }

    .rte-bg-color-picker.rte-theme-dark .rte-bg-color-swatch:hover {
      border-color: #7a8ba5;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
    }

    .rte-bg-color-picker.rte-theme-dark .rte-bg-color-swatch.selected {
      border-color: #58a6ff;
      box-shadow: 0 0 0 1px rgba(88, 166, 255, 0.4);
    }

    .rte-bg-color-picker.rte-theme-dark .rte-bg-color-input,
    .rte-bg-color-picker.rte-theme-dark .rte-bg-color-text-input {
      background: #111827;
      border-color: #4b5563;
      color: #e2e8f0;
    }

    .rte-bg-color-picker.rte-theme-dark .rte-bg-color-text-input::placeholder {
      color: #94a3b8;
    }

    .rte-bg-color-picker.rte-theme-dark .rte-bg-color-text-input:focus {
      border-color: #58a6ff;
    }
  `, document.head.appendChild(e);
}
function q() {
  const e = document.createElement("div");
  e.className = "rte-bg-color-picker", T(N) && e.classList.add("rte-theme-dark"), e.addEventListener("click", (h) => h.stopPropagation());
  const t = document.createElement("div");
  t.className = "rte-bg-color-picker-header";
  const c = document.createElement("span");
  c.className = "rte-bg-color-picker-title", c.textContent = "Background Color";
  const r = document.createElement("button");
  r.type = "button", r.className = "rte-bg-color-picker-close", r.id = "rte-bg-color-close", r.setAttribute("aria-label", "Close"), r.textContent = "×", t.appendChild(c), t.appendChild(r);
  const n = document.createElement("div");
  n.className = "rte-bg-color-picker-body";
  const o = document.createElement("div");
  o.className = "rte-bg-color-section";
  const l = document.createElement("div");
  l.className = "rte-bg-color-preview";
  const s = document.createElement("div");
  s.className = "rte-bg-color-preview-swatch", s.id = "rte-bg-color-preview-swatch";
  const d = document.createElement("span");
  d.className = "rte-bg-color-preview-hex", d.id = "rte-bg-color-preview-hex", l.appendChild(s), l.appendChild(d), o.appendChild(l);
  const p = document.createElement("div");
  p.className = "rte-bg-color-section";
  const y = document.createElement("div");
  y.className = "rte-bg-color-section-label", y.textContent = "Colors";
  const x = document.createElement("div");
  x.className = "rte-bg-color-grid", x.id = "rte-bg-color-grid", I.forEach((h) => {
    const b = document.createElement("button");
    b.type = "button", b.className = "rte-bg-color-swatch", b.style.backgroundColor = h, b.dataset.color = h, b.title = h, x.appendChild(b);
  }), p.appendChild(y), p.appendChild(x);
  const C = document.createElement("div");
  C.className = "rte-bg-color-section";
  const E = document.createElement("div");
  E.className = "rte-bg-color-section-label", E.textContent = "Custom";
  const w = document.createElement("div");
  w.className = "rte-bg-color-custom";
  const m = document.createElement("input");
  m.type = "color", m.className = "rte-bg-color-input", m.id = "rte-bg-color-input", m.value = i;
  const u = document.createElement("input");
  return u.type = "text", u.className = "rte-bg-color-text-input", u.id = "rte-bg-color-text-input", u.placeholder = "#FFFF00", u.value = i.toUpperCase(), u.maxLength = 7, w.appendChild(m), w.appendChild(u), C.appendChild(E), C.appendChild(w), n.appendChild(o), n.appendChild(p), n.appendChild(C), e.appendChild(t), e.appendChild(n), e;
}
function _() {
  if (!a) return;
  a.querySelector("#rte-bg-color-close")?.addEventListener("click", () => f());
  const t = a.querySelector("#rte-bg-color-grid");
  t && t.addEventListener("click", (n) => {
    const o = n.target;
    if (o.classList.contains("rte-bg-color-swatch")) {
      const l = o.dataset.color;
      l && (i = l, v(l), f());
    }
  });
  const c = a.querySelector("#rte-bg-color-input");
  c && (c.addEventListener("change", (n) => {
    const o = n.target.value.toUpperCase();
    i = o, v(o), f();
  }), c.addEventListener("input", (n) => {
    i = n.target.value.toUpperCase(), S(), B();
  }));
  const r = a.querySelector("#rte-bg-color-text-input");
  r && (r.addEventListener("change", (n) => {
    let o = n.target.value.trim();
    o && !o.startsWith("#") && (o = "#" + o), /^#[0-9A-F]{6}$/i.test(o) && (i = o.toUpperCase(), v(i), f());
  }), r.addEventListener("input", (n) => {
    let o = n.target.value.trim();
    o && !o.startsWith("#") && (o = "#" + o, r.value = o), /^#[0-9A-F]{6}$/i.test(o) && (i = o.toUpperCase(), S(), B());
  }));
}
function S() {
  if (!a) return;
  const e = a.querySelector("#rte-bg-color-preview-swatch"), t = a.querySelector("#rte-bg-color-preview-hex"), c = a.querySelector("#rte-bg-color-input"), r = a.querySelector("#rte-bg-color-text-input");
  e && (e.style.backgroundColor = i), t && (t.textContent = i.toUpperCase()), c && (c.value = i), r && (r.value = i.toUpperCase());
}
function B() {
  if (!a) return;
  a.querySelectorAll(".rte-bg-color-swatch").forEach((t) => {
    t.dataset.color?.toUpperCase() === i.toUpperCase() ? t.classList.add("selected") : t.classList.remove("selected");
  });
}
function z() {
  try {
    const e = window.getSelection();
    if (!e || e.rangeCount === 0)
      return "#ffff00";
    const c = e.getRangeAt(0).commonAncestorContainer, r = c.nodeType === 1 ? c : c.parentElement;
    if (r) {
      const n = r.closest('[style*="background-color"]');
      if (n) {
        const o = n.style.backgroundColor;
        if (o)
          return H(o);
      }
    }
    return "#ffff00";
  } catch {
    return "#ffff00";
  }
}
function H(e) {
  if (e.startsWith("#"))
    return e.toUpperCase();
  const t = e.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/);
  if (t) {
    const c = parseInt(t[1]), r = parseInt(t[2]), n = parseInt(t[3]);
    return "#" + [c, r, n].map((o) => {
      const l = o.toString(16);
      return l.length === 1 ? "0" + l : l;
    }).join("").toUpperCase();
  }
  return "#ffff00";
}
function v(e) {
  const t = P({
    color: e,
    className: "rte-bg-color",
    styleProperty: "backgroundColor",
    commands: ["hiliteColor", "backColor"],
    savedRange: L,
    getActiveEditorRoot: A,
    warnMessage: "[BackgroundColor] Could not apply highlight for current selection"
  });
  return t && console.log("[BackgroundColor] Applied color:", e), t;
}
function f() {
  g && (g.destroy(), g = null), a && (a.remove(), a = null), N = null, L = null;
}
function D() {
  if (U(), a)
    return f(), !0;
  const e = M("openBackgroundColorPicker");
  if (!e)
    return !1;
  const t = window.getSelection();
  return !t || t.isCollapsed ? (alert("Please select text to apply background color"), !1) : (t.rangeCount > 0 && (L = t.getRangeAt(0).cloneRange()), i = z(), a = q(), document.body.appendChild(a), N = e, g && (g.destroy(), g = null), g = R({
    popover: a,
    anchor: e,
    onClose: f,
    gap: 8,
    margin: 8,
    zIndex: 1e4
  }), S(), B(), _(), !0);
}
const O = () => ({
  name: "backgroundColor",
  marks: {
    backgroundColor: {
      attrs: {
        color: { default: "#ffffff" }
      },
      parseDOM: [
        {
          tag: 'span[style*="background-color"]',
          getAttrs: (e) => {
            const r = (e.getAttribute("style") || "").match(/background-color:\s*([^;]+)/);
            return r ? { color: r[1] } : null;
          }
        },
        {
          tag: "mark",
          getAttrs: (e) => ({ color: e.style.backgroundColor || "#ffff00" })
        }
      ],
      toDOM: (e) => [
        "span",
        {
          style: `background-color: ${e.attrs?.color || "#ffffff"}`,
          class: "rte-bg-color"
        },
        0
      ]
    }
  },
  toolbar: [
    {
      label: "Background Color",
      command: "openBackgroundColorPicker",
      icon: '<svg width="24" height="24" focusable="false"><g fill-rule="evenodd"><path class="tox-icon-highlight-bg-color__color" d="M3 18h18v3H3z" fill="#000000"></path><path fill-rule="nonzero" d="M7.7 16.7H3l3.3-3.3-.7-.8L10.2 8l4 4.1-4 4.2c-.2.2-.6.2-.8 0l-.6-.7-1.1 1.1zm5-7.5L11 7.4l3-2.9a2 2 0 0 1 2.6 0L18 6c.7.7.7 2 0 2.7l-2.9 2.9-1.8-1.8-.5-.6"></path></g></svg>',
      shortcut: "Mod-Shift-h"
    }
  ],
  commands: {
    openBackgroundColorPicker: () => D(),
    setBackgroundColor: (e) => e ? v(e) : !1
  },
  keymap: {
    "Mod-Shift-h": "openBackgroundColorPicker"
  }
});
export {
  O as BackgroundColorPlugin
};
