import { __exports as d } from "./index-LexicalList.dev2.js";
import { __require as Z } from "./index-Lexical.js";
import { __require as ee } from "./index-LexicalUtils.js";
var V;
function ie() {
  if (V) return d;
  V = 1;
  var h = Z(), T = ee();
  function O(s) {
    let e = 1, t = s.getParent();
    for (; t != null; ) {
      if (u(t)) {
        const n = t.getParent();
        if (c(n)) {
          e++, t = n.getParent();
          continue;
        }
        throw Error("A ListItemNode must have a ListNode for a parent.");
      }
      return e;
    }
    return e;
  }
  function b(s) {
    let e = s.getParent();
    if (!c(e))
      throw Error("A ListItemNode must have a ListNode for a parent.");
    let t = e;
    for (; t !== null; )
      t = t.getParent(), c(t) && (e = t);
    return e;
  }
  function P(s) {
    let e = [];
    const t = s.getChildren().filter(u);
    for (let n = 0; n < t.length; n++) {
      const r = t[n], i = r.getFirstChild();
      c(i) ? e = e.concat(P(i)) : e.push(r);
    }
    return e;
  }
  function L(s) {
    return u(s) && c(s.getFirstChild());
  }
  function K(s) {
    let e = s;
    for (; e.getNextSibling() == null && e.getPreviousSibling() == null; ) {
      const t = e.getParent();
      if (t == null || !(u(e) || c(e)))
        break;
      e = t;
    }
    e.remove();
  }
  function A(s) {
    return _().append(s);
  }
  function x(s, e) {
    return u(s) && (e.length === 0 || e.length === 1 && s.is(e[0]) && s.getChildrenSize() === 0);
  }
  function B(s) {
    const e = s.getParent();
    let t = 1;
    if (e != null)
      if (c(e))
        t = e.getStart();
      else
        throw Error("$getListItemValue: list node is not parent of list item node");
    const n = s.getPreviousSiblings();
    for (let r = 0; r < n.length; r++) {
      const i = n[r];
      u(i) && !c(i.getFirstChild()) && t++;
    }
    return t;
  }
  function q(s, e) {
    s.update(() => {
      const t = h.$getSelection();
      if (h.$INTERNAL_isPointSelection(t)) {
        const n = t.getNodes(), i = t.anchor.getNode(), o = i.getParent();
        if (x(i, n)) {
          const l = m(e);
          if (h.$isRootOrShadowRoot(o)) {
            i.replace(l);
            const a = _();
            h.$isElementNode(i) && (a.setFormat(i.getFormatType()), a.setIndent(i.getIndent())), l.append(a);
          } else if (u(i)) {
            const a = i.getParentOrThrow();
            S(l, a.getChildren()), a.replace(l);
          }
          return;
        } else {
          const l = /* @__PURE__ */ new Set();
          for (let a = 0; a < n.length; a++) {
            const f = n[a];
            if (h.$isElementNode(f) && f.isEmpty() && !u(f) && !l.has(f.getKey())) {
              k(f, e);
              continue;
            }
            if (h.$isLeafNode(f)) {
              let g = f.getParent();
              for (; g != null; ) {
                const C = g.getKey();
                if (c(g)) {
                  if (!l.has(C)) {
                    const N = m(e);
                    S(N, g.getChildren()), g.replace(N), p(N), l.add(C);
                  }
                  break;
                } else {
                  const N = g.getParent();
                  if (h.$isRootOrShadowRoot(N) && !l.has(C)) {
                    l.add(C), k(g, e);
                    break;
                  }
                  g = N;
                }
              }
            }
          }
        }
      }
    });
  }
  function S(s, e) {
    s.splice(s.getChildrenSize(), 0, e);
  }
  function k(s, e) {
    if (c(s))
      return s;
    const t = s.getPreviousSibling(), n = s.getNextSibling(), r = _();
    if (r.setFormat(s.getFormatType()), r.setIndent(s.getIndent()), S(r, s.getChildren()), c(t) && e === t.getListType())
      return t.append(r), s.remove(), c(n) && e === n.getListType() && (S(t, n.getChildren()), n.remove()), t;
    if (c(n) && e === n.getListType())
      return n.getFirstChildOrThrow().insertBefore(r), s.remove(), n;
    {
      const i = m(e);
      return i.append(r), s.replace(i), p(i), i;
    }
  }
  function w(s, e) {
    const t = s.getLastChild(), n = e.getFirstChild();
    t && n && L(t) && L(n) && (w(t.getFirstChild(), n.getFirstChild()), n.remove());
    const r = e.getChildren();
    r.length > 0 && (s.append(...r), p(s)), e.remove();
  }
  function H(s) {
    s.update(() => {
      const e = h.$getSelection();
      if (h.$isRangeSelection(e)) {
        const t = /* @__PURE__ */ new Set(), n = e.getNodes(), r = e.anchor.getNode();
        if (x(r, n))
          t.add(b(r));
        else
          for (let i = 0; i < n.length; i++) {
            const o = n[i];
            if (h.$isLeafNode(o)) {
              const l = T.$getNearestNodeOfType(o, v);
              l != null && t.add(b(l));
            }
          }
        for (const i of t) {
          let o = i;
          const l = P(i);
          for (const a of l) {
            const f = h.$createParagraphNode();
            S(f, a.getChildren()), o.insertAfter(f), o = f, a.__key === e.anchor.key && e.anchor.set(f.getKey(), 0, "element"), a.__key === e.focus.key && e.focus.set(f.getKey(), 0, "element"), a.remove();
          }
          i.remove();
        }
      }
    });
  }
  function p(s, e) {
    const t = e || s.getChildren();
    if (t !== void 0)
      for (let n = 0; n < t.length; n++) {
        const r = t[n];
        if (u(r)) {
          const i = r.getValue(), o = B(r);
          i !== o && r.setValue(o);
        }
      }
  }
  function J(s) {
    const e = /* @__PURE__ */ new Set();
    if (L(s) || e.has(s.getKey()))
      return;
    const t = s.getParent(), n = s.getNextSibling(), r = s.getPreviousSibling();
    if (L(n) && L(r)) {
      const i = r.getFirstChild();
      if (c(i)) {
        i.append(s);
        const o = n.getFirstChild();
        if (c(o)) {
          const l = o.getChildren();
          S(i, l), n.remove(), e.add(n.getKey());
        }
        p(i);
      }
    } else if (L(n)) {
      const i = n.getFirstChild();
      if (c(i)) {
        const o = i.getFirstChild();
        o !== null && o.insertBefore(s), p(i);
      }
    } else if (L(r)) {
      const i = r.getFirstChild();
      c(i) && (i.append(s), p(i));
    } else if (c(t)) {
      const i = _(), o = m(t.getListType());
      i.append(o), o.append(s), r ? r.insertAfter(i) : n ? n.insertBefore(i) : t.append(i), p(o);
    }
    c(t) && p(t);
  }
  function U(s) {
    if (L(s))
      return;
    const e = s.getParent(), t = e ? e.getParent() : void 0, n = t ? t.getParent() : void 0;
    if (c(n) && u(t) && c(e)) {
      const r = e ? e.getFirstChild() : void 0, i = e ? e.getLastChild() : void 0;
      if (s.is(r))
        t.insertBefore(s), e.isEmpty() && t.remove();
      else if (s.is(i))
        t.insertAfter(s), e.isEmpty() && t.remove();
      else {
        const o = e.getListType(), l = _(), a = m(o);
        l.append(a), s.getPreviousSiblings().forEach((C) => a.append(C));
        const f = _(), g = m(o);
        f.append(g), S(g, s.getNextSiblings()), t.insertBefore(l), t.insertAfter(f), t.replace(s);
      }
      p(e), p(n);
    }
  }
  function G() {
    const s = h.$getSelection();
    if (!h.$isRangeSelection(s) || !s.isCollapsed())
      return !1;
    const e = s.anchor.getNode();
    if (!u(e) || e.getChildrenSize() !== 0)
      return !1;
    const t = b(e), n = e.getParent();
    if (!c(n))
      throw Error("A ListItemNode must have a ListNode for a parent.");
    const r = n.getParent();
    let i;
    if (h.$isRootOrShadowRoot(r))
      i = h.$createParagraphNode(), t.insertAfter(i);
    else if (u(r))
      i = _(), r.insertAfter(i);
    else
      return !1;
    i.select();
    const o = e.getNextSiblings();
    if (o.length > 0) {
      const l = m(n.getListType());
      if (h.$isParagraphNode(i))
        i.insertAfter(l);
      else {
        const a = _();
        a.append(l), i.insertAfter(a);
      }
      o.forEach((a) => {
        a.remove(), l.append(a);
      });
    }
    return K(e), !0;
  }
  class v extends h.ElementNode {
    /** @internal */
    /** @internal */
    static getType() {
      return "listitem";
    }
    static clone(e) {
      return new v(e.__value, e.__checked, e.__key);
    }
    constructor(e, t, n) {
      super(n), this.__value = e === void 0 ? 1 : e, this.__checked = t;
    }
    createDOM(e) {
      const t = document.createElement("li"), n = this.getParent();
      return c(n) && n.getListType() === "check" && M(t, this, null), t.value = this.__value, $(t, e.theme, this), t;
    }
    updateDOM(e, t, n) {
      const r = this.getParent();
      return c(r) && r.getListType() === "check" && M(t, this, e), t.value = this.__value, $(t, n.theme, this), !1;
    }
    static transform() {
      return (e) => {
        const t = e.getParent();
        if (c(t)) {
          if (p(t), !u(e))
            throw Error("node is not a ListItemNode");
          t.getListType() !== "check" && e.getChecked() != null && e.setChecked(void 0);
        }
      };
    }
    static importDOM() {
      return {
        li: (e) => ({
          conversion: z,
          priority: 0
        })
      };
    }
    static importJSON(e) {
      const t = _();
      return t.setChecked(e.checked), t.setValue(e.value), t.setFormat(e.format), t.setDirection(e.direction), t;
    }
    exportDOM(e) {
      const t = this.createDOM(e._config);
      return t.style.textAlign = this.getFormatType(), {
        element: t
      };
    }
    exportJSON() {
      return {
        ...super.exportJSON(),
        checked: this.getChecked(),
        type: "listitem",
        value: this.getValue(),
        version: 1
      };
    }
    append(...e) {
      for (let t = 0; t < e.length; t++) {
        const n = e[t];
        if (h.$isElementNode(n) && this.canMergeWith(n)) {
          const r = n.getChildren();
          this.append(...r), n.remove();
        } else
          super.append(n);
      }
      return this;
    }
    replace(e, t) {
      if (u(e))
        return super.replace(e);
      this.setIndent(0);
      const n = this.getParentOrThrow();
      if (!c(n)) return e;
      if (n.__first === this.getKey())
        n.insertBefore(e);
      else if (n.__last === this.getKey())
        n.insertAfter(e);
      else {
        const r = m(n.getListType());
        let i = this.getNextSibling();
        for (; i; ) {
          const o = i;
          i = i.getNextSibling(), r.append(o);
        }
        n.insertAfter(e), e.insertAfter(r);
      }
      if (t) {
        if (!h.$isElementNode(e))
          throw Error("includeChildren should only be true for ElementNodes");
        this.getChildren().forEach((r) => {
          e.append(r);
        });
      }
      return this.remove(), n.getChildrenSize() === 0 && n.remove(), e;
    }
    insertAfter(e, t = !0) {
      const n = this.getParentOrThrow();
      if (!c(n))
        throw Error("insertAfter: list node is not parent of list item node");
      const r = this.getNextSiblings();
      if (u(e)) {
        const i = super.insertAfter(e, t), o = e.getParentOrThrow();
        return c(o) && p(o), i;
      }
      if (c(e)) {
        let i = e;
        const o = e.getChildren();
        for (let l = o.length - 1; l >= 0; l--)
          i = o[l], this.insertAfter(i, t);
        return i;
      }
      if (n.insertAfter(e, t), r.length !== 0) {
        const i = m(n.getListType());
        r.forEach((o) => i.append(o)), e.insertAfter(i, t);
      }
      return e;
    }
    remove(e) {
      const t = this.getPreviousSibling(), n = this.getNextSibling();
      if (super.remove(e), t && n && L(t) && L(n))
        w(t.getFirstChild(), n.getFirstChild()), n.remove();
      else if (n) {
        const r = n.getParent();
        c(r) && p(r);
      }
    }
    insertNewAfter(e, t = !0) {
      const n = _(this.__checked == null ? void 0 : !1);
      return this.insertAfter(n, t), n;
    }
    collapseAtStart(e) {
      const t = h.$createParagraphNode();
      this.getChildren().forEach((l) => t.append(l));
      const r = this.getParentOrThrow(), i = r.getParentOrThrow(), o = u(i);
      if (r.getChildrenSize() === 1)
        if (o)
          r.remove(), i.select();
        else {
          r.insertBefore(t), r.remove();
          const l = e.anchor, a = e.focus, f = t.getKey();
          l.type === "element" && l.getNode().is(this) && l.set(f, l.offset, "element"), a.type === "element" && a.getNode().is(this) && a.set(f, a.offset, "element");
        }
      else
        r.insertBefore(t), this.remove();
      return !0;
    }
    getValue() {
      return this.getLatest().__value;
    }
    setValue(e) {
      const t = this.getWritable();
      t.__value = e;
    }
    getChecked() {
      return this.getLatest().__checked;
    }
    setChecked(e) {
      const t = this.getWritable();
      t.__checked = e;
    }
    toggleChecked() {
      this.setChecked(!this.__checked);
    }
    getIndent() {
      const e = this.getParent();
      if (e === null)
        return this.getLatest().__indent;
      let t = e.getParentOrThrow(), n = 0;
      for (; u(t); )
        t = t.getParentOrThrow().getParentOrThrow(), n++;
      return n;
    }
    setIndent(e) {
      if (!(typeof e == "number" && e > -1))
        throw Error("Invalid indent value.");
      let t = this.getIndent();
      for (; t !== e; )
        t < e ? (J(this), t++) : (U(this), t--);
      return this;
    }
    insertBefore(e) {
      if (u(e)) {
        const t = this.getParentOrThrow();
        if (c(t)) {
          const n = this.getNextSiblings();
          p(t, n);
        }
      }
      return super.insertBefore(e);
    }
    canInsertAfter(e) {
      return u(e);
    }
    canReplaceWith(e) {
      return u(e);
    }
    canMergeWith(e) {
      return h.$isParagraphNode(e) || u(e);
    }
    extractWithChild(e, t) {
      if (!h.$isRangeSelection(t))
        return !1;
      const n = t.anchor.getNode(), r = t.focus.getNode();
      return this.isParentOf(n) && this.isParentOf(r) && this.getTextContent().length === t.getTextContent().length;
    }
    isParentRequired() {
      return !0;
    }
    createParentElementNode() {
      return m("bullet");
    }
  }
  function $(s, e, t) {
    const n = [], r = [], i = e.list, o = i ? i.listitem : void 0;
    let l;
    if (i && i.nested && (l = i.nested.listitem), o !== void 0) {
      const a = o.split(" ");
      n.push(...a);
    }
    if (i) {
      const a = t.getParent(), f = c(a) && a.getListType() === "check", g = t.getChecked();
      (!f || g) && r.push(i.listitemUnchecked), (!f || !g) && r.push(i.listitemChecked), f && n.push(g ? i.listitemChecked : i.listitemUnchecked);
    }
    if (l !== void 0) {
      const a = l.split(" ");
      t.getChildren().some((f) => c(f)) ? n.push(...a) : r.push(...a);
    }
    r.length > 0 && T.removeClassNamesFromElement(s, ...r), n.length > 0 && T.addClassNamesToElement(s, ...n);
  }
  function M(s, e, t, n) {
    c(e.getFirstChild()) ? (s.removeAttribute("role"), s.removeAttribute("tabIndex"), s.removeAttribute("aria-checked")) : (s.setAttribute("role", "checkbox"), s.setAttribute("tabIndex", "-1"), (!t || e.__checked !== t.__checked) && s.setAttribute("aria-checked", e.getChecked() ? "true" : "false"));
  }
  function z(s) {
    const e = T.isHTMLElement(s) && s.getAttribute("aria-checked") === "true";
    return {
      node: _(e)
    };
  }
  function _(s) {
    return h.$applyNodeReplacement(new v(void 0, s));
  }
  function u(s) {
    return s instanceof v;
  }
  class E extends h.ElementNode {
    /** @internal */
    /** @internal */
    /** @internal */
    static getType() {
      return "list";
    }
    static clone(e) {
      const t = e.__listType || F[e.__tag];
      return new E(t, e.__start, e.__key);
    }
    constructor(e, t, n) {
      super(n);
      const r = F[e] || e;
      this.__listType = r, this.__tag = r === "number" ? "ol" : "ul", this.__start = t;
    }
    getTag() {
      return this.__tag;
    }
    setListType(e) {
      const t = this.getWritable();
      t.__listType = e, t.__tag = e === "number" ? "ol" : "ul";
    }
    getListType() {
      return this.__listType;
    }
    getStart() {
      return this.__start;
    }
    // View
    createDOM(e, t) {
      const n = this.__tag, r = document.createElement(n);
      return this.__start !== 1 && r.setAttribute("start", String(this.__start)), r.__lexicalListType = this.__listType, D(r, e.theme, this), r;
    }
    updateDOM(e, t, n) {
      return e.__tag !== this.__tag ? !0 : (D(t, n.theme, this), !1);
    }
    static importDOM() {
      return {
        ol: (e) => ({
          conversion: R,
          priority: 0
        }),
        ul: (e) => ({
          conversion: R,
          priority: 0
        })
      };
    }
    static importJSON(e) {
      const t = m(e.listType, e.start);
      return t.setFormat(e.format), t.setIndent(e.indent), t.setDirection(e.direction), t;
    }
    exportDOM(e) {
      const {
        element: t
      } = super.exportDOM(e);
      return t && T.isHTMLElement(t) && (this.__start !== 1 && t.setAttribute("start", String(this.__start)), this.__listType === "check" && t.setAttribute("__lexicalListType", "check")), {
        element: t
      };
    }
    exportJSON() {
      return {
        ...super.exportJSON(),
        listType: this.getListType(),
        start: this.getStart(),
        tag: this.getTag(),
        type: "list",
        version: 1
      };
    }
    canBeEmpty() {
      return !1;
    }
    canIndent() {
      return !1;
    }
    append(...e) {
      for (let t = 0; t < e.length; t++) {
        const n = e[t];
        if (u(n))
          super.append(n);
        else {
          const r = _();
          if (c(n))
            r.append(n);
          else if (h.$isElementNode(n)) {
            const i = h.$createTextNode(n.getTextContent());
            r.append(i);
          } else
            r.append(n);
          super.append(r);
        }
      }
      return p(this), this;
    }
    extractWithChild(e) {
      return u(e);
    }
  }
  function D(s, e, t) {
    const n = [], r = [], i = e.list;
    if (i !== void 0) {
      const o = i[`${t.__tag}Depth`] || [], l = O(t) - 1, a = l % o.length, f = o[a], g = i[t.__tag];
      let C;
      const N = i.nested;
      if (N !== void 0 && N.list && (C = N.list), g !== void 0 && n.push(g), f !== void 0) {
        const I = f.split(" ");
        n.push(...I);
        for (let y = 0; y < o.length; y++)
          y !== a && r.push(t.__tag + y);
      }
      if (C !== void 0) {
        const I = C.split(" ");
        l > 1 ? n.push(...I) : r.push(...I);
      }
    }
    r.length > 0 && T.removeClassNamesFromElement(s, ...r), n.length > 0 && T.addClassNamesToElement(s, ...n);
  }
  function W(s) {
    const e = [];
    for (let t = 0; t < s.length; t++) {
      const n = s[t];
      if (u(n)) {
        e.push(n);
        const r = n.getChildren();
        r.length > 1 && r.forEach((i) => {
          c(i) && e.push(A(i));
        });
      } else
        e.push(A(n));
    }
    return e;
  }
  function R(s) {
    const e = s.nodeName.toLowerCase();
    let t = null;
    if (e === "ol") {
      const n = s.start;
      t = m("number", n);
    } else e === "ul" && (T.isHTMLElement(s) && s.getAttribute("__lexicallisttype") === "check" ? t = m("check") : t = m("bullet"));
    return {
      after: W,
      node: t
    };
  }
  const F = {
    ol: "number",
    ul: "bullet"
  };
  function m(s, e = 1) {
    return h.$applyNodeReplacement(new E(s, e));
  }
  function c(s) {
    return s instanceof E;
  }
  const Y = h.createCommand("INSERT_UNORDERED_LIST_COMMAND"), j = h.createCommand("INSERT_ORDERED_LIST_COMMAND"), Q = h.createCommand("INSERT_CHECK_LIST_COMMAND"), X = h.createCommand("REMOVE_LIST_COMMAND");
  return d.$createListItemNode = _, d.$createListNode = m, d.$getListDepth = O, d.$handleListInsertParagraph = G, d.$isListItemNode = u, d.$isListNode = c, d.INSERT_CHECK_LIST_COMMAND = Q, d.INSERT_ORDERED_LIST_COMMAND = j, d.INSERT_UNORDERED_LIST_COMMAND = Y, d.ListItemNode = v, d.ListNode = E, d.REMOVE_LIST_COMMAND = X, d.insertList = q, d.removeList = H, d;
}
export {
  ie as __require
};
