// Bounded device-local undo history. Receipts remain attached to each snapshot.
export class WorkspaceHistory {
  constructor(initial, limit = 20) { this.entries = [structuredClone(initial)]; this.index = 0; this.limit = limit; }
  get canUndo() { return this.index > 0; }
  get canRedo() { return this.index < this.entries.length - 1; }
  commit(value) {
    if (JSON.stringify(value) === JSON.stringify(this.entries[this.index])) return;
    this.entries.splice(this.index + 1);
    this.entries.push(structuredClone(value));
    if (this.entries.length > this.limit) this.entries.shift();
    this.index = this.entries.length - 1;
  }
  undo() { if (!this.canUndo) throw new Error('Nothing to undo.'); return structuredClone(this.entries[--this.index]); }
  redo() { if (!this.canRedo) throw new Error('Nothing to redo.'); return structuredClone(this.entries[++this.index]); }
}
