var l = /* @__PURE__ */ ((o) => (o[o.TYPE = 3] = "TYPE", o[o.LEVEL = 12] = "LEVEL", o[o.ATTRIBUTE = 13] = "ATTRIBUTE", o[o.BLOT = 14] = "BLOT", o[o.INLINE = 7] = "INLINE", o[o.BLOCK = 11] = "BLOCK", o[o.BLOCK_BLOT = 10] = "BLOCK_BLOT", o[o.INLINE_BLOT = 6] = "INLINE_BLOT", o[o.BLOCK_ATTRIBUTE = 9] = "BLOCK_ATTRIBUTE", o[o.INLINE_ATTRIBUTE = 5] = "INLINE_ATTRIBUTE", o[o.ANY = 15] = "ANY", o))(l || {});
class A {
  constructor(t, e, s = {}) {
    this.attrName = t, this.keyName = e;
    const r = l.TYPE & l.ATTRIBUTE;
    this.scope = s.scope != null ? (
      // Ignore type bits, force attribute bit
      s.scope & l.LEVEL | r
    ) : l.ATTRIBUTE, s.whitelist != null && (this.whitelist = s.whitelist);
  }
  static keys(t) {
    return Array.from(t.attributes).map((e) => e.name);
  }
  add(t, e) {
    return this.canAdd(t, e) ? (t.setAttribute(this.keyName, e), !0) : !1;
  }
  canAdd(t, e) {
    return this.whitelist == null ? !0 : typeof e == "string" ? this.whitelist.indexOf(e.replace(/["']/g, "")) > -1 : this.whitelist.indexOf(e) > -1;
  }
  remove(t) {
    t.removeAttribute(this.keyName);
  }
  value(t) {
    const e = t.getAttribute(this.keyName);
    return this.canAdd(t, e) && e ? e : "";
  }
}
class f extends Error {
  constructor(t) {
    t = "[Parchment] " + t, super(t), this.message = t, this.name = this.constructor.name;
  }
}
const k = class v {
  constructor() {
    this.attributes = {}, this.classes = {}, this.tags = {}, this.types = {};
  }
  static find(t, e = !1) {
    if (t == null)
      return null;
    if (this.blots.has(t))
      return this.blots.get(t) || null;
    if (e) {
      let s = null;
      try {
        s = t.parentNode;
      } catch {
        return null;
      }
      return this.find(s, e);
    }
    return null;
  }
  create(t, e, s) {
    const r = this.query(e);
    if (r == null)
      throw new f(`Unable to create ${e} blot`);
    const i = r, a = (
      // @ts-expect-error Fix me later
      e instanceof Node || e.nodeType === Node.TEXT_NODE ? e : i.create(s)
    ), h = new i(t, a, s);
    return v.blots.set(h.domNode, h), h;
  }
  find(t, e = !1) {
    return v.find(t, e);
  }
  query(t, e = l.ANY) {
    let s;
    return typeof t == "string" ? s = this.types[t] || this.attributes[t] : t instanceof Text || t.nodeType === Node.TEXT_NODE ? s = this.types.text : typeof t == "number" ? t & l.LEVEL & l.BLOCK ? s = this.types.block : t & l.LEVEL & l.INLINE && (s = this.types.inline) : t instanceof Element && ((t.getAttribute("class") || "").split(/\s+/).some((r) => (s = this.classes[r], !!s)), s = s || this.tags[t.tagName]), s == null ? null : "scope" in s && e & l.LEVEL & s.scope && e & l.TYPE & s.scope ? s : null;
  }
  register(...t) {
    return t.map((e) => {
      const s = "blotName" in e, r = "attrName" in e;
      if (!s && !r)
        throw new f("Invalid definition");
      if (s && e.blotName === "abstract")
        throw new f("Cannot register abstract class");
      const i = s ? e.blotName : r ? e.attrName : void 0;
      return this.types[i] = e, r ? typeof e.keyName == "string" && (this.attributes[e.keyName] = e) : s && (e.className && (this.classes[e.className] = e), e.tagName && (Array.isArray(e.tagName) ? e.tagName = e.tagName.map((a) => a.toUpperCase()) : e.tagName = e.tagName.toUpperCase(), (Array.isArray(e.tagName) ? e.tagName : [e.tagName]).forEach((a) => {
        (this.tags[a] == null || e.className == null) && (this.tags[a] = e);
      }))), e;
    });
  }
};
k.blots = /* @__PURE__ */ new WeakMap();
let C = k;
function I(o, t) {
  return (o.getAttribute("class") || "").split(/\s+/).filter((e) => e.indexOf(`${t}-`) === 0);
}
class W extends A {
  static keys(t) {
    return (t.getAttribute("class") || "").split(/\s+/).map((e) => e.split("-").slice(0, -1).join("-"));
  }
  add(t, e) {
    return this.canAdd(t, e) ? (this.remove(t), t.classList.add(`${this.keyName}-${e}`), !0) : !1;
  }
  remove(t) {
    I(t, this.keyName).forEach((e) => {
      t.classList.remove(e);
    }), t.classList.length === 0 && t.removeAttribute("class");
  }
  value(t) {
    const e = (I(t, this.keyName)[0] || "").slice(this.keyName.length + 1);
    return this.canAdd(t, e) ? e : "";
  }
}
const K = W;
function E(o) {
  const t = o.split("-"), e = t.slice(1).map((s) => s[0].toUpperCase() + s.slice(1)).join("");
  return t[0] + e;
}
class Y extends A {
  static keys(t) {
    return (t.getAttribute("style") || "").split(";").map((e) => e.split(":")[0].trim());
  }
  add(t, e) {
    return this.canAdd(t, e) ? (t.style[E(this.keyName)] = e, !0) : !1;
  }
  remove(t) {
    t.style[E(this.keyName)] = "", t.getAttribute("style") || t.removeAttribute("style");
  }
  value(t) {
    const e = t.style[E(this.keyName)];
    return this.canAdd(t, e) ? e : "";
  }
}
const q = Y;
class V {
  constructor(t) {
    this.attributes = {}, this.domNode = t, this.build();
  }
  attribute(t, e) {
    e ? t.add(this.domNode, e) && (t.value(this.domNode) != null ? this.attributes[t.attrName] = t : delete this.attributes[t.attrName]) : (t.remove(this.domNode), delete this.attributes[t.attrName]);
  }
  build() {
    this.attributes = {};
    const t = C.find(this.domNode);
    if (t == null)
      return;
    const e = A.keys(this.domNode), s = K.keys(this.domNode), r = q.keys(this.domNode);
    e.concat(s).concat(r).forEach((i) => {
      const a = t.scroll.query(i, l.ATTRIBUTE);
      a instanceof A && (this.attributes[a.attrName] = a);
    });
  }
  copy(t) {
    Object.keys(this.attributes).forEach((e) => {
      const s = this.attributes[e].value(this.domNode);
      t.format(e, s);
    });
  }
  move(t) {
    this.copy(t), Object.keys(this.attributes).forEach((e) => {
      this.attributes[e].remove(this.domNode);
    }), this.attributes = {};
  }
  values() {
    return Object.keys(this.attributes).reduce(
      (t, e) => (t[e] = this.attributes[e].value(this.domNode), t),
      {}
    );
  }
}
const _ = V, M = class {
  constructor(t, e) {
    this.scroll = t, this.domNode = e, C.blots.set(e, this), this.prev = null, this.next = null;
  }
  static create(t) {
    if (this.tagName == null)
      throw new f("Blot definition missing tagName");
    let e, s;
    return Array.isArray(this.tagName) ? (typeof t == "string" ? (s = t.toUpperCase(), parseInt(s, 10).toString() === s && (s = parseInt(s, 10))) : typeof t == "number" && (s = t), typeof s == "number" ? e = document.createElement(this.tagName[s - 1]) : s && this.tagName.indexOf(s) > -1 ? e = document.createElement(s) : e = document.createElement(this.tagName[0])) : e = document.createElement(this.tagName), this.className && e.classList.add(this.className), e;
  }
  // Hack for accessing inherited static methods
  get statics() {
    return this.constructor;
  }
  attach() {
  }
  clone() {
    const t = this.domNode.cloneNode(!1);
    return this.scroll.create(t);
  }
  detach() {
    this.parent != null && this.parent.removeChild(this), C.blots.delete(this.domNode);
  }
  deleteAt(t, e) {
    this.isolate(t, e).remove();
  }
  formatAt(t, e, s, r) {
    const i = this.isolate(t, e);
    if (this.scroll.query(s, l.BLOT) != null && r)
      i.wrap(s, r);
    else if (this.scroll.query(s, l.ATTRIBUTE) != null) {
      const a = this.scroll.create(this.statics.scope);
      i.wrap(a), a.format(s, r);
    }
  }
  insertAt(t, e, s) {
    const r = s == null ? this.scroll.create("text", e) : this.scroll.create(e, s), i = this.split(t);
    this.parent.insertBefore(r, i || void 0);
  }
  isolate(t, e) {
    const s = this.split(t);
    if (s == null)
      throw new Error("Attempt to isolate at end");
    return s.split(e), s;
  }
  length() {
    return 1;
  }
  offset(t = this.parent) {
    return this.parent == null || this === t ? 0 : this.parent.children.offset(this) + this.parent.offset(t);
  }
  optimize(t) {
    this.statics.requiredContainer && !(this.parent instanceof this.statics.requiredContainer) && this.wrap(this.statics.requiredContainer.blotName);
  }
  remove() {
    this.domNode.parentNode != null && this.domNode.parentNode.removeChild(this.domNode), this.detach();
  }
  replaceWith(t, e) {
    const s = typeof t == "string" ? this.scroll.create(t, e) : t;
    return this.parent != null && (this.parent.insertBefore(s, this.next || void 0), this.remove()), s;
  }
  split(t, e) {
    return t === 0 ? this : this.next;
  }
  update(t, e) {
  }
  wrap(t, e) {
    const s = typeof t == "string" ? this.scroll.create(t, e) : t;
    if (this.parent != null && this.parent.insertBefore(s, this.next || void 0), typeof s.appendChild != "function")
      throw new f(`Cannot wrap ${t}`);
    return s.appendChild(this), s;
  }
};
M.blotName = "abstract";
let P = M;
const R = class extends P {
  /**
   * Returns the value represented by domNode if it is this Blot's type
   * No checking that domNode can represent this Blot type is required so
   * applications needing it should check externally before calling.
   */
  static value(t) {
    return !0;
  }
  /**
   * Given location represented by node and offset from DOM Selection Range,
   * return index to that location.
   */
  index(t, e) {
    return this.domNode === t || this.domNode.compareDocumentPosition(t) & Node.DOCUMENT_POSITION_CONTAINED_BY ? Math.min(e, 1) : -1;
  }
  /**
   * Given index to location within blot, return node and offset representing
   * that location, consumable by DOM Selection Range
   */
  position(t, e) {
    let s = Array.from(this.parent.domNode.childNodes).indexOf(this.domNode);
    return t > 0 && (s += 1), [this.parent.domNode, s];
  }
  /**
   * Return value represented by this blot
   * Should not change without interaction from API or
   * user change detectable by update()
   */
  value() {
    return {
      [this.statics.blotName]: this.statics.value(this.domNode) || !0
    };
  }
};
R.scope = l.INLINE_BLOT;
let j = R;
const B = j;
class S {
  constructor() {
    this.head = null, this.tail = null, this.length = 0;
  }
  append(...t) {
    if (this.insertBefore(t[0], null), t.length > 1) {
      const e = t.slice(1);
      this.append(...e);
    }
  }
  at(t) {
    const e = this.iterator();
    let s = e();
    for (; s && t > 0; )
      t -= 1, s = e();
    return s;
  }
  contains(t) {
    const e = this.iterator();
    let s = e();
    for (; s; ) {
      if (s === t)
        return !0;
      s = e();
    }
    return !1;
  }
  indexOf(t) {
    const e = this.iterator();
    let s = e(), r = 0;
    for (; s; ) {
      if (s === t)
        return r;
      r += 1, s = e();
    }
    return -1;
  }
  insertBefore(t, e) {
    t != null && (this.remove(t), t.next = e, e != null ? (t.prev = e.prev, e.prev != null && (e.prev.next = t), e.prev = t, e === this.head && (this.head = t)) : this.tail != null ? (this.tail.next = t, t.prev = this.tail, this.tail = t) : (t.prev = null, this.head = this.tail = t), this.length += 1);
  }
  offset(t) {
    let e = 0, s = this.head;
    for (; s != null; ) {
      if (s === t)
        return e;
      e += s.length(), s = s.next;
    }
    return -1;
  }
  remove(t) {
    this.contains(t) && (t.prev != null && (t.prev.next = t.next), t.next != null && (t.next.prev = t.prev), t === this.head && (this.head = t.next), t === this.tail && (this.tail = t.prev), this.length -= 1);
  }
  iterator(t = this.head) {
    return () => {
      const e = t;
      return t != null && (t = t.next), e;
    };
  }
  find(t, e = !1) {
    const s = this.iterator();
    let r = s();
    for (; r; ) {
      const i = r.length();
      if (t < i || e && t === i && (r.next == null || r.next.length() !== 0))
        return [r, t];
      t -= i, r = s();
    }
    return [null, 0];
  }
  forEach(t) {
    const e = this.iterator();
    let s = e();
    for (; s; )
      t(s), s = e();
  }
  forEachAt(t, e, s) {
    if (e <= 0)
      return;
    const [r, i] = this.find(t);
    let a = t - i;
    const h = this.iterator(r);
    let n = h();
    for (; n && a < t + e; ) {
      const c = n.length();
      t > a ? s(
        n,
        t - a,
        Math.min(e, a + c - t)
      ) : s(n, 0, Math.min(c, t + e - a)), a += c, n = h();
    }
  }
  map(t) {
    return this.reduce((e, s) => (e.push(t(s)), e), []);
  }
  reduce(t, e) {
    const s = this.iterator();
    let r = s();
    for (; r; )
      e = t(e, r), r = s();
    return e;
  }
}
function O(o, t) {
  const e = t.find(o);
  if (e)
    return e;
  try {
    return t.create(o);
  } catch {
    const s = t.create(l.INLINE);
    return Array.from(o.childNodes).forEach((r) => {
      s.domNode.appendChild(r);
    }), o.parentNode && o.parentNode.replaceChild(s.domNode, o), s.attach(), s;
  }
}
const U = class u extends P {
  constructor(t, e) {
    super(t, e), this.uiNode = null, this.build();
  }
  appendChild(t) {
    this.insertBefore(t);
  }
  attach() {
    super.attach(), this.children.forEach((t) => {
      t.attach();
    });
  }
  attachUI(t) {
    this.uiNode != null && this.uiNode.remove(), this.uiNode = t, u.uiClass && this.uiNode.classList.add(u.uiClass), this.uiNode.setAttribute("contenteditable", "false"), this.domNode.insertBefore(this.uiNode, this.domNode.firstChild);
  }
  /**
   * Called during construction, should fill its own children LinkedList.
   */
  build() {
    this.children = new S(), Array.from(this.domNode.childNodes).filter((t) => t !== this.uiNode).reverse().forEach((t) => {
      try {
        const e = O(t, this.scroll);
        this.insertBefore(e, this.children.head || void 0);
      } catch (e) {
        if (e instanceof f)
          return;
        throw e;
      }
    });
  }
  deleteAt(t, e) {
    if (t === 0 && e === this.length())
      return this.remove();
    this.children.forEachAt(t, e, (s, r, i) => {
      s.deleteAt(r, i);
    });
  }
  descendant(t, e = 0) {
    const [s, r] = this.children.find(e);
    return t.blotName == null && t(s) || t.blotName != null && s instanceof t ? [s, r] : s instanceof u ? s.descendant(t, r) : [null, -1];
  }
  descendants(t, e = 0, s = Number.MAX_VALUE) {
    let r = [], i = s;
    return this.children.forEachAt(
      e,
      s,
      (a, h, n) => {
        (t.blotName == null && t(a) || t.blotName != null && a instanceof t) && r.push(a), a instanceof u && (r = r.concat(
          a.descendants(t, h, i)
        )), i -= n;
      }
    ), r;
  }
  detach() {
    this.children.forEach((t) => {
      t.detach();
    }), super.detach();
  }
  enforceAllowedChildren() {
    let t = !1;
    this.children.forEach((e) => {
      t || this.statics.allowedChildren.some(
        (s) => e instanceof s
      ) || (e.statics.scope === l.BLOCK_BLOT ? (e.next != null && this.splitAfter(e), e.prev != null && this.splitAfter(e.prev), e.parent.unwrap(), t = !0) : e instanceof u ? e.unwrap() : e.remove());
    });
  }
  formatAt(t, e, s, r) {
    this.children.forEachAt(t, e, (i, a, h) => {
      i.formatAt(a, h, s, r);
    });
  }
  insertAt(t, e, s) {
    const [r, i] = this.children.find(t);
    if (r)
      r.insertAt(i, e, s);
    else {
      const a = s == null ? this.scroll.create("text", e) : this.scroll.create(e, s);
      this.appendChild(a);
    }
  }
  insertBefore(t, e) {
    t.parent != null && t.parent.children.remove(t);
    let s = null;
    this.children.insertBefore(t, e || null), t.parent = this, e != null && (s = e.domNode), (this.domNode.parentNode !== t.domNode || this.domNode.nextSibling !== s) && this.domNode.insertBefore(t.domNode, s), t.attach();
  }
  length() {
    return this.children.reduce((t, e) => t + e.length(), 0);
  }
  moveChildren(t, e) {
    this.children.forEach((s) => {
      t.insertBefore(s, e);
    });
  }
  optimize(t) {
    if (super.optimize(t), this.enforceAllowedChildren(), this.uiNode != null && this.uiNode !== this.domNode.firstChild && this.domNode.insertBefore(this.uiNode, this.domNode.firstChild), this.children.length === 0)
      if (this.statics.defaultChild != null) {
        const e = this.scroll.create(this.statics.defaultChild.blotName);
        this.appendChild(e);
      } else
        this.remove();
  }
  path(t, e = !1) {
    const [s, r] = this.children.find(t, e), i = [[this, t]];
    return s instanceof u ? i.concat(s.path(r, e)) : (s != null && i.push([s, r]), i);
  }
  removeChild(t) {
    this.children.remove(t);
  }
  replaceWith(t, e) {
    const s = typeof t == "string" ? this.scroll.create(t, e) : t;
    return s instanceof u && this.moveChildren(s), super.replaceWith(s);
  }
  split(t, e = !1) {
    if (!e) {
      if (t === 0)
        return this;
      if (t === this.length())
        return this.next;
    }
    const s = this.clone();
    return this.parent && this.parent.insertBefore(s, this.next || void 0), this.children.forEachAt(t, this.length(), (r, i, a) => {
      const h = r.split(i, e);
      h != null && s.appendChild(h);
    }), s;
  }
  splitAfter(t) {
    const e = this.clone();
    for (; t.next != null; )
      e.appendChild(t.next);
    return this.parent && this.parent.insertBefore(e, this.next || void 0), e;
  }
  unwrap() {
    this.parent && this.moveChildren(this.parent, this.next || void 0), this.remove();
  }
  update(t, e) {
    const s = [], r = [];
    t.forEach((i) => {
      i.target === this.domNode && i.type === "childList" && (s.push(...i.addedNodes), r.push(...i.removedNodes));
    }), r.forEach((i) => {
      if (i.parentNode != null && // @ts-expect-error Fix me later
      i.tagName !== "IFRAME" && document.body.compareDocumentPosition(i) & Node.DOCUMENT_POSITION_CONTAINED_BY)
        return;
      const a = this.scroll.find(i);
      a != null && (a.domNode.parentNode == null || a.domNode.parentNode === this.domNode) && a.detach();
    }), s.filter((i) => i.parentNode === this.domNode && i !== this.uiNode).sort((i, a) => i === a ? 0 : i.compareDocumentPosition(a) & Node.DOCUMENT_POSITION_FOLLOWING ? 1 : -1).forEach((i) => {
      let a = null;
      i.nextSibling != null && (a = this.scroll.find(i.nextSibling));
      const h = O(i, this.scroll);
      (h.next !== a || h.next == null) && (h.parent != null && h.parent.removeChild(this), this.insertBefore(h, a || void 0));
    }), this.enforceAllowedChildren();
  }
};
U.uiClass = "";
let X = U;
const b = X;
function F(o, t) {
  if (Object.keys(o).length !== Object.keys(t).length)
    return !1;
  for (const e in o)
    if (o[e] !== t[e])
      return !1;
  return !0;
}
const d = class m extends b {
  static create(t) {
    return super.create(t);
  }
  static formats(t, e) {
    const s = e.query(m.blotName);
    if (!(s != null && t.tagName === s.tagName)) {
      if (typeof this.tagName == "string")
        return !0;
      if (Array.isArray(this.tagName))
        return t.tagName.toLowerCase();
    }
  }
  constructor(t, e) {
    super(t, e), this.attributes = new _(this.domNode);
  }
  format(t, e) {
    if (t === this.statics.blotName && !e)
      this.children.forEach((s) => {
        s instanceof m || (s = s.wrap(m.blotName, !0)), this.attributes.copy(s);
      }), this.unwrap();
    else {
      const s = this.scroll.query(t, l.INLINE);
      if (s == null)
        return;
      s instanceof A ? this.attributes.attribute(s, e) : e && (t !== this.statics.blotName || this.formats()[t] !== e) && this.replaceWith(t, e);
    }
  }
  formats() {
    const t = this.attributes.values(), e = this.statics.formats(this.domNode, this.scroll);
    return e != null && (t[this.statics.blotName] = e), t;
  }
  formatAt(t, e, s, r) {
    this.formats()[s] != null || this.scroll.query(s, l.ATTRIBUTE) ? this.isolate(t, e).format(s, r) : super.formatAt(t, e, s, r);
  }
  optimize(t) {
    super.optimize(t);
    const e = this.formats();
    if (Object.keys(e).length === 0)
      return this.unwrap();
    const s = this.next;
    s instanceof m && s.prev === this && F(e, s.formats()) && (s.moveChildren(this), s.remove());
  }
  replaceWith(t, e) {
    const s = super.replaceWith(t, e);
    return this.attributes.copy(s), s;
  }
  update(t, e) {
    super.update(t, e), t.some(
      (s) => s.target === this.domNode && s.type === "attributes"
    ) && this.attributes.build();
  }
  wrap(t, e) {
    const s = super.wrap(t, e);
    return s instanceof m && this.attributes.move(s), s;
  }
};
d.allowedChildren = [d, B], d.blotName = "inline", d.scope = l.INLINE_BLOT, d.tagName = "SPAN";
let G = d;
const Z = G, p = class T extends b {
  static create(t) {
    return super.create(t);
  }
  static formats(t, e) {
    const s = e.query(T.blotName);
    if (!(s != null && t.tagName === s.tagName)) {
      if (typeof this.tagName == "string")
        return !0;
      if (Array.isArray(this.tagName))
        return t.tagName.toLowerCase();
    }
  }
  constructor(t, e) {
    super(t, e), this.attributes = new _(this.domNode);
  }
  format(t, e) {
    const s = this.scroll.query(t, l.BLOCK);
    s != null && (s instanceof A ? this.attributes.attribute(s, e) : t === this.statics.blotName && !e ? this.replaceWith(T.blotName) : e && (t !== this.statics.blotName || this.formats()[t] !== e) && this.replaceWith(t, e));
  }
  formats() {
    const t = this.attributes.values(), e = this.statics.formats(this.domNode, this.scroll);
    return e != null && (t[this.statics.blotName] = e), t;
  }
  formatAt(t, e, s, r) {
    this.scroll.query(s, l.BLOCK) != null ? this.format(s, r) : super.formatAt(t, e, s, r);
  }
  insertAt(t, e, s) {
    if (s == null || this.scroll.query(e, l.INLINE) != null)
      super.insertAt(t, e, s);
    else {
      const r = this.split(t);
      if (r != null) {
        const i = this.scroll.create(e, s);
        r.parent.insertBefore(i, r);
      } else
        throw new Error("Attempt to insertAt after block boundaries");
    }
  }
  replaceWith(t, e) {
    const s = super.replaceWith(t, e);
    return this.attributes.copy(s), s;
  }
  update(t, e) {
    super.update(t, e), t.some(
      (s) => s.target === this.domNode && s.type === "attributes"
    ) && this.attributes.build();
  }
};
p.blotName = "block", p.scope = l.BLOCK_BLOT, p.tagName = "P", p.allowedChildren = [
  Z,
  p,
  B
];
let H = p;
const w = H, x = class extends b {
  checkMerge() {
    return this.next !== null && this.next.statics.blotName === this.statics.blotName;
  }
  deleteAt(t, e) {
    super.deleteAt(t, e), this.enforceAllowedChildren();
  }
  formatAt(t, e, s, r) {
    super.formatAt(t, e, s, r), this.enforceAllowedChildren();
  }
  insertAt(t, e, s) {
    super.insertAt(t, e, s), this.enforceAllowedChildren();
  }
  optimize(t) {
    super.optimize(t), this.children.length > 0 && this.next != null && this.checkMerge() && (this.next.moveChildren(this), this.next.remove());
  }
};
x.blotName = "container", x.scope = l.BLOCK_BLOT;
let J = x;
const Q = J;
class tt extends B {
  static formats(t, e) {
  }
  format(t, e) {
    super.formatAt(0, this.length(), t, e);
  }
  formatAt(t, e, s, r) {
    t === 0 && e === this.length() ? this.format(s, r) : super.formatAt(t, e, s, r);
  }
  formats() {
    return this.statics.formats(this.domNode, this.scroll);
  }
}
const ht = tt, et = {
  attributes: !0,
  characterData: !0,
  characterDataOldValue: !0,
  childList: !0,
  subtree: !0
}, st = 100, N = class extends b {
  constructor(t, e) {
    super(null, e), this.registry = t, this.scroll = this, this.build(), this.observer = new MutationObserver((s) => {
      this.update(s);
    }), this.observer.observe(this.domNode, et), this.attach();
  }
  create(t, e) {
    return this.registry.create(this, t, e);
  }
  find(t, e = !1) {
    const s = this.registry.find(t, e);
    return s ? s.scroll === this ? s : e ? this.find(s.scroll.domNode.parentNode, !0) : null : null;
  }
  query(t, e = l.ANY) {
    return this.registry.query(t, e);
  }
  register(...t) {
    return this.registry.register(...t);
  }
  build() {
    this.scroll != null && super.build();
  }
  detach() {
    super.detach(), this.observer.disconnect();
  }
  deleteAt(t, e) {
    this.update(), t === 0 && e === this.length() ? this.children.forEach((s) => {
      s.remove();
    }) : super.deleteAt(t, e);
  }
  formatAt(t, e, s, r) {
    this.update(), super.formatAt(t, e, s, r);
  }
  insertAt(t, e, s) {
    this.update(), super.insertAt(t, e, s);
  }
  optimize(t = [], e = {}) {
    super.optimize(e);
    const s = e.mutationsMap || /* @__PURE__ */ new WeakMap();
    let r = Array.from(this.observer.takeRecords());
    for (; r.length > 0; )
      t.push(r.pop());
    const i = (n, c = !0) => {
      n == null || n === this || n.domNode.parentNode != null && (s.has(n.domNode) || s.set(n.domNode, []), c && i(n.parent));
    }, a = (n) => {
      s.has(n.domNode) && (n instanceof b && n.children.forEach(a), s.delete(n.domNode), n.optimize(e));
    };
    let h = t;
    for (let n = 0; h.length > 0; n += 1) {
      if (n >= st)
        throw new Error("[Parchment] Maximum optimize iterations reached");
      for (h.forEach((c) => {
        const g = this.find(c.target, !0);
        g != null && (g.domNode === c.target && (c.type === "childList" ? (i(this.find(c.previousSibling, !1)), Array.from(c.addedNodes).forEach(($) => {
          const y = this.find($, !1);
          i(y, !1), y instanceof b && y.children.forEach((D) => {
            i(D, !1);
          });
        })) : c.type === "attributes" && i(g.prev)), i(g));
      }), this.children.forEach(a), h = Array.from(this.observer.takeRecords()), r = h.slice(); r.length > 0; )
        t.push(r.pop());
    }
  }
  update(t, e = {}) {
    t = t || this.observer.takeRecords();
    const s = /* @__PURE__ */ new WeakMap();
    t.map((r) => {
      const i = this.find(r.target, !0);
      return i == null ? null : s.has(i.domNode) ? (s.get(i.domNode).push(r), null) : (s.set(i.domNode, [r]), i);
    }).forEach((r) => {
      r != null && r !== this && s.has(r.domNode) && r.update(s.get(r.domNode) || [], e);
    }), e.mutationsMap = s, s.has(this.domNode) && super.update(s.get(this.domNode), e), this.optimize(t, e);
  }
};
N.blotName = "scroll", N.defaultChild = w, N.allowedChildren = [w, Q], N.scope = l.BLOCK_BLOT, N.tagName = "DIV";
let rt = N;
const ct = rt, L = class z extends B {
  static create(t) {
    return document.createTextNode(t);
  }
  static value(t) {
    return t.data;
  }
  constructor(t, e) {
    super(t, e), this.text = this.statics.value(this.domNode);
  }
  deleteAt(t, e) {
    this.domNode.data = this.text = this.text.slice(0, t) + this.text.slice(t + e);
  }
  index(t, e) {
    return this.domNode === t ? e : -1;
  }
  insertAt(t, e, s) {
    s == null ? (this.text = this.text.slice(0, t) + e + this.text.slice(t), this.domNode.data = this.text) : super.insertAt(t, e, s);
  }
  length() {
    return this.text.length;
  }
  optimize(t) {
    super.optimize(t), this.text = this.statics.value(this.domNode), this.text.length === 0 ? this.remove() : this.next instanceof z && this.next.prev === this && (this.insertAt(this.length(), this.next.value()), this.next.remove());
  }
  position(t, e = !1) {
    return [this.domNode, t];
  }
  split(t, e = !1) {
    if (!e) {
      if (t === 0)
        return this;
      if (t === this.length())
        return this.next;
    }
    const s = this.scroll.create(this.domNode.splitText(t));
    return this.parent.insertBefore(s, this.next || void 0), this.text = this.statics.value(this.domNode), s;
  }
  update(t, e) {
    t.some((s) => s.type === "characterData" && s.target === this.domNode) && (this.text = this.statics.value(this.domNode));
  }
  value() {
    return this.text;
  }
};
L.blotName = "text", L.scope = l.INLINE_BLOT;
let it = L;
const ut = it;
export {
  A as Attributor,
  _ as AttributorStore,
  w as BlockBlot,
  K as ClassAttributor,
  Q as ContainerBlot,
  ht as EmbedBlot,
  Z as InlineBlot,
  B as LeafBlot,
  b as ParentBlot,
  C as Registry,
  l as Scope,
  ct as ScrollBlot,
  q as StyleAttributor,
  ut as TextBlot
};
