const f = '.rte-content, .editora-content, [contenteditable="true"]', y = "[data-editora-editor], .rte-editor, .editora-editor, editora-editor", g = /* @__PURE__ */ new Set(["P", "DIV", "H1", "H2", "H3", "H4", "H5", "H6", "PRE"]), E = "p,div,h1,h2,h3,h4,h5,h6,ul,ol,blockquote,pre,table", B = /* @__PURE__ */ new Set(["p", "h1", "h2", "h3", "h4", "h5", "h6"]);
function H(e) {
  return e ? e.nodeType === Node.ELEMENT_NODE ? e : e.parentElement : null;
}
function S(e) {
  const n = e?.contentElement;
  if (n instanceof HTMLElement) return n;
  const t = e?.editorElement;
  if (t instanceof HTMLElement) {
    const a = t.querySelector(f);
    if (a instanceof HTMLElement) return a;
    if (t.getAttribute("contenteditable") === "true") return t;
  }
  const r = window.getSelection();
  if (r && r.rangeCount > 0) {
    const a = H(r.getRangeAt(0).startContainer), c = a?.closest(f);
    if (c instanceof HTMLElement) return c;
    const u = a?.closest(y)?.querySelector(f);
    if (u instanceof HTMLElement) return u;
  }
  const o = document.activeElement?.closest(f);
  return o instanceof HTMLElement ? o : document.querySelector(f);
}
function N(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;
}
function T(e, n) {
  let t = H(n);
  if (!t || t === e || !e.contains(t)) return null;
  for (; t.parentElement && t.parentElement !== e; )
    t = t.parentElement;
  if (t.parentElement !== e) return null;
  if (t.matches(E)) return t;
  const r = t.closest(E);
  return r instanceof HTMLElement && r.parentElement === e ? r : t;
}
function v(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;
}
function w(e, n) {
  const t = T(e, n.startContainer), r = T(e, n.endContainer);
  if (!t) return [];
  if (!r || t === r) return [t];
  const o = Array.from(e.children).filter(
    (a) => a instanceof HTMLElement
  ).filter((a) => v(n, a));
  return o.length > 0 ? o : [t];
}
function A(e, n) {
  Array.from(e.attributes).forEach((t) => {
    n.setAttribute(t.name, t.value);
  });
}
function p(e, n) {
  for (; e.firstChild; )
    n.appendChild(e.firstChild);
}
function R(e) {
  return e.tagName !== "P" || e.attributes.length > 0 || (e.textContent || "").trim() !== "" ? !1 : !e.querySelector("img, video, table, iframe, hr, pre, ul, ol");
}
function d(e, n) {
  const t = document.createElement(e);
  return n && A(n, t), t;
}
function C(e, n) {
  const t = d(n, e);
  return p(e, t), t.innerHTML.trim() || (t.innerHTML = "<br>"), t;
}
function I(e) {
  const n = e.cloneNode(!1);
  return n instanceof HTMLElement ? n : document.createElement(e.tagName.toLowerCase());
}
function L(e, n, t) {
  const r = e.parentNode;
  if (!r) return [];
  const s = document.createDocumentFragment(), o = [];
  let a = null;
  const c = () => {
    a && a.children.length > 0 && s.appendChild(a), a = null;
  };
  return Array.from(e.children).forEach((i) => {
    if (i instanceof HTMLLIElement) {
      if (n.has(i)) {
        c();
        const u = C(i, t);
        s.appendChild(u), o.push(u);
        return;
      }
      a || (a = I(e)), a.appendChild(i);
    }
  }), c(), r.insertBefore(s, e), r.removeChild(e), o;
}
function _(e, n) {
  if (e.tagName.toLowerCase() === n) return [e];
  const t = d(n, e);
  return p(e, t), t.innerHTML.trim() || (t.innerHTML = "<br>"), e.parentNode?.replaceChild(t, e), [t];
}
function O(e, n) {
  const t = e.parentNode;
  if (!t) return [];
  const r = document.createDocumentFragment(), s = [];
  return Array.from(e.childNodes).forEach((o) => {
    if (o instanceof HTMLElement && R(o)) {
      o.remove();
      return;
    }
    if (o instanceof HTMLUListElement || o instanceof HTMLOListElement) {
      Array.from(o.children).forEach((c) => {
        if (!(c instanceof HTMLLIElement)) return;
        const i = C(c, n);
        r.appendChild(i), s.push(i);
      }), o.remove();
      return;
    }
    if (o instanceof HTMLElement && g.has(o.tagName)) {
      const c = d(n, o);
      p(o, c), r.appendChild(c), s.push(c);
      return;
    }
    if (o.nodeType === Node.TEXT_NODE && (o.textContent || "").trim() === "") {
      o.remove();
      return;
    }
    const a = d(n, e);
    a.appendChild(o), r.appendChild(a), s.push(a);
  }), t.insertBefore(r, e), t.removeChild(e), s;
}
function b(e) {
  const n = window.getSelection();
  if (!n) return;
  const t = document.createRange(), r = e instanceof HTMLElement && e.querySelector("p,li,h1,h2,h3,h4,h5,h6") || e;
  t.selectNodeContents(r), t.collapse(!0), n.removeAllRanges(), n.addRange(t);
}
function k(e) {
  e.dispatchEvent(new Event("input", { bubbles: !0 }));
}
function x(e, n, t) {
  const r = window.execEditorCommand;
  if (typeof r == "function") {
    r("recordDomTransaction", e, n, t);
    return;
  }
  const s = window.executeEditorCommand;
  typeof s == "function" && s("recordDomTransaction", { editor: e, beforeHTML: n, afterHTML: t });
}
function D(e, n) {
  const t = /* @__PURE__ */ new Map();
  return Array.from(e.querySelectorAll("li")).filter(
    (s) => s instanceof HTMLLIElement && v(n, s)
  ).forEach((s) => {
    const o = s.parentElement;
    (o instanceof HTMLUListElement || o instanceof HTMLOListElement) && o.parentElement === e && (t.has(o) || t.set(o, /* @__PURE__ */ new Set()), t.get(o)?.add(s));
  }), t;
}
function m(e, n) {
  const t = e.toLowerCase();
  if (!B.has(t)) return !1;
  const r = S(n);
  if (!r) return !1;
  const s = N(r);
  if (!s) return !1;
  const o = r.innerHTML, a = [], c = D(r, s);
  c.size > 0 ? c.forEach((u, l) => {
    a.push(...L(l, u, t));
  }) : w(r, s).forEach((l) => {
    if (l instanceof HTMLUListElement || l instanceof HTMLOListElement) {
      const h = new Set(Array.from(l.children).filter(
        (M) => M instanceof HTMLLIElement
      ));
      a.push(...L(l, h, t));
      return;
    }
    if (g.has(l.tagName)) {
      const h = l.querySelector(":scope > ul, :scope > ol, :scope > blockquote, :scope > table");
      a.push(
        ...h ? O(l, t) : _(l, t)
      );
    }
  });
  const i = r.innerHTML;
  return o === i || a.length === 0 ? !1 : (x(r, o, i), k(r), b(a[0]), !0);
}
const P = () => ({
  name: "heading",
  // Toolbar button configuration with dropdown
  toolbar: [
    {
      label: "P",
      command: "setBlockType",
      type: "dropdown",
      options: [
        { value: "p", label: "P" },
        { value: "h1", label: "H1" },
        { value: "h2", label: "H2" },
        { value: "h3", label: "H3" },
        { value: "h4", label: "H4" },
        { value: "h5", label: "H5" },
        { value: "h6", label: "H6" }
      ],
      icon: '<svg width="24" height="24" focusable="false"><path d="M16.1 8.6 14.2 4l-1.4.5 2.8 7.4c.1.4.5.6.9.6h.1c.4-.1.6-.5.6-.9l1.8-4.8-1.4-.5-1.5 2.3ZM4 11.5h6V10H4v1.5ZM18.5 3v1L17 7l.9.9L20.7 3h-2.2ZM5.5 12h1v7h1v-7h1v-.5h-3V12Zm4 0h1v7h1v-7h1v-.5h-3V12Zm10 1.5a2 2 0 0 0-2-2h-1v7.5h1v-2.7h1a2 2 0 0 0 2-2v-.8Zm-2 1.3h-1v-2.3h1a.8.8 0 1 1 0 1.6v.7Z" fill-rule="evenodd"></path></svg>'
    }
  ],
  // Native command implementations
  commands: {
    /**
     * Set block type to specific heading level or paragraph
     */
    setBlockType: (e, n) => e ? m(e, n) : !1,
    /**
     * Set heading level 1
     */
    setHeading1: (e, n) => m("h1", n),
    /**
     * Set heading level 2
     */
    setHeading2: (e, n) => m("h2", n),
    /**
     * Set heading level 3
     */
    setHeading3: (e, n) => m("h3", n),
    /**
     * Set to paragraph
     */
    setParagraph: (e, n) => m("p", n)
  },
  // Keyboard shortcuts
  keymap: {
    "Mod-Alt-1": "setHeading1",
    "Mod-Alt-2": "setHeading2",
    "Mod-Alt-3": "setHeading3",
    "Mod-Alt-0": "setParagraph"
  }
});
export {
  P as HeadingPlugin
};
