"use client";
import { $insertDataTransferForRichText as R, copyToClipboard as S } from "../clipboard/LexicalClipboard.prod.js";
import { $shouldOverrideDefaultCharacterSelection as I, $moveCharacter as P } from "../selection/LexicalSelection.prod.js";
import { addClassNamesToElement as k, mergeRegister as U, objectKlassEquals as g, $getNearestBlockElementAncestorOrThrow as j, $findMatchingParent as B } from "../utils/LexicalUtils.prod.js";
import { ElementNode as z, isHTMLElement as G, $createParagraphNode as h, $applyNodeReplacement as H, setNodeIndentFromDOM as X, KEY_TAB_COMMAND as V, $getSelection as i, $isRangeSelection as u, COMMAND_PRIORITY_EDITOR as a, KEY_SPACE_COMMAND as Z, PASTE_COMMAND as ee, createCommand as te, isDOMNode as ne, isSelectionCapturedInDecoratorInput as re, PASTE_TAG as se, CUT_COMMAND as ae, $isNodeSelection as d, COPY_COMMAND as oe, SELECT_ALL_COMMAND as ie, $selectAll as ue, DRAGOVER_COMMAND as ce, $getNearestNodeFromDOMNode as T, $isDecoratorNode as E, DRAGSTART_COMMAND as le, DROP_COMMAND as de, $createRangeSelection as fe, $isTextNode as me, $normalizeSelection__EXPERIMENTAL as ge, $setSelection as pe, KEY_ESCAPE_COMMAND as Ce, KEY_ENTER_COMMAND as he, INSERT_LINE_BREAK_COMMAND as x, INSERT_PARAGRAPH_COMMAND as $, KEY_DELETE_COMMAND as Ne, DELETE_CHARACTER_COMMAND as A, KEY_BACKSPACE_COMMAND as De, $isRootNode as Ee, OUTDENT_CONTENT_COMMAND as w, KEY_ARROW_RIGHT_COMMAND as Me, KEY_ARROW_LEFT_COMMAND as _e, KEY_ARROW_DOWN_COMMAND as Ae, $getRoot as Oe, $getAdjacentNode as F, KEY_ARROW_UP_COMMAND as Te, INDENT_CONTENT_COMMAND as ve, INSERT_TAB_COMMAND as ye, $insertNodes as Re, $createTabNode as Se, FORMAT_ELEMENT_COMMAND as Ie, FORMAT_TEXT_COMMAND as Pe, REMOVE_TEXT_COMMAND as xe, CONTROLLED_TEXT_INSERTION_COMMAND as $e, DELETE_LINE_COMMAND as we, DELETE_WORD_COMMAND as Fe, CLICK_COMMAND as Ke, $isElementNode as q } from "../../lexical/Lexical.prod.js";
function K(n, e) {
  if (document.caretRangeFromPoint !== void 0) {
    const t = document.caretRangeFromPoint(n, e);
    return t === null ? null : { node: t.startContainer, offset: t.startOffset };
  }
  if (document.caretPositionFromPoint !== "undefined") {
    const t = document.caretPositionFromPoint(n, e);
    return t === null ? null : { node: t.offsetNode, offset: t.offset };
  }
  return null;
}
const p = typeof window < "u" && window.document !== void 0 && window.document.createElement !== void 0, Le = p && "documentMode" in document ? document.documentMode : null, be = !(!p || !("InputEvent" in window) || Le) && "getTargetRanges" in new window.InputEvent("input"), Ye = p && /Version\/[\d.]+.*Safari/.test(navigator.userAgent), L = p && /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream, We = p && /^(?=.*Chrome).*/i.test(navigator.userAgent), Je = p && /AppleWebKit\/[\d.]+/.test(navigator.userAgent) && !We, b = te("DRAG_DROP_PASTE_FILE");
class M extends z {
  static getType() {
    return "quote";
  }
  static clone(e) {
    return new M(e.__key);
  }
  createDOM(e) {
    const t = document.createElement("blockquote");
    return k(t, e.theme.quote), t;
  }
  updateDOM(e, t) {
    return !1;
  }
  static importDOM() {
    return { blockquote: (e) => ({ conversion: ke, priority: 0 }) };
  }
  exportDOM(e) {
    const { element: t } = super.exportDOM(e);
    if (G(t)) {
      this.isEmpty() && t.append(document.createElement("br"));
      const r = this.getFormatType();
      r && (t.style.textAlign = r);
      const s = this.getDirection();
      s && (t.dir = s);
    }
    return { element: t };
  }
  static importJSON(e) {
    return Q().updateFromJSON(e);
  }
  insertNewAfter(e, t) {
    const r = h(), s = this.getDirection();
    return r.setDirection(s), this.insertAfter(r, t), r;
  }
  collapseAtStart() {
    const e = h();
    return this.getChildren().forEach(((t) => e.append(t))), this.replace(e), !0;
  }
  canMergeWhenEmpty() {
    return !0;
  }
}
function Q() {
  return H(new M());
}
function Xe(n) {
  return n instanceof M;
}
class _ extends z {
  static getType() {
    return "heading";
  }
  static clone(e) {
    return new _(e.__tag, e.__key);
  }
  constructor(e, t) {
    super(t), this.__tag = e;
  }
  getTag() {
    return this.__tag;
  }
  setTag(e) {
    const t = this.getWritable();
    return this.__tag = e, t;
  }
  createDOM(e) {
    const t = this.__tag, r = document.createElement(t), s = e.theme.heading;
    if (s !== void 0) {
      const o = s[t];
      k(r, o);
    }
    return r;
  }
  updateDOM(e, t, r) {
    return e.__tag !== this.__tag;
  }
  static importDOM() {
    return { h1: (e) => ({ conversion: m, priority: 0 }), h2: (e) => ({ conversion: m, priority: 0 }), h3: (e) => ({ conversion: m, priority: 0 }), h4: (e) => ({ conversion: m, priority: 0 }), h5: (e) => ({ conversion: m, priority: 0 }), h6: (e) => ({ conversion: m, priority: 0 }), p: (e) => {
      const t = e.firstChild;
      return t !== null && Y(t) ? { conversion: () => ({ node: null }), priority: 3 } : null;
    }, span: (e) => Y(e) ? { conversion: (t) => ({ node: C("h1") }), priority: 3 } : null };
  }
  exportDOM(e) {
    const { element: t } = super.exportDOM(e);
    if (G(t)) {
      this.isEmpty() && t.append(document.createElement("br"));
      const r = this.getFormatType();
      r && (t.style.textAlign = r);
      const s = this.getDirection();
      s && (t.dir = s);
    }
    return { element: t };
  }
  static importJSON(e) {
    return C(e.tag).updateFromJSON(e);
  }
  updateFromJSON(e) {
    return super.updateFromJSON(e).setTag(e.tag);
  }
  exportJSON() {
    return { ...super.exportJSON(), tag: this.getTag() };
  }
  insertNewAfter(e, t = !0) {
    const r = e ? e.anchor.offset : 0, s = this.getLastDescendant(), o = !s || e && e.anchor.key === s.getKey() && r === s.getTextContentSize() || !e ? h() : C(this.getTag()), l = this.getDirection();
    if (o.setDirection(l), this.insertAfter(o, t), r === 0 && !this.isEmpty() && e) {
      const c = h();
      c.select(), this.replace(c, !0);
    }
    return o;
  }
  collapseAtStart() {
    const e = this.isEmpty() ? h() : C(this.getTag());
    return this.getChildren().forEach(((t) => e.append(t))), this.replace(e), !0;
  }
  extractWithChild() {
    return !0;
  }
}
function Y(n) {
  return n.nodeName.toLowerCase() === "span" && n.style.fontSize === "26pt";
}
function m(n) {
  const e = n.nodeName.toLowerCase();
  let t = null;
  return e !== "h1" && e !== "h2" && e !== "h3" && e !== "h4" && e !== "h5" && e !== "h6" || (t = C(e), n.style !== null && (X(n, t), t.setFormat(n.style.textAlign))), { node: t };
}
function ke(n) {
  const e = Q();
  return n.style !== null && (e.setFormat(n.style.textAlign), X(n, e)), { node: e };
}
function C(n = "h1") {
  return H(new _(n));
}
function qe(n) {
  return n instanceof _;
}
function D(n) {
  let e = null;
  if (g(n, DragEvent) ? e = n.dataTransfer : g(n, ClipboardEvent) && (e = n.clipboardData), e === null) return [!1, [], !1];
  const t = e.types, r = t.includes("Files"), s = t.includes("text/html") || t.includes("text/plain");
  return [r, Array.from(e.files), s];
}
function W(n) {
  const e = i();
  if (!u(e)) return !1;
  const t = /* @__PURE__ */ new Set(), r = e.getNodes();
  for (let s = 0; s < r.length; s++) {
    const o = r[s], l = o.getKey();
    if (t.has(l)) continue;
    const c = B(o, ((N) => q(N) && !N.isInline()));
    if (c === null) continue;
    const f = c.getKey();
    c.canIndent() && !t.has(f) && (t.add(f), n(c));
  }
  return t.size > 0;
}
function J(n) {
  const e = T(n);
  return E(e);
}
function O(n) {
  for (const e of ["lowercase", "uppercase", "capitalize"]) n.hasFormat(e) && n.toggleFormat(e);
}
function Qe(n) {
  return U(n.registerCommand(Ke, ((e) => {
    const t = i();
    return !!d(t) && (t.clear(), !0);
  }), a), n.registerCommand(A, ((e) => {
    const t = i();
    return u(t) ? (t.deleteCharacter(e), !0) : !!d(t) && (t.deleteNodes(), !0);
  }), a), n.registerCommand(Fe, ((e) => {
    const t = i();
    return !!u(t) && (t.deleteWord(e), !0);
  }), a), n.registerCommand(we, ((e) => {
    const t = i();
    return !!u(t) && (t.deleteLine(e), !0);
  }), a), n.registerCommand($e, ((e) => {
    const t = i();
    if (typeof e == "string") t !== null && t.insertText(e);
    else {
      if (t === null) return !1;
      const r = e.dataTransfer;
      if (r != null) R(r, t, n);
      else if (u(t)) {
        const s = e.data;
        return s && t.insertText(s), !0;
      }
    }
    return !0;
  }), a), n.registerCommand(xe, (() => {
    const e = i();
    return !!u(e) && (e.removeText(), !0);
  }), a), n.registerCommand(Pe, ((e) => {
    const t = i();
    return !!u(t) && (t.formatText(e), !0);
  }), a), n.registerCommand(Ie, ((e) => {
    const t = i();
    if (!u(t) && !d(t)) return !1;
    const r = t.getNodes();
    for (const s of r) {
      const o = B(s, ((l) => q(l) && !l.isInline()));
      o !== null && o.setFormat(e);
    }
    return !0;
  }), a), n.registerCommand(x, ((e) => {
    const t = i();
    return !!u(t) && (t.insertLineBreak(e), !0);
  }), a), n.registerCommand($, (() => {
    const e = i();
    return !!u(e) && (e.insertParagraph(), !0);
  }), a), n.registerCommand(ye, (() => (Re([Se()]), !0)), a), n.registerCommand(ve, (() => W(((e) => {
    const t = e.getIndent();
    e.setIndent(t + 1);
  }))), a), n.registerCommand(w, (() => W(((e) => {
    const t = e.getIndent();
    t > 0 && e.setIndent(Math.max(0, t - 1));
  }))), a), n.registerCommand(Te, ((e) => {
    const t = i();
    if (d(t)) {
      const r = t.getNodes();
      if (r.length > 0) return r[0].selectPrevious(), !0;
    } else if (u(t)) {
      const r = F(t.focus, !0);
      if (!e.shiftKey && E(r) && !r.isIsolated() && !r.isInline()) return r.selectPrevious(), e.preventDefault(), !0;
    }
    return !1;
  }), a), n.registerCommand(Ae, ((e) => {
    const t = i();
    if (d(t)) {
      const r = t.getNodes();
      if (r.length > 0) return r[0].selectNext(0, 0), !0;
    } else if (u(t)) {
      if ((function(s) {
        const o = s.focus;
        return o.key === "root" && o.offset === Oe().getChildrenSize();
      })(t)) return e.preventDefault(), !0;
      const r = F(t.focus, !1);
      if (!e.shiftKey && E(r) && !r.isIsolated() && !r.isInline()) return r.selectNext(), e.preventDefault(), !0;
    }
    return !1;
  }), a), n.registerCommand(_e, ((e) => {
    const t = i();
    if (d(t)) {
      const r = t.getNodes();
      if (r.length > 0) return e.preventDefault(), r[0].selectPrevious(), !0;
    }
    if (!u(t)) return !1;
    if (I(t, !0)) {
      const r = e.shiftKey;
      return e.preventDefault(), P(t, r, !0), !0;
    }
    return !1;
  }), a), n.registerCommand(Me, ((e) => {
    const t = i();
    if (d(t)) {
      const s = t.getNodes();
      if (s.length > 0) return e.preventDefault(), s[0].selectNext(0, 0), !0;
    }
    if (!u(t)) return !1;
    const r = e.shiftKey;
    return !!I(t, !1) && (e.preventDefault(), P(t, r, !1), !0);
  }), a), n.registerCommand(De, ((e) => {
    if (J(e.target)) return !1;
    const t = i();
    if (u(t)) {
      if ((function(r) {
        if (!r.isCollapsed()) return !1;
        const { anchor: s } = r;
        if (s.offset !== 0) return !1;
        const o = s.getNode();
        if (Ee(o)) return !1;
        const l = j(o);
        return l.getIndent() > 0 && (l.is(o) || o.is(l.getFirstDescendant()));
      })(t)) return e.preventDefault(), n.dispatchCommand(w, void 0);
      if (L && navigator.language === "ko-KR") return !1;
    } else if (!d(t)) return !1;
    return e.preventDefault(), n.dispatchCommand(A, !0);
  }), a), n.registerCommand(Ne, ((e) => {
    if (J(e.target)) return !1;
    const t = i();
    return !(!u(t) && !d(t)) && (e.preventDefault(), n.dispatchCommand(A, !1));
  }), a), n.registerCommand(he, ((e) => {
    const t = i();
    if (!u(t)) return !1;
    if (O(t), e !== null) {
      if ((L || Ye || Je) && be) return !1;
      if (e.preventDefault(), e.shiftKey) return n.dispatchCommand(x, !1);
    }
    return n.dispatchCommand($, void 0);
  }), a), n.registerCommand(Ce, (() => {
    const e = i();
    return !!u(e) && (n.blur(), !0);
  }), a), n.registerCommand(de, ((e) => {
    const [, t] = D(e);
    if (t.length > 0) {
      const s = K(e.clientX, e.clientY);
      if (s !== null) {
        const { offset: o, node: l } = s, c = T(l);
        if (c !== null) {
          const f = fe();
          if (me(c)) f.anchor.set(c.getKey(), o, "text"), f.focus.set(c.getKey(), o, "text");
          else {
            const v = c.getParentOrThrow().getKey(), y = c.getIndexWithinParent() + 1;
            f.anchor.set(v, y, "element"), f.focus.set(v, y, "element");
          }
          const N = ge(f);
          pe(N);
        }
        n.dispatchCommand(b, t);
      }
      return e.preventDefault(), !0;
    }
    const r = i();
    return !!u(r);
  }), a), n.registerCommand(le, ((e) => {
    const [t] = D(e), r = i();
    return !(t && !u(r));
  }), a), n.registerCommand(ce, ((e) => {
    const [t] = D(e), r = i();
    if (t && !u(r)) return !1;
    const s = K(e.clientX, e.clientY);
    if (s !== null) {
      const o = T(s.node);
      E(o) && e.preventDefault();
    }
    return !0;
  }), a), n.registerCommand(ie, (() => (ue(), !0)), a), n.registerCommand(oe, ((e) => (S(n, g(e, ClipboardEvent) ? e : null), !0)), a), n.registerCommand(ae, ((e) => ((async function(t, r) {
    await S(r, g(t, ClipboardEvent) ? t : null), r.update((() => {
      const s = i();
      u(s) ? s.removeText() : d(s) && s.getNodes().forEach(((o) => o.remove()));
    }));
  })(e, n), !0)), a), n.registerCommand(ee, ((e) => {
    const [, t, r] = D(e);
    return t.length > 0 && !r ? (n.dispatchCommand(b, t), !0) : ne(e.target) && re(e.target) ? !1 : i() !== null && ((function(s, o) {
      s.preventDefault(), o.update((() => {
        const l = i(), c = g(s, InputEvent) || g(s, KeyboardEvent) ? null : s.clipboardData;
        c != null && l !== null && R(c, l, o);
      }), { tag: se });
    })(e, n), !0);
  }), a), n.registerCommand(Z, ((e) => {
    const t = i();
    return u(t) && O(t), !1;
  }), a), n.registerCommand(V, ((e) => {
    const t = i();
    return u(t) && O(t), !1;
  }), a));
}
export {
  C as $createHeadingNode,
  Q as $createQuoteNode,
  qe as $isHeadingNode,
  Xe as $isQuoteNode,
  b as DRAG_DROP_PASTE,
  _ as HeadingNode,
  M as QuoteNode,
  D as eventFiles,
  Qe as registerRichText
};
