var c = Object.defineProperty;
var f = (n, i, t) => i in n ? c(n, i, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[i] = t;
var h = (n, i, t) => (f(n, typeof i != "symbol" ? i + "" : i, t), t);
import { Scope as g } from "../../../../parchment@3.0.0/node_modules/parchment/dist/parchment.mjs";
import p from "../core/module.mjs";
import o from "../core/quill.mjs";
class m extends p {
  constructor(t, s) {
    super(t, s);
    h(this, "lastRecorded", 0);
    h(this, "ignoreChange", !1);
    h(this, "stack", {
      undo: [],
      redo: []
    });
    h(this, "currentRange", null);
    this.quill.on(o.events.EDITOR_CHANGE, (e, r, l, a) => {
      e === o.events.SELECTION_CHANGE ? r && a !== o.sources.SILENT && (this.currentRange = r) : e === o.events.TEXT_CHANGE && (this.ignoreChange || (!this.options.userOnly || a === o.sources.USER ? this.record(r, l) : this.transform(r)), this.currentRange = d(this.currentRange, r));
    }), this.quill.keyboard.addBinding({
      key: "z",
      shortKey: !0
    }, this.undo.bind(this)), this.quill.keyboard.addBinding({
      key: ["z", "Z"],
      shortKey: !0,
      shiftKey: !0
    }, this.redo.bind(this)), /Win/i.test(navigator.platform) && this.quill.keyboard.addBinding({
      key: "y",
      shortKey: !0
    }, this.redo.bind(this)), this.quill.root.addEventListener("beforeinput", (e) => {
      e.inputType === "historyUndo" ? (this.undo(), e.preventDefault()) : e.inputType === "historyRedo" && (this.redo(), e.preventDefault());
    });
  }
  change(t, s) {
    if (this.stack[t].length === 0)
      return;
    const e = this.stack[t].pop();
    if (!e)
      return;
    const r = this.quill.getContents(), l = e.delta.invert(r);
    this.stack[s].push({
      delta: l,
      range: d(e.range, l)
    }), this.lastRecorded = 0, this.ignoreChange = !0, this.quill.updateContents(e.delta, o.sources.USER), this.ignoreChange = !1, this.restoreSelection(e);
  }
  clear() {
    this.stack = {
      undo: [],
      redo: []
    };
  }
  cutoff() {
    this.lastRecorded = 0;
  }
  record(t, s) {
    if (t.ops.length === 0)
      return;
    this.stack.redo = [];
    let e = t.invert(s), r = this.currentRange;
    const l = Date.now();
    if (
      // @ts-expect-error Fix me later
      this.lastRecorded + this.options.delay > l && this.stack.undo.length > 0
    ) {
      const a = this.stack.undo.pop();
      a && (e = e.compose(a.delta), r = a.range);
    } else
      this.lastRecorded = l;
    e.length() !== 0 && (this.stack.undo.push({
      delta: e,
      range: r
    }), this.stack.undo.length > this.options.maxStack && this.stack.undo.shift());
  }
  redo() {
    this.change("redo", "undo");
  }
  transform(t) {
    u(this.stack.undo, t), u(this.stack.redo, t);
  }
  undo() {
    this.change("undo", "redo");
  }
  restoreSelection(t) {
    if (t.range)
      this.quill.setSelection(t.range, o.sources.USER);
    else {
      const s = R(this.quill.scroll, t.delta);
      this.quill.setSelection(s, o.sources.USER);
    }
  }
}
h(m, "DEFAULTS", {
  delay: 1e3,
  maxStack: 100,
  userOnly: !1
});
function u(n, i) {
  let t = i;
  for (let s = n.length - 1; s >= 0; s -= 1) {
    const e = n[s];
    n[s] = {
      delta: t.transform(e.delta, !0),
      range: e.range && d(e.range, t)
    }, t = e.delta.transform(t), n[s].delta.length() === 0 && n.splice(s, 1);
  }
}
function y(n, i) {
  const t = i.ops[i.ops.length - 1];
  return t == null ? !1 : t.insert != null ? typeof t.insert == "string" && t.insert.endsWith(`
`) : t.attributes != null ? Object.keys(t.attributes).some((s) => n.query(s, g.BLOCK) != null) : !1;
}
function R(n, i) {
  const t = i.reduce((e, r) => e + (r.delete || 0), 0);
  let s = i.length() - t;
  return y(n, i) && (s -= 1), s;
}
function d(n, i) {
  if (!n)
    return n;
  const t = i.transformPosition(n.index), s = i.transformPosition(n.index + n.length);
  return {
    index: t,
    length: s - t
  };
}
export {
  m as default,
  R as getLastChangeIndex
};
