(function(i,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("../../core/plugin/base-plugin")):typeof define=="function"&&define.amd?define(["exports","../../core/plugin/base-plugin"],d):(i=typeof globalThis<"u"?globalThis:i||self,d(i.TbwGridPlugin_undoRedo={},i.TbwGrid))})(this,(function(i,d){"use strict";function S(e,t,n){const c=[...e.undoStack,t];for(;c.length>n;)c.shift();return{undoStack:c,redoStack:[]}}function r(e){if(e.undoStack.length===0)return{newState:e,action:null};const t=[...e.undoStack],n=t.pop();return n?{newState:{undoStack:t,redoStack:[...e.redoStack,n]},action:n}:{newState:e,action:null}}function u(e){if(e.redoStack.length===0)return{newState:e,action:null};const t=[...e.redoStack],n=t.pop();return n?{newState:{undoStack:[...e.undoStack,n],redoStack:t},action:n}:{newState:e,action:null}}function k(e){return e.undoStack.length>0}function h(e){return e.redoStack.length>0}function l(){return{undoStack:[],redoStack:[]}}function f(e,t,n,c){return{type:"cell-edit",rowIndex:e,field:t,oldValue:n,newValue:c,timestamp:Date.now()}}class w extends d.BaseGridPlugin{static dependencies=[{name:"editing",required:!0,reason:"UndoRedoPlugin tracks cell edit history"}];name="undoRedo";get defaultConfig(){return{maxHistorySize:100}}undoStack=[];redoStack=[];detach(){this.undoStack=[],this.redoStack=[]}onKeyDown(t){const n=(t.ctrlKey||t.metaKey)&&t.key==="z"&&!t.shiftKey,c=(t.ctrlKey||t.metaKey)&&(t.key==="y"||t.key==="z"&&t.shiftKey);if(n){const o=r({undoStack:this.undoStack,redoStack:this.redoStack});if(o.action){const a=this.rows;a[o.action.rowIndex]&&(a[o.action.rowIndex][o.action.field]=o.action.oldValue),this.undoStack=o.newState.undoStack,this.redoStack=o.newState.redoStack,this.emit("undo",{action:o.action,type:"undo"}),this.requestRender()}return!0}if(c){const o=u({undoStack:this.undoStack,redoStack:this.redoStack});if(o.action){const a=this.rows;a[o.action.rowIndex]&&(a[o.action.rowIndex][o.action.field]=o.action.newValue),this.undoStack=o.newState.undoStack,this.redoStack=o.newState.redoStack,this.emit("redo",{action:o.action,type:"redo"}),this.requestRender()}return!0}return!1}recordEdit(t,n,c,o){const a=f(t,n,c,o),s=S({undoStack:this.undoStack,redoStack:this.redoStack},a,this.config.maxHistorySize??100);this.undoStack=s.undoStack,this.redoStack=s.redoStack}undo(){const t=r({undoStack:this.undoStack,redoStack:this.redoStack});if(t.action){const n=this.rows;n[t.action.rowIndex]&&(n[t.action.rowIndex][t.action.field]=t.action.oldValue),this.undoStack=t.newState.undoStack,this.redoStack=t.newState.redoStack,this.requestRender()}return t.action}redo(){const t=u({undoStack:this.undoStack,redoStack:this.redoStack});if(t.action){const n=this.rows;n[t.action.rowIndex]&&(n[t.action.rowIndex][t.action.field]=t.action.newValue),this.undoStack=t.newState.undoStack,this.redoStack=t.newState.redoStack,this.requestRender()}return t.action}canUndo(){return k({undoStack:this.undoStack,redoStack:this.redoStack})}canRedo(){return h({undoStack:this.undoStack,redoStack:this.redoStack})}clearHistory(){const t=l();this.undoStack=t.undoStack,this.redoStack=t.redoStack}getUndoStack(){return[...this.undoStack]}getRedoStack(){return[...this.redoStack]}}i.UndoRedoPlugin=w,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));
//# sourceMappingURL=undo-redo.umd.js.map
