const u = "data-editora-selection-marker";
function w(t) {
  return t ? t.nodeType === Node.ELEMENT_NODE ? t : t.parentElement : null;
}
function m(t) {
  return t ? t.getAttribute("contenteditable") === "true" ? t : t.querySelector('[contenteditable="true"]') : null;
}
function y(t) {
  if (t?.contentElement instanceof HTMLElement) return t.contentElement;
  const e = m(t?.editorElement ?? null);
  if (e) return e;
  const n = typeof window < "u" ? window.__editoraCommandEditorRoot : null, o = n instanceof HTMLElement ? m(n) : null;
  if (o) return o;
  const a = window.getSelection();
  if (a && a.rangeCount > 0) {
    const l = w(a.getRangeAt(0).startContainer)?.closest('.rte-content, .editora-content, [contenteditable="true"]');
    if (l instanceof HTMLElement) return l;
  }
  const r = document.activeElement;
  if (r instanceof HTMLElement) {
    const c = r.closest('.rte-content, .editora-content, [contenteditable="true"]');
    if (c instanceof HTMLElement) return c;
  }
  return document.querySelector('.rte-content, .editora-content, [contenteditable="true"]');
}
function R(t) {
  const e = window.getSelection();
  if (!e || e.rangeCount === 0) return !1;
  const n = e.getRangeAt(0);
  return t.contains(n.startContainer) && t.contains(n.endContainer);
}
function E(t) {
  const e = document.createElement("span");
  return e.setAttribute(u, t), e.style.display = "inline-block", e.style.width = "0", e.style.height = "0", e.style.overflow = "hidden", e.style.lineHeight = "0", e.appendChild(document.createTextNode("\uFEFF")), e;
}
function S(t) {
  const e = window.getSelection();
  if (!e || e.rangeCount === 0) return null;
  const n = e.getRangeAt(0);
  if (!t.contains(n.startContainer) || !t.contains(n.endContainer))
    return null;
  const o = n.collapsed, a = E("start"), r = o ? null : E("end"), c = n.cloneRange();
  if (r) {
    const i = c.cloneRange();
    i.collapse(!1), i.insertNode(r);
  }
  const l = c.cloneRange();
  return l.collapse(!0), l.insertNode(a), () => {
    const i = window.getSelection(), s = document.createRange(), d = t.querySelector(
      `[${u}="start"]`
    ), f = t.querySelector(
      `[${u}="end"]`
    );
    if (!i || !d) {
      t.querySelectorAll(`[${u}]`).forEach((g) => g.remove());
      return;
    }
    o || !f ? (s.setStartBefore(d), s.collapse(!0)) : (s.setStartAfter(d), s.setEndBefore(f)), i.removeAllRanges(), i.addRange(s), d.remove(), f?.remove();
  };
}
function b(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 {
      }
  }
}
function p(t) {
  const e = y(t.context);
  if (!e) return !1;
  const n = e.innerHTML;
  R(e) && e.focus({ preventScroll: !0 });
  let o = !1;
  try {
    o = document.execCommand(t.command, !1);
  } catch {
    o = !1;
  }
  const a = e.innerHTML;
  if (t.normalize && a !== n) {
    const c = S(e);
    t.normalize(e), c?.();
  }
  const r = e.innerHTML;
  return n !== r && (b(e, n, r), e.dispatchEvent(new Event("input", { bubbles: !0 }))), o !== !1 || n !== r;
}
export {
  p as a
};
