@univerjs/sheets-formula
Version:
| Package Name | UMD Namespace | Version | License | Downloads | Contains CSS | Contains i18n locales | | --- | --- | --- | --- | --- | :---: | :---: | | `@univerjs/sheets-formula` | `UniverSheetsFormula` | [![][npm-version-shield]][npm-version-link] | ![
1 lines • 416 kB
JavaScript
(function(P,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("@univerjs/core"),require("@univerjs/sheets"),require("@univerjs/engine-formula"),require("rxjs"),require("@univerjs/rpc")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@univerjs/sheets","@univerjs/engine-formula","rxjs","@univerjs/rpc"],c):(P=typeof globalThis<"u"?globalThis:P||self,c(P.UniverSheetsFormula={},P.UniverCore,P.UniverSheets,P.UniverEngineFormula,P.rxjs,P.UniverRpc))})(this,(function(P,c,s,t,bt,it){"use strict";var _a=Object.defineProperty;var Da=(P,c,s)=>c in P?_a(P,c,{enumerable:!0,configurable:!0,writable:!0,value:s}):P[c]=s;var F=(P,c,s)=>Da(P,typeof c!="symbol"?c+"":c,s);const yt={id:"formula.command.insert-function",type:c.CommandType.COMMAND,handler:async(r,e)=>{const{list:a,listOfRangeHasNumber:n}=e,i=r.get(c.ICommandService),o=new c.ObjectMatrix;a.forEach(f=>{const{range:m,primary:l,formula:p}=f,{row:L,column:N}=l,T=c.generateRandomId(6);o.setValue(L,N,{f:p,si:T});const{startRow:S,startColumn:R,endRow:C,endColumn:d}=m;for(let A=S;A<=C;A++)for(let E=R;E<=d;E++)(A!==L||E!==N)&&o.setValue(A,E,{si:T})}),n&&n.length>0&&n.forEach(f=>{const{primary:m,formula:l}=f;o.setValue(m.row,m.column,{f:l})});const u={value:o.getData()};return i.executeCommand(s.SetRangeValuesCommand.id,u)}},xt={id:"sheets-formula.command.quick-sum",type:c.CommandType.COMMAND,handler:async r=>{const a=r.get(s.SheetsSelectionsService).getCurrentLastSelection();if(!a)return!1;const n=r.get(c.IUniverInstanceService),i=s.getSheetCommandTarget(n);if(!i)return!1;const o=a.range,{worksheet:u}=i;let f=s.findFirstNonEmptyCell(o,u);if(!f)return!1;f=s.alignToMergedCellsBorders(f,u);const m=s.expandToContinuousRange({startRow:f.startRow,startColumn:f.startColumn,endRow:o.endRow,endColumn:o.endColumn},{left:!0,right:!0,up:!0,down:!0},u),l=new c.ObjectMatrix,p=s.alignToMergedCellsBorders({startRow:m.endRow,endRow:m.endRow,startColumn:m.startColumn,endColumn:m.endColumn},u);if(!c.Rectangle.equals(p,m))for(const T of u.iterateByColumn(p))(!T.value||!u.cellHasValue(T.value))&&l.setValue(T.row,T.col,{f:`=SUM(${t.serializeRange({startColumn:T.col,endColumn:T.col,startRow:m.startRow,endRow:T.row-1})})`});const L=s.alignToMergedCellsBorders({startRow:m.startRow,startColumn:m.endColumn,endRow:m.endRow,endColumn:m.endColumn},u);if(!c.Rectangle.equals(L,m))for(const T of u.iterateByRow(L))(!T.value||!u.cellHasValue(T.value))&&l.setValue(T.row,T.col,{f:`=SUM(${t.serializeRange({startColumn:m.startColumn,endColumn:T.col-1,startRow:T.row,endRow:T.row})})`});const N=r.get(c.ICommandService);return(await c.sequenceExecuteAsync([{id:s.SetRangeValuesCommand.id,params:{range:m,value:l.getMatrix()}},{id:s.SetSelectionsOperation.id,params:{unitId:i.unitId,subUnitId:i.subUnitId,selections:[{range:m,primary:c.Rectangle.contains(m,a.primary)?a.primary:{...f,actualRow:f.startRow,actualColumn:f.startColumn},style:null}]}}],N)).result}},$="sheets-formula.base.config";var Y=(r=>(r[r.FORCED=0]="FORCED",r[r.WHEN_EMPTY=1]="WHEN_EMPTY",r[r.NO_CALCULATION=2]="NO_CALCULATION",r))(Y||{});const Ut={},Xt="sheets-formula.remote.config",qt={};var Wt=Object.getOwnPropertyDescriptor,gt=(r,e,a,n)=>{for(var i=n>1?void 0:n?Wt(e,a):e,o=r.length-1,u;o>=0;o--)(u=r[o])&&(i=u(i)||i);return i},Et=(r,e)=>(a,n)=>e(a,n,r);P.ImageFormulaCellInterceptorController=class extends c.Disposable{constructor(a,n,i){super();F(this,"_errorValueCell",{v:t.ErrorType.VALUE,t:c.CellValueType.STRING});F(this,"_refreshRender");this._commandService=a,this._sheetInterceptorService=n,this._formulaDataModel=i,this._initialize()}_initialize(){this._commandExecutedListener(),this._initInterceptorCellContent()}_commandExecutedListener(){this.disposeWithMe(this._commandService.onCommandExecuted(async a=>{if(a.id===t.SetImageFormulaDataMutation.id){const n=a.params;if(!n)return;const{imageFormulaData:i}=n;if(!i||i.length===0)return;const o=await Promise.all(i.map(f=>this._getImageNatureSize(f))),u={};o.forEach(f=>{const{unitId:m,sheetId:l,row:p,column:L,...N}=f;u[m]||(u[m]={}),u[m][l]||(u[m][l]=new c.ObjectMatrix),u[m][l].setValue(p,L,N)}),this._formulaDataModel.mergeUnitImageFormulaData(u),this._refreshRender()}}))}_initInterceptorCellContent(){this.disposeWithMe(this._sheetInterceptorService.intercept(s.INTERCEPTOR_POINT.CELL_CONTENT,{priority:s.InterceptCellContentPriority.CELL_IMAGE,effect:c.InterceptorEffectEnum.Value|c.InterceptorEffectEnum.Style,handler:(a,n,i)=>{var D,U;const{unitId:o,subUnitId:u,row:f,col:m}=n,l=this._formulaDataModel.getUnitImageFormulaData(),p=(U=(D=l==null?void 0:l[o])==null?void 0:D[u])==null?void 0:U.getValue(f,m);if(!p)return i(a);const{source:L,height:N,width:T,isErrorImage:S,imageNaturalWidth:R,imageNaturalHeight:C}=p;if(S)return i(this._errorValueCell);const d=T||R,A=N||C,E=c.createDocumentModelWithStyle("",{}),I={unitId:o,subUnitId:u,drawingId:c.generateRandomId(),drawingType:c.DrawingTypeEnum.DRAWING_IMAGE,imageSourceType:c.ImageSourceType.URL,source:L,transform:{left:0,top:0,width:d,height:A},docTransform:{size:{width:d,height:A},positionH:{relativeFrom:c.ObjectRelativeFromH.PAGE,posOffset:0},positionV:{relativeFrom:c.ObjectRelativeFromV.PARAGRAPH,posOffset:0},angle:0},behindDoc:c.BooleanNumber.FALSE,title:"",description:"",layoutType:c.PositionedObjectLayoutType.INLINE,wrapText:c.WrapTextType.BOTH_SIDES,distB:0,distL:0,distR:0,distT:0},O=c.BuildTextUtils.drawing.add({documentDataModel:E,drawings:[I],selection:{collapsed:!0,startOffset:0,endOffset:0}});return O?(E.apply(O),i({...a,p:E.getSnapshot()})):i(this._errorValueCell)}}))}async _getImageNatureSize(a){const n=await this._getImageSize(a.source);return n.image?{...a,isErrorImage:!1,imageNaturalHeight:n.height,imageNaturalWidth:n.width}:{...a,isErrorImage:!0}}async _getImageSize(a){return new Promise(n=>{const i=new Image;i.src=a,i.onload=()=>{n({width:i.width,height:i.height,image:i})},i.onerror=()=>{n({width:0,height:0,image:null})}})}registerRefreshRenderFunction(a){this._refreshRender=a}},P.ImageFormulaCellInterceptorController=gt([Et(0,c.ICommandService),Et(1,c.Inject(s.SheetInterceptorService)),Et(2,c.Inject(t.FormulaDataModel))],P.ImageFormulaCellInterceptorController);var Kt=Object.getOwnPropertyDescriptor,kt=(r,e,a,n)=>{for(var i=n>1?void 0:n?Kt(e,a):e,o=r.length-1,u;o>=0;o--)(u=r[o])&&(i=u(i)||i);return i},W=(r,e)=>(a,n)=>e(a,n,r);const ht={done:0,count:0},It={onlyLocal:!0};P.TriggerCalculationController=class extends c.Disposable{constructor(a,n,i,o,u,f,m){super();F(this,"_waitingCommandQueue",[]);F(this,"_executingDirtyData",{forceCalculation:!1,dirtyRanges:[],dirtyNameMap:{},dirtyDefinedNameMap:{},dirtyUnitFeatureMap:{},dirtyUnitOtherFormulaMap:{},clearDependencyTreeCache:{}});F(this,"_setTimeoutKey",-1);F(this,"_startExecutionTime",0);F(this,"_totalCalculationTaskCount",0);F(this,"_doneCalculationTaskCount",0);F(this,"_executionInProgressParams",null);F(this,"_restartCalculation",!1);F(this,"_progress$",new bt.BehaviorSubject(ht));F(this,"progress$",this._progress$.asObservable());this._commandService=a,this._activeDirtyManagerService=n,this._logService=i,this._configService=o,this._formulaDataModel=u,this._localeService=f,this._registerOtherFormulaService=m,this._commandExecutedListener(),this._initialExecuteFormulaProcessListener(),this._initialExecuteFormula()}_emitProgress(a){this._progress$.next({done:this._doneCalculationTaskCount,count:this._totalCalculationTaskCount,label:a})}_startProgress(){this._doneCalculationTaskCount=0,this._totalCalculationTaskCount=1;const a=this._localeService.t("formula.progress.analyzing");this._emitProgress(a)}_calculateProgress(a){if(this._executionInProgressParams){const{totalFormulasToCalculate:n,completedFormulasCount:i,totalArrayFormulasToCalculate:o,completedArrayFormulasCount:u}=this._executionInProgressParams;if(this._doneCalculationTaskCount=i+u,this._totalCalculationTaskCount=n+o,this._totalCalculationTaskCount===0)return;this._emitProgress(a)}}_completeProgress(){this._doneCalculationTaskCount=this._totalCalculationTaskCount=1;const a=this._localeService.t("formula.progress.done");this._emitProgress(a)}clearProgress(){this._doneCalculationTaskCount=0,this._totalCalculationTaskCount=0,this._emitProgress()}dispose(){super.dispose(),this._progress$.next(ht),this._progress$.complete(),clearTimeout(this._setTimeoutKey)}_getCalculationMode(){var n;const a=this._configService.getConfig($);return(n=a==null?void 0:a.initialFormulaComputing)!=null?n:Y.WHEN_EMPTY}_commandExecutedListener(){this.disposeWithMe(this._commandService.beforeCommandExecuted(a=>{if(a.id===t.SetFormulaCalculationStartMutation.id||a.id===t.SetFormulaStringBatchCalculationMutation.id){const n=a.params;if(a.id===t.SetFormulaCalculationStartMutation.id){const i=this._configService.getConfig(t.ENGINE_FORMULA_RETURN_DEPENDENCY_TREE)||!1;n.isCalculateTreeModel=i}n.maxIteration=this._configService.getConfig(t.ENGINE_FORMULA_CYCLE_REFERENCE_COUNT),n.rowData=this._formulaDataModel.getHiddenRowsFiltered()}})),this.disposeWithMe(this._commandService.onCommandExecuted((a,n)=>{if(this._activeDirtyManagerService.get(a.id)){if(a.id===s.SetRangeValuesMutation.id){const i=a.params;if(n&&n.onlyLocal===!0||i.trigger===s.SetStyleCommand.id||i.trigger===s.SetBorderCommand.id||i.trigger===s.ClearSelectionFormatCommand.id)return}this._waitingCommandQueue.push(a),clearTimeout(this._setTimeoutKey),this._setTimeoutKey=setTimeout(()=>{const i=this._generateDirty(this._waitingCommandQueue);this._executingDirtyData=this._mergeDirty(this._executingDirtyData,i),this._executionInProgressParams==null?this._commandService.executeCommand(t.SetFormulaCalculationStartMutation.id,{...this._executingDirtyData},It):(this._restartCalculation=!0,this._commandService.executeCommand(t.SetFormulaCalculationStopMutation.id,{})),this._waitingCommandQueue=[]},100)}}))}_generateDirty(a){const n=[],i={},o={},u={},f={},m={};let l=!1;for(const p of a){const L=this._activeDirtyManagerService.get(p.id);if(L==null)continue;const N=L.getDirtyData(p),{dirtyRanges:T,dirtyNameMap:S,dirtyDefinedNameMap:R,dirtyUnitFeatureMap:C,dirtyUnitOtherFormulaMap:d,clearDependencyTreeCache:A,forceCalculation:E=!1}=N;T!=null&&this._mergeDirtyRanges(n,T),S!=null&&this._mergeDirtyNameMap(i,S),R!=null&&this._mergeDirtyNameMap(o,R),C!=null&&this._mergeDirtyUnitFeatureOrOtherFormulaMap(u,C),d!=null&&this._mergeDirtyUnitFeatureOrOtherFormulaMap(f,d),A!=null&&this._mergeDirtyNameMap(m,A),l=l||E}return{dirtyRanges:n,dirtyNameMap:i,dirtyDefinedNameMap:o,dirtyUnitFeatureMap:u,dirtyUnitOtherFormulaMap:f,forceCalculation:l,clearDependencyTreeCache:m}}_mergeDirty(a,n){const i=[...a.dirtyRanges,...n.dirtyRanges],o={...a.dirtyNameMap},u={...a.dirtyDefinedNameMap},f={...a.dirtyUnitFeatureMap},m={...a.dirtyUnitOtherFormulaMap},l={...a.clearDependencyTreeCache};this._mergeDirtyNameMap(o,n.dirtyNameMap),this._mergeDirtyNameMap(u,n.dirtyDefinedNameMap),this._mergeDirtyUnitFeatureOrOtherFormulaMap(f,n.dirtyUnitFeatureMap),this._mergeDirtyUnitFeatureOrOtherFormulaMap(m,n.dirtyUnitOtherFormulaMap),this._mergeDirtyNameMap(l,n.clearDependencyTreeCache);const p=a.forceCalculation||n.forceCalculation;return{dirtyRanges:i,dirtyNameMap:o,dirtyDefinedNameMap:u,dirtyUnitFeatureMap:f,dirtyUnitOtherFormulaMap:m,forceCalculation:p,clearDependencyTreeCache:l}}_mergeDirtyRanges(a,n){for(const i of n){let o=!1;for(const u of a)if(i.unitId===u.unitId&&i.sheetId===u.sheetId){const{startRow:f,startColumn:m,endRow:l,endColumn:p}=i.range,{startRow:L,startColumn:N,endRow:T,endColumn:S}=u.range;if(f===L&&m===N&&l===T&&p===S){o=!0;break}}o||a.push(i)}}_mergeDirtyNameMap(a,n){Object.keys(n).forEach(i=>{a[i]==null&&(a[i]={}),Object.keys(n[i]).forEach(o=>{var u;(u=n[i])!=null&&u[o]&&(a[i][o]=n[i][o])})})}_mergeDirtyUnitFeatureOrOtherFormulaMap(a,n){Object.keys(n).forEach(i=>{a[i]==null&&(a[i]={}),Object.keys(n[i]).forEach(o=>{a[i][o]==null&&(a[i][o]={}),Object.keys(n[i][o]).forEach(u=>{a[i][o][u]=n[i][o][u]||!1})})})}_initialExecuteFormulaProcessListener(){let a=null,n=0;this.disposeWithMe(this._commandService.onCommandExecuted(i=>{if(i.id===t.SetFormulaCalculationStopMutation.id&&this.clearProgress(),i.id!==t.SetFormulaCalculationNotificationMutation.id)return;const o=i.params;if(o.stageInfo!=null){const{stage:u}=o.stageInfo;if(u===t.FormulaExecuteStageType.START)n===0&&(this._startExecutionTime=performance.now()),n++,a!==null&&(clearTimeout(a),a=null),a=setTimeout(()=>{a=null,this._startProgress()},1e3);else if(u===t.FormulaExecuteStageType.CURRENTLY_CALCULATING){if(this._executionInProgressParams=o.stageInfo,a===null){const f=this._localeService.t("formula.progress.calculating");this._calculateProgress(f)}}else if(u===t.FormulaExecuteStageType.START_DEPENDENCY_ARRAY_FORMULA){if(this._executionInProgressParams=o.stageInfo,a===null){const f=this._localeService.t("formula.progress.array-analysis");this._calculateProgress(f)}}else if(u===t.FormulaExecuteStageType.CURRENTLY_CALCULATING_ARRAY_FORMULA&&(this._executionInProgressParams=o.stageInfo,a===null)){const f=this._localeService.t("formula.progress.array-calculation");this._calculateProgress(f)}}else{const u=o.functionsExecutedState;let f="";switch(n--,u){case t.FormulaExecutedStateType.NOT_EXECUTED:f="No tasks are being executed anymore",this._resetExecutingDirtyData();break;case t.FormulaExecutedStateType.STOP_EXECUTION:f="The execution of the formula has been stopped",n=0;break;case t.FormulaExecutedStateType.SUCCESS:f="Formula calculation succeeded",(n===0||n===-1)&&(f+=`. Total time consumed: ${performance.now()-this._startExecutionTime} ms`),this._resetExecutingDirtyData();break;case t.FormulaExecutedStateType.INITIAL:f="Waiting for calculation",this._resetExecutingDirtyData();break}(n===0||n===-1)&&(a?(clearTimeout(a),a=null,this.clearProgress()):this._completeProgress(),n=0,this._doneCalculationTaskCount=0,this._totalCalculationTaskCount=0),u===t.FormulaExecutedStateType.STOP_EXECUTION&&this._restartCalculation?(this._restartCalculation=!1,this._commandService.executeCommand(t.SetFormulaCalculationStartMutation.id,{...this._executingDirtyData},It)):this._executionInProgressParams=null,this._logService.debug("[TriggerCalculationController]",f)}}))}_resetExecutingDirtyData(){this._executingDirtyData={dirtyRanges:[],dirtyNameMap:{},dirtyDefinedNameMap:{},dirtyUnitFeatureMap:{},dirtyUnitOtherFormulaMap:{},forceCalculation:!1,clearDependencyTreeCache:{}}}_initialExecuteFormula(){const a=this._getCalculationMode(),n=this._getDirtyDataByCalculationMode(a);this._commandService.executeCommand(t.SetTriggerFormulaCalculationStartMutation.id,n,It),this._registerOtherFormulaService.calculateStarted$.next(!0)}_getDirtyDataByCalculationMode(a){const n=a===Y.FORCED,i=a===Y.WHEN_EMPTY?this._formulaDataModel.getFormulaDirtyRanges():[];return{forceCalculation:n,dirtyRanges:i,dirtyNameMap:{},dirtyDefinedNameMap:{},dirtyUnitFeatureMap:{},dirtyUnitOtherFormulaMap:{},clearDependencyTreeCache:{}}}},P.TriggerCalculationController=kt([W(0,c.ICommandService),W(1,t.IActiveDirtyManagerService),W(2,c.ILogService),W(3,c.IConfigService),W(4,c.Inject(t.FormulaDataModel)),W(5,c.Inject(c.LocaleService)),W(6,c.Inject(t.RegisterOtherFormulaService))],P.TriggerCalculationController);function Qt(r,e,a){var n;return r==null||r[e]==null||((n=r[e])==null?void 0:n[a])==null}function At(r,e,a){var n;if(a){if(r&&r[e]&&((n=r[e])!=null&&n[a]))return delete r[e][a],{[e]:{[a]:null}}}else if(r&&r[e])return delete r[e],{[e]:null}}var b=(r=>(r[r.MoveRange=0]="MoveRange",r[r.MoveRows=1]="MoveRows",r[r.MoveCols=2]="MoveCols",r[r.InsertRow=3]="InsertRow",r[r.InsertColumn=4]="InsertColumn",r[r.RemoveRow=5]="RemoveRow",r[r.RemoveColumn=6]="RemoveColumn",r[r.DeleteMoveLeft=7]="DeleteMoveLeft",r[r.DeleteMoveUp=8]="DeleteMoveUp",r[r.InsertMoveDown=9]="InsertMoveDown",r[r.InsertMoveRight=10]="InsertMoveRight",r[r.SetName=11]="SetName",r[r.RemoveSheet=12]="RemoveSheet",r[r.SetDefinedName=13]="SetDefinedName",r[r.RemoveDefinedName=14]="RemoveDefinedName",r))(b||{});const jt=[11,12,13,14];function $t(r,e,a){const{type:n}=a;return jt.includes(n)?zt(r,e):Zt(r,e,a)}function zt(r,e){const a=[],n=[];return Object.keys(e).forEach(i=>{const o=e[i],u=r[i];if(o==null||u==null)return!0;Object.keys(o).forEach(f=>{const m=new c.ObjectMatrix(o[f]||{}),l=new c.ObjectMatrix(u[f]||{}),p=new c.ObjectMatrix,L=new c.ObjectMatrix;if(m.forValue((C,d,A)=>{if(A==null)return!0;const E=tt(A);E!==null&&(p.setValue(C,d,E),L.setValue(C,d,l.getValue(C,d)))}),p.getSizeOf()===0)return;const N={subUnitId:f,unitId:i,cellValue:p.getMatrix()},T={id:s.SetRangeValuesMutation.id,params:N};n.push(T);const S={subUnitId:f,unitId:i,cellValue:L.getMatrix()},R={id:s.SetRangeValuesMutation.id,params:S};a.push(R)})}),{undos:a,redos:n}}function Zt(r,e,a){const{redoFormulaData:n,undoFormulaData:i}=Jt(r,e,a),o=[],u=[];return Object.keys(n).forEach(f=>{Object.keys(n[f]).forEach(m=>{if(Object.keys(n[f][m]).length!==0){const l={subUnitId:m,unitId:f,cellValue:n[f][m]},p={id:s.SetRangeValuesMutation.id,params:l};o.push(p)}})}),Object.keys(i).forEach(f=>{Object.keys(i[f]).forEach(m=>{if(Object.keys(i[f][m]).length!==0){const l={subUnitId:m,unitId:f,cellValue:i[f][m]},p={id:s.SetRangeValuesMutation.id,params:l};u.push(p)}})}),{undos:u,redos:o}}function Jt(r,e,a){const n={},i={},{unitId:o,sheetId:u}=a;return new Set([...Object.keys(r),...Object.keys(e)]).forEach(m=>{if(Qt(r,m,u))return;new Set([...Object.keys(r[m]||{}),...Object.keys(e[m]||{})]).forEach(p=>{var A,E;const L=(A=r[m])==null?void 0:A[p],N=(E=e[m])==null?void 0:E[p],T=new c.ObjectMatrix(L||{}),S=new c.ObjectMatrix(N||{});let R=[];m!==o||p!==u?R=ee(S):R=te(T,S,a);const C=Le(R,T,S),d=Te(R,T);n[m]||(n[m]={}),i[m]||(i[m]={}),n[m][p]={...n[m][p],...C},i[m][p]={...i[m][p],...d}})}),{redoFormulaData:n,undoFormulaData:i}}function te(r,e,a){const{type:n,from:i,to:o,range:u}=a,f=[];return r.forValue((m,l,p)=>{if(p==null||!Ft(p))return!0;const L=c.cellToRange(m,l);let N=null,T=!1;if([0,1,2].includes(n))N=ae(n,i,o,L);else if(u!=null){const S=ne(L,a);N=S.newCell,T=S.isReverse}if(c.Tools.diffValue(L,N)&&!e.getValue(m,l))return!0;T?f.unshift({oldCell:L,newCell:N}):f.push({oldCell:L,newCell:N})}),f}function ee(r){const e=[];return r.forValue((a,n,i)=>{if(i==null||!Ft(i))return!0;const o=c.cellToRange(a,n);e.push({oldCell:o,newCell:o})}),e}function ae(r,e,a,n){if(e==null||a==null)return null;switch(r){case 0:return ie(e,a,n);case 1:return re(e,a,n);case 2:return oe(e,a,n);default:return null}}function ne(r,e){const{type:a,rangeFilteredRows:n}=e,i=e.range;let o=null,u=!1;switch(a){case 3:o=ue(i,r),u=!0;break;case 4:o=ce(i,r),u=!0;break;case 5:o=fe(i,r,n);break;case 6:o=me(i,r);break;case 7:o=se(i,r);break;case 8:o=le(i,r);break;case 9:o=pe(i,r),u=!0;break;case 10:o=de(i,r),u=!0;break}return{newCell:o,isReverse:u}}function ie(r,e,a){const n=s.handleMoveRange({id:s.EffectRefRangId.MoveRangeCommandId,params:{toRange:e,fromRange:r}},a);return s.runRefRangeMutations(n,a)}function re(r,e,a){const n=s.handleMoveRows({id:s.EffectRefRangId.MoveRowsCommandId,params:{toRange:e,fromRange:r}},a);return s.runRefRangeMutations(n,a)}function oe(r,e,a){const n=s.handleMoveCols({id:s.EffectRefRangId.MoveColsCommandId,params:{toRange:e,fromRange:r}},a);return s.runRefRangeMutations(n,a)}function ue(r,e){const a=s.handleInsertRow({id:s.EffectRefRangId.InsertRowCommandId,params:{range:r,unitId:"",subUnitId:"",direction:c.Direction.DOWN}},e);return s.runRefRangeMutations(a,e)}function ce(r,e){const a=s.handleInsertCol({id:s.EffectRefRangId.InsertColCommandId,params:{range:r,unitId:"",subUnitId:"",direction:c.Direction.RIGHT}},e);return s.runRefRangeMutations(a,e)}function fe(r,e,a){const n=s.handleIRemoveRow({id:s.EffectRefRangId.RemoveRowCommandId,params:{range:r}},e,a);return s.runRefRangeMutations(n,e)}function me(r,e){const a=s.handleIRemoveCol({id:s.EffectRefRangId.RemoveColCommandId,params:{range:r}},e);return s.runRefRangeMutations(a,e)}function se(r,e){const a=s.handleDeleteRangeMoveLeft({id:s.EffectRefRangId.DeleteRangeMoveLeftCommandId,params:{range:r}},e);return s.runRefRangeMutations(a,e)}function le(r,e){const a=s.handleDeleteRangeMoveUp({id:s.EffectRefRangId.DeleteRangeMoveUpCommandId,params:{range:r}},e);return s.runRefRangeMutations(a,e)}function pe(r,e){const a=s.handleInsertRangeMoveDown({id:s.EffectRefRangId.InsertRangeMoveDownCommandId,params:{range:r}},e);return s.runRefRangeMutations(a,e)}function de(r,e){const a=s.handleInsertRangeMoveRight({id:s.EffectRefRangId.InsertRangeMoveRightCommandId,params:{range:r}},e);return s.runRefRangeMutations(a,e)}function Le(r,e,a){var i,o,u;const n=new c.ObjectMatrix({});for(let f=0;f<r.length;f++){const{oldCell:m,newCell:l}=r[f];if((i=n.getValue(m.startRow,m.startColumn))!=null&&i.f||(o=n.getValue(m.startRow,m.startColumn))!=null&&o.si||n.setValue(m.startRow,m.startColumn,{f:null,si:null}),l){const p=(u=a.getValue(m.startRow,m.startColumn))!=null?u:e.getValue(m.startRow,m.startColumn),L=tt(p);n.setValue(l.startRow,l.startColumn,L)}}return n.getMatrix()}function Te(r,e){const a=new c.ObjectMatrix({});for(let n=r.length-1;n>=0;n--){const{oldCell:i,newCell:o}=r[n],u=e.getValue(i.startRow,i.startColumn),f=tt(u);if(a.setValue(i.startRow,i.startColumn,f),o){const m=e.getValue(o.startRow,o.startColumn),l=tt(m);a.setValue(o.startRow,o.startColumn,l!=null?l:{f:null,si:null})}}return a.getMatrix()}function tt(r){if(r==null)return{f:null,si:null};const{f:e,si:a,x:n=0,y:i=0}=r,o=c.isFormulaString(e),u=c.isFormulaId(a);if(!o&&!u)return{f:null,si:null};const f={};return u&&(f.si=a),o&&n===0&&i===0&&(f.f=e),f.f===void 0&&(f.f=null),f.si===void 0&&(f.si=null),f}function Ne(r){const e=new c.ObjectMatrix({});return new c.ObjectMatrix(r).forValue((n,i,o)=>{const u=tt(o);u!==void 0&&e.setValue(n,i,u)}),e.getMatrix()}function Ft(r){const e=(r==null?void 0:r.f)||"",a=(r==null?void 0:r.si)||"",n=c.isFormulaString(e),i=c.isFormulaId(a);return!!(n||i)}function Bt(r,e,a,n,i,o){if((i==null||i.length===0)&&(o==null||o.length===0)){if(r===a&&e===n)return!0}else if((r===i||i==null||i.length===0)&&e===o)return!0;return!1}function vt(r,e,a=0,n=0){const i=[];for(let o=0,u=r.length;o<u;o++){const f=r[o];if(typeof f=="string"||f.nodeType!==t.sequenceNodeType.REFERENCE||e.includes(o)){i.push(f);continue}const{token:m}=f,l=t.deserializeRangeWithSheetWithCache(m),{range:p,sheetName:L,unitId:N}=l,T=c.Rectangle.moveOffset(p,a,n);i.push({...f,token:t.serializeRangeToRefString({range:T,unitId:N,sheetName:L})})}return i}function Vt(r,e,a,n){const{type:i,unitId:o,sheetId:u,range:f,from:m,to:l,rangeFilteredRows:p}=e,{range:L,sheetId:N,unitId:T,sheetName:S,refOffsetX:R,refOffsetY:C}=r;if(!Bt(o,u,a,n,T,N))return;const d=c.Rectangle.moveOffset(L,R,C);let A=null;if(i===b.MoveRange){if(m==null||l==null)return;const E=Ct(d,m),I=c.getIntersectRange(d,m);if(I==null||E!==4)return;const O=s.handleMoveRange({id:s.EffectRefRangId.MoveRangeCommandId,params:{toRange:l,fromRange:m}},I),D=s.runRefRangeMutations(O,I);if(D==null)return t.ErrorType.REF;A=St(E,D,m,l,d,I)}else if(i===b.MoveRows){if(m==null||l==null)return;const E=Ct(d,m);let I=c.getIntersectRange(d,m);if(I==null&&(m.endRow<d.startRow&&l.endRow<=d.startRow||m.startRow>d.endRow&&l.startRow>d.endRow))return;I==null&&(I={startRow:d.startRow,endRow:d.endRow,startColumn:d.startColumn,endColumn:d.endColumn,rangeType:c.RANGE_TYPE.NORMAL});const O=s.handleMoveRows({id:s.EffectRefRangId.MoveRowsCommandId,params:{toRange:l,fromRange:m}},I),D=s.runRefRangeMutations(O,I);if(D==null)return t.ErrorType.REF;A=St(E,D,m,l,d,I)}else if(i===b.MoveCols){if(m==null||l==null)return;const E=Ct(d,m);let I=c.getIntersectRange(d,m);if(I==null&&(m.endColumn<d.startColumn&&l.endColumn<=d.startColumn||m.startColumn>d.endColumn&&l.startColumn>d.endColumn))return;I==null&&(I={startRow:d.startRow,endRow:d.endRow,startColumn:d.startColumn,endColumn:d.endColumn,rangeType:c.RANGE_TYPE.NORMAL});const O=s.handleMoveCols({id:s.EffectRefRangId.MoveColsCommandId,params:{toRange:l,fromRange:m}},I),D=s.runRefRangeMutations(O,I);if(D==null)return t.ErrorType.REF;A=St(E,D,m,l,d,I)}if(f!=null){if(i===b.InsertRow){const E=s.handleInsertRow({id:s.EffectRefRangId.InsertRowCommandId,params:{range:f,unitId:"",subUnitId:"",direction:c.Direction.DOWN}},d),I=s.runRefRangeMutations(E,d);if(I==null)return;A={...d,...I}}else if(i===b.InsertColumn){const E=s.handleInsertCol({id:s.EffectRefRangId.InsertColCommandId,params:{range:f,unitId:"",subUnitId:"",direction:c.Direction.RIGHT}},d),I=s.runRefRangeMutations(E,d);if(I==null)return;A={...d,...I}}else if(i===b.RemoveRow){const E=s.handleIRemoveRow({id:s.EffectRefRangId.RemoveRowCommandId,params:{range:f}},d,p),I=s.runRefRangeMutations(E,d);if(I==null)return t.ErrorType.REF;A={...d,...I}}else if(i===b.RemoveColumn){const E=s.handleIRemoveCol({id:s.EffectRefRangId.RemoveColCommandId,params:{range:f}},d),I=s.runRefRangeMutations(E,d);if(I==null)return t.ErrorType.REF;A={...d,...I}}else if(i===b.DeleteMoveLeft){const E=s.handleDeleteRangeMoveLeft({id:s.EffectRefRangId.DeleteRangeMoveLeftCommandId,params:{range:f}},d),I=s.runRefRangeMutations(E,d);if(I==null)return t.ErrorType.REF;A={...d,...I}}else if(i===b.DeleteMoveUp){const E=s.handleDeleteRangeMoveUp({id:s.EffectRefRangId.DeleteRangeMoveUpCommandId,params:{range:f}},d),I=s.runRefRangeMutations(E,d);if(I==null)return t.ErrorType.REF;A={...d,...I}}else if(i===b.InsertMoveDown){const E=s.handleInsertRangeMoveDown({id:s.EffectRefRangId.InsertRangeMoveDownCommandId,params:{range:f}},d),I=s.runRefRangeMutations(E,d);if(I==null)return;A={...d,...I}}else if(i===b.InsertMoveRight){const E=s.handleInsertRangeMoveRight({id:s.EffectRefRangId.InsertRangeMoveRightCommandId,params:{range:f}},d),I=s.runRefRangeMutations(E,d);if(I==null)return;A={...d,...I}}}if(A!=null)return t.serializeRangeToRefString({range:A,sheetName:S,unitId:T})}function St(r,e,a,n,i,o){const{startRow:u,endRow:f,startColumn:m,endColumn:l,rangeType:p}=et(e),{startRow:L,startColumn:N,endRow:T,endColumn:S,rangeType:R=c.RANGE_TYPE.NORMAL}=et(a),{startRow:C,startColumn:d,endRow:A,endColumn:E}=et(n),{startRow:I,endRow:O,startColumn:D,endColumn:U}=et(o),{startRow:_,endRow:x,startColumn:y,endColumn:q,rangeType:h=c.RANGE_TYPE.NORMAL}=et(i),M={...i};function B(){return p===c.RANGE_TYPE.COLUMN&&h===c.RANGE_TYPE.COLUMN?!0:m>=y&&l<=q}function K(){return p===c.RANGE_TYPE.ROW&&h===c.RANGE_TYPE.ROW?!0:u>=_&&f<=x}if(r===0)if(B())if(u<_)M.startRow=u;else if(u>=x)M.endRow-=T+1-_;else return;else return;else if(r===1)if(B())if(f>x)M.endRow=f;else if(f<=_)M.startRow+=x-L+1;else return;else return;else if(r===2)if(K())if(m<y)M.startColumn=m;else if(m>=q)M.endColumn-=S+1-y;else return;else return;else if(r===3)if(K())if(l>q)M.endColumn=l;else if(l<=y)M.startColumn+=q-N+1;else return;else return;else r===4?(M.startRow=u,M.startColumn=m,M.endRow=f,M.endColumn=l):N<=y&&S>=q||R===c.RANGE_TYPE.ROW&&h===c.RANGE_TYPE.ROW?a.endRow<_?(C>=_&&(M.startRow-=T-L+1),C>=x&&(M.endRow-=T-L+1)):a.startRow>x?(A<=x&&(M.endRow+=T-L+1),A<=_&&(M.startRow+=T-L+1)):a.startRow>=_&&a.endRow<=x&&(C<=_?M.startRow+=T-L+1:C>=x&&(M.endRow-=T-L+1)):L<=_&&T>=x||R===c.RANGE_TYPE.COLUMN&&h===c.RANGE_TYPE.COLUMN?a.endColumn<y?(d>=y&&(M.startColumn-=S-N+1),d>=q&&(M.endColumn-=S-N+1)):a.startColumn>q?(E<=q&&(M.endColumn+=S-N+1),E<=y&&(M.startColumn+=S-N+1)):a.startColumn>=y&&a.endColumn<=q&&(d<=y?M.startColumn+=S-N+1:d>=q&&(M.endColumn-=S-N+1)):((d<=U+1&&E>=q||d<=y&&E>=D-1)&&C<=_&&A>=x||(C<=O+1&&A>=x||C<=_&&A>=I-1)&&d<=y&&E>=q,M.startRow=u,M.startColumn=m,M.endRow=f,M.endColumn=l);return M}function Ct(r,e){const a=z(r.startRow),n=Z(r.endRow),i=z(r.startColumn),o=Z(r.endColumn),u=z(e.startRow),f=Z(e.endRow),m=z(e.startColumn),l=Z(e.endColumn);function p(){return r.rangeType===c.RANGE_TYPE.COLUMN&&e.rangeType===c.RANGE_TYPE.COLUMN?!0:a>=u&&n<=f}function L(){return r.rangeType===c.RANGE_TYPE.ROW&&e.rangeType===c.RANGE_TYPE.ROW?!0:i>=m&&o<=l}function N(){return r.rangeType===c.RANGE_TYPE.ALL&&e.rangeType===c.RANGE_TYPE.ALL}return p()&&L()||N()?4:L()&&a>=u&&a<=f&&n>f?0:L()&&n>=u&&n<=f&&a<u?1:p()&&i>=m&&i<=l&&o>l?2:p()&&o>=m&&o<=l&&i<m?3:null}function z(r){return isNaN(r)?-1/0:r}function Z(r){return isNaN(r)?1/0:r}function et(r){const{startRow:e,endRow:a,startColumn:n,endColumn:i}=r;return{...r,startRow:z(e),endRow:Z(a),startColumn:z(n),endColumn:Z(i)}}function Gt(r,e){const{id:a}=e;let n=null;switch(a){case s.MoveRangeCommand.id:n=Pe(e,r);break;case s.MoveRowsCommand.id:n=Ee(e,r);break;case s.MoveColsCommand.id:n=Ie(e,r);break;case s.InsertRowCommand.id:n=Ae(e);break;case s.InsertColCommand.id:n=Se(e);break;case s.InsertRangeMoveRightCommand.id:n=Ce(e,r);break;case s.InsertRangeMoveDownCommand.id:n=Re(e,r);break;case s.RemoveRowCommand.id:n=Oe(e,r);break;case s.RemoveColCommand.id:n=Me(e,r);break;case s.DeleteRangeMoveUpCommand.id:n=_e(e,r);break;case s.DeleteRangeMoveLeftCommand.id:n=De(e,r);break;case s.SetWorksheetNameCommand.id:n=be(e,r);break;case s.RemoveSheetCommand.id:n=ye(e,r);break;case s.SetDefinedNameCommand.id:n=xe(e,r);break;case s.RemoveDefinedNameCommand.id:n=Ue(e,r);break}return n}function v(r){var n;const e=r.getUnitId(),a=((n=r.getActiveSheet())==null?void 0:n.getSheetId())||"";return{unitId:e,sheetId:a}}function Pe(r,e){const{params:a}=r;if(!a)return null;const{fromRange:n,toRange:i}=a;if(!n||!i)return null;const{unitId:o,sheetId:u}=v(e);return{type:b.MoveRange,from:n,to:i,unitId:o,sheetId:u}}function Ee(r,e){const{params:a}=r;if(!a)return null;const{fromRange:{startRow:n,endRow:i},toRange:{startRow:o,endRow:u}}=a,f=e.getUnitId(),m=e.getActiveSheet();if(!m)return null;const l=m.getSheetId(),p={startRow:n,startColumn:0,endRow:i,endColumn:m.getColumnCount()-1,rangeType:c.RANGE_TYPE.ROW},L={startRow:o,startColumn:0,endRow:u,endColumn:m.getColumnCount()-1,rangeType:c.RANGE_TYPE.ROW};return{type:b.MoveRows,from:p,to:L,unitId:f,sheetId:l}}function Ie(r,e){const{params:a}=r;if(!a)return null;const{fromRange:{startColumn:n,endColumn:i},toRange:{startColumn:o,endColumn:u}}=a,f=e.getUnitId(),m=e.getActiveSheet();if(!m)return null;const l=m.getSheetId(),p={startRow:0,startColumn:n,endRow:m.getRowCount()-1,endColumn:i,rangeType:c.RANGE_TYPE.COLUMN},L={startRow:0,startColumn:o,endRow:m.getRowCount()-1,endColumn:u,rangeType:c.RANGE_TYPE.COLUMN};return{type:b.MoveCols,from:p,to:L,unitId:f,sheetId:l}}function Ae(r){const{params:e}=r;if(!e)return null;const{range:a,unitId:n,subUnitId:i}=e;return{type:b.InsertRow,range:a,unitId:n,sheetId:i}}function Se(r){const{params:e}=r;if(!e)return null;const{range:a,unitId:n,subUnitId:i}=e;return{type:b.InsertColumn,range:a,unitId:n,sheetId:i}}function Ce(r,e){const{params:a}=r;if(!a)return null;const{range:n}=a,{unitId:i,sheetId:o}=v(e);return{type:b.InsertMoveRight,range:n,unitId:i,sheetId:o}}function Re(r,e){const{params:a}=r;if(!a)return null;const{range:n}=a,{unitId:i,sheetId:o}=v(e);return{type:b.InsertMoveDown,range:n,unitId:i,sheetId:o}}function Oe(r,e){var u,f;const{params:a}=r;if(!a)return null;const{range:n}=a,{unitId:i,sheetId:o}=v(e);return{type:b.RemoveRow,range:n,unitId:i,sheetId:o,rangeFilteredRows:(f=(u=e.getSheetBySheetId(o))==null?void 0:u.getRangeFilterRows(n))!=null?f:[]}}function Me(r,e){const{params:a}=r;if(!a)return null;const{range:n}=a,{unitId:i,sheetId:o}=v(e);return{type:b.RemoveColumn,range:n,unitId:i,sheetId:o}}function _e(r,e){const{params:a}=r;if(!a)return null;const{range:n}=a,{unitId:i,sheetId:o}=v(e);return{type:b.DeleteMoveUp,range:n,unitId:i,sheetId:o}}function De(r,e){const{params:a}=r;if(!a)return null;const{range:n}=a,{unitId:i,sheetId:o}=v(e);return{type:b.DeleteMoveLeft,range:n,unitId:i,sheetId:o}}function be(r,e){const{params:a}=r;if(!a)return null;const{unitId:n,subUnitId:i,name:o}=a,{unitId:u,sheetId:f}=v(e);return{type:b.SetName,unitId:n||u,sheetId:i||f,sheetName:o}}function ye(r,e){const{params:a}=r;if(!a)return null;const{unitId:n,subUnitId:i}=a,{unitId:o,sheetId:u}=v(e);return{type:b.RemoveSheet,unitId:n||o,sheetId:i||u}}function xe(r,e){const{params:a}=r;if(!a)return null;const{unitId:n,name:i,id:o}=a,{sheetId:u}=v(e);return{type:b.SetDefinedName,unitId:n,sheetId:u,definedName:i,definedNameId:o}}function Ue(r,e){const{params:a}=r;if(!a)return null;const{unitId:n,name:i,id:o}=a,{sheetId:u}=v(e);return{type:b.RemoveDefinedName,unitId:n,sheetId:u,definedName:i,definedNameId:o}}var qe=Object.getOwnPropertyDescriptor,he=(r,e,a,n)=>{for(var i=n>1?void 0:n?qe(e,a):e,o=r.length-1,u;o>=0;o--)(u=r[o])&&(i=u(i)||i);return i},rt=(r,e)=>(a,n)=>e(a,n,r);P.UpdateDefinedNameController=class extends c.Disposable{constructor(e,a,n,i){super(),this._definedNamesService=e,this._univerInstanceService=a,this._sheetInterceptorService=n,this._lexerTreeBuilder=i,this._initialize()}_initialize(){this._commandExecutedListener()}_commandExecutedListener(){this.disposeWithMe(this._sheetInterceptorService.interceptCommand({getMutations:e=>{if(e.id===s.SetDefinedNameCommand.id||e.id===s.RemoveDefinedNameCommand.id)return{redos:[],undos:[]};const a=this._univerInstanceService.getCurrentUnitForType(c.UniverInstanceType.UNIVER_SHEET);if(a==null)return{redos:[],undos:[]};const n=Gt(a,e);return n?this._getUpdateDefinedNameMutations(a,n):{redos:[],undos:[]}}}))}_getUpdateDefinedNameMutations(e,a){const{type:n,unitId:i,sheetId:o}=a,u=this._definedNamesService.getDefinedNameMap(i);if(!u)return{redos:[],undos:[]};const f=[],m=[];return Object.values(u).forEach(l=>{var d;const{formulaOrRefString:p}=l,L=this._lexerTreeBuilder.sequenceNodesBuilder(p);if(L==null)return!0;let N=!1;const T=[];for(let A=0,E=L.length;A<E;A++){const I=L[A];if(typeof I=="string"||I.nodeType!==t.sequenceNodeType.REFERENCE)continue;const{token:O}=I,D=t.deserializeRangeWithSheetWithCache(O),{range:U,sheetName:_,unitId:x}=D,y=((d=e.getSheetBySheetName(_))==null?void 0:d.getSheetId())||"",q={range:U,sheetId:y,unitId:x,sheetName:_,refOffsetX:0,refOffsetY:0};let h=null;if(n===b.RemoveSheet)h=this._removeSheet(l,i,o);else if(n===b.SetName){const{sheetId:M,sheetName:B}=a;if(B==null||y==null||y.length===0||M!==y)continue;h=t.serializeRangeToRefString({range:U,sheetName:B,unitId:x})}else h=Vt(q,a,i,o);h!=null&&(L[A]={...I,token:h},N=!0,T.push(A))}if(!N)return!0;const S=t.generateStringWithSequence(vt(L,T)),R={id:t.SetDefinedNameMutation.id,params:{unitId:i,...l,formulaOrRefString:S}};f.push(R);const C={id:t.SetDefinedNameMutation.id,params:{unitId:i,...l}};m.push(C)}),{redos:f,undos:m}}_removeSheet(e,a,n){var u;const{formulaOrRefString:i}=e;return((u=this._definedNamesService.getWorksheetByRef(a,i))==null?void 0:u.getSheetId())===n?t.ErrorType.REF:null}},P.UpdateDefinedNameController=he([rt(0,t.IDefinedNamesService),rt(1,c.IUniverInstanceService),rt(2,c.Inject(s.SheetInterceptorService)),rt(3,c.Inject(t.LexerTreeBuilder))],P.UpdateDefinedNameController);var Fe=Object.getOwnPropertyDescriptor,Be=(r,e,a,n)=>{for(var i=n>1?void 0:n?Fe(e,a):e,o=r.length-1,u;o>=0;o--)(u=r[o])&&(i=u(i)||i);return i},X=(r,e)=>(a,n)=>e(a,n,r);P.UpdateFormulaController=class extends c.Disposable{constructor(e,a,n,i,o,u,f,m){super(),this._univerInstanceService=e,this._commandService=a,this._lexerTreeBuilder=n,this._formulaDataModel=i,this._sheetInterceptorService=o,this._definedNamesService=u,this._configService=f,this._injector=m,this._commandExecutedListener()}_commandExecutedListener(){this.disposeWithMe(this._sheetInterceptorService.interceptCommand({getMutations:e=>this._getUpdateFormula(e)})),this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(e.params)if(e.id===s.RemoveSheetMutation.id){const{subUnitId:a,unitId:n}=e.params;this._handleWorkbookDisposed(n,a)}else e.id===s.InsertSheetMutation.id&&this._handleInsertSheetMutation(e.params)})),this.disposeWithMe(this._commandService.beforeCommandExecuted((e,a)=>{if(e.id===s.SetRangeValuesMutation.id){const n=e.params;if(a&&a.onlyLocal===!0||a&&a.syncOnly===!0||n.trigger===s.SetStyleCommand.id||n.trigger===s.SetBorderCommand.id||n.trigger===s.ClearSelectionFormatCommand.id)return;this._handleSetRangeValuesMutation(n)}})),this.disposeWithMe(this._univerInstanceService.getTypeOfUnitAdded$(c.UniverInstanceType.UNIVER_SHEET).subscribe(e=>this._handleWorkbookAdded(e))),this.disposeWithMe(this._univerInstanceService.getTypeOfUnitDisposed$(c.UniverInstanceType.UNIVER_SHEET).pipe(bt.map(e=>e.getUnitId())).subscribe(e=>this._handleWorkbookDisposed(e)))}_handleSetRangeValuesMutation(e){const{subUnitId:a,unitId:n,cellValue:i}=e;if(i==null)return;const o=this._formulaDataModel.updateFormulaData(n,a,i);if(Object.keys(o).length===0)return;const u={[n]:{[a]:o}};this._commandService.executeCommand(s.SetRangeValuesMutation.id,{unitId:n,subUnitId:a,cellValue:Ne(o)},{onlyLocal:!0,fromFormula:!0}),this._formulaDataModel.updateArrayFormulaCellData(n,a,i),this._formulaDataModel.updateArrayFormulaRange(n,a,i),this._formulaDataModel.updateImageFormulaData(n,a,i),this._commandService.executeCommand(t.SetFormulaDataMutation.id,{formulaData:u},{onlyLocal:!0}),this._commandService.executeCommand(t.SetArrayFormulaDataMutation.id,{arrayFormulaRange:this._formulaDataModel.getArrayFormulaRange(),arrayFormulaCellData:this._formulaDataModel.getArrayFormulaCellData()},{onlyLocal:!0,remove:!0})}_handleWorkbookDisposed(e,a){const n=this._formulaDataModel.getFormulaData(),i=At(n,e,a),o=this._formulaDataModel.getArrayFormulaRange(),u=At(o,e,a),f=this._formulaDataModel.getArrayFormulaCellData(),m=At(f,e,a);i&&this._commandService.executeCommand(t.SetFormulaDataMutation.id,{formulaData:i},{onlyLocal:!0}),u&&m&&this._commandService.executeCommand(t.SetArrayFormulaDataMutation.id,{arrayFormulaRange:o,arrayFormulaCellData:f},{onlyLocal:!0})}_handleInsertSheetMutation(e){const{sheet:a,unitId:n}=e,i=this._formulaDataModel.getFormulaData(),{id:o,cellData:u}=a,f=new c.ObjectMatrix(u),m=t.initSheetFormulaData(i,n,o,f);this._commandService.executeCommand(t.SetFormulaDataMutation.id,{formulaData:m},{onlyLocal:!0})}_handleWorkbookAdded(e){var l;const a={},n=e.getUnitId(),i={[n]:{}};e.getSheets().forEach(p=>{var S;const L=p.getCellMatrix(),N=p.getSheetId(),T=t.initSheetFormulaData(a,n,N,L);i[n][N]=(S=T[n])==null?void 0:S[N]}),this._commandService.executeCommand(t.SetFormulaDataMutation.id,{formulaData:i},{onlyLocal:!0});const u=this._configService.getConfig($),f=(l=u==null?void 0:u.initialFormulaComputing)!=null?l:Y.WHEN_EMPTY,m=this._getDirtyDataByCalculationMode(f);this._commandService.executeCommand(t.SetTriggerFormulaCalculationStartMutation.id,m,{onlyLocal:!0})}_getDirtyDataByCalculationMode(e){const a=e===Y.FORCED,n=e===Y.WHEN_EMPTY?this._formulaDataModel.getFormulaDirtyRanges():[];return{forceCalculation:a,dirtyRanges:n,dirtyNameMap:{},dirtyDefinedNameMap:{},dirtyUnitFeatureMap:{},dirtyUnitOtherFormulaMap:{},clearDependencyTreeCache:{}}}_getUpdateFormula(e){const a=this._univerInstanceService.getCurrentUnitForType(c.UniverInstanceType.UNIVER_SHEET);if(!a)return{undos:[],redos:[]};const n=Gt(a,e);if(n){const{unitSheetNameMap:i}=this._formulaDataModel.getCalculateData(),o=this._formulaDataModel.getFormulaData(),{newFormulaData:u}=this._getFormulaReferenceMoveInfo(o,i,n),{undos:f,redos:m}=$t(o,u,n);return{undos:f,redos:m}}return{undos:[],redos:[]}}_getFormulaReferenceMoveInfo(e,a,n){if(!c.Tools.isDefine(e))return{newFormulaData:{},oldFormulaData:{}};const i=Object.keys(e);if(i.length===0)return{newFormulaData:{},oldFormulaData:{}};const o={},u={};for(const f of i){const m=e[f];if(m==null)continue;const l=Object.keys(m);c.Tools.isDefine(o[f])||(o[f]={}),c.Tools.isDefine(u[f])||(u[f]={});for(const p of l){const L=new c.ObjectMatrix(m[p]||{}),N=new c.ObjectMatrix,T=[];L.forValue((S,R,C)=>{var q;if(!C)return!0;const{f:d,x:A,y:E,si:I}=C,O=this._lexerTreeBuilder.sequenceNodesBuilder(d);if(O==null)return!0;let D=!1;const U=[],{type:_,from:x}=n;for(let h=0,M=O.length;h<M;h++){const B=O[h];if(typeof B=="string")continue;const{token:K,nodeType:k}=B;if((_===b.SetDefinedName||_===b.RemoveDefinedName)&&(k===t.sequenceNodeType.DEFINED_NAME||k===t.sequenceNodeType.FUNCTION)){const{definedNameId:Pt,definedName:J}=n;if(Pt===void 0||J===void 0)continue;const j=this._definedNamesService.getValueById(f,Pt);if(j==null||j.name!==K)continue;O[h]={...B,token:_===b.SetDefinedName?J:t.ErrorType.REF},D=!0,U.push(h);continue}else if(k!==t.sequenceNodeType.REFERENCE)continue;const Q=t.deserializeRangeWithSheetWithCache(K),{range:V,sheetName:H,unitId:w}=Q,Nt=w==null||w.length===0?f:w,G=((q=a==null?void 0:a[Nt])==null?void 0:q[H])||"";if(!Bt(n.unitId,n.sheetId,f,p,w,G))continue;const Dt={range:V,sheetId:G,unitId:w,sheetName:H,refOffsetX:A||0,refOffsetY:E||0};let nt=null;if(_===b.SetName){const{unitId:Pt,sheetId:J,sheetName:j}=n;if(j==null||G==null||G.length===0||J!==G)continue;nt=t.serializeRangeToRefString({range:V,sheetName:j,unitId:w})}else if(_===b.RemoveSheet){const{unitId:Pt,sheetId:J,sheetName:j}=n;if(G==null||G.length===0||J!==G)continue;nt=t.ErrorType.REF}else _!==b.SetDefinedName&&(nt=Vt(Dt,n,f,p));nt!=null&&(O[h]={...B,token:nt},D=!0,U.push(h),I&&(A!=null?A:0)===0&&(E!=null?E:0)===0&&T.push(I))}if(!D)if(I&&[b.MoveRows,b.MoveCols,b.MoveRange].includes(_)){if(x&&x.startRow<=S&&S<=x.endRow&&x.startColumn<=R&&R<=x.endColumn)(A!=null?A:0)===0&&(E!=null?E:0)===0&&T.push(I);else if(!T.includes(I))return!0}else return!0;const y=vt(O,U,A,E);N.setValue(S,R,{f:`=${t.generateStringWithSequence(y)}`})}),u[f]&&(u[f][p]=N.getData())}}return{newFormulaData:u}}},P.UpdateFormulaController=Be([X(0,c.IUniverInstanceService),X(1,c.ICommandService),X(2,c.Inject(t.LexerTreeBuilder)),X(3,c.Inject(t.FormulaDataModel)),X(4,c.Inject(s.SheetInterceptorService)),X(5,t.IDefinedNamesService),X(6,c.IConfigService),X(7,c.Inject(c.Injector))],P.UpdateFormulaController);const ve="SHEETS_FORMULA_PLUGIN";var Ve=Object.getOwnPropertyDescriptor,Ge=(r,e,a,n)=>{for(var i=n>1?void 0:n?Ve(e,a):e,o=r.length-1,u;o>=0;o--)(u=r[o])&&(i=u(i)||i);return i},Rt=(r,e)=>(a,n)=>e(a,n,r);let ot=class extends c.Disposable{constructor(r,e,a){super(),this._activeDirtyManagerService=r,this._univerInstanceService=e,this._formulaDataModel=a,this._initialize()}_initialize(){this._initialConversion()}_initialConversion(){this._activeDirtyManagerService.register(s.SetRangeValuesMutation.id,{commandId:s.SetRangeValuesMutation.id,getDirtyData:r=>{const e=r.params;return e.trigger===s.SetStyleCommand.id?{}:{dirtyRanges:this._getSetRangeValuesMutationDirtyRange(e)}}}),this._initialMove(),this._initialRowAndColumn(),this._initialHideRow(),this._initialSheet(),this._initialDefinedName()}_initialMove(){this._activeDirtyManagerService.register(s.MoveRangeMutation.id,{commandId:s.MoveRangeMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyRanges:this._getMoveRangeMutationDirtyRange(e),clearDependencyTreeCache:{[e.unitId]:{[e.to.subUnitId]:"1",[e.from.subUnitId]:"1"}}}}}),this._activeDirtyManagerService.register(s.MoveRowsMutation.id,{commandId:s.MoveRowsMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyRanges:this._getMoveRowsMutationDirtyRange(e),clearDependencyTreeCache:{[e.unitId]:{[e.subUnitId]:"1"}}}}}),this._activeDirtyManagerService.register(s.MoveColsMutation.id,{commandId:s.MoveColsMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyRanges:this._getMoveRowsMutationDirtyRange(e),clearDependencyTreeCache:{[e.unitId]:{[e.subUnitId]:"1"}}}}}),this._activeDirtyManagerService.register(s.ReorderRangeMutation.id,{commandId:s.ReorderRangeMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyRanges:this._getReorderRangeMutationDirtyRange(e),clearDependencyTreeCache:{[e.unitId]:{[e.subUnitId]:"1"}}}}})}_initialRowAndColumn(){this._activeDirtyManagerService.register(s.RemoveRowMutation.id,{commandId:s.RemoveRowMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyRanges:this._getRemoveRowOrColumnMutation(e,!0),clearDependencyTreeCache:{[e.unitId]:{[e.subUnitId]:"1"}}}}}),this._activeDirtyManagerService.register(s.RemoveColMutation.id,{commandId:s.RemoveColMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyRanges:this._getRemoveRowOrColumnMutation(e,!1),clearDependencyTreeCache:{[e.unitId]:{[e.subUnitId]:"1"}}}}}),this._activeDirtyManagerService.register(s.InsertColMutation.id,{commandId:s.InsertColMutation.id,getDirtyData:r=>{const e=r.params;return{clearDependencyTreeCache:{[e.unitId]:{[e.subUnitId]:"1"}}}}}),this._activeDirtyManagerService.register(s.InsertRowMutation.id,{commandId:s.InsertRowMutation.id,getDirtyData:r=>{const e=r.params;return{clearDependencyTreeCache:{[e.unitId]:{[e.subUnitId]:"1"}}}}})}_initialHideRow(){this._activeDirtyManagerService.register(s.SetRowHiddenMutation.id,{commandId:s.SetRowHiddenMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyRanges:this._getHideRowMutation(e),clearDependencyTreeCache:{[e.unitId]:{[e.subUnitId]:"1"}}}}}),this._activeDirtyManagerService.register(s.SetRowVisibleMutation.id,{commandId:s.SetRowVisibleMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyRanges:this._getHideRowMutation(e),clearDependencyTreeCache:{[e.unitId]:{[e.subUnitId]:"1"}}}}})}_initialSheet(){this._activeDirtyManagerService.register(t.SetTriggerFormulaCalculationStartMutation.id,{commandId:t.SetTriggerFormulaCalculationStartMutation.id,getDirtyData:r=>({...r.params})}),this._activeDirtyManagerService.register(s.RemoveSheetMutation.id,{commandId:s.RemoveSheetMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyNameMap:this._getRemoveSheetMutation(e),clearDependencyTreeCache:{[e.unitId]:{[e.subUnitId]:"1"}}}}}),this._activeDirtyManagerService.register(s.InsertSheetMutation.id,{commandId:s.InsertSheetMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyNameMap:this._getInsertSheetMutation(e)}}})}_initialDefinedName(){this._activeDirtyManagerService.register(t.SetDefinedNameMutation.id,{commandId:t.SetDefinedNameMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyDefinedNameMap:this._getDefinedNameMutation(e)}}}),this._activeDirtyManagerService.register(t.RemoveDefinedNameMutation.id,{commandId:t.RemoveDefinedNameMutation.id,getDirtyData:r=>{const e=r.params;return{dirtyDefinedNameMap:this._getDefinedNameMutation(e)}}})}_getDefinedNameMutation(r){const{unitId:e,name:a,formulaOrRefString:n}=r,i={};return r==null?{}:(i[e]={},i[e][a]=n,i)}_getSetRangeValuesMutationDirtyRange(r){const{subUnitId:e,unitId:a,cellValue:n}=r,i=[];return n==null||(i.push(...this._getDirtyRangesByCellValue(a,e,n)),i.push(...this._getDirtyRangesForArrayFormula(a,e,n))),i}_getMoveRangeMutationDirtyRange(r){const{unitId:e,from:a,to:n}=r,i=[];return i.push(...this._getDirtyRangesByCellValue(e,a.subUnitId,a.value)),i.push(...this._getDirtyRangesByCellValue(e,n.subUnitId,n.value)),i.push(...this._getDirtyRangesForArrayFormula(e,n.subUnitId,n.value)),i}_getMoveRowsMutationDirtyRange(r){const{subUnitId:e,unitId:a,sourceRange:n,targetRange:i}=r,o=[],u=this._rangeToMatrix(n).getData(),f=this._rangeToMatrix(i).getData();return o.push(...this._getDirtyRangesByCellValue(a,e,u)),o.push(...this._getDirtyRangesByCellValue(a,e,f)),o.push(...this._getDirtyRangesForArrayFormula(a,e,f)),o}_getReorderRangeMutationDirtyRange(r){const{unitId:e,subUnitId:a,range:n}=r,i=this._rangeToMatrix(n).getData(),o=[];return o.push(...this._getDirtyRangesByCellValue(e,a,i)),o.push(...this._getDirtyRangesForArrayFormula(e,a,i)),o}_getRemoveRowOrColumnMutation(r,e=!0){const{subUnitId:a,unitId:n,range:i}=r,o=[],u=this._univerInstanceService.getUniverSheetInstance(n),f=u==null?void 0:u.getSheetBySheetId(a),m=(f==null?void 0:f.getRowCount())||0,l=(f==null?void 0:f.getColumnCount())||0;let p=null;const{startRow:L,endRow:N,startColumn:T,endColumn:S}=i;e===!0?p=this._rangeToMatrix({startRow:L,startColumn:0,endRow:N,endColumn:l-1}):p=this._rangeToMatrix({startRow:0,startColumn:T,endRow:m,endColumn:S});const R=p.getData();return o.push(...this._getDirtyRangesByCellValue(n,a,R)),o.push(...this._getDirtyRangesForArrayFormula(n,a,R)),o}_getHideRowMutation(r){const{subUnitId:e,unitId:a,ranges:n}=r,i=[];return n.forEach(o=>{const u=this._rangeToMatrix(o).getMatrix();i.push(...this._getDirtyRangesByCellValue(a,e,u))}),i}_getRemoveSheetMutation(r){const e={},{subUnitId:a,unitId:n,subUnitName:i}=r;return e[n]==null&&(e[n]={}),e[n][a]=i,e}_getInsertSheetMutation(r){const e={},{sheet:a,unitId:n}=r;return e[n]==null&&(e[n]={}),e[n][a.id]=a.name,e}_rangeToMatrix(r){const e=new c.ObjectMatrix,{startRow:a,startColumn:n,endRow:i,endColumn:o}=r;for(let u=a;u<=i;u++)for(let f=n;f<=o;f++)e.setValue(u,f,{});return e}_getDirtyRangesByCellValue(r,e,a){const n=[];return a==null||new c.ObjectMatrix(a).getDiscreteRanges().forEach(u=>{n.push({unitId:r,sheetId:e,range:u})}),n}_getDirtyRangesForArrayFormula(r,e,a){var u,f;const n=[];if(a==null)return n;const i=new c.ObjectMatrix(a),o=this._formulaDataModel.getArrayFormulaRange();if((u=o==null?void 0:o[r])!=null&&u[e]){const m=new c.ObjectMatrix((f=o==null?void 0:o[r])==null?void 0:f[e]);i.forValue((l,p)=>{m.forValue((L,N,T)=>{if(T==null)return!0;const{startRow:S,startColumn:R,endRow:C,endColumn:d}=T;l>=S&&l<=C&&p>=R&&p<=d&&n.push({unitId:r,sheetId:e,range:{startRow:S,startColumn:R,endRow:S,endColumn:R}})})})}return n}};ot=Ge([Rt(0,t.IActiveDirtyManagerService),Rt(1,c.IUniverInstanceService),Rt(2,c.Inject(t.FormulaDataModel))],ot);var He=Object.getOwnPropertyDescriptor,we=(r,e,a,n)=>{for(var i=n>1?void 0:n?He(e,a):e,o=r.length-1,u;o>=0;o--)(u=r[o])&&(i=u(i)||i);return i},g=(r,e)=>(a,n)=>e(a,n,r);let ut=class extends c.Disposable{constructor(r,e,a,n,i,o,u){super(),this._commandService=r,this._configService=e,this._sheetInterceptorService=a,this._formulaDataModel=n,this._lexerTreeBuilder=i,this._functionService=o,this._definedNamesService=u,this._initialize()}_initialize(){this._commandExecutedListener(),this._initInterceptorCellContent()}_commandExecutedListener(){this.disposeWithMe(this._commandService.onCommandExecuted(r=>{var a;const e=(a=this._configService.getConfig($))==null?void 0:a.writeArrayFormulaToSnapshot;if(r.id===t.SetArrayFormulaDataMutation.id){const n=r.params;if(n==null)return;const{arrayFormulaRange:i,arrayFormulaCellData:o,arrayFormulaEmbedded:u}=n;this._formulaDataModel.setArrayFormulaRange(i),this._formulaDataModel.se