const b = "p,div,li,ul,ol,table,thead,tbody,tfoot,tr,td,th,h1,h2,h3,h4,h5,h6,blockquote,pre";
function p(e) {
  return !!e.cloneContents().querySelector(b);
}
function v(e, t) {
  return (e?.nodeType === Node.ELEMENT_NODE ? e : e?.parentElement ?? null)?.closest('[contenteditable="true"]') || t()?.querySelector('[contenteditable="true"]') || document.querySelector('[contenteditable="true"]');
}
function C(e, t) {
  const n = v(e, t);
  n && n.dispatchEvent(new Event("input", { bubbles: !0 }));
}
function A(e, t) {
  let r = e.startContainer.nodeType === Node.TEXT_NODE ? e.startContainer.parentElement : e.startContainer;
  for (; r && r !== document.body; ) {
    if (r.classList.contains(t)) {
      const c = document.createRange();
      if (c.selectNodeContents(r), c.compareBoundaryPoints(Range.START_TO_START, e) <= 0 && c.compareBoundaryPoints(Range.END_TO_END, e) >= 0)
        return r;
    }
    r = r.parentElement;
  }
  return null;
}
function N(e) {
  try {
    if (e.savedRange) {
      const o = window.getSelection();
      o && (o.removeAllRanges(), o.addRange(e.savedRange.cloneRange()));
    }
    const t = window.getSelection();
    if (!t || t.rangeCount === 0 || t.isCollapsed)
      return !1;
    const n = t.getRangeAt(0);
    if (n.collapsed)
      return !1;
    const r = A(n, e.className);
    if (r)
      return e.styleProperty === "backgroundColor" ? r.style.backgroundColor = e.color : r.style.color = e.color, C(r, e.getActiveEditorRoot), !0;
    v(
      n.commonAncestorContainer,
      e.getActiveEditorRoot
    )?.focus({ preventScroll: !0 });
    try {
      document.execCommand("styleWithCSS", !1, "true");
    } catch {
    }
    let l = !1;
    if (e.commands.forEach((o) => {
      l || (l = document.execCommand(o, !1, e.color));
    }), !l && !p(n)) {
      const o = document.createElement("span");
      e.styleProperty === "backgroundColor" ? o.style.backgroundColor = e.color : o.style.color = e.color, o.className = e.className;
      const a = n.extractContents();
      o.appendChild(a), n.insertNode(o), n.setStartAfter(o), n.collapse(!0), t.removeAllRanges(), t.addRange(n), l = !0;
    }
    return l ? (C(n.commonAncestorContainer, e.getActiveEditorRoot), !0) : (e.warnMessage && console.warn(e.warnMessage), !1);
  } catch (t) {
    return e.warnMessage ? console.error(e.warnMessage, t) : console.error("[ColorApply] Failed to apply color", t), !1;
  }
}
function T({
  popover: e,
  anchor: t,
  onClose: n,
  gap: r = 6,
  margin: c = 8,
  zIndex: l = 1e4
}) {
  e.style.position = "fixed", e.style.zIndex = `${l}`, e.style.visibility = "hidden";
  const o = () => {
    if (!e.isConnected || !t.isConnected) {
      n();
      return;
    }
    const s = t.getBoundingClientRect();
    if (s.width === 0 && s.height === 0) {
      n();
      return;
    }
    const i = e.getBoundingClientRect(), w = i.width || e.offsetWidth || 220, f = i.height || e.offsetHeight || 260, y = window.innerWidth, E = window.innerHeight;
    let u = s.left, d = s.bottom + r;
    if (u + w > y - c && (u = y - w - c), u = Math.max(c, u), d + f > E - c) {
      const g = s.top - f - r;
      g >= c ? d = g : d = Math.max(c, E - f - c);
    }
    d < c && (d = c), e.style.left = `${Math.round(u)}px`, e.style.top = `${Math.round(d)}px`, e.style.visibility = "visible";
  }, a = () => {
    o();
  }, m = (s) => {
    const i = s.target;
    i && (e.contains(i) || t.contains(i) || n());
  }, h = (s) => {
    s.key === "Escape" && n();
  };
  window.addEventListener("resize", a), window.addEventListener("scroll", a, !0), document.addEventListener("keydown", h);
  const R = window.requestAnimationFrame(() => {
    document.addEventListener("mousedown", m, !0);
  });
  return o(), { reposition: o, destroy: () => {
    window.cancelAnimationFrame(R), window.removeEventListener("resize", a), window.removeEventListener("scroll", a, !0), document.removeEventListener("keydown", h), document.removeEventListener("mousedown", m, !0);
  } };
}
export {
  N as a,
  T as b
};
