var v = Object.defineProperty;
var D = (c, t, s) => t in c ? v(c, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : c[t] = s;
var u = (c, t, s) => (D(c, typeof t != "symbol" ? t + "" : t, s), s);
import { ScrollBlot as T, Scope as d, LeafBlot as A, ContainerBlot as _ } from "../../../../parchment@3.0.0/node_modules/parchment/dist/parchment.mjs";
import p, { D as E } from "../../../../quill-delta@5.1.0/node_modules/quill-delta/dist/Delta.mjs";
import f from "../core/emitter.mjs";
import k, { BlockEmbed as b, bubbleFormats as B } from "./block.mjs";
import R from "./break.mjs";
import U from "./container.mjs";
function O(c) {
  return c instanceof k || c instanceof b;
}
function N(c) {
  return typeof c.updateContent == "function";
}
class g extends T {
  constructor(t, s, e) {
    let {
      emitter: i
    } = e;
    super(t, s), this.emitter = i, this.batch = !1, this.optimize(), this.enable(), this.domNode.addEventListener("dragstart", (n) => this.handleDragStart(n));
  }
  batchStart() {
    Array.isArray(this.batch) || (this.batch = []);
  }
  batchEnd() {
    if (!this.batch)
      return;
    const t = this.batch;
    this.batch = !1, this.update(t);
  }
  emitMount(t) {
    this.emitter.emit(f.events.SCROLL_BLOT_MOUNT, t);
  }
  emitUnmount(t) {
    this.emitter.emit(f.events.SCROLL_BLOT_UNMOUNT, t);
  }
  emitEmbedUpdate(t, s) {
    this.emitter.emit(f.events.SCROLL_EMBED_UPDATE, t, s);
  }
  deleteAt(t, s) {
    const [e, i] = this.line(t), [n] = this.line(t + s);
    if (super.deleteAt(t, s), n != null && e !== n && i > 0) {
      if (e instanceof b || n instanceof b) {
        this.optimize();
        return;
      }
      const r = n.children.head instanceof R ? null : n.children.head;
      e.moveChildren(n, r), e.remove();
    }
    this.optimize();
  }
  enable() {
    let t = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : !0;
    this.domNode.setAttribute("contenteditable", t ? "true" : "false");
  }
  formatAt(t, s, e, i) {
    super.formatAt(t, s, e, i), this.optimize();
  }
  insertAt(t, s, e) {
    if (t >= this.length())
      if (e == null || this.scroll.query(s, d.BLOCK) == null) {
        const i = this.scroll.create(this.statics.defaultChild.blotName);
        this.appendChild(i), e == null && s.endsWith(`
`) ? i.insertAt(0, s.slice(0, -1), e) : i.insertAt(0, s, e);
      } else {
        const i = this.scroll.create(s, e);
        this.appendChild(i);
      }
    else
      super.insertAt(t, s, e);
    this.optimize();
  }
  insertBefore(t, s) {
    if (t.statics.scope === d.INLINE_BLOT) {
      const e = this.scroll.create(this.statics.defaultChild.blotName);
      e.appendChild(t), super.insertBefore(e, s);
    } else
      super.insertBefore(t, s);
  }
  insertContents(t, s) {
    const e = this.deltaToRenderBlocks(s.concat(new p().insert(`
`))), i = e.pop();
    if (i == null)
      return;
    this.batchStart();
    const n = e.shift();
    if (n) {
      const a = n.type === "block" && (n.delta.length() === 0 || !this.descendant(b, t)[0] && t < this.length()), o = n.type === "block" ? n.delta : new p().insert({
        [n.key]: n.value
      });
      y(this, t, o);
      const h = n.type === "block" ? 1 : 0, m = t + o.length() + h;
      a && this.insertAt(m - 1, `
`);
      const I = B(this.line(t)[0]), L = E.AttributeMap.diff(I, n.attributes) || {};
      Object.keys(L).forEach((C) => {
        this.formatAt(m - 1, 1, C, L[C]);
      }), t = m;
    }
    let [r, l] = this.children.find(t);
    if (e.length && (r && (r = r.split(l), l = 0), e.forEach((a) => {
      if (a.type === "block") {
        const o = this.createBlock(a.attributes, r || void 0);
        y(o, 0, a.delta);
      } else {
        const o = this.create(a.key, a.value);
        this.insertBefore(o, r || void 0), Object.keys(a.attributes).forEach((h) => {
          o.format(h, a.attributes[h]);
        });
      }
    })), i.type === "block" && i.delta.length()) {
      const a = r ? r.offset(r.scroll) + l : this.length();
      y(this, a, i.delta);
    }
    this.batchEnd(), this.optimize();
  }
  isEnabled() {
    return this.domNode.getAttribute("contenteditable") === "true";
  }
  leaf(t) {
    const s = this.path(t).pop();
    if (!s)
      return [null, -1];
    const [e, i] = s;
    return e instanceof A ? [e, i] : [null, -1];
  }
  line(t) {
    return t === this.length() ? this.line(t - 1) : this.descendant(O, t);
  }
  lines() {
    let t = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : 0, s = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : Number.MAX_VALUE;
    const e = (i, n, r) => {
      let l = [], a = r;
      return i.children.forEachAt(n, r, (o, h, m) => {
        O(o) ? l.push(o) : o instanceof _ && (l = l.concat(e(o, h, a))), a -= m;
      }), l;
    };
    return e(this, t, s);
  }
  optimize() {
    let t = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : [], s = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
    this.batch || (super.optimize(t, s), t.length > 0 && this.emitter.emit(f.events.SCROLL_OPTIMIZE, t, s));
  }
  path(t) {
    return super.path(t).slice(1);
  }
  remove() {
  }
  update(t) {
    if (this.batch) {
      Array.isArray(t) && (this.batch = this.batch.concat(t));
      return;
    }
    let s = f.sources.USER;
    typeof t == "string" && (s = t), Array.isArray(t) || (t = this.observer.takeRecords()), t = t.filter((e) => {
      let {
        target: i
      } = e;
      const n = this.find(i, !0);
      return n && !N(n);
    }), t.length > 0 && this.emitter.emit(f.events.SCROLL_BEFORE_UPDATE, s, t), super.update(t.concat([])), t.length > 0 && this.emitter.emit(f.events.SCROLL_UPDATE, s, t);
  }
  updateEmbedAt(t, s, e) {
    const [i] = this.descendant((n) => n instanceof b, t);
    i && i.statics.blotName === s && N(i) && i.updateContent(e);
  }
  handleDragStart(t) {
    t.preventDefault();
  }
  deltaToRenderBlocks(t) {
    const s = [];
    let e = new p();
    return t.forEach((i) => {
      const n = i == null ? void 0 : i.insert;
      if (n)
        if (typeof n == "string") {
          const r = n.split(`
`);
          r.slice(0, -1).forEach((a) => {
            e.insert(a, i.attributes), s.push({
              type: "block",
              delta: e,
              attributes: i.attributes ?? {}
            }), e = new p();
          });
          const l = r[r.length - 1];
          l && e.insert(l, i.attributes);
        } else {
          const r = Object.keys(n)[0];
          if (!r)
            return;
          this.query(r, d.INLINE) ? e.push(i) : (e.length() && s.push({
            type: "block",
            delta: e,
            attributes: {}
          }), e = new p(), s.push({
            type: "blockEmbed",
            key: r,
            value: n[r],
            attributes: i.attributes ?? {}
          }));
        }
    }), e.length() && s.push({
      type: "block",
      delta: e,
      attributes: {}
    }), s;
  }
  createBlock(t, s) {
    let e;
    const i = {};
    Object.entries(t).forEach((l) => {
      let [a, o] = l;
      this.query(a, d.BLOCK & d.BLOT) != null ? e = a : i[a] = o;
    });
    const n = this.create(e || this.statics.defaultChild.blotName, e ? t[e] : void 0);
    this.insertBefore(n, s || void 0);
    const r = n.length();
    return Object.entries(i).forEach((l) => {
      let [a, o] = l;
      n.formatAt(0, r, a, o);
    }), n;
  }
}
u(g, "blotName", "scroll"), u(g, "className", "ql-editor"), u(g, "tagName", "DIV"), u(g, "defaultChild", k), u(g, "allowedChildren", [k, b, U]);
function y(c, t, s) {
  s.reduce((e, i) => {
    const n = E.Op.length(i);
    let r = i.attributes || {};
    if (i.insert != null) {
      if (typeof i.insert == "string") {
        const l = i.insert;
        c.insertAt(e, l);
        const [a] = c.descendant(A, e), o = B(a);
        r = E.AttributeMap.diff(o, r) || {};
      } else if (typeof i.insert == "object") {
        const l = Object.keys(i.insert)[0];
        if (l == null)
          return e;
        if (c.insertAt(e, l, i.insert[l]), c.scroll.query(l, d.INLINE) != null) {
          const [o] = c.descendant(A, e), h = B(o);
          r = E.AttributeMap.diff(h, r) || {};
        }
      }
    }
    return Object.keys(r).forEach((l) => {
      c.formatAt(e, n, l, r[l]);
    }), e + n;
  }, t);
}
export {
  g as default
};
