import b from "../../../../rope-sequence@1.3.4/node_modules/rope-sequence/dist/index.js";
import { Mapping as k } from "../../../../prosemirror-transform@1.10.4/node_modules/prosemirror-transform/dist/index.js";
import { Plugin as H, PluginKey as O } from "../../../../prosemirror-state@1.4.3/node_modules/prosemirror-state/dist/index.js";
const D = 500;
class c {
  constructor(t, e) {
    this.items = t, this.eventCount = e;
  }
  // Pop the latest event off the branch's history and apply it
  // to a document transform.
  popEvent(t, e) {
    if (this.eventCount == 0)
      return null;
    let i = this.items.length;
    for (; ; i--)
      if (this.items.get(i - 1).selection) {
        --i;
        break;
      }
    let s, p;
    e && (s = this.remapping(i, this.items.length), p = s.maps.length);
    let l = t.tr, r, m, u = [], f = [];
    return this.items.forEach((a, o) => {
      if (!a.step) {
        s || (s = this.remapping(i, o + 1), p = s.maps.length), p--, f.push(a);
        return;
      }
      if (s) {
        f.push(new g(a.map));
        let d = a.step.map(s.slice(p)), v;
        d && l.maybeStep(d).doc && (v = l.mapping.maps[l.mapping.maps.length - 1], u.push(new g(v, void 0, void 0, u.length + f.length))), p--, v && s.appendMap(v, p);
      } else
        l.maybeStep(a.step);
      if (a.selection)
        return r = s ? a.selection.map(s.slice(p)) : a.selection, m = new c(this.items.slice(0, i).append(f.reverse().concat(u)), this.eventCount - 1), !1;
    }, this.items.length, 0), { remaining: m, transform: l, selection: r };
  }
  // Create a new branch with the given transform added.
  addTransform(t, e, i, s) {
    let p = [], l = this.eventCount, r = this.items, m = !s && r.length ? r.get(r.length - 1) : null;
    for (let f = 0; f < t.steps.length; f++) {
      let a = t.steps[f].invert(t.docs[f]), o = new g(t.mapping.maps[f], a, e), d;
      (d = m && m.merge(o)) && (o = d, f ? p.pop() : r = r.slice(0, r.length - 1)), p.push(o), e && (l++, e = void 0), s || (m = o);
    }
    let u = l - i.depth;
    return u > y && (r = x(r, u), l -= u), new c(r.append(p), l);
  }
  remapping(t, e) {
    let i = new k();
    return this.items.forEach((s, p) => {
      let l = s.mirrorOffset != null && p - s.mirrorOffset >= t ? i.maps.length - s.mirrorOffset : void 0;
      i.appendMap(s.map, l);
    }, t, e), i;
  }
  addMaps(t) {
    return this.eventCount == 0 ? this : new c(this.items.append(t.map((e) => new g(e))), this.eventCount);
  }
  // When the collab module receives remote changes, the history has
  // to know about those, so that it can adjust the steps that were
  // rebased on top of the remote changes, and include the position
  // maps for the remote changes in its array of items.
  rebased(t, e) {
    if (!this.eventCount)
      return this;
    let i = [], s = Math.max(0, this.items.length - e), p = t.mapping, l = t.steps.length, r = this.eventCount;
    this.items.forEach((o) => {
      o.selection && r--;
    }, s);
    let m = e;
    this.items.forEach((o) => {
      let d = p.getMirror(--m);
      if (d == null)
        return;
      l = Math.min(l, d);
      let v = p.maps[d];
      if (o.step) {
        let S = t.steps[d].invert(t.docs[d]), E = o.selection && o.selection.map(p.slice(m + 1, d));
        E && r++, i.push(new g(v, S, E));
      } else
        i.push(new g(v));
    }, s);
    let u = [];
    for (let o = e; o < l; o++)
      u.push(new g(p.maps[o]));
    let f = this.items.slice(0, s).append(u).append(i), a = new c(f, r);
    return a.emptyItemCount() > D && (a = a.compress(this.items.length - i.length)), a;
  }
  emptyItemCount() {
    let t = 0;
    return this.items.forEach((e) => {
      e.step || t++;
    }), t;
  }
  // Compressing a branch means rewriting it to push the air (map-only
  // items) out. During collaboration, these naturally accumulate
  // because each remote change adds one. The `upto` argument is used
  // to ensure that only the items below a given level are compressed,
  // because `rebased` relies on a clean, untouched set of items in
  // order to associate old items with rebased steps.
  compress(t = this.items.length) {
    let e = this.remapping(0, t), i = e.maps.length, s = [], p = 0;
    return this.items.forEach((l, r) => {
      if (r >= t)
        s.push(l), l.selection && p++;
      else if (l.step) {
        let m = l.step.map(e.slice(i)), u = m && m.getMap();
        if (i--, u && e.appendMap(u, i), m) {
          let f = l.selection && l.selection.map(e.slice(i));
          f && p++;
          let a = new g(u.invert(), m, f), o, d = s.length - 1;
          (o = s.length && s[d].merge(a)) ? s[d] = o : s.push(a);
        }
      } else l.map && i--;
    }, this.items.length, 0), new c(b.from(s.reverse()), p);
  }
}
c.empty = new c(b.empty, 0);
function x(n, t) {
  let e;
  return n.forEach((i, s) => {
    if (i.selection && t-- == 0)
      return e = s, !1;
  }), n.slice(e);
}
class g {
  constructor(t, e, i, s) {
    this.map = t, this.step = e, this.selection = i, this.mirrorOffset = s;
  }
  merge(t) {
    if (this.step && t.step && !t.selection) {
      let e = t.step.merge(this.step);
      if (e)
        return new g(e.getMap().invert(), e, this.selection);
    }
  }
}
class h {
  constructor(t, e, i, s, p) {
    this.done = t, this.undone = e, this.prevRanges = i, this.prevTime = s, this.prevComposition = p;
  }
}
const y = 20;
function F(n, t, e, i) {
  let s = e.getMeta(w), p;
  if (s)
    return s.historyState;
  e.getMeta(j) && (n = new h(n.done, n.undone, null, 0, -1));
  let l = e.getMeta("appendedTransaction");
  if (e.steps.length == 0)
    return n;
  if (l && l.getMeta(w))
    return l.getMeta(w).redo ? new h(n.done.addTransform(e, void 0, i, M(t)), n.undone, I(e.mapping.maps), n.prevTime, n.prevComposition) : new h(n.done, n.undone.addTransform(e, void 0, i, M(t)), null, n.prevTime, n.prevComposition);
  if (e.getMeta("addToHistory") !== !1 && !(l && l.getMeta("addToHistory") === !1)) {
    let r = e.getMeta("composition"), m = n.prevTime == 0 || !l && n.prevComposition != r && (n.prevTime < (e.time || 0) - i.newGroupDelay || !G(e, n.prevRanges)), u = l ? C(n.prevRanges, e.mapping) : I(e.mapping.maps);
    return new h(n.done.addTransform(e, m ? t.selection.getBookmark() : void 0, i, M(t)), c.empty, u, e.time, r ?? n.prevComposition);
  } else return (p = e.getMeta("rebased")) ? new h(n.done.rebased(e, p), n.undone.rebased(e, p), C(n.prevRanges, e.mapping), n.prevTime, n.prevComposition) : new h(n.done.addMaps(e.mapping.maps), n.undone.addMaps(e.mapping.maps), C(n.prevRanges, e.mapping), n.prevTime, n.prevComposition);
}
function G(n, t) {
  if (!t)
    return !1;
  if (!n.docChanged)
    return !0;
  let e = !1;
  return n.mapping.maps[0].forEach((i, s) => {
    for (let p = 0; p < t.length; p += 2)
      i <= t[p + 1] && s >= t[p] && (e = !0);
  }), e;
}
function I(n) {
  let t = [];
  for (let e = n.length - 1; e >= 0 && t.length == 0; e--)
    n[e].forEach((i, s, p, l) => t.push(p, l));
  return t;
}
function C(n, t) {
  if (!n)
    return null;
  let e = [];
  for (let i = 0; i < n.length; i += 2) {
    let s = t.map(n[i], 1), p = t.map(n[i + 1], -1);
    s <= p && e.push(s, p);
  }
  return e;
}
function K(n, t, e) {
  let i = M(t), s = w.get(t).spec.config, p = (e ? n.undone : n.done).popEvent(t, i);
  if (!p)
    return null;
  let l = p.selection.resolve(p.transform.doc), r = (e ? n.done : n.undone).addTransform(p.transform, t.selection.getBookmark(), s, i), m = new h(e ? r : p.remaining, e ? p.remaining : r, null, 0, -1);
  return p.transform.setSelection(l).setMeta(w, { redo: e, historyState: m });
}
let T = !1, P = null;
function M(n) {
  let t = n.plugins;
  if (P != t) {
    T = !1, P = t;
    for (let e = 0; e < t.length; e++)
      if (t[e].spec.historyPreserveItems) {
        T = !0;
        break;
      }
  }
  return T;
}
const w = new O("history"), j = new O("closeHistory");
function W(n = {}) {
  return n = {
    depth: n.depth || 100,
    newGroupDelay: n.newGroupDelay || 500
  }, new H({
    key: w,
    state: {
      init() {
        return new h(c.empty, c.empty, null, 0, -1);
      },
      apply(t, e, i) {
        return F(e, i, t, n);
      }
    },
    config: n,
    props: {
      handleDOMEvents: {
        beforeinput(t, e) {
          let i = e.inputType, s = i == "historyUndo" ? A : i == "historyRedo" ? U : null;
          return s ? (e.preventDefault(), s(t.state, t.dispatch)) : !1;
        }
      }
    }
  });
}
function R(n, t) {
  return (e, i) => {
    let s = w.getState(e);
    if (!s || (n ? s.undone : s.done).eventCount == 0)
      return !1;
    if (i) {
      let p = K(s, e, n);
      p && i(t ? p.scrollIntoView() : p);
    }
    return !0;
  };
}
const A = R(!1, !0), U = R(!0, !0);
export {
  W as history,
  U as redo,
  A as undo
};
//# sourceMappingURL=index.js.map
