"use client";
class m {
  /**
   * Create a new edit map.
   */
  constructor() {
    this.map = [];
  }
  /**
   * Create an edit: a remove and/or add at a certain place.
   *
   * @param {number} index
   * @param {number} remove
   * @param {Array<Event>} add
   * @returns {undefined}
   */
  add(h, t, s) {
    e(this, h, t, s);
  }
  // To do: add this when moving to `micromark`.
  // /**
  //  * Create an edit: but insert `add` before existing additions.
  //  *
  //  * @param {number} index
  //  * @param {number} remove
  //  * @param {Array<Event>} add
  //  * @returns {undefined}
  //  */
  // addBefore(index, remove, add) {
  //   addImplementation(this, index, remove, add, true)
  // }
  /**
   * Done, change the events.
   *
   * @param {Array<Event>} events
   * @returns {undefined}
   */
  consume(h) {
    if (this.map.sort(function(n, l) {
      return n[0] - l[0];
    }), this.map.length === 0)
      return;
    let t = this.map.length;
    const s = [];
    for (; t > 0; )
      t -= 1, s.push(h.slice(this.map[t][0] + this.map[t][1]), this.map[t][2]), h.length = this.map[t][0];
    s.push(h.slice()), h.length = 0;
    let i = s.pop();
    for (; i; ) {
      for (const n of i)
        h.push(n);
      i = s.pop();
    }
    this.map.length = 0;
  }
}
function e(p, h, t, s) {
  let i = 0;
  if (!(t === 0 && s.length === 0)) {
    for (; i < p.map.length; ) {
      if (p.map[i][0] === h) {
        p.map[i][1] += t, p.map[i][2].push(...s);
        return;
      }
      i += 1;
    }
    p.map.push([h, t, s]);
  }
}
export {
  m as EditMap
};
