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