import { LeafBlot as L, Scope as j, EmbedBlot as R, ParentBlot as z } from "../../../../parchment@3.0.0/node_modules/parchment/dist/parchment.mjs";
import h, { D as y } from "../../../../quill-delta@5.1.0/node_modules/quill-delta/dist/Delta.mjs";
import A, { BlockEmbed as B, bubbleFormats as E } from "../blots/block.mjs";
import F from "../blots/break.mjs";
import H from "../blots/cursor.mjs";
import C, { escapeText as I } from "../blots/text.mjs";
import { Range as S } from "./selection.mjs";
import O from "../../../../lodash-es@4.17.21/node_modules/lodash-es/merge.mjs";
import D from "../../../../lodash-es@4.17.21/node_modules/lodash-es/cloneDeep.mjs";
import P from "../../../../lodash-es@4.17.21/node_modules/lodash-es/isEqual.mjs";
const V = /^[ -~]*$/;
class tt {
  constructor(e) {
    this.scroll = e, this.delta = this.getDelta();
  }
  applyDelta(e) {
    this.scroll.update();
    let t = this.scroll.length();
    this.scroll.batchStart();
    const n = M(e), s = new h();
    return q(n.ops.slice()).reduce((r, i) => {
      const a = y.Op.length(i);
      let c = i.attributes || {}, f = !1, m = !1;
      if (i.insert != null) {
        if (s.retain(a), typeof i.insert == "string") {
          const u = i.insert;
          m = !u.endsWith(`
`) && (t <= r || !!this.scroll.descendant(B, r)[0]), this.scroll.insertAt(r, u);
          const [w, g] = this.scroll.line(r);
          let b = O({}, E(w));
          if (w instanceof A) {
            const [k] = w.descendant(L, g);
            k && (b = O(b, E(k)));
          }
          c = y.AttributeMap.diff(b, c) || {};
        } else if (typeof i.insert == "object") {
          const u = Object.keys(i.insert)[0];
          if (u == null)
            return r;
          const w = this.scroll.query(u, j.INLINE) != null;
          if (w)
            (t <= r || this.scroll.descendant(B, r)[0]) && (m = !0);
          else if (r > 0) {
            const [g, b] = this.scroll.descendant(L, r - 1);
            g instanceof C ? g.value()[b] !== `
` && (f = !0) : g instanceof R && g.statics.scope === j.INLINE_BLOT && (f = !0);
          }
          if (this.scroll.insertAt(r, u, i.insert[u]), w) {
            const [g] = this.scroll.descendant(L, r);
            if (g) {
              const b = O({}, E(g));
              c = y.AttributeMap.diff(b, c) || {};
            }
          }
        }
        t += a;
      } else if (s.push(i), i.retain !== null && typeof i.retain == "object") {
        const u = Object.keys(i.retain)[0];
        if (u == null)
          return r;
        this.scroll.updateEmbedAt(r, u, i.retain[u]);
      }
      Object.keys(c).forEach((u) => {
        this.scroll.formatAt(r, a, u, c[u]);
      });
      const p = f ? 1 : 0, d = m ? 1 : 0;
      return t += p + d, s.retain(p), s.delete(d), r + a + p + d;
    }, 0), s.reduce((r, i) => typeof i.delete == "number" ? (this.scroll.deleteAt(r, i.delete), r) : r + y.Op.length(i), 0), this.scroll.batchEnd(), this.scroll.optimize(), this.update(n);
  }
  deleteText(e, t) {
    return this.scroll.deleteAt(e, t), this.update(new h().retain(e).delete(t));
  }
  formatLine(e, t) {
    let n = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
    this.scroll.update(), Object.keys(n).forEach((o) => {
      this.scroll.lines(e, Math.max(t, 1)).forEach((r) => {
        r.format(o, n[o]);
      });
    }), this.scroll.optimize();
    const s = new h().retain(e).retain(t, D(n));
    return this.update(s);
  }
  formatText(e, t) {
    let n = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
    Object.keys(n).forEach((o) => {
      this.scroll.formatAt(e, t, o, n[o]);
    });
    const s = new h().retain(e).retain(t, D(n));
    return this.update(s);
  }
  getContents(e, t) {
    return this.delta.slice(e, e + t);
  }
  getDelta() {
    return this.scroll.lines().reduce((e, t) => e.concat(t.delta()), new h());
  }
  getFormat(e) {
    let t = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 0, n = [], s = [];
    t === 0 ? this.scroll.path(e).forEach((i) => {
      const [a] = i;
      a instanceof A ? n.push(a) : a instanceof L && s.push(a);
    }) : (n = this.scroll.lines(e, t), s = this.scroll.descendants(L, e, t));
    const [o, r] = [n, s].map((i) => {
      const a = i.shift();
      if (a == null)
        return {};
      let c = E(a);
      for (; Object.keys(c).length > 0; ) {
        const f = i.shift();
        if (f == null)
          return c;
        c = W(E(f), c);
      }
      return c;
    });
    return {
      ...o,
      ...r
    };
  }
  getHTML(e, t) {
    const [n, s] = this.scroll.line(e);
    if (n) {
      const o = n.length();
      return n.length() >= s + t && !(s === 0 && t === o) ? T(n, s, t, !0) : T(this.scroll, e, t, !0);
    }
    return "";
  }
  getText(e, t) {
    return this.getContents(e, t).filter((n) => typeof n.insert == "string").map((n) => n.insert).join("");
  }
  insertContents(e, t) {
    const n = M(t), s = new h().retain(e).concat(n);
    return this.scroll.insertContents(e, n), this.update(s);
  }
  insertEmbed(e, t, n) {
    return this.scroll.insertAt(e, t, n), this.update(new h().retain(e).insert({
      [t]: n
    }));
  }
  insertText(e, t) {
    let n = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
    return t = t.replace(/\r\n/g, `
`).replace(/\r/g, `
`), this.scroll.insertAt(e, t), Object.keys(n).forEach((s) => {
      this.scroll.formatAt(e, t.length, s, n[s]);
    }), this.update(new h().retain(e).insert(t, D(n)));
  }
  isBlank() {
    if (this.scroll.children.length === 0)
      return !0;
    if (this.scroll.children.length > 1)
      return !1;
    const e = this.scroll.children.head;
    if ((e == null ? void 0 : e.statics.blotName) !== A.blotName)
      return !1;
    const t = e;
    return t.children.length > 1 ? !1 : t.children.head instanceof F;
  }
  removeFormat(e, t) {
    const n = this.getText(e, t), [s, o] = this.scroll.line(e + t);
    let r = 0, i = new h();
    s != null && (r = s.length() - o, i = s.delta().slice(o, o + r - 1).insert(`
`));
    const c = this.getContents(e, t + r).diff(new h().insert(n).concat(i)), f = new h().retain(e).concat(c);
    return this.applyDelta(f);
  }
  update(e) {
    let t = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [], n = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : void 0;
    const s = this.delta;
    if (t.length === 1 && t[0].type === "characterData" && // @ts-expect-error Fix me later
    t[0].target.data.match(V) && this.scroll.find(t[0].target)) {
      const o = this.scroll.find(t[0].target), r = E(o), i = o.offset(this.scroll), a = t[0].oldValue.replace(H.CONTENTS, ""), c = new h().insert(a), f = new h().insert(o.value()), m = n && {
        oldRange: v(n.oldRange, -i),
        newRange: v(n.newRange, -i)
      };
      e = new h().retain(i).concat(c.diff(f, m)).reduce((d, u) => u.insert ? d.insert(u.insert, r) : d.push(u), new h()), this.delta = s.compose(e);
    } else
      this.delta = this.getDelta(), (!e || !P(s.compose(e), this.delta)) && (e = s.diff(this.delta, n));
    return e;
  }
}
function $(l, e, t) {
  if (l.length === 0) {
    const [d] = N(t.pop());
    return e <= 0 ? `</li></${d}>` : `</li></${d}>${$([], e - 1, t)}`;
  }
  const [{
    child: n,
    offset: s,
    length: o,
    indent: r,
    type: i
  }, ...a] = l, [c, f] = N(i);
  if (r > e)
    return t.push(i), r === e + 1 ? `<${c}><li${f}>${T(n, s, o)}${$(a, r, t)}` : `<${c}><li>${$(l, e + 1, t)}`;
  const m = t[t.length - 1];
  if (r === e && i === m)
    return `</li><li${f}>${T(n, s, o)}${$(a, r, t)}`;
  const [p] = N(t.pop());
  return `</li></${p}>${$(l, e - 1, t)}`;
}
function T(l, e, t) {
  let n = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : !1;
  if ("html" in l && typeof l.html == "function")
    return l.html(e, t);
  if (l instanceof C)
    return I(l.value().slice(e, e + t));
  if (l instanceof z) {
    if (l.statics.blotName === "list-container") {
      const c = [];
      return l.children.forEachAt(e, t, (f, m, p) => {
        const d = "formats" in f && typeof f.formats == "function" ? f.formats() : {};
        c.push({
          child: f,
          offset: m,
          length: p,
          indent: d.indent || 0,
          type: d.list
        });
      }), $(c, -1, []);
    }
    const s = [];
    if (l.children.forEachAt(e, t, (c, f, m) => {
      s.push(T(c, f, m));
    }), n || l.statics.blotName === "list")
      return s.join("");
    const {
      outerHTML: o,
      innerHTML: r
    } = l.domNode, [i, a] = o.split(`>${r}<`);
    return i === "<table" ? `<table style="border: 1px solid #000;">${s.join("")}<${a}` : `${i}>${s.join("")}<${a}`;
  }
  return l.domNode instanceof Element ? l.domNode.outerHTML : "";
}
function W(l, e) {
  return Object.keys(e).reduce((t, n) => {
    if (l[n] == null)
      return t;
    const s = e[n];
    return s === l[n] ? t[n] = s : Array.isArray(s) ? s.indexOf(l[n]) < 0 ? t[n] = s.concat([l[n]]) : t[n] = s : t[n] = [s, l[n]], t;
  }, {});
}
function N(l) {
  const e = l === "ordered" ? "ol" : "ul";
  switch (l) {
    case "checked":
      return [e, ' data-list="checked"'];
    case "unchecked":
      return [e, ' data-list="unchecked"'];
    default:
      return [e, ""];
  }
}
function M(l) {
  return l.reduce((e, t) => {
    if (typeof t.insert == "string") {
      const n = t.insert.replace(/\r\n/g, `
`).replace(/\r/g, `
`);
      return e.insert(n, t.attributes);
    }
    return e.push(t);
  }, new h());
}
function v(l, e) {
  let {
    index: t,
    length: n
  } = l;
  return new S(t + e, n);
}
function q(l) {
  const e = [];
  return l.forEach((t) => {
    typeof t.insert == "string" ? t.insert.split(`
`).forEach((s, o) => {
      o && e.push({
        insert: `
`,
        attributes: t.attributes
      }), s && e.push({
        insert: s,
        attributes: t.attributes
      });
    }) : e.push(t);
  }), e;
}
export {
  tt as default
};
