import { getDefaultExportFromCjs as N } from "../../../../../../_virtual/_commonjsHelpers.mjs";
import { __module as E } from "../../../../../../_virtual/Delta.mjs";
import { d as v } from "../../../../fast-diff@1.3.0/node_modules/fast-diff/diff.mjs";
import { l as I } from "../../../../lodash.clonedeep@4.5.0/node_modules/lodash.clonedeep/index.mjs";
import { l as $ } from "../../../../lodash.isequal@4.5.0/node_modules/lodash.isequal/index.mjs";
import "./AttributeMap.mjs";
import "./Op.mjs";
import "./OpIterator.mjs";
import { __exports as C } from "../../../../../../_virtual/AttributeMap.mjs";
import { __exports as H } from "../../../../../../_virtual/Op.mjs";
import { __exports as D } from "../../../../../../_virtual/OpIterator.mjs";
(function(j, m) {
  Object.defineProperty(m, "__esModule", { value: !0 }), m.AttributeMap = m.OpIterator = m.Op = void 0;
  const k = v, O = I, w = $, y = C;
  m.AttributeMap = y.default;
  const d = H;
  m.Op = d.default;
  const p = D;
  m.OpIterator = p.default;
  const T = "\0", L = (g, t) => {
    if (typeof g != "object" || g === null)
      throw new Error(`cannot retain a ${typeof g}`);
    if (typeof t != "object" || t === null)
      throw new Error(`cannot retain a ${typeof t}`);
    const e = Object.keys(g)[0];
    if (!e || e !== Object.keys(t)[0])
      throw new Error(`embed types not matched: ${e} != ${Object.keys(t)[0]}`);
    return [e, g[e], t[e]];
  };
  class h {
    constructor(t) {
      Array.isArray(t) ? this.ops = t : t != null && Array.isArray(t.ops) ? this.ops = t.ops : this.ops = [];
    }
    static registerEmbed(t, e) {
      this.handlers[t] = e;
    }
    static unregisterEmbed(t) {
      delete this.handlers[t];
    }
    static getHandler(t) {
      const e = this.handlers[t];
      if (!e)
        throw new Error(`no handlers for embed type "${t}"`);
      return e;
    }
    insert(t, e) {
      const n = {};
      return typeof t == "string" && t.length === 0 ? this : (n.insert = t, e != null && typeof e == "object" && Object.keys(e).length > 0 && (n.attributes = e), this.push(n));
    }
    delete(t) {
      return t <= 0 ? this : this.push({ delete: t });
    }
    retain(t, e) {
      if (typeof t == "number" && t <= 0)
        return this;
      const n = { retain: t };
      return e != null && typeof e == "object" && Object.keys(e).length > 0 && (n.attributes = e), this.push(n);
    }
    push(t) {
      let e = this.ops.length, n = this.ops[e - 1];
      if (t = O(t), typeof n == "object") {
        if (typeof t.delete == "number" && typeof n.delete == "number")
          return this.ops[e - 1] = { delete: n.delete + t.delete }, this;
        if (typeof n.delete == "number" && t.insert != null && (e -= 1, n = this.ops[e - 1], typeof n != "object"))
          return this.ops.unshift(t), this;
        if (w(t.attributes, n.attributes)) {
          if (typeof t.insert == "string" && typeof n.insert == "string")
            return this.ops[e - 1] = { insert: n.insert + t.insert }, typeof t.attributes == "object" && (this.ops[e - 1].attributes = t.attributes), this;
          if (typeof t.retain == "number" && typeof n.retain == "number")
            return this.ops[e - 1] = { retain: n.retain + t.retain }, typeof t.attributes == "object" && (this.ops[e - 1].attributes = t.attributes), this;
        }
      }
      return e === this.ops.length ? this.ops.push(t) : this.ops.splice(e, 0, t), this;
    }
    chop() {
      const t = this.ops[this.ops.length - 1];
      return t && typeof t.retain == "number" && !t.attributes && this.ops.pop(), this;
    }
    filter(t) {
      return this.ops.filter(t);
    }
    forEach(t) {
      this.ops.forEach(t);
    }
    map(t) {
      return this.ops.map(t);
    }
    partition(t) {
      const e = [], n = [];
      return this.forEach((s) => {
        (t(s) ? e : n).push(s);
      }), [e, n];
    }
    reduce(t, e) {
      return this.ops.reduce(t, e);
    }
    changeLength() {
      return this.reduce((t, e) => e.insert ? t + d.default.length(e) : e.delete ? t - e.delete : t, 0);
    }
    length() {
      return this.reduce((t, e) => t + d.default.length(e), 0);
    }
    slice(t = 0, e = 1 / 0) {
      const n = [], s = new p.default(this.ops);
      let r = 0;
      for (; r < e && s.hasNext(); ) {
        let i;
        r < t ? i = s.next(t - r) : (i = s.next(e - r), n.push(i)), r += d.default.length(i);
      }
      return new h(n);
    }
    compose(t) {
      const e = new p.default(this.ops), n = new p.default(t.ops), s = [], r = n.peek();
      if (r != null && typeof r.retain == "number" && r.attributes == null) {
        let a = r.retain;
        for (; e.peekType() === "insert" && e.peekLength() <= a; )
          a -= e.peekLength(), s.push(e.next());
        r.retain - a > 0 && n.next(r.retain - a);
      }
      const i = new h(s);
      for (; e.hasNext() || n.hasNext(); )
        if (n.peekType() === "insert")
          i.push(n.next());
        else if (e.peekType() === "delete")
          i.push(e.next());
        else {
          const a = Math.min(e.peekLength(), n.peekLength()), l = e.next(a), f = n.next(a);
          if (f.retain) {
            const o = {};
            if (typeof l.retain == "number")
              o.retain = typeof f.retain == "number" ? a : f.retain;
            else if (typeof f.retain == "number")
              l.retain == null ? o.insert = l.insert : o.retain = l.retain;
            else {
              const c = l.retain == null ? "insert" : "retain", [b, x, A] = L(l[c], f.retain), M = h.getHandler(b);
              o[c] = {
                [b]: M.compose(x, A, c === "retain")
              };
            }
            const u = y.default.compose(l.attributes, f.attributes, typeof l.retain == "number");
            if (u && (o.attributes = u), i.push(o), !n.hasNext() && w(i.ops[i.ops.length - 1], o)) {
              const c = new h(e.rest());
              return i.concat(c).chop();
            }
          } else
            typeof f.delete == "number" && (typeof l.retain == "number" || typeof l.retain == "object" && l.retain !== null) && i.push(f);
        }
      return i.chop();
    }
    concat(t) {
      const e = new h(this.ops.slice());
      return t.ops.length > 0 && (e.push(t.ops[0]), e.ops = e.ops.concat(t.ops.slice(1))), e;
    }
    diff(t, e) {
      if (this.ops === t.ops)
        return new h();
      const n = [this, t].map((l) => l.map((f) => {
        if (f.insert != null)
          return typeof f.insert == "string" ? f.insert : T;
        const o = l === t ? "on" : "with";
        throw new Error("diff() called " + o + " non-document");
      }).join("")), s = new h(), r = k(n[0], n[1], e, !0), i = new p.default(this.ops), a = new p.default(t.ops);
      return r.forEach((l) => {
        let f = l[1].length;
        for (; f > 0; ) {
          let o = 0;
          switch (l[0]) {
            case k.INSERT:
              o = Math.min(a.peekLength(), f), s.push(a.next(o));
              break;
            case k.DELETE:
              o = Math.min(f, i.peekLength()), i.next(o), s.delete(o);
              break;
            case k.EQUAL:
              o = Math.min(i.peekLength(), a.peekLength(), f);
              const u = i.next(o), c = a.next(o);
              w(u.insert, c.insert) ? s.retain(o, y.default.diff(u.attributes, c.attributes)) : s.push(c).delete(o);
              break;
          }
          f -= o;
        }
      }), s.chop();
    }
    eachLine(t, e = `
`) {
      const n = new p.default(this.ops);
      let s = new h(), r = 0;
      for (; n.hasNext(); ) {
        if (n.peekType() !== "insert")
          return;
        const i = n.peek(), a = d.default.length(i) - n.peekLength(), l = typeof i.insert == "string" ? i.insert.indexOf(e, a) - a : -1;
        if (l < 0)
          s.push(n.next());
        else if (l > 0)
          s.push(n.next(l));
        else {
          if (t(s, n.next(1).attributes || {}, r) === !1)
            return;
          r += 1, s = new h();
        }
      }
      s.length() > 0 && t(s, {}, r);
    }
    invert(t) {
      const e = new h();
      return this.reduce((n, s) => {
        if (s.insert)
          e.delete(d.default.length(s));
        else {
          if (typeof s.retain == "number" && s.attributes == null)
            return e.retain(s.retain), n + s.retain;
          if (s.delete || typeof s.retain == "number") {
            const r = s.delete || s.retain;
            return t.slice(n, n + r).forEach((a) => {
              s.delete ? e.push(a) : s.retain && s.attributes && e.retain(d.default.length(a), y.default.invert(s.attributes, a.attributes));
            }), n + r;
          } else if (typeof s.retain == "object" && s.retain !== null) {
            const r = t.slice(n, n + 1), i = new p.default(r.ops).next(), [a, l, f] = L(s.retain, i.insert), o = h.getHandler(a);
            return e.retain({ [a]: o.invert(l, f) }, y.default.invert(s.attributes, i.attributes)), n + 1;
          }
        }
        return n;
      }, 0), e.chop();
    }
    transform(t, e = !1) {
      if (e = !!e, typeof t == "number")
        return this.transformPosition(t, e);
      const n = t, s = new p.default(this.ops), r = new p.default(n.ops), i = new h();
      for (; s.hasNext() || r.hasNext(); )
        if (s.peekType() === "insert" && (e || r.peekType() !== "insert"))
          i.retain(d.default.length(s.next()));
        else if (r.peekType() === "insert")
          i.push(r.next());
        else {
          const a = Math.min(s.peekLength(), r.peekLength()), l = s.next(a), f = r.next(a);
          if (l.delete)
            continue;
          if (f.delete)
            i.push(f);
          else {
            const o = l.retain, u = f.retain;
            let c = typeof u == "object" && u !== null ? u : a;
            if (typeof o == "object" && o !== null && typeof u == "object" && u !== null) {
              const b = Object.keys(o)[0];
              if (b === Object.keys(u)[0]) {
                const x = h.getHandler(b);
                x && (c = {
                  [b]: x.transform(o[b], u[b], e)
                });
              }
            }
            i.retain(c, y.default.transform(l.attributes, f.attributes, e));
          }
        }
      return i.chop();
    }
    transformPosition(t, e = !1) {
      e = !!e;
      const n = new p.default(this.ops);
      let s = 0;
      for (; n.hasNext() && s <= t; ) {
        const r = n.peekLength(), i = n.peekType();
        if (n.next(), i === "delete") {
          t -= Math.min(r, t - s);
          continue;
        } else
          i === "insert" && (s < t || !e) && (t += r);
        s += r;
      }
      return t;
    }
  }
  h.Op = d.default, h.OpIterator = p.default, h.AttributeMap = y.default, h.handlers = {}, m.default = h, j.exports = h, j.exports.default = h;
})(E, E.exports);
var R = E.exports;
const K = /* @__PURE__ */ N(R);
export {
  R as D,
  K as default
};
