(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(o,t,n){const c=[...o.undoStack,t];for(;c.length>n;)c.shift();return{undoStack:c,redoStack:[]}}function r(o){if(o.undoStack.length===0)return{newState:o,action:null};const t=[...o.undoStack],n=t.pop();return n?{newState:{undoStack:t,redoStack:[...o.redoStack,n]},action:n}:{newState:o,action:null}}function u(o){if(o.redoStack.length===0)return{newState:o,action:null};const t=[...o.redoStack],n=t.pop();return n?{newState:{undoStack:[...o.undoStack,n],redoStack:t},action:n}:{newState:o,action:null}}function k(o){return o.undoStack.length>0}function h(o){return o.redoStack.length>0}function l(){return{undoStack:[],redoStack:[]}}function f(o,t,n,c){return{type:"cell-edit",rowIndex:o,field:t,oldValue:n,newValue:c,timestamp:Date.now()}}class w extends d.BaseGridPlugin{name="undoRedo";version="1.0.0";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 e=r({undoStack:this.undoStack,redoStack:this.redoStack});if(e.action){const a=this.rows;a[e.action.rowIndex]&&(a[e.action.rowIndex][e.action.field]=e.action.oldValue),this.undoStack=e.newState.undoStack,this.redoStack=e.newState.redoStack,this.emit("undo",{action:e.action,type:"undo"}),this.requestRender()}return!0}if(c){const e=u({undoStack:this.undoStack,redoStack:this.redoStack});if(e.action){const a=this.rows;a[e.action.rowIndex]&&(a[e.action.rowIndex][e.action.field]=e.action.newValue),this.undoStack=e.newState.undoStack,this.redoStack=e.newState.redoStack,this.emit("redo",{action:e.action,type:"redo"}),this.requestRender()}return!0}return!1}recordEdit(t,n,c,e){const a=f(t,n,c,e),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
