"use strict";(self["webpackChunkaida"]=self["webpackChunkaida"]||[]).push([[3217],{23217:function(t,e,o){o.r(e),o.d(e,{Command:function(){return n},CompositeCommand:function(){return s},FunctionCommand:function(){return r}});o(21703);class n{constructor(t={}){this.name=t.name||"未命名命令",this.description=t.description||"",this.undoable=!1!==t.undoable,this.timestamp=Date.now()}execute(){throw new Error("子类必须实现execute方法")}undo(){if(!this.undoable)throw new Error("此命令不支持撤销");throw new Error("可撤销命令必须实现undo方法")}getInfo(){return{name:this.name,description:this.description,undoable:this.undoable,timestamp:this.timestamp}}}class s extends n{constructor(t=[],e={}){super({name:e.name||"复合命令",description:e.description||"批量执行多个命令",...e}),this.commands=Array.isArray(t)?t:[],this.executedCommands=[],this.isExecuting=!1}addCommand(t){if(!t||"function"!==typeof t.execute)throw new Error("无效的命令对象");return this.commands.push(t),this}addCommands(t){return Array.isArray(t)&&t.forEach((t=>this.addCommand(t))),this}async execute(){if(this.isExecuting)throw new Error("复合命令正在执行中");this.isExecuting=!0,this.executedCommands=[];try{const e=[];for(const o of this.commands)try{console.log(`📦 复合命令执行子命令: ${o.constructor.name}`);const t=o.execute(),n=this._isPromise(t)?await t:t;e.push(n),this.executedCommands.push(o),console.log(`✅ 子命令执行成功: ${o.constructor.name}`)}catch(t){throw console.error(`❌ 子命令执行失败: ${o.constructor.name}`,t),await this._rollbackExecutedCommands(),new Error(`复合命令执行失败：${t.message}`)}return this.isExecuting=!1,console.log(`✅ 复合命令执行完成，共执行 ${e.length} 个子命令`),e}catch(t){throw this.isExecuting=!1,t}}async undo(){if(this.isExecuting)throw new Error("复合命令正在执行中，无法撤销");if(0===this.executedCommands.length)return console.warn("没有已执行的子命令需要撤销"),!0;console.log(`↩️ 开始撤销复合命令，共 ${this.executedCommands.length} 个子命令`);try{const e=[...this.executedCommands].reverse(),o=[];for(const n of e)if("function"===typeof n.undo)try{console.log(`↩️ 撤销子命令: ${n.constructor.name}`);const t=n.undo(),e=this._isPromise(t)?await t:t;o.push(e),console.log(`✅ 子命令撤销成功: ${n.constructor.name}`)}catch(t){console.error(`❌ 子命令撤销失败: ${n.constructor.name}`,t)}else console.warn(`⚠️ 子命令不支持撤销: ${n.constructor.name}`);return this.executedCommands=[],console.log("✅ 复合命令撤销完成"),o}catch(t){throw console.error("❌ 复合命令撤销过程中发生错误:",t),t}}async _rollbackExecutedCommands(){console.log(`🔄 开始回滚已执行的 ${this.executedCommands.length} 个子命令`);const t=[...this.executedCommands].reverse();for(const o of t)if("function"===typeof o.undo)try{console.log(`🔄 回滚子命令: ${o.constructor.name}`);const t=o.undo();this._isPromise(t)&&await t,console.log(`✅ 子命令回滚成功: ${o.constructor.name}`)}catch(e){console.error(`❌ 子命令回滚失败: ${o.constructor.name}`,e)}this.executedCommands=[],console.log("✅ 回滚完成")}_isPromise(t){return t&&"object"===typeof t&&"function"===typeof t.then&&"function"===typeof t.catch}getInfo(){return{...super.getInfo(),commandCount:this.commands.length,executedCount:this.executedCommands.length,isExecuting:this.isExecuting,subCommands:this.commands.map((t=>t.getInfo?t.getInfo():t.constructor.name))}}}class r extends n{constructor(t,e=null,o={}){if(super({name:o.name||"函数命令",undoable:"function"===typeof e,...o}),"function"!==typeof t)throw new Error("执行函数不能为空");this.executeFn=t,this.undoFn=e,this.executeResult=null}async execute(){const t=this.executeFn();return this.executeResult=this._isPromise(t)?await t:t,this.executeResult}async undo(){if(!this.undoFn)throw new Error("此函数命令不支持撤销");const t=this.undoFn(this.executeResult);return this._isPromise(t)?await t:t}_isPromise(t){return t&&"object"===typeof t&&"function"===typeof t.then&&"function"===typeof t.catch}}}}]);