const d = '.rte-content, .editora-content, [contenteditable="true"]', w = "[data-editora-editor], .rte-editor, .editora-editor, editora-editor", T = [
  "p",
  "div",
  "h1",
  "h2",
  "h3",
  "h4",
  "h5",
  "h6",
  "ul",
  "ol",
  "blockquote",
  "pre",
  "table"
].join(",");
function E(e) {
  return e ? e.nodeType === Node.ELEMENT_NODE ? e : e.parentElement : null;
}
function y(e) {
  const t = e?.contentElement;
  if (t instanceof HTMLElement) return t;
  const n = e?.editorElement;
  if (n instanceof HTMLElement) {
    const c = n.querySelector(d);
    if (c instanceof HTMLElement) return c;
    if (n.getAttribute("contenteditable") === "true") return n;
  }
  const o = window.getSelection();
  if (o && o.rangeCount > 0) {
    const c = E(o.getRangeAt(0).startContainer), i = c?.closest(d);
    if (i instanceof HTMLElement) return i;
    const f = c?.closest(w)?.querySelector(d);
    if (f instanceof HTMLElement) return f;
  }
  const s = document.activeElement?.closest(d);
  return s instanceof HTMLElement ? s : document.querySelector(d);
}
function B(e) {
  const t = window.getSelection();
  if (!t || t.rangeCount === 0) return null;
  const n = t.getRangeAt(0);
  return !e.contains(n.startContainer) || !e.contains(n.endContainer) ? null : n;
}
function C(e, t) {
  let n = E(t);
  if (!n || n === e || !e.contains(n)) return null;
  for (; n.parentElement && n.parentElement !== e; )
    n = n.parentElement;
  if (n.parentElement !== e) return null;
  if (n.matches(T)) return n;
  const o = n.closest(T);
  return o instanceof HTMLElement && o.parentElement === e ? o : n;
}
function S(e, t) {
  if (typeof e.intersectsNode == "function")
    try {
      return e.intersectsNode(t);
    } catch {
      return !1;
    }
  const n = document.createRange();
  return n.selectNode(t), e.compareBoundaryPoints(Range.END_TO_START, n) < 0 && e.compareBoundaryPoints(Range.START_TO_END, n) > 0;
}
function b(e, t) {
  const n = C(e, t.startContainer), o = C(e, t.endContainer);
  if (!n) return [];
  if (!o || n === o) return [n];
  const s = Array.from(e.children).filter(
    (c) => c instanceof HTMLElement
  ).filter((c) => S(t, c));
  return s.length > 0 ? s : [n];
}
function k(e, t) {
  const o = E(t)?.closest("blockquote");
  return o instanceof HTMLQuoteElement && e.contains(o) ? o : null;
}
function v(e) {
  return e.tagName !== "P" || e.attributes.length > 0 || (e.textContent || "").trim() !== "" ? !1 : !e.querySelector("img, video, table, iframe, hr, pre, ul, ol");
}
function R(e) {
  Array.from(e.children).forEach((t) => {
    v(t) && t.remove();
  });
}
function p(e) {
  R(e);
  const t = e.parentNode;
  if (!t) return;
  Array.from(e.childNodes).forEach((o) => {
    t.insertBefore(o, e);
  }), t.removeChild(e);
}
function L(e) {
  return e.nodeType === Node.TEXT_NODE && (e.textContent || "").trim() === "";
}
function N(e) {
  const t = Array.from(e.childNodes).filter((o) => !L(o));
  if (t.length !== 1) return null;
  const n = t[0];
  return n instanceof HTMLQuoteElement ? n : null;
}
function M(e) {
  const t = document.createElement("blockquote");
  return Array.from(e.childNodes).filter((o) => !L(o)).forEach((o) => {
    t.appendChild(o);
  }), t.childNodes.length || (t.innerHTML = "<br>"), e.appendChild(t), t;
}
function x(e, t) {
  const n = Array.from(e.querySelectorAll("li")).filter(
    (r) => r instanceof HTMLLIElement && S(t, r)
  ), o = new Set(n);
  return n.filter((r) => {
    let s = r.parentElement?.closest("li");
    for (; s; ) {
      if (o.has(s)) return !1;
      s = s.parentElement?.closest("li");
    }
    return !0;
  });
}
function m(e) {
  const t = window.getSelection();
  if (!t) return;
  const n = document.createRange(), o = e instanceof HTMLElement && e.querySelector("p,li,h1,h2,h3,h4,h5,h6") || e;
  n.selectNodeContents(o), n.collapse(!0), t.removeAllRanges(), t.addRange(n);
}
function q(e) {
  e.dispatchEvent(new Event("input", { bubbles: !0 }));
}
function O(e, t, n) {
  const o = window.execEditorCommand;
  if (typeof o == "function") {
    o("recordDomTransaction", e, t, n);
    return;
  }
  const r = window.executeEditorCommand;
  typeof r == "function" && r("recordDomTransaction", { editor: e, beforeHTML: t, afterHTML: n });
}
function A(e) {
  const t = y(e);
  if (!t) return !1;
  const n = B(t);
  if (!n) return !1;
  const o = t.innerHTML, r = k(t, n.startContainer), s = k(t, n.endContainer), c = x(t, n), i = b(t, n), h = i.length > 0 && i.every((l) => l.tagName === "BLOCKQUOTE");
  if (r && r === s) {
    const l = r.firstChild || r;
    p(r), m(l);
  } else if (c.length > 0) {
    const l = c.map(N);
    if (l.every(Boolean)) {
      const u = l[0]?.firstChild || c[0];
      l.forEach((a) => {
        a && p(a);
      }), m(u);
    } else {
      const u = c[0];
      c.forEach((a) => {
        N(a) || M(a);
      }), m(u);
    }
  } else if (h) {
    const l = i[0].firstChild || i[0];
    i.forEach(p), m(l);
  } else {
    const l = i.filter((a) => a.tagName !== "BLOCKQUOTE");
    if (l.length === 0) return !1;
    const g = document.createElement("blockquote"), u = l[0];
    u.parentNode?.insertBefore(g, u), l.forEach((a) => {
      g.appendChild(a);
    }), m(g);
  }
  const f = t.innerHTML;
  return o === f ? !1 : (O(t, o, f), q(t), !0);
}
const H = () => ({
  name: "blockquote",
  // Schema definition for blockquote node
  nodes: {
    blockquote: {
      content: "block+",
      group: "block",
      parseDOM: [{ tag: "blockquote" }],
      toDOM: () => ["blockquote", 0]
    }
  },
  // Toolbar button configuration
  toolbar: [
    {
      label: "Quote",
      command: "toggleBlockquote",
      icon: '<svg fill="#000000" height="24px" width="24px" version="1.1" id="Icons" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 32 32" xml:space="preserve"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <g> <path d="M13,11c0.6,0,1-0.4,1-1s-0.4-1-1-1c-5,0-9,4-9,9c0,2.8,2.2,5,5,5s5-2.2,5-5s-2.2-5-5-5c-0.3,0-0.7,0-1,0.1 C9.3,11.8,11,11,13,11z"></path> <path d="M23,13c-0.3,0-0.7,0-1,0.1c1.3-1.3,3-2.1,5-2.1c0.6,0,1-0.4,1-1s-0.4-1-1-1c-5,0-9,4-9,9c0,2.8,2.2,5,5,5s5-2.2,5-5 S25.8,13,23,13z"></path> </g> </g></svg>',
      shortcut: "Mod-Shift-9"
    }
  ],
  // Native command implementations
  commands: {
    /**
     * Toggle blockquote formatting on current selection
     */
    toggleBlockquote: (e, t) => A(t)
  },
  // Keyboard shortcuts
  keymap: {
    "Mod-Shift-9": "toggleBlockquote"
  }
});
export {
  H as BlockquotePlugin
};
