@univerjs/engine-formula 0.10.14-nightly.202511070628 → 0.10.14-nightly.202511070640

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/umd/index.js CHANGED
@@ -1,4 +1,4 @@
1
- (function(w,M){typeof exports=="object"&&typeof module<"u"?M(exports,require("@univerjs/core"),require("rxjs"),require("@univerjs/rpc")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","rxjs","@univerjs/rpc"],M):(w=typeof globalThis<"u"?globalThis:w||self,M(w.UniverEngineFormula={},w.UniverCore,w.rxjs,w.UniverRpc))})(this,(function(w,M,De,Qu){"use strict";var kE=Object.defineProperty;var $E=(w,M,De)=>M in w?kE(w,M,{enumerable:!0,configurable:!0,writable:!0,value:De}):w[M]=De;var A=(w,M,De)=>$E(w,typeof M!="symbol"?M+"":M,De);var ja;var Rt=(i=>(i.FALSE="FALSE",i.TRUE="TRUE",i))(Rt||{}),Nt=(i=>(i[i.SUCCESS=0]="SUCCESS",i[i.ERROR=1]="ERROR",i))(Nt||{}),et=(i=>(i[i.FRONT=0]="FRONT",i[i.BACK=1]="BACK",i))(et||{});function Ua(i,u,e,t,r){for(let n=0,s=i.length;n<s;n++){const a=i[n];if(u!==a.unitId||e!==a.sheetId)continue;const{startRow:o,startColumn:c,endRow:l,endColumn:f}=a.range;if(t>=o&&t<=l&&r>=c&&r<=f)return!0}return!1}var h=(i=>(i.DIV_BY_ZERO="#DIV/0!",i.NAME="#NAME?",i.VALUE="#VALUE!",i.NUM="#NUM!",i.NA="#N/A",i.CYCLE="#CYCLE!",i.REF="#REF!",i.SPILL="#SPILL!",i.CALC="#CALC!",i.ERROR="#ERROR!",i.CONNECT="#GETTING_DATA",i.NULL="#NULL!",i))(h||{});const pt=new Set(Object.values(h)),va=[...new Set(Object.values(h).map(i=>i.length))];var Ta=(i=>(i[i.Financial=0]="Financial",i[i.Date=1]="Date",i[i.Math=2]="Math",i[i.Statistical=3]="Statistical",i[i.Lookup=4]="Lookup",i[i.Database=5]="Database",i[i.Text=6]="Text",i[i.Logical=7]="Logical",i[i.Information=8]="Information",i[i.Engineering=9]="Engineering",i[i.Cube=10]="Cube",i[i.Compatibility=11]="Compatibility",i[i.Web=12]="Web",i[i.Array=13]="Array",i[i.Univer=14]="Univer",i[i.User=15]="User",i[i.DefinedName=16]="DefinedName",i))(Ta||{}),Z=(i=>(i.PLUS="+",i.MINUS="-",i.MULTIPLY="*",i.DIVIDED="/",i.CONCATENATE="&",i.POWER="^",i.EQUALS="=",i.NOT_EQUAL="<>",i.GREATER_THAN=">",i.GREATER_THAN_OR_EQUAL=">=",i.LESS_THAN="<",i.LESS_THAN_OR_EQUAL="<=",i))(Z||{}),$=(i=>(i.EQUALS="=",i.NOT_EQUAL="<>",i.GREATER_THAN=">",i.GREATER_THAN_OR_EQUAL=">=",i.LESS_THAN="<",i.LESS_THAN_OR_EQUAL="<=",i))($||{});const Ir=new Map([["<>",4],["<",4],[">=",4],["=",4],[">",4],["<=",4],["&",3],["+",2],["-",2],["/",1],["*",1],["^",0]]),Vt=new Set(Ir.keys()),qu=new Set(["=","<>",">",">=","<","<="]);var X=(i=>(i.OPEN_BRACKET="(",i.CLOSE_BRACKET=")",i.COMMA=",",i.SINGLE_QUOTATION="'",i.DOUBLE_QUOTATION='"',i.OPEN_BRACES="{",i.CLOSE_BRACES="}",i.COLON=":",i.OPEN_SQUARE_BRACKET="[",i.CLOSE_SQUARE_BRACKET="]",i))(X||{}),qe=(i=>(i.PERCENTAGE="%",i.POUND="#",i))(qe||{});const rs=new Set(["%","#"]);var Ve=(i=>(i.AT="@",i.MINUS="-",i.PLUS="+",i))(Ve||{});const Ku=" ",Ia=[...Object.values($),...Object.values(Z),...Object.values(X),...Object.values(qe),...Object.values(Ve)];function hr(i){return Ia.includes(i)}function Fa(i){for(const u of Ia)if(i.indexOf(u)>-1)return!0;return!1}function Xu(i){return i[0]==="'"&&i[i.length-1]==="'"?i.substring(1,i.length-1):i}function Zu(i){return hr(i)&&i!==X.CLOSE_BRACES&&i!==X.CLOSE_BRACKET&&i!==X.SINGLE_QUOTATION&&i!==X.DOUBLE_QUOTATION||i===" "}const zu=new Set([Z.PLUS,Z.MINUS,Z.MULTIPLY,Z.DIVIDED,Z.CONCATENATE,Z.POWER,Z.EQUALS,Z.NOT_EQUAL,Z.GREATER_THAN,Z.GREATER_THAN_OR_EQUAL,Z.LESS_THAN,Z.LESS_THAN_OR_EQUAL,X.OPEN_BRACKET,X.COMMA,X.COLON,X.OPEN_BRACES,X.OPEN_SQUARE_BRACKET]);function Ju(i){return zu.has(i)}const ec=new Set([Z.PLUS,Z.MINUS,Z.MULTIPLY,Z.DIVIDED,Z.CONCATENATE,Z.POWER,Z.EQUALS,Z.NOT_EQUAL,Z.GREATER_THAN,Z.GREATER_THAN_OR_EQUAL,Z.LESS_THAN,Z.LESS_THAN_OR_EQUAL,X.OPEN_BRACKET,X.COMMA,X.COLON,X.OPEN_BRACES,X.OPEN_SQUARE_BRACKET,qe.PERCENTAGE,qe.POUND]);function tc(i){return ec.has(i)}const Ba='\\[([^\\[\\]\\/?:"<>|*\\\\]+)\\]',ka=new RegExp(Ba),rc="((?![\\[\\]\\/?*\\\\]).)*!",Ot="$",ns="\\s*?:\\s*?",Fr="[A-Za-z]+",Br="[1-9][0-9]*",zt=`'?(${Ba})?(${rc})?'?`,ss=`\\${Ot}?${Fr}\\${Ot}?${Br}`,nc=`^(${Ve.AT})?${zt}${ss}${ns}${ss}$`,$a=new RegExp(nc),sc=`^${zt}\\s*?${ss}(${qe.POUND})?$`,Ya=new RegExp(sc),ac=`^${zt}\\${Ot}?${Br}${ns}\\${Ot}?${Br}$`,Ha=new RegExp(ac),ic=`^${zt}\\${Ot}?${Fr}${ns}\\${Ot}?${Fr}$`,Ga=new RegExp(ic),oc=`^${zt}\\s*?\\${Ot}?${Br}$`,Wa=new RegExp(oc),uc=`^${zt}\\s*?\\${Ot}?${Fr}$`,Qa=new RegExp(uc),cc="{.*?}",qa=new RegExp(cc,"g");function gr(i){return Ya.lastIndex=0,Ya.test(i)}function lc(i){return $a.lastIndex=0,$a.test(i)}function Ka(i){return Ha.lastIndex=0,Ha.test(i)}function Xa(i){return Ga.lastIndex=0,Ga.test(i)}function Za(i){return Wa.lastIndex=0,Wa.test(i)}function za(i){return Qa.lastIndex=0,Qa.test(i)}function fc(i){return qa.lastIndex=0,qa.test(i)}function as(i){return gr(i)||lc(i)||Ka(i)||Xa(i)}function Ja(i){const u={};return Object.keys(i).forEach(e=>{const t=i[e];if(t==null)return!0;u[e]==null&&(u[e]={}),Object.keys(t).forEach(r=>{const n=t[r];u[e][r]=new M.ObjectMatrix(n)})}),u}function mc(i){const u={};for(const e in i){const t=i[e];if(t!=null){u[e]==null&&(u[e]={});for(const r in t){const n=t[r],s={};n.forValue((a,o,c)=>{s[a]===void 0&&(s[a]={}),s[a][o]=c}),u[e][r]=s}}}return u}const ei={id:"formula.mutation.register-function",type:M.CommandType.MUTATION,handler:()=>!0},kr={id:"formula.mutation.set-array-formula-data",type:M.CommandType.MUTATION,handler:(i,u)=>!0},hc=/[^0-9]/g,gc=/[^A-Za-z]/g;function dr(i){let u=i[0]==="$";const e=i.substring(1);let t=e.indexOf("$")>-1;return M.Tools.isStringNumber(e)&&u&&!t&&(u=!1,t=!0),u&&t?M.AbsoluteRefType.ALL:u?M.AbsoluteRefType.COLUMN:t?M.AbsoluteRefType.ROW:M.AbsoluteRefType.NONE}function dc(i){const u=i.split("!");u.length>1&&(i=u[u.length-1]);const e=i.split(":");return e.length>1?{startAbsoluteRefType:dr(e[0]),endAbsoluteRefType:dr(e[1])}:{startAbsoluteRefType:dr(e[0])}}function ti(i=M.AbsoluteRefType.NONE){let u="",e="";return i===M.AbsoluteRefType.ROW?u="$":i===M.AbsoluteRefType.COLUMN?e="$":i===M.AbsoluteRefType.ALL&&(u="$",e="$"),{rowAbsoluteString:u,columnAbsoluteString:e}}function Ct(i){const{startColumn:u,startRow:e,endColumn:t,endRow:r,startAbsoluteRefType:n,endAbsoluteRefType:s,rangeType:a=M.RANGE_TYPE.NORMAL}=i,o=ti(n),c=ti(s);if(a===M.RANGE_TYPE.ROW||a===M.RANGE_TYPE.ALL){const m=`${o.rowAbsoluteString}${e+1}`,d=`${c.rowAbsoluteString}${r+1}`;return`${m}:${d}`}if(a===M.RANGE_TYPE.COLUMN){const m=`${o.columnAbsoluteString}${M.Tools.chatAtABC(u)}`,d=`${c.columnAbsoluteString}${M.Tools.chatAtABC(t)}`;return`${m}:${d}`}const l=`${o.columnAbsoluteString}${M.Tools.chatAtABC(u)}${o.rowAbsoluteString}${e+1}`,f=`${c.columnAbsoluteString}${M.Tools.chatAtABC(t)}${c.rowAbsoluteString}${r+1}`;return l===f?l:`${l}:${f}`}function ri(i,u){return`${ii(i)}!${Ct(u)}`}function ni(i,u,e){return Gr(i)||Gr(u)?`'[${Wr(i)}]${Wr(u)}'!${Ct(e)}`:`[${i}]${u}!${Ct(e)}`}function $r(i){const{unitId:u,sheetName:e,range:t}=i;return u!=null&&u.length>0&&e!=null&&e.length>0?ni(u,e,t):e!=null&&e.length>0?ri(e,t):Ct(t)}function Yr(i){const u=Number.parseInt(i.replace(hc,""))-1,e=M.Tools.ABCatNum(i.replace(gc,"")),t=dr(i);return{row:u,column:e,absoluteRefType:t}}function Hr(i){const u=ka.exec(i);let e="";u!=null&&(e=u[0].trim(),e=is(e.slice(1,e.length-1)),i=i.replace(ka,""));const t=i.indexOf("!");let r="",n="";return t>-1?(r=i.substring(0,t),r[0]==="'"&&r[r.length-1]==="'"&&(r=r.substring(1,r.length-1)),r=is(r),n=i.substring(t+1)):n=i,{refBody:n,sheetName:r,unitId:e}}function Jt(i){const{refBody:u,sheetName:e,unitId:t}=Hr(i),r=u.indexOf(":");if(r===-1){const _=Yr(u),C=_.row,E=_.column,b=_.absoluteRefType;return{unitId:t,sheetName:e,range:{startRow:C,startColumn:E,endRow:C,endColumn:E,startAbsoluteRefType:b,endAbsoluteRefType:b}}}const n=u.substring(0,r),s=u.substring(r+1),a=Yr(n),o=Yr(s),c=a.row>o.row?o.row:a.row,l=a.column>o.column?o.column:a.column,f=a.row>o.row?a.row:o.row,m=a.column>o.column?a.column:o.column;let d=M.RANGE_TYPE.NORMAL;return Number.isNaN(c)&&Number.isNaN(f)?d=M.RANGE_TYPE.COLUMN:Number.isNaN(l)&&Number.isNaN(m)&&(d=M.RANGE_TYPE.ROW),{unitId:t,sheetName:e,range:{startRow:c,startColumn:l,endRow:f,endColumn:m,startAbsoluteRefType:a.absoluteRefType,endAbsoluteRefType:o.absoluteRefType,rangeType:d}}}const Cc=["LOG10"];function Cr(i){const u=si(i);if(!as(u)||Cc.includes(u.toUpperCase().trim()))return!1;const{range:e}=Jt(u);return!(e.endColumn>=16384)}function si(i){const u=[];let e=!1;for(let t=0,r=i.length;t<r;t++){const n=i[t];if(n===Ku&&!e)u.push(n);else{if(!e&&(n===Ve.AT||n===Ve.MINUS||n===Ve.PLUS))continue;u.push(n),e=!0}}return u.join("")}function _c(i,u){const e=(i==null?void 0:i.split(","))||[];return i===""||e.length===0?[]:ai(i)?e.map(n=>{const s=Jt(n);return{unitId:s.unitId,sheetId:u(s.sheetName),range:s.range,sheetName:s.sheetName}}):[]}function ai(i){return((i==null?void 0:i.split(","))||[]).every(e=>Cr(e.trim()))}function Gr(i){return i.length===0?!1:!!(Fa(i)||Ac(i)||Ec(i)||bc(i)||/[\s!$%^&*()+\-=\[\]{};':"\\|,.<>\/?()]/.test(i))}function ii(i){return Gr(i)?`'${Wr(i)}'`:i}function Wr(i){return i.replace(/'/g,"''")}function is(i){return i.replace(/''/g,"'")}function Ac(i){const u=i.match(/[1-9][0-9]{0,6}/);return/^[A-Z]+[1-9][0-9]{0,6}$/.test(i)&&u!==null}function Ec(i){return/^(R(-?[0-9]+)?C(-?[0-9]+)?|C(-?[0-9]+)?|R(-?[0-9]+)?)$/.test(i)}function bc(i){return!new RegExp("^\\p{Letter}","u").test(i.charAt(0))}var yc=Object.getOwnPropertyDescriptor,Rc=(i,u,e,t)=>{for(var r=t>1?void 0:t?yc(u,e):u,n=i.length-1,s;n>=0;n--)(s=i[n])&&(r=s(r)||r);return r},Nc=(i,u)=>(e,t)=>u(e,t,i);w.DefinedNamesService=class extends M.Disposable{constructor(e){super();A(this,"_definedNameMap",{});A(this,"_nameCacheMap",{});A(this,"_update$",new De.Subject);A(this,"update$",this._update$.asObservable());A(this,"_currentRange",{unitId:"",sheetId:"",range:{startRow:0,endRow:0,startColumn:0,endColumn:0}});A(this,"_currentRange$",new De.Subject);A(this,"currentRange$",this._currentRange$.asObservable());A(this,"_focusRange$",new De.Subject);A(this,"focusRange$",this._focusRange$.asObservable());this._univerInstanceService=e}dispose(){super.dispose(),this._definedNameMap={},this._nameCacheMap={},this._update$.complete(),this._currentRange$.complete(),this._focusRange$.complete()}getWorksheetByRef(e,t){var n;const{sheetName:r}=Hr(t);return(n=this._univerInstanceService.getUnit(e))==null?void 0:n.getSheetBySheetName(r)}focusRange(e,t){const r=this.getValueById(e,t);r!==void 0&&this._focusRange$.next({...r,unitId:e})}setCurrentRange(e){this._currentRange=e,this._currentRange$.next(e)}getCurrentRange(){return this._currentRange}getCurrentRangeForString(){return Ct(this._currentRange.range)}registerDefinedNames(e,t){this._definedNameMap[e]=t,this._updateCache(e),this._update()}registerDefinedName(e,t){this._definedNameMap[e]===void 0&&(this._definedNameMap[e]={}),this._definedNameMap[e][t.id]=t,this._updateCache(e),this._update()}removeDefinedName(e,t){var r;(r=this._definedNameMap[e])==null||delete r[t],this._updateCache(e),this._update()}removeUnitDefinedName(e){delete this._definedNameMap[e],this._updateCache(e),this._update()}getDefinedNameMap(e){return this._definedNameMap[e]}getValueByName(e,t){const r=this._nameCacheMap[e];if(r)return r[t]||null;const n=this._definedNameMap[e];if(n===void 0)return null;let s=null;for(const a of Object.values(n))if(a.name===t){s=a;break}return s&&(this._nameCacheMap[e]=this._nameCacheMap[e]||{},this._nameCacheMap[e][t]=s),s}getValueById(e,t){var r;return(r=this._definedNameMap[e])==null?void 0:r[t]}hasDefinedName(e){return this._definedNameMap[e]===void 0?!1:(Array.from(Object.values(this._definedNameMap[e])).length||0)!==0}_update(){this._update$.next(null)}_updateCache(e){const t=this._definedNameMap[e];if(t===void 0){delete this._nameCacheMap[e];return}this._nameCacheMap[e]={};for(const r of Object.values(t))this._nameCacheMap[e][r.name]=r}},w.DefinedNamesService=Rc([Nc(0,M.IUniverInstanceService)],w.DefinedNamesService);const Tt=M.createIdentifier("univer.formula.defined-names.service"),pc=(i,u)=>{const{unitId:e,id:t}=u;return{...i.get(Tt).getValueById(e,t),unitId:e}},Qr={id:"formula.mutation.set-defined-name",type:M.CommandType.MUTATION,handler:(i,u)=>{if(u==null)return!1;const e=i.get(Tt),{id:t,unitId:r,name:n,formulaOrRefString:s,comment:a,hidden:o,localSheetId:c}=u;return e.registerDefinedName(r,{id:t,name:n.trim(),formulaOrRefString:s.trim(),comment:a==null?void 0:a.trim(),hidden:o,localSheetId:c}),!0}},os={id:"formula.mutation.remove-defined-name",type:M.CommandType.MUTATION,handler:(i,u)=>{if(u==null)return!1;const e=i.get(Tt),{unitId:t,id:r}=u;return e.removeDefinedName(t,r),!0}},qr={id:"formula.mutation.set-feature-calculation",type:M.CommandType.MUTATION,handler:()=>!0},Kr={id:"formula.mutation.remove-feature-calculation",type:M.CommandType.MUTATION,handler:()=>!0},us={id:"formula.mutation.set-formula-calculation-start",type:M.CommandType.MUTATION,handler:()=>!0},cs={id:"formula.mutation.set-formula-calculation-stop",type:M.CommandType.MUTATION,handler:()=>!0},_r={id:"formula.mutation.set-formula-calculation-notification",type:M.CommandType.MUTATION,handler:()=>!0},ls={id:"formula.mutation.set-formula-calculation-result",type:M.CommandType.MUTATION,handler:()=>!0},fs={id:"formula.mutation.set-formula-data",type:M.CommandType.MUTATION,handler:(i,u)=>!0},Xr={id:"formula.mutation.set-other-formula",type:M.CommandType.MUTATION,handler:()=>!0},Zr={id:"formula.mutation.remove-other-formula",type:M.CommandType.MUTATION,handler:()=>!0},oi={id:"formula.mutation.set-super-table",type:M.CommandType.MUTATION,handler:()=>!0},ui={id:"formula.mutation.remove-super-table",type:M.CommandType.MUTATION,handler:()=>!0},ms={id:"formula.mutation.set-super-table-option",type:M.CommandType.MUTATION,handler:()=>!0};class _t{constructor(u){A(this,"_cache");this._cache=new M.LRUMap(u)}set(u,e){const t=this._hash(u);this._cache.set(t,e)}get(u){const e=this._hash(u);return this._cache.get(e)}clear(){this._cache.clear()}delete(u){this._cache.delete(this._hash(u))}forEach(u,e){this._cache.forEach(u,e)}_hash(u){return u.length<=64?u:M.hashAlgorithm(u).toString()}}const at="P_1",Mt="R_1",er="L_1",Ar="LR_1",ci="LO_1",Vc="LET",Er="LAMBDA",Oc="CUBE",Mc=new Set(["RAND","RANDBETWEEN","NOW","TODAY"]),hs=new _t(1e5);function It(i){const u=hs.get(i);if(u)return u;const e=Jt(i);return hs.set(i,e),Jt(i)}function Sc(){hs.clear()}var Ie=(i=>(i[i.NORMAL=0]="NORMAL",i[i.NUMBER=1]="NUMBER",i[i.STRING=2]="STRING",i[i.FUNCTION=3]="FUNCTION",i[i.REFERENCE=4]="REFERENCE",i[i.ARRAY=5]="ARRAY",i[i.DEFINED_NAME=6]="DEFINED_NAME",i))(Ie||{});function gs(i){let u="";for(const e of i)typeof e=="string"?u+=e:u+=e.token;return u}class ie{constructor(){A(this,"_parent");A(this,"_token",Mt);A(this,"_children",[]);A(this,"_lambdaId");A(this,"_functionDefinitionPrivacyVar");A(this,"_lambdaParameter","");A(this,"_startIndex",-1);A(this,"_endIndex",-1);A(this,"_definedNames",[])}dispose(){var u;this._children.forEach(e=>{typeof e!="string"&&e.dispose()}),(u=this._functionDefinitionPrivacyVar)==null||u.clear(),this._functionDefinitionPrivacyVar=null,this._children=[],this._parent=null,this._definedNames=[]}getDefinedNames(){return this._definedNames}getStartIndex(){return this._startIndex}getLambdaId(){return this._lambdaId}setLambdaId(u){this._lambdaId=u}getFunctionDefinitionPrivacyVar(){return this._functionDefinitionPrivacyVar}setLambdaPrivacyVar(u){this._functionDefinitionPrivacyVar=u}getLambdaParameter(){return this._lambdaParameter}setLambdaParameter(u){this._lambdaParameter=u}getParent(){return this._parent}setParent(u){this._parent=u}getChildren(){return this._children}setChildren(u){this._children=u}addChildren(u){this._children.push(u)}addChildrenFirst(u){this._children.unshift(u)}getToken(){return this._token}setToken(u){this._token=u}setIndex(u,e){this._startIndex=u,this._endIndex=e}setDefinedNames(u){this._definedNames=u}hasDefinedNames(){return this._definedNames.length>0}replaceChild(u,e){const t=this._getIndexInParent(u);t!=null&&(this.getChildren().splice(t,1,e),e.setParent(this))}changeToParent(u){const e=this.getParent();e&&e.removeChild(this),this.setParent(u),u.getChildren().push(this)}removeChild(u){const e=this._getIndexInParent(u);e!=null&&this.getChildren().splice(e,1)}serialize(){const u=this.getToken(),e=this.getChildren(),t=[],r=e.length;for(let n=0;n<r;n++){const s=e[n];s instanceof ie?t.push(s.serialize()):t.push(s)}return{token:u,st:this._startIndex,ed:this._endIndex,children:t}}_getIndexInParent(u){const e=this.getChildren(),t=e.length;for(let r=0;r<t;r++)if(e[r]===u)return r}}const li=2e3,ds=new _t(li),Cs=new _t(li);class zr extends M.Disposable{constructor(){super(...arguments);A(this,"_currentLexerNode",new ie);A(this,"_upLevel",0);A(this,"_segment","");A(this,"_bracketState",[]);A(this,"_squareBracketState",0);A(this,"_bracesState",0);A(this,"_singleQuotationState",0);A(this,"_doubleQuotationState",0);A(this,"_lambdaState",!1);A(this,"_colonState",!1);A(this,"_formulaErrorCount",0);A(this,"_tableBracketState",!1)}dispose(){this._resetTemp(),this._currentLexerNode.dispose(),ds.clear(),Cs.clear()}getUpLevel(){return this._upLevel}isColonClose(){return this._colonState===!1}isColonOpen(){return this._colonState===!0}isDoubleQuotationClose(){return this._doubleQuotationState===0}isLambdaOpen(){return this._lambdaState===!0}isLambdaClose(){return this._lambdaState===!1}isSingleQuotationClose(){return this._singleQuotationState===0}isBracesClose(){return this._bracesState===0}isBracketClose(){return this._bracketState.length===0}isSquareBracketClose(){return this._squareBracketState===0}getCurrentLexerNode(){return this._currentLexerNode}getFunctionAndParameter(e,t){const r=this._getCurrentParamIndex(e,t);if(r==null||r===h.VALUE)return;const n=r[0];if(typeof n=="string")return;let s=n.getParent(),a=n;for(;s;){const o=s.getToken();if(o!==at&&!hr(o)&&s.getStartIndex()!==-1){const c=s.getChildren().indexOf(a);return{functionName:o,paramIndex:c}}a=s,s=s.getParent()}}checkIfAddBracket(e){let t=0,r=e.length-1,n=e[r];for(;(n===X.CLOSE_BRACKET||n===" ")&&r>=0;)n===X.CLOSE_BRACKET&&t++,n=e[--r];const s=this._getCurrentParamIndex(e,e.length-2);if(s==null||s===h.VALUE)return 0;const a=s[0];if(typeof a=="string")return 0;let o=a.getParent(),c=0;for(s[1]===X.OPEN_BRACKET&&c++;o;){const l=o.getToken();l!==at&&l!==X.COLON&&o.getStartIndex()!==-1&&l.toUpperCase()!==Er&&(t===0?c+=1:t--),o=o.getParent()}return c}sequenceNodesBuilder(e){const t=Cs.get(e);if(t)return[...t];const r=this._getSequenceArray(e);if(r.length===0)return;const n=this.getSequenceNode(r);return Cs.set(e,[...n]),n}convertRefersToAbsolute(e,t,r,n=""){const s=this.sequenceNodesBuilder(e);if(s==null)return e;let a="";e.substring(0,1)===Z.EQUALS&&(a=Z.EQUALS);for(let o=0,c=s.length;o<c;o++){const l=s[o];if(typeof l!="string"&&l.nodeType===Ie.REFERENCE){const{token:f,endIndex:m}=l,d=It(f);if(d==null)continue;const{range:_,sheetName:C,unitId:E}=d,b={..._,startAbsoluteRefType:t,endAbsoluteRefType:r},y=$r({range:b,unitId:E,sheetName:C||n}),N=y.length-f.length;s[o]={...l,token:y,endIndex:m+N};for(let V=o+1;V<c;V++){const S=s[V];typeof S!="string"&&(S.startIndex+=N,S.endIndex+=N)}}}return`${a}${gs(s)}`}moveFormulaRefOffset(e,t,r,n=!1){const s=this.sequenceNodesBuilder(e);if(s==null)return e;const a=[];for(let o=0,c=s.length;o<c;o++){const l=s[o];if(typeof l=="string"||l.nodeType!==Ie.REFERENCE){a.push(l);continue}const{token:f}=l,m=It(f),{sheetName:d,unitId:_}=m;let C=m.range;if(!n&&C.startAbsoluteRefType===M.AbsoluteRefType.ALL&&C.endAbsoluteRefType===M.AbsoluteRefType.ALL){a.push(l);continue}else C=M.moveRangeByOffset(C,t,r,n);let E="";M.isValidRange(C)?E=$r({range:C,unitId:_,sheetName:d}):E=h.REF,a.push({...l,token:E})}return`=${gs(a)}`}_formulaSpellCheck(){if(this._currentLexerNode.getChildren().length===0)return!0;const e=this._currentLexerNode.getChildren()[0];return!!(e instanceof ie||Vt.has(e)||rs.has(e)||e===Ve.AT||e===X.COMMA||e===X.COLON||e===X.OPEN_BRACKET)}getSequenceNode(e){const t=[];let r=!1;for(let n=0,s=e.length;n<s;n++){const a=e[n],o=e[n-1],{segment:c,currentString:l}=a;if(l===X.DOUBLE_QUOTATION&&(r=!0),(c!==""||n===0)&&n!==s-1){t.push(l);continue}let f=(o==null?void 0:o.segment)||"";const m=n-f.length;let d=n-1;const _=n-1;if(n===s-1&&this._isLastMergeString(l)&&(f+=l,d+=1),f===""||Ir.has(f)){t.push(l);continue}const C=f.trim(),E=si(C);r===!0&&C[C.length-1]===X.DOUBLE_QUOTATION&&C[0]!==X.OPEN_BRACES?(r=!1,this._processPushSequenceNode(t,Ie.STRING,f,m,d,_)):gr(E)&&Cr(E)?this._processPushSequenceNode(t,Ie.REFERENCE,f,m,d,_):M.Tools.isStringNumber(C)?this._processPushSequenceNode(t,Ie.NUMBER,f,m,d,_):C.length>0&&this._processPushSequenceNode(t,Ie.FUNCTION,f,m,d,_),(n!==s-1||!this._isLastMergeString(l))&&t.push(l)}return this._mergeSequenceNodeReference(t)}_processPushSequenceNode(e,t,r,n,s,a){this._pushSequenceNode(e,{nodeType:t,token:r,startIndex:n,endIndex:s},a)}_getCurrentParamIndex(e,t){return this._nodeMaker(e,void 0,t)}_isLastMergeString(e){return e===X.DOUBLE_QUOTATION||M.Tools.isStringNumber(e)||!hr(e)}_mergeSequenceNodeReference(e){const t=[],r=e.length;let n=0;for(;n<r;){const s=e[n];if(typeof s=="string"){const a=e[n-1];if(s.trim()===X.CLOSE_BRACES&&a!=null&&typeof a!="string"&&a.nodeType===Ie.FUNCTION&&a.token.trim().substring(0,1)===X.OPEN_BRACES){a.nodeType=Ie.ARRAY,a.token+=s,a.endIndex+=s.length,n++;continue}t.push(s)}else{const a=e[n+1],o=e[n+2];a===X.COLON&&typeof s!="string"&&o!=null&&typeof o!="string"&&Cr((s.token+a+o.token).trim())&&(s.nodeType=Ie.REFERENCE,s.token+=a+o.token,s.endIndex=o.endIndex,n+=2),t.push(s)}n++}return this._minusSplitSequenceNode(t)}_minusSplitSequenceNode(e){const t=[];for(const r of e){if(typeof r!="string"){const s=r.token.match(/^(\s*([-@+]\s*)+)(.*)$/);if(s){const a=s[1],o=s[3];if(Cr(o.trim())){const c=a.length,l=r.startIndex,f=r.startIndex+c-1,m={nodeType:Ie.NORMAL,token:a,startIndex:l,endIndex:f},d={nodeType:Ie.REFERENCE,token:o,startIndex:f+1,endIndex:r.endIndex};t.push(m),t.push(d);continue}}}t.push(r)}return t}_pushSequenceNode(e,t,r){const n=r-t.startIndex+1;e.splice(e.length-n,n,t)}nodeMakerTest(e){return this._nodeMaker(e)}treeBuilder(e,t=!0,r,n){if(t===!0){const m=ds.get(e),d=n==null?void 0:n(e);if(m&&!d)return m}this._resetCurrentLexerNode(),this._currentLexerNode.setToken(Mt);const s=[];let a=this._nodeMaker(e,s);if(a===h.VALUE||s.length===0)return a;let o=!1,c="",l=[];if(r){const{hasDefinedName:m,sequenceString:d,definedNames:_}=r(s);o=m,c=d,l=_}if(o&&(this._resetCurrentLexerNode(),this._currentLexerNode.setToken(Mt),a=this._nodeMaker(`=${c}`),a===h.VALUE))return a;const f=this._getTopNode(this._currentLexerNode);if(f&&(this._currentLexerNode=f),t){if(!this._suffixExpressionHandler(this._currentLexerNode))return h.VALUE;ds.set(e,this._currentLexerNode)}return o&&this._currentLexerNode.setDefinedNames(l),this._currentLexerNode}_suffixExpressionHandler(e){var o;const t=e.getChildren();if(!t)return!1;const r=t.length,n=[],s=[];let a=!0;for(let c=0;c<r;c++){const l=t[c];if(l instanceof ie)a=this._suffixExpressionHandler(l),n.push(l);else{const f=l.trim();if(f==="")continue;if(Vt.has(f)){if(f===Z.PLUS&&this._deletePlusForPreNode(t[c-1]))continue;if(f!==Z.PLUS&&f!==Z.MINUS&&this._deletePlusForPreNode(t[c-1]))return!1;for(;s.length>0;){const m=(o=s[s.length-1])==null?void 0:o.trim();if(!m||m===X.OPEN_BRACKET)break;const d=Ir.get(m),_=Ir.get(f);if(d===void 0||_===void 0)break;if(_>=d)n.push(s.pop());else break}s.push(l)}else if(f===X.OPEN_BRACKET)s.push(l);else if(f===X.CLOSE_BRACKET)this._processSuffixExpressionCloseBracket(n,s,t,c);else{if(this._checkCloseBracket(t[c-1]))return!1;n.push(l)}}}return a?this._processSuffixExpressionRemain(n,s,e):!1}_processSuffixExpressionRemain(e,t,r){const n=e.length,s=e[n-1];for(;t.length>0;){const a=t.pop();if(!(s instanceof ie)&&(a===X.OPEN_BRACKET||a===X.CLOSE_BRACKET))return!1;e.push(a)}return r.setChildren(e),!0}_processSuffixExpressionCloseBracket(e,t,r,n){var s;if(this._checkOpenBracket(r[n-1])||this._checkOperator(r[n-1]))return!1;for(;t.length>0;){const a=(s=t[t.length-1])==null?void 0:s.trim();if(!a)break;if(a===X.OPEN_BRACKET){t.pop();break}e.push(t.pop())}}_checkCloseBracket(e){return e===X.CLOSE_BRACKET||e instanceof ie}_checkOpenBracket(e){return e===X.OPEN_BRACKET}_checkOperator(e){return e==null||e instanceof ie?!1:Vt.has(e)}_deletePlusForPreNode(e){if(e==null)return!0;if(!(e instanceof ie)){const t=e.trim();if(Vt.has(t)||t===X.OPEN_BRACKET)return!0}return!1}_resetCurrentLexerNode(){this._currentLexerNode=new ie}_resetSegment(){this._segment=""}_openBracket(e=0){this._bracketState.push(e)}_closeBracket(){this._bracketState.pop()}_openSquareBracket(){this._squareBracketState+=1}_closeSquareBracket(){this._squareBracketState-=1}_getCurrentBracket(){const e=this._bracketState;return e[e.length-1]}_changeCurrentBracket(e){const t=this._bracketState;t[t.length-1]=e}_openBraces(){this._bracesState+=1}_closeBraces(){this._bracesState-=1}_openSingleQuotation(){this._singleQuotationState+=1}_closeSingleQuotation(){this._singleQuotationState-=1}_openDoubleQuotation(){this._doubleQuotationState+=1}_closeDoubleQuotation(){this._doubleQuotationState-=1}_openLambda(){this._lambdaState=!0}_closeLambda(){this._lambdaState=!1}_openColon(e){this._upLevel=e,this._colonState=!0}_closeColon(){this._upLevel=0,this._colonState=!1}_isTableBracket(){return this._tableBracketState}_openTableBracket(){this._tableBracketState=!0}_closeTableBracket(){this._tableBracketState=!1}_formalErrorOccurred(){this._formulaErrorCount+=1}_hasFormalError(){return this._formulaErrorCount>0}_getLastChildCurrentLexerNode(){const e=this._currentLexerNode.getChildren();if(e&&e.length>0){const t=e[e.length-1];if(t instanceof ie)return t}return!1}_getLastChildCurrent(){const e=this._currentLexerNode.getChildren();return e&&e.length>0?e[e.length-1]:!1}_setParentCurrentLexerNode(){const e=this._currentLexerNode.getParent();return e?(this._currentLexerNode=e,!0):!1}_setAncestorCurrentLexerNode(){var r,n,s,a,o,c;const e=(r=this._currentLexerNode)==null?void 0:r.getParent();let t=!1;if(e&&e.getToken()===er){if((n=e==null?void 0:e.getParent())!=null&&n.getParent()){const l=(a=(s=this._currentLexerNode.getParent())==null?void 0:s.getParent())==null?void 0:a.getParent();l&&(this._currentLexerNode=l),t=!0}}else if(e!=null&&e.getParent()){const l=(o=this._currentLexerNode.getParent())==null?void 0:o.getParent();l&&(this._currentLexerNode=l),t=!0}for(let l=0;l<this._upLevel;l++){const f=(c=this._currentLexerNode)==null?void 0:c.getParent();f&&(this._currentLexerNode=f),this._currentLexerNode?t=!0:t=!1}return t}_segmentCount(){return this._segment.trim().length}_pushSegment(e){this._segment+=e}_pushNodeToChildren(e,t=!1){let r=e;if(r!==""){const n=this._currentLexerNode.getChildren();if(!(r instanceof ie)&&this.isColonOpen()){const s=new ie;s.setToken(r),s.setParent(this._currentLexerNode),r=s}t?n.unshift(r):n.push(r)}this.isColonOpen()&&(this._setAncestorCurrentLexerNode(),this._closeColon())}_setCurrentLexerNode(e,t=!1){this._pushNodeToChildren(e,t),e.setParent(this._currentLexerNode),this._currentLexerNode=e}_newAndPushCurrentLexerNode(e,t,r=!1){const n=new ie;n.setToken(e),n.setIndex(t-e.length,t-1),this._setCurrentLexerNode(n,r)}_getTopNode(e){let t=e;for(;t!=null&&t.getParent();)t=t.getParent();return t}_removeLastChild(){this._currentLexerNode.getChildren().splice(-1)}_formulaErrorLastTokenCheck(e,t){const r=this._findPreviousToken(e,t)||"",n=e.length-1===t;if(!n&&this._isOperatorToken(r)||n&&Ju(r))return!0;if(rs.has(r)){const s=this._findSecondLastNonSpaceToken(e,t);if(s==null||tc(s))return!0}return!1}_findPreviousToken(e,t){let r=t;for(;r>=0;){const n=e[r];if(n!==" ")return n;r--}}_findSecondLastNonSpaceToken(e,t){let r=t,n=0;for(;r>=0;){const s=e[r];if(s!==" "&&(n++,n===2))return s;r--}return null}_findNextToken(e,t){let r=t;for(;r>=0;){const n=e[r];if(n!==" ")return n;r++}}_unexpectedEndingTokenExcludeOperator(e){return e===X.OPEN_BRACKET||e===X.COMMA||e===Z.EQUALS||e===""}_unexpectedEndingToken(e){return!!(this._isOperatorToken(e)||this._unexpectedEndingTokenExcludeOperator(e))}_isOperatorToken(e){return!!Vt.has(e)}_getSequenceArray(e){const t=[];return this._nodeMaker(e,t),t}_resetTemp(){this._currentLexerNode=new ie,this._upLevel=0,this._segment="",this._bracketState=[],this._bracesState=0,this._singleQuotationState=0,this._doubleQuotationState=0,this._lambdaState=!1,this._colonState=!1,this._formulaErrorCount=0}_checkErrorState(){return!!(this._bracketState.length>0||this._bracesState>0||this._singleQuotationState>0||this._doubleQuotationState>0||this._hasFormalError())}_checkSimilarErrorToken(e,t,r){let n=t;if(e!==qe.POUND)return!0;let s=r[++n];for(;s===" ";)s=r[++n];return!!hr(s)}_checkIfErrorObject(e,t){return!!this._findErrorObject(e,t)}_findErrorObject(e,t){for(let r=0;r<va.length;r++){const n=va[r],s=t.slice(e,e+n).join("").toUpperCase();if(pt.has(s))return s}}_nodeMaker(e,t,r){let n=e.replace(/\r\n$|\r|\n/g," ");n.substring(0,1)===Z.EQUALS&&(n=n.substring(1));const s=n.split(""),a=s.length;this._resetTemp(),this._formulaErrorLastTokenCheck(s,a-1)&&this._formalErrorOccurred();let o=0;for(;o<a;){const c=s[o];if(r===o)return[this._currentLexerNode,c];if(c===qe.POUND&&this.isSingleQuotationClose()&&this.isDoubleQuotationClose()&&this.isBracesClose()&&this.isSquareBracketClose()&&this._checkIfErrorObject(o,s)){const l=this._findErrorObject(o,s);if(l==null)return h.VALUE;this._pushNodeToChildren(l);for(let f=0;f<l.length;f++){const m=s[o];this._pushSegment(m),this._addSequenceArray(t,m,o),o++}this._resetSegment();continue}else if(c===X.OPEN_BRACKET&&this.isSingleQuotationClose()&&this.isDoubleQuotationClose())if(this._segmentCount()>0||this.isLambdaOpen()){this.isLambdaClose()&&(this._newAndPushCurrentLexerNode(this._segment,o),this._resetSegment()),this._openBracket(1),this._closeLambda();const l=s[o+1];if(l&&l===X.CLOSE_BRACKET){if(!this._setParentCurrentLexerNode()&&o!==a-1)return h.VALUE;this._addSequenceArray(t,c,o),o++,this._addSequenceArray(t,l,o),o++,this._closeBracket();continue}else l&&this._newAndPushCurrentLexerNode(at,o)}else this._pushNodeToChildren(c),this._openBracket(0),this._resetSegment();else if(c===X.CLOSE_BRACKET&&this.isSingleQuotationClose()&&this.isDoubleQuotationClose()){this._formulaErrorLastTokenCheck(s,o-1)&&this._formalErrorOccurred(),this._pushNodeToChildren(this._segment),this._resetSegment();const l=this._getCurrentBracket();if(l===0)this._pushNodeToChildren(c);else if(l===1){const f=s[o+1];if(f&&f===X.OPEN_BRACKET){if(!this._setParentCurrentLexerNode()&&o!==a-1)return h.VALUE;this._newAndPushCurrentLexerNode(er,o,!0),this._openLambda()}else if(!this._setAncestorCurrentLexerNode()&&o!==a-1)return h.VALUE}else return h.VALUE;this._closeBracket()}else if(c===X.OPEN_BRACES&&this.isSingleQuotationClose()&&this.isDoubleQuotationClose()){if(this._pushSegment(c),this._openBraces(),!this._formulaSpellCheck())return h.VALUE}else if(c===X.CLOSE_BRACES&&this.isSingleQuotationClose()&&this.isDoubleQuotationClose())this._pushSegment(c),this._pushNodeToChildren(this._segment),this._resetSegment(),this._closeBraces();else if(c===X.OPEN_SQUARE_BRACKET&&this.isSingleQuotationClose()&&this.isDoubleQuotationClose())this._segment.length>0&&this._openTableBracket(),this._pushSegment(c),this._openSquareBracket();else if(c===X.CLOSE_SQUARE_BRACKET&&this.isSingleQuotationClose()&&this.isDoubleQuotationClose())this._closeSquareBracket(),this.isSquareBracketClose()?(this._pushSegment(c),this._isTableBracket()&&(this._pushNodeToChildren(this._segment),this._resetSegment()),this._closeTableBracket()):this._pushSegment(c);else if(c===X.DOUBLE_QUOTATION&&this.isSingleQuotationClose()&&this.isSquareBracketClose()){if(this.isDoubleQuotationClose())this._openDoubleQuotation();else{const l=s[o+1];l&&l===X.DOUBLE_QUOTATION?o++:this._closeDoubleQuotation()}this._pushSegment(c)}else if(c===X.SINGLE_QUOTATION&&this.isDoubleQuotationClose()){if(this.isSingleQuotationClose())this._openSingleQuotation(),this._segmentCount()===0&&this._resetSegment();else{const l=s[o+1];if(l&&l===X.SINGLE_QUOTATION){this._pushSegment(c),this._addSequenceArray(t,c,o),o++,this._pushSegment(l),this._addSequenceArray(t,l,o),o++;continue}else this._closeSingleQuotation()}this._pushSegment(c)}else if(c===X.COMMA&&this.isSingleQuotationClose()&&this.isDoubleQuotationClose()&&this.isBracesClose()&&this.isSquareBracketClose()){this._formulaErrorLastTokenCheck(s,o-1)&&this._formalErrorOccurred();const l=this._getCurrentBracket();if(l===1||l==null){if(this._pushNodeToChildren(this._segment),this._resetSegment(),!this._setParentCurrentLexerNode()&&o!==a-1&&l!=null)return h.VALUE;this._newAndPushCurrentLexerNode(at,o)}else{const f=new ie;f.setToken(Oc);const m=new ie;m.setToken(at),m.changeToParent(f);const d=this._currentLexerNode.getParent();if(d&&d.getToken()===X.COLON){const _=d.getParent();if(!_)return h.VALUE;d.changeToParent(m),_.setChildren([]),f.changeToParent(_)}else return h.VALUE;this._changeCurrentBracket(1),this._pushNodeToChildren(this._segment),this._resetSegment(),this._currentLexerNode=f,this._newAndPushCurrentLexerNode(at,o)}}else if(c===X.COLON&&this.isSingleQuotationClose()&&this.isDoubleQuotationClose()&&this.isBracesClose()&&this.isSquareBracketClose()){const l=new ie;l.setToken(c);const f=new ie;f.setToken(at),f.setParent(l);const m=new ie;m.setToken(at),m.setParent(l),l.getChildren().push(f,m);let d=l,_=0;if(this._segmentCount()>0){let C,E,b=0;const y=this._segment.trim(),N=y[0],V=y[1];if(N===Ve.MINUS&&(C=new ie,C.setToken(Ve.MINUS),b++),(N===Ve.AT||V===Ve.AT)&&(E=new ie,E.setToken(Ve.AT),C&&(C.addChildren(E),E.setParent(C)),b++),b>0&&(this._segment=y.slice(b)),_=b,E)if(E.addChildren(l),l.setParent(E),E.getParent()){const D=E.getParent();D&&(d=D)}else d=E;else C&&(d=C,C.addChildren(l),l.setParent(C));const S=new ie;S.setToken(this._segment),S.setParent(f),f.getChildren().push(S),this._resetSegment()}else{const C=this._getLastChildCurrentLexerNode();C&&C.changeToParent(f)}this._setCurrentLexerNode(d),this._currentLexerNode=m,this._openColon(_)}else if(rs.has(c)&&this._checkSimilarErrorToken(c,o,s)&&this.isSingleQuotationClose()&&this.isDoubleQuotationClose()&&this.isSquareBracketClose()&&this.isBracesClose()){this._pushNodeToChildren(this._segment);const l=new ie;l.setToken(c);const f=this._getLastChildCurrent();f instanceof ie?f.changeToParent(l):f!==!1&&(l.getChildren().push(f),this._removeLastChild()),this._pushNodeToChildren(l),l.setParent(this._currentLexerNode),this._resetSegment()}else if(Vt.has(c)&&this.isSingleQuotationClose()&&this.isDoubleQuotationClose()&&this.isSquareBracketClose()&&this.isBracesClose()){let l=this._segment.trim();if(c===Z.MINUS&&l===""){const f=this._findPreviousToken(s,o-1)||"",m=this._findNextToken(s,o+1)||"";if(this._unexpectedEndingTokenExcludeOperator(f)&&this._isOperatorToken(m)){this._pushNodeToChildren("0"),this._pushNodeToChildren(Z.MINUS),this._addSequenceArray(t,c,o),this._resetSegment(),o++;continue}else if(this._unexpectedEndingToken(f))if(m===Z.PLUS){this._pushSegment(Z.MINUS),this._addSequenceArray(t,c,o),this._addSequenceArray(t,Z.PLUS,o+1),o+=2;continue}else{this._pushSegment(Z.MINUS),this._addSequenceArray(t,c,o),o++;continue}}else if(this._segment.length>0&&this._isScientificNotation(s,o,c)){this._pushSegment(c),this._addSequenceArray(t,c,o),o++;continue}else this._segment.length>0&&l===""?l=this._segment:(this._pushNodeToChildren(this._segment),l="");if(c===Z.LESS_THAN||c===Z.GREATER_THAN){const f=s[o+1];if(f&&Vt.has(c+f)){this._pushNodeToChildren(l+c+f),this._resetSegment(),this._addSequenceArray(t,c,o),o++,this._addSequenceArray(t,f,o),o++;continue}else this._pushNodeToChildren(l+c)}else this._pushNodeToChildren(l+c);this._resetSegment()}else(this._segment!==""||c!==" ")&&this._pushSegment(c);this._addSequenceArray(t,c,o),o++}if(this._pushNodeToChildren(this._segment),this._checkErrorState())return h.VALUE}_isScientificNotation(e,t,r){const n=e[t-2];if(n&&Number.isNaN(Number(n))||!(r===Z.MINUS||r===Z.PLUS))return!1;const s=e[t+1];if(s&&Number.isNaN(Number(s)))return!1;const a=e[t-1];return a&&a.toUpperCase()==="E"}_addSequenceArray(e,t,r){e==null||e.push({segment:this._segment,currentString:t,cur:r,currentLexerNode:this._currentLexerNode})}}function Dc(i,u,e,t,r,n,s){const a=(s==null?void 0:s.f)||"",o=(s==null?void 0:s.si)||"",c=M.isFormulaString(a),l=M.isFormulaId(o),f=i.getValue(r,n),m=(f==null?void 0:f.f)||"",d=(f==null?void 0:f.si)||"";function _(){var C;if(M.isFormulaString(m)&&M.isFormulaId(d)){const E=(C=e==null?void 0:e[d])==null?void 0:C.f;E?t.set(d,E):t.set(d,m)}}c&&l?(d!==o&&_(),i.setValue(r,n,{f:a,si:o}),e[o]={f:a,r,c:n},u.setValue(r,n,{f:a,si:o})):c&&!l?(m!==a&&_(),i.setValue(r,n,{f:a}),u.setValue(r,n,{f:a})):!c&&l?(d!==o&&_(),i.setValue(r,n,{f:"",si:o})):!c&&!l&&i.getValue(r,n)&&(_(),i.realDeleteValue(r,n),u.setValue(r,n,null))}function wc(i,u,e,t){const r=i==null?void 0:i.getValue(e,t);if(r==null)return!0;const n=[];i.forValue((l,f,m)=>{l===e&&f===t||M.Rectangle.intersects(m,r)&&n.push(m)});const{startRow:s,startColumn:a,endRow:o,endColumn:c}=r;for(let l=s;l<=o;l++)for(let f=a;f<=c;f++){let m=!1;const d=M.cellToRange(l,f);n.some(_=>M.Rectangle.contains(_,d)?(m=!0,!0):!1),m||u.realDeleteValue(l,f)}}var Lc=Object.getOwnPropertyDescriptor,Pc=(i,u,e,t)=>{for(var r=t>1?void 0:t?Lc(u,e):u,n=i.length-1,s;n>=0;n--)(s=i[n])&&(r=s(r)||r);return r},fi=(i,u)=>(e,t)=>u(e,t,i);w.FormulaDataModel=class extends M.Disposable{constructor(e,t){super();A(this,"_arrayFormulaRange",{});A(this,"_arrayFormulaCellData",{});this._univerInstanceService=e,this._lexerTreeBuilder=t}dispose(){super.dispose(),this._arrayFormulaRange={},this._arrayFormulaCellData={}}clearPreviousArrayFormulaCellData(e){Object.keys(e).forEach(t=>{const r=e[t];if(r==null)return!0;Object.keys(r).forEach(n=>{var l,f,m,d;const s=r[n],a=(f=(l=this._arrayFormulaRange)==null?void 0:l[t])==null?void 0:f[n];if(a==null)return!0;const o=new M.ObjectMatrix(a);let c=new M.ObjectMatrix;((m=this._arrayFormulaCellData[t])==null?void 0:m[n])!=null&&(c=new M.ObjectMatrix((d=this._arrayFormulaCellData[t])==null?void 0:d[n])),s.forValue((_,C)=>{const E=o.getValue(_,C);if(E==null)return!0;const{startRow:b,startColumn:y,endRow:N,endColumn:V}=E;for(let S=b;S<=N;S++)for(let D=y;D<=V;D++)c.setValue(S,D,null);o.realDeleteValue(_,C)}),this._arrayFormulaCellData[t]&&(this._arrayFormulaCellData[t][n]=c.getData())})})}mergeArrayFormulaCellData(e){Object.keys(e).forEach(t=>{const r=e[t];if(r==null)return!0;this._arrayFormulaRange[t]==null&&(this._arrayFormulaRange[t]={}),this._arrayFormulaCellData[t]==null&&(this._arrayFormulaCellData[t]={}),Object.keys(r).forEach(n=>{var c,l;const s=r[n],a=new M.ObjectMatrix((c=this._arrayFormulaRange[t])==null?void 0:c[n]),o=new M.ObjectMatrix((l=this._arrayFormulaCellData[t])==null?void 0:l[n]);s.forValue((f,m)=>{const d=a==null?void 0:a.getValue(f,m);if(d==null)return!0;const{startRow:_,startColumn:C,endRow:E,endColumn:b}=d;for(let y=_;y<=E;y++)for(let N=C;N<=b;N++)o.setValue(y,N,null)}),s.forValue((f,m,d)=>{o.setValue(f,m,d)}),this._arrayFormulaCellData[t]&&(this._arrayFormulaCellData[t][n]=o.getData())})})}getFormulaData(){const e={},t=this._univerInstanceService.getAllUnitsForType(M.UniverInstanceType.UNIVER_SHEET);return t.length===0||t.forEach(r=>{const n=r.getUnitId();e[n]={},r.getSheets().forEach(a=>{const o=a.getCellMatrix(),c=a.getSheetId();_s(e,n,c,o)})}),e}getSheetFormulaData(e,t){const r={},n=this._univerInstanceService.getUnit(e);if(n==null)return{};r[e]={};const s=n.getSheetBySheetId(t);if(s==null)return{};const a=s.getCellMatrix();return _s(r,e,t,a),r[e][t]}getArrayFormulaRange(){return this._arrayFormulaRange}setArrayFormulaRange(e){this._arrayFormulaRange=e}getArrayFormulaCellData(){return this._arrayFormulaCellData}setArrayFormulaCellData(e){this._arrayFormulaCellData=e}mergeArrayFormulaRange(e){Object.keys(e).forEach(t=>{const r=e[t];if(r==null)return!0;this._arrayFormulaRange[t]||(this._arrayFormulaRange[t]={}),Object.keys(r).forEach(n=>{var o;const s=new M.ObjectMatrix(r[n]),a=new M.ObjectMatrix((o=this._arrayFormulaRange[t])==null?void 0:o[n]);s.forValue((c,l,f)=>{a.setValue(c,l,f)}),this._arrayFormulaRange[t]&&(this._arrayFormulaRange[t][n]=a.getData())})})}deleteArrayFormulaRange(e,t,r,n){var o;const s=(o=this._arrayFormulaRange[e])==null?void 0:o[t];if(s==null)return;const a=new M.ObjectMatrix(s);a.getValue(r,n)&&(a.realDeleteValue(r,n),this._arrayFormulaRange[e]&&(this._arrayFormulaRange[e][t]=a.getData()))}getCalculateData(){const e=this._univerInstanceService.getAllUnitsForType(M.UniverInstanceType.UNIVER_SHEET),t={},r={},n={};for(const s of e){const a=s.getUnitId(),o=s.getSheets(),c={},l={};for(const f of o){const m=f.getSheetId(),d=f.getConfig();c[m]={cellData:new M.ObjectMatrix(d.cellData),rowCount:d.rowCount,columnCount:d.columnCount,rowData:d.rowData,columnData:d.columnData,defaultRowHeight:d.defaultRowHeight,defaultColumnWidth:d.defaultColumnWidth},l[f.getName()]=f.getSheetId()}t[a]=c,r[a]=s.getStyles(),n[a]=l}return{allUnitData:t,unitStylesData:r,unitSheetNameMap:n}}getHiddenRowsFiltered(){const e=this._univerInstanceService.getAllUnitsForType(M.UniverInstanceType.UNIVER_SHEET),t={};for(const r of e){const n=r.getUnitId(),s=r.getSheets();t[n]={};for(const a of s){const o=a.getSheetId();t[n][o]={};const c=0,l=a.getRowCount()-1,f={};for(let m=c;m<=l;m++)a.getRowVisible(m)||(f[m]={hd:M.BooleanNumber.TRUE});t[n][o]=f}}return t}updateFormulaData(e,t,r){const n=new M.ObjectMatrix(r),s=this._getSheetFormulaIdMap(e,t),a=new Map,o=this.getFormulaData();o[e]==null&&(o[e]={});const c=o[e];c[t]==null&&(c[t]={});const l=new M.ObjectMatrix(c[t]||{}),f=new M.ObjectMatrix;return n.forValue((m,d,_)=>{Dc(l,f,s,a,m,d,_)}),l.forValue((m,d,_)=>{const C=(_==null?void 0:_.f)||"",E=(_==null?void 0:_.si)||"";if(M.isFormulaId(E)){const b=s==null?void 0:s[E],y=a.get(E);if(b&&!M.isFormulaString(C)){const N=b.f,V=d-b.c,S=m-b.r;l.setValue(m,d,{f:N,si:E,x:V,y:S}),f.setValue(m,d,{f:N,si:E,x:V,y:S})}else if(typeof y=="string"){const N=(_==null?void 0:_.x)||0,V=(_==null?void 0:_.y)||0,S=this._lexerTreeBuilder.moveFormulaRefOffset(y,N,V);a.set(E,{r:m,c:d,f:S}),l.setValue(m,d,{f:S,si:E}),f.setValue(m,d,{f:S,si:E})}else if(typeof y=="object"){const N=d-y.c,V=m-y.r;l.setValue(m,d,{f:y.f,si:E,x:N,y:V}),f.setValue(m,d,{f:y.f,si:E,x:N,y:V})}}}),f.getMatrix()}updateArrayFormulaRange(e,t,r){var o;const n=(o=this._arrayFormulaRange[e])==null?void 0:o[t];if(!n)return;const s=new M.ObjectMatrix(n);new M.ObjectMatrix(r).forValue((c,l,f)=>{s.realDeleteValue(c,l)})}updateArrayFormulaCellData(e,t,r){var l,f;const n=(l=this._arrayFormulaRange[e])==null?void 0:l[t];if(!n)return;const s=new M.ObjectMatrix(n),a=(f=this._arrayFormulaCellData[e])==null?void 0:f[t];if(!a)return;const o=new M.ObjectMatrix(a);new M.ObjectMatrix(r).forValue((m,d,_)=>{wc(s,o,m,d)})}getFormulaStringByCell(e,t,r,n){const s=this._univerInstanceService.getUnit(n);if(s==null)return null;const a=s.getSheetBySheetId(r);if(a==null)return null;const o=a.getCellMatrix(),c=o.getValue(e,t);if(c==null)return null;const{f:l,si:f}=c;if(M.isFormulaString(l))return l;if(M.isFormulaId(f)){let m=null;return o.forValue((d,_,C)=>{if(C==null)return!0;const{f:E,si:b}=C;if(M.isFormulaString(E)&&f===b)return m=this._lexerTreeBuilder.moveFormulaRefOffset(E,t-_,e-d),!1}),m}return null}getFormulaDirtyRanges(){const e=this.getFormulaData(),t=[];for(const r in e){const n=e[r];if(!n)continue;const s=this._univerInstanceService.getUnit(r);if(s)for(const a in n){const o=n[a];if(!o)continue;const c=s.getSheetBySheetId(a);if(!c)continue;const l={};for(const f of Object.keys(o)){const m=Number(f);for(const d in o[m]){const _=Number(d),C=c.getCellRaw(m,_),E=M.isFormulaString(C==null?void 0:C.f)||M.isFormulaId(C==null?void 0:C.si),b=(C==null?void 0:C.v)===void 0;if(!(E&&b))continue;l[_]||(l[_]=[]);const y=l[_].slice(-1)[0];y&&y.endRow===m-1?y.endRow=m:l[_].push({startRow:m,endRow:m})}}for(const f in l){const m=l[f];for(let d=0;d<m.length;d++){const _=m[d];t.push({unitId:r,sheetId:a,range:{rangeType:M.RANGE_TYPE.NORMAL,startRow:_.startRow,endRow:_.endRow,startColumn:Number(f),endColumn:Number(f)}})}}}}return t}_getSheetFormulaIdMap(e,t){const r={},n=this._univerInstanceService.getUnit(e);if(n==null)return r;const s=n.getSheetBySheetId(t);return s==null||s.getCellMatrix().forValue((o,c,l)=>{if(l==null)return!0;const{f,si:m}=l;M.isFormulaString(f)&&M.isFormulaId(m)&&(r[m]={f,r:o,c})}),r}},w.FormulaDataModel=Pc([fi(0,M.IUniverInstanceService),fi(1,M.Inject(zr))],w.FormulaDataModel);function _s(i,u,e,t){i[u]||(i[u]={}),i[u][e]||(i[u][e]={});const r=new Map,n=new M.ObjectMatrix(i[u][e]);t.forValue((a,o,c)=>{const l=(c==null?void 0:c.f)||"",f=(c==null?void 0:c.si)||"",m=M.isFormulaString(l),d=M.isFormulaId(f);m&&d?(n.setValue(a,o,{f:l,si:f}),r.set(f,{f:l,r:a,c:o})):m&&!d?n.setValue(a,o,{f:l}):!m&&d&&n.setValue(a,o,{f:"",si:f})}),n.forValue((a,o,c)=>{const l=(c==null?void 0:c.f)||"",f=(c==null?void 0:c.si)||"";if(M.isFormulaId(f)&&!M.isFormulaString(l)){const m=r.get(f);if(m){const d=m.f,_=o-m.c,C=a-m.r;n.setValue(a,o,{f:d,si:f,x:_,y:C})}else n.realDeleteValue(a,o)}});const s=n.getMatrix();return{[u]:{[e]:s}}}const jc=class xa{constructor(u,e){this.low=u,this.high=e}clone(){return new xa(this.low,this.high)}get max(){return this.clone()}less_than(u){return this.low<u.low||this.low===u.low&&this.high<u.high}equal_to(u){return this.low===u.low&&this.high===u.high}intersect(u){return!this.not_intersect(u)}not_intersect(u){return this.high<u.low||u.high<this.low}merge(u){return new xa(this.low===void 0?u.low:this.low<u.low?this.low:u.low,this.high===void 0?u.high:this.high>u.high?this.high:u.high)}output(){return[this.low,this.high]}static comparable_max(u,e){return u.merge(e)}static comparable_less_than(u,e){return u<e}},Pe=0,Ae=1;class Ft{constructor(u=void 0,e=void 0,t=null,r=null,n=null,s=Ae){if(this.left=t,this.right=r,this.parent=n,this.color=s,this.item={key:u,value:e},u&&u instanceof Array&&u.length===2&&!Number.isNaN(u[0])&&!Number.isNaN(u[1])){let[a,o]=u;a>o&&([a,o]=[o,a]),this.item.key=new jc(a,o)}this.max=this.item.key?this.item.key.max:void 0}isNil(){return this.item.key===void 0&&this.item.value===void 0&&this.left===null&&this.right===null&&this.color===Ae}_value_less_than(u){return this.item.value&&u.item.value&&this.item.value.less_than?this.item.value.less_than(u.item.value):this.item.value<u.item.value}less_than(u){return this.item.value===this.item.key&&u.item.value===u.item.key?this.item.key.less_than(u.item.key):this.item.key.less_than(u.item.key)||this.item.key.equal_to(u.item.key)&&this._value_less_than(u)}_value_equal(u){return this.item.value&&u.item.value&&this.item.value.equal_to?this.item.value.equal_to(u.item.value):this.item.value===u.item.value}equal_to(u){return this.item.value===this.item.key&&u.item.value===u.item.key?this.item.key.equal_to(u.item.key):this.item.key.equal_to(u.item.key)&&this._value_equal(u)}intersect(u){return this.item.key.intersect(u.item.key)}copy_data(u){this.item.key=u.item.key,this.item.value=u.item.value}update_max(){if(this.max=this.item.key?this.item.key.max:void 0,this.right&&this.right.max){const u=this.item.key.constructor.comparable_max;this.max=u(this.max,this.right.max)}if(this.left&&this.left.max){const u=this.item.key.constructor.comparable_max;this.max=u(this.max,this.left.max)}}not_intersect_left_subtree(u){const e=this.item.key.constructor.comparable_less_than;let t=this.left.max.high!==void 0?this.left.max.high:this.left.max;return e(t,u.item.key.low)}not_intersect_right_subtree(u){const e=this.item.key.constructor.comparable_less_than;let t=this.right.max.low!==void 0?this.right.max.low:this.right.item.key.low;return e(u.item.key.high,t)}}class As{constructor(){this.root=null,this.nil_node=new Ft}get size(){let u=0;return this.tree_walk(this.root,()=>u++),u}get keys(){let u=[];return this.tree_walk(this.root,e=>u.push(e.item.key.output?e.item.key.output():e.item.key)),u}get values(){let u=[];return this.tree_walk(this.root,e=>u.push(e.item.value)),u}get items(){let u=[];return this.tree_walk(this.root,e=>u.push({key:e.item.key.output?e.item.key.output():e.item.key,value:e.item.value})),u}isEmpty(){return this.root==null||this.root===this.nil_node}clear(){this.root=null}insert(u,e=u){if(u===void 0)return;let t=new Ft(u,e,this.nil_node,this.nil_node,null,Pe);return this.tree_insert(t),this.recalc_max(t),t}exist(u,e=u){let t=new Ft(u,e);return!!this.tree_search(this.root,t)}remove(u,e=u){let t=new Ft(u,e),r=this.tree_search(this.root,t);return r&&this.tree_delete(r),r}search(u,e=(t,r)=>t===r?r.output():t){let t=new Ft(u),r=[];return this.tree_search_interval(this.root,t,r),r.map(n=>e(n.item.value,n.item.key))}intersect_any(u){let e=new Ft(u);return this.tree_find_any_interval(this.root,e)}forEach(u){this.tree_walk(this.root,e=>u(e.item.key,e.item.value))}map(u){const e=new As;return this.tree_walk(this.root,t=>e.insert(t.item.key,u(t.item.value,t.item.key))),e}*iterate(u,e=(t,r)=>t===r?r.output():t){let t;for(u?t=this.tree_search_nearest_forward(this.root,new Ft(u)):this.root&&(t=this.local_minimum(this.root));t;)yield e(t.item.value,t.item.key),t=this.tree_successor(t)}recalc_max(u){let e=u;for(;e.parent!=null;)e.parent.update_max(),e=e.parent}tree_insert(u){let e=this.root,t=null;if(this.root==null||this.root===this.nil_node)this.root=u;else{for(;e!==this.nil_node;)t=e,u.less_than(e)?e=e.left:e=e.right;u.parent=t,u.less_than(t)?t.left=u:t.right=u}this.insert_fixup(u)}insert_fixup(u){let e,t;for(e=u;e!==this.root&&e.parent.color===Pe;)e.parent===e.parent.parent.left?(t=e.parent.parent.right,t.color===Pe?(e.parent.color=Ae,t.color=Ae,e.parent.parent.color=Pe,e=e.parent.parent):(e===e.parent.right&&(e=e.parent,this.rotate_left(e)),e.parent.color=Ae,e.parent.parent.color=Pe,this.rotate_right(e.parent.parent))):(t=e.parent.parent.left,t.color===Pe?(e.parent.color=Ae,t.color=Ae,e.parent.parent.color=Pe,e=e.parent.parent):(e===e.parent.left&&(e=e.parent,this.rotate_right(e)),e.parent.color=Ae,e.parent.parent.color=Pe,this.rotate_left(e.parent.parent)));this.root.color=Ae}tree_delete(u){let e,t;u.left===this.nil_node||u.right===this.nil_node?e=u:e=this.tree_successor(u),e.left!==this.nil_node?t=e.left:t=e.right,t.parent=e.parent,e===this.root?this.root=t:(e===e.parent.left?e.parent.left=t:e.parent.right=t,e.parent.update_max()),this.recalc_max(t),e!==u&&(u.copy_data(e),u.update_max(),this.recalc_max(u)),e.color===Ae&&this.delete_fixup(t)}delete_fixup(u){let e=u,t;for(;e!==this.root&&e.parent!=null&&e.color===Ae;)e===e.parent.left?(t=e.parent.right,t.color===Pe&&(t.color=Ae,e.parent.color=Pe,this.rotate_left(e.parent),t=e.parent.right),t.left.color===Ae&&t.right.color===Ae?(t.color=Pe,e=e.parent):(t.right.color===Ae&&(t.color=Pe,t.left.color=Ae,this.rotate_right(t),t=e.parent.right),t.color=e.parent.color,e.parent.color=Ae,t.right.color=Ae,this.rotate_left(e.parent),e=this.root)):(t=e.parent.left,t.color===Pe&&(t.color=Ae,e.parent.color=Pe,this.rotate_right(e.parent),t=e.parent.left),t.left.color===Ae&&t.right.color===Ae?(t.color=Pe,e=e.parent):(t.left.color===Ae&&(t.color=Pe,t.right.color=Ae,this.rotate_left(t),t=e.parent.left),t.color=e.parent.color,e.parent.color=Ae,t.left.color=Ae,this.rotate_right(e.parent),e=this.root));e.color=Ae}tree_search(u,e){if(!(u==null||u===this.nil_node))return e.equal_to(u)?u:e.less_than(u)?this.tree_search(u.left,e):this.tree_search(u.right,e)}tree_search_nearest_forward(u,e){let t,r=u;for(;r&&r!==this.nil_node;)r.less_than(e)?r.intersect(e)?(t=r,r=r.left):r=r.right:((!t||r.less_than(t))&&(t=r),r=r.left);return t||null}tree_search_interval(u,e,t){u!=null&&u!==this.nil_node&&(u.left!==this.nil_node&&!u.not_intersect_left_subtree(e)&&this.tree_search_interval(u.left,e,t),u.intersect(e)&&t.push(u),u.right!==this.nil_node&&!u.not_intersect_right_subtree(e)&&this.tree_search_interval(u.right,e,t))}tree_find_any_interval(u,e){let t=!1;return u!=null&&u!==this.nil_node&&(u.left!==this.nil_node&&!u.not_intersect_left_subtree(e)&&(t=this.tree_find_any_interval(u.left,e)),t||(t=u.intersect(e)),!t&&u.right!==this.nil_node&&!u.not_intersect_right_subtree(e)&&(t=this.tree_find_any_interval(u.right,e))),t}local_minimum(u){let e=u;for(;e.left!=null&&e.left!==this.nil_node;)e=e.left;return e}local_maximum(u){let e=u;for(;e.right!=null&&e.right!==this.nil_node;)e=e.right;return e}tree_successor(u){let e,t,r;if(u.right!==this.nil_node)e=this.local_minimum(u.right);else{for(t=u,r=u.parent;r!=null&&r.right===t;)t=r,r=r.parent;e=r}return e}rotate_left(u){let e=u.right;u.right=e.left,e.left!==this.nil_node&&(e.left.parent=u),e.parent=u.parent,u===this.root?this.root=e:u===u.parent.left?u.parent.left=e:u.parent.right=e,e.left=u,u.parent=e,u!=null&&u!==this.nil_node&&u.update_max(),e=u.parent,e!=null&&e!==this.nil_node&&e.update_max()}rotate_right(u){let e=u.left;u.left=e.right,e.right!==this.nil_node&&(e.right.parent=u),e.parent=u.parent,u===this.root?this.root=e:u===u.parent.left?u.parent.left=e:u.parent.right=e,e.right=u,u.parent=e,u!==null&&u!==this.nil_node&&u.update_max(),e=u.parent,e!=null&&e!==this.nil_node&&e.update_max()}tree_walk(u,e){u!=null&&u!==this.nil_node&&(this.tree_walk(u.left,e),e(u),this.tree_walk(u.right,e))}testRedBlackProperty(){let u=!0;return this.tree_walk(this.root,function(e){e.color===Pe&&(e.left.color===Ae&&e.right.color===Ae||(u=!1))}),u}testBlackHeightProperty(u){let e=0,t=0,r=0;if(u.color===Ae&&e++,u.left!==this.nil_node?t=this.testBlackHeightProperty(u.left):t=1,u.right!==this.nil_node?r=this.testBlackHeightProperty(u.right):r=1,t!==r)throw new Error("Red-black height property violated");return e+=t,e}}class xc{constructor(){A(this,"_cache",new Map);A(this,"_continueBuildingCache",new Map)}set(u,e,t,r,n,s=!1){if(!this.shouldContinueBuildingCache(u,e,t,n)&&!s)return;let a=this._cache.get(u);a==null&&(a=new Map,this._cache.set(u,a));let o=a.get(e);o==null&&(o=new Map,a.set(e,o));let c=o.get(t);if(c==null&&(c=new Map,o.set(t,c)),s){for(const[f,m]of c)if(m.has(n)){m.delete(n);break}}let l=c.get(r);l==null&&(l=new Set,c.set(r,l)),l.add(n)}getCellValuePositions(u,e,t){var r,n;return(n=(r=this._cache.get(u))==null?void 0:r.get(e))==null?void 0:n.get(t)}getCellPositions(u,e,t,r,n){var a,o,c;const s=(c=(o=(a=this._cache.get(u))==null?void 0:a.get(e))==null?void 0:o.get(t))==null?void 0:c.get(r);return s&&[...s].filter(l=>n.some(([f,m])=>l>=f&&l<=m))}setContinueBuildingCache(u,e,t,r,n){if(t===-1||r===-1||n===-1)return;let s=this._continueBuildingCache.get(u);s==null&&(s=new Map,this._continueBuildingCache.set(u,s));let a=s.get(e);a==null&&(a=new Map,s.set(e,a));let o=a.get(t);if(o==null){o=new As,o.insert([r,n]),a.set(t,o);return}this._handleNewInterval(o,r,n)}shouldContinueBuildingCache(u,e,t,r){var a,o;if(t===-1||r===-1)return!1;const n=(o=(a=this._continueBuildingCache.get(u))==null?void 0:a.get(e))==null?void 0:o.get(t);return n?n.search([r,r]).length===0:!0}canUseCache(u,e,t,r,n){var f,m;const s=(m=(f=this._continueBuildingCache.get(u))==null?void 0:f.get(e))==null?void 0:m.get(t);if(t===-1||r===-1||n===-1||!s)return{rowsInCache:[],rowsNotInCache:[]};const a=s.search([r,n]);if(a.length===0)return{rowsInCache:[],rowsNotInCache:[]};a.sort((d,_)=>d[0]-_[0]);const o=[],c=[];let l=r;for(let d=0;d<a.length;d++){const[_,C]=a[d];if(l>=_){if(n<=C){o.push([l,n]);break}o.push([l,C]),l=C+1,d===a.length-1&&l<=n&&c.push([l,n])}else{if(n>C){o.push([_,C]),c.push([l,_-1]),l=C+1,d===a.length-1&&l<=n&&c.push([l,n]);continue}o.push([_,n]),c.push([l,_-1])}}return{rowsInCache:o,rowsNotInCache:c}}clear(){this._cache.clear(),this._continueBuildingCache.clear()}_handleNewInterval(u,e,t){let r=u.search([e,t]);if(r.length===0){const a=[e-1<0?0:e-1,t+1];if(r=u.search(a),r.length===0){u.insert([e,t]);return}}let n=e,s=t;for(const a of r)n=Math.min(n,a[0]),s=Math.max(s,a[1]),u.remove(a);u.insert([n,s])}}const Ye=new xc,Jr="engine-formula.config",mi=1,Uc="CYCLE_REFERENCE_COUNT",hi={};class gi extends M.Disposable{constructor(){super(...arguments);A(this,"_getRowFilteredCallback")}register(e){this._getRowFilteredCallback=e}getRowFiltered(e,t,r){var n;return this._getRowFilteredCallback&&(n=this._getRowFilteredCallback(e,t,r))!=null?n:!1}}const Es=M.createIdentifier("univer.formula.sheet-row-filtered.service");var vc=Object.getOwnPropertyDescriptor,Tc=(i,u,e,t)=>{for(var r=t>1?void 0:t?vc(u,e):u,n=i.length-1,s;n>=0;n--)(s=i[n])&&(r=s(r)||r);return r},en=(i,u)=>(e,t)=>u(e,t,i);w.FormulaCurrentConfigService=class extends M.Disposable{constructor(e,t,r,n){super();A(this,"_unitData",{});A(this,"_unitStylesData",{});A(this,"_arrayFormulaCellData",{});A(this,"_arrayFormulaRange",{});A(this,"_formulaData",{});A(this,"_sheetNameMap",{});A(this,"_forceCalculate",!1);A(this,"_clearDependencyTreeCache",{});A(this,"_dirtyRanges",[]);A(this,"_dirtyNameMap",{});A(this,"_dirtyDefinedNameMap",{});A(this,"_dirtyUnitFeatureMap",{});A(this,"_dirtyUnitOtherFormulaMap",{});A(this,"_excludedCell");A(this,"_sheetIdToNameMap",{});A(this,"_executeUnitId","");A(this,"_executeSubUnitId","");this._univerInstanceService=e,this._localeService=t,this._formulaDataModel=r,this._sheetRowFilteredService=n}dispose(){super.dispose(),this._unitData={},this._unitStylesData={},this._arrayFormulaCellData={},this._arrayFormulaRange={},this._formulaData={},this._sheetNameMap={},this._clearDependencyTreeCache={},this._dirtyRanges=[],this._dirtyNameMap={},this._dirtyDefinedNameMap={},this._dirtyUnitFeatureMap={},this._dirtyUnitOtherFormulaMap={},this._excludedCell={},this._sheetIdToNameMap={}}getExecuteUnitId(){return this._executeUnitId}getExecuteSubUnitId(){return this._executeSubUnitId}setExecuteUnitId(e){this._executeUnitId=e}setExecuteSubUnitId(e){this._executeSubUnitId=e}getExcludedRange(){return this._excludedCell}getUnitData(){return this._unitData}getUnitStylesData(){return this._unitStylesData}getFormulaData(){return this._formulaData}getArrayFormulaCellData(){return this._arrayFormulaCellData}getArrayFormulaRange(){return this._arrayFormulaRange}getSheetNameMap(){return this._sheetNameMap}isForceCalculate(){return this._forceCalculate}getDirtyRanges(){return this._dirtyRanges}getDirtyNameMap(){return this._dirtyNameMap}getDirtyDefinedNameMap(){return this._dirtyDefinedNameMap}getDirtyUnitFeatureMap(){return this._dirtyUnitFeatureMap}getDirtyUnitOtherFormulaMap(){return this._dirtyUnitOtherFormulaMap}getSheetName(e,t){return this._sheetIdToNameMap[e]==null?"":this._sheetIdToNameMap[e][t]||""}getClearDependencyTreeCache(){return this._clearDependencyTreeCache}getLocale(){return this._localeService.getCurrentLocale()}getSheetsInfo(){const e=this._univerInstanceService.getCurrentUnitForType(M.UniverInstanceType.UNIVER_SHEET),{id:t,sheetOrder:r}=e.getSnapshot();return{sheetOrder:r,sheetNameMap:this._sheetIdToNameMap[t]}}getSheetRowColumnCount(e,t){const r=this._univerInstanceService.getUnit(e),n=r==null?void 0:r.getSheetBySheetId(t),s=n==null?void 0:n.getSnapshot();if(!s)return{rowCount:0,columnCount:0};const{rowCount:a,columnCount:o}=s;return{rowCount:a,columnCount:o}}getFilteredOutRows(e,t,r,n){const s=[];for(let a=r;a<=n;a++)this._sheetRowFilteredService.getRowFiltered(e,t,a)&&s.push(a);return s}load(e){if(e.allUnitData&&e.unitSheetNameMap&&e.unitStylesData)this._unitData=e.allUnitData,this._unitStylesData=e.unitStylesData,this._sheetNameMap=e.unitSheetNameMap;else{const{allUnitData:t,unitSheetNameMap:r,unitStylesData:n}=this._loadSheetData();this._unitData=t,this._unitStylesData=n,this._sheetNameMap=r}e.rowData&&this._applyUnitRowData(e.rowData),this._formulaData=e.formulaData,this._arrayFormulaCellData=Ja(e.arrayFormulaCellData),this._arrayFormulaRange=e.arrayFormulaRange,this._forceCalculate=e.forceCalculate,this._clearDependencyTreeCache=e.clearDependencyTreeCache||{},this._dirtyRanges=e.dirtyRanges,this._dirtyNameMap=e.dirtyNameMap,this._dirtyDefinedNameMap=e.dirtyDefinedNameMap,this._dirtyUnitFeatureMap=e.dirtyUnitFeatureMap,this._dirtyUnitOtherFormulaMap=e.dirtyUnitOtherFormulaMap,this._excludedCell=e.excludedCell,this._mergeNameMap(this._sheetNameMap,this._dirtyNameMap)}getDirtyData(){return{forceCalculation:this._forceCalculate,dirtyRanges:this._dirtyRanges,dirtyNameMap:this._dirtyNameMap,dirtyDefinedNameMap:this._dirtyDefinedNameMap,dirtyUnitFeatureMap:this._dirtyUnitFeatureMap,dirtyUnitOtherFormulaMap:this._dirtyUnitOtherFormulaMap,clearDependencyTreeCache:this._clearDependencyTreeCache}}loadDirtyRangesAndExcludedCell(e,t){this._dirtyRanges=e,this._excludedCell=t,this._dirtyNameMap={}}registerUnitData(e){this._unitData=e}registerFormulaData(e){this._formulaData=e}registerSheetNameMap(e){this._sheetNameMap=e}_mergeNameMap(e,t){Object.keys(t).forEach(r=>{t[r]&&Object.keys(t[r]).forEach(n=>{e[r]==null&&(e[r]={}),e[r][t[r][n]]=n})}),this._sheetIdToNameMap={},Object.keys(e).forEach(r=>{Object.keys(e[r]).forEach(n=>{this._sheetIdToNameMap[r]==null&&(this._sheetIdToNameMap[r]={}),this._sheetIdToNameMap[r][e[r][n]]=n})})}_loadSheetData(){const e=this._univerInstanceService.getCurrentUnitForType(M.UniverInstanceType.UNIVER_SHEET),t=e==null?void 0:e.getActiveSheet();return this._executeUnitId=e==null?void 0:e.getUnitId(),this._executeSubUnitId=t==null?void 0:t.getSheetId(),this._formulaDataModel.getCalculateData()}_applyUnitRowData(e){for(const t in e)if(e[t]!=null)for(const r in e[t])e[t][r]!=null&&(this._unitData[t]==null&&(this._unitData[t]={}),this._unitData[t][r]==null&&(this._unitData[t][r]={cellData:new M.ObjectMatrix({}),rowCount:0,columnCount:0,rowData:{},columnData:{}}),this._unitData[t][r].rowData=e[t][r])}},w.FormulaCurrentConfigService=Tc([en(0,M.IUniverInstanceService),en(1,M.Inject(M.LocaleService)),en(2,M.Inject(w.FormulaDataModel)),en(3,M.Inject(Es))],w.FormulaCurrentConfigService);const At=M.createIdentifier("univer.formula.current-data.service");var Ic=Object.getOwnPropertyDescriptor,Fc=(i,u,e,t)=>{for(var r=t>1?void 0:t?Ic(u,e):u,n=i.length-1,s;n>=0;n--)(s=i[n])&&(r=s(r)||r);return r},bs=(i,u)=>(e,t)=>u(e,t,i);w.Lexer=class extends M.Disposable{constructor(u,e,t){super(),this._definedNamesService=u,this._lexerTreeBuilder=e,this._formulaCurrentConfigService=t}treeBuilder(u,e=!0){return this._lexerTreeBuilder.treeBuilder(u,e,this._injectDefinedName.bind(this),this._simpleCheckDefinedName.bind(this))}_simpleCheckDefinedName(u){const e=this._formulaCurrentConfigService.getDirtyDefinedNameMap(),t=this._formulaCurrentConfigService.getExecuteUnitId();if(t!=null&&e[t]!=null){const r=Object.keys(e[t]);for(let n=0,s=r.length;n<s;n++){const a=r[n];if(u.indexOf(a)>-1)return!0}}return!1}_checkDefinedNameDirty(u){const e=this._formulaCurrentConfigService.getDirtyDefinedNameMap(),t=this._formulaCurrentConfigService.getExecuteUnitId();if(t!=null&&e[t]!=null){const r=Object.keys(e[t]);for(let n=0,s=r.length;n<s;n++)if(r[n]===u)return!0}return!1}_injectDefinedName(u){const e=this._formulaCurrentConfigService.getExecuteUnitId();if(e==null)return{sequenceString:"",hasDefinedName:!1,definedNames:[]};const t=this._lexerTreeBuilder.getSequenceNode(u);let r="",n=!1;const s=[];for(let a=0,o=t.length;a<o;a++){const c=t[a];if(typeof c=="string"){r+=c;continue}const{nodeType:l,token:f}=c;if(l===Ie.REFERENCE||l===Ie.FUNCTION){const m=this._definedNamesService.getValueByName(e,f);if(m){let d=m.formulaOrRefString;d.substring(0,1)===Z.EQUALS&&(d=d.substring(1)),r+=d,s.push(m.name),n=!0}else this._checkDefinedNameDirty(f)?(r+=h.NAME,n=!0,s.push(f)):r+=f}else r+=f}return{sequenceString:r,hasDefinedName:n,definedNames:s}}},w.Lexer=Fc([bs(0,Tt),bs(1,M.Inject(zr)),bs(2,At)],w.Lexer);function tn(i){if(i==null)return!0;const{v:u,f:e,si:t,p:r}=i;return!(!(u==null||typeof u=="string"&&u.length===0)||e!=null&&e.length>0||t!=null&&t.length>0||r!=null)}function di(i,u,e,t,r){var s;const n=Object.keys(r);for(const a of n){const o=r[a],c=(s=o==null?void 0:o[t])==null?void 0:s[e];if(c==null)continue;const l=c.getValue(i,u);if(l!=null)return l}}const Bc=["$","£","¥","¤","֏","؋","৳","฿","៛","₡","₦","₩","₪","₫","€","₭","₮","₱","₲","₴","₸","₹","₺","₼","₽","₾","₿"],kc={currency:2,date:4,datetime:5,error:11,fraction:7,general:0,grouped:11,number:1,percent:6,scientific:8,text:9,time:5,unknown:11};function $c(i,u,e){var o,c;if(i==null||u==null)return u;const t=(e==null?void 0:e.getStyleByCell(i))||i.s,r=(e==null?void 0:e.getStyleByCell(u))||u.s;if(t==null||r==null||typeof t!="object"||typeof r!="object")return u;const n=(o=t==null?void 0:t.n)==null?void 0:o.pattern,s=(c=r==null?void 0:r.n)==null?void 0:c.pattern;if(n==null||s==null)return u;const a=n||s;return r.n.pattern=a,u}const ys=new _t(1e5);function Ci(i){const u=ys.get(i);if(u!==void 0)return u;const e=Hc(i);return ys.set(i,e),e}function Yc(){ys.clear()}function Hc(i){if(Gc(i))return 3;const u=M.numfmt.getFormatInfo(i).type||"unknown";return kc[u]}function Gc(i){return!!Bc.find(u=>i.includes(u))&&i.startsWith("_(")}function rn(i,u,e){if(i==="")return u;if(u==="")return i;const t=Ci(i),r=Ci(u);return e===Z.PLUS||e===Z.MINUS?t===4&&r===4||t===11&&r===11?"":u:e===Z.MULTIPLY||e===Z.DIVIDED?t===6&&r===6||t===7&&r===7||t===8&&r===8||t===9&&r===9?u:"":i||u}const Wc=new Map([[M.LocaleType.EN_US,"$"],[M.LocaleType.RU_RU,"₽"],[M.LocaleType.VI_VN,"₫"],[M.LocaleType.ZH_CN,"¥"],[M.LocaleType.ZH_TW,"NT$"],[M.LocaleType.FR_FR,"€"],[M.LocaleType.FA_IR,"﷼"],[M.LocaleType.KO_KR,"₩"],[M.LocaleType.ES_ES,"€"],[M.LocaleType.CA_ES,"€"]]);function _i(i){return Wc.get(i)||"$"}function He(i,u=2){let e=u;u>127&&(e=127);let t="";return e>0&&(t=`.${"0".repeat(e)}`),`"${_i(i)}"#,##0${t}_);[Red]("${_i(i)}"#,##0${t})`}function Qc(i,u,e=2){return M.numfmt.format(He(i,e),u)}const Rs=new _t(1e5);function qc(i){let u=i;u.startsWith('"')&&u.endsWith('"')&&(u=u.slice(1,-1));const e=Rs.get(u);if(e)return{isNumberPattern:!0,value:e.value,pattern:e.pattern};const t=M.numfmt.parseNumber(u);if(t&&t.z)return Ns(u,t.v,t.z);const r=M.numfmt.parseDate(u);if(r&&r.z)return Ns(u,r.v,r.z);const n=M.numfmt.parseTime(u);return n&&n.z?Ns(u,n.v,n.z):{isNumberPattern:!1}}function Ns(i,u,e){return Rs.set(i,{value:u,pattern:e}),{isNumberPattern:!0,value:u,pattern:e}}function Kc(){Rs.clear()}class nn{constructor(){A(this,"pattern","")}dispose(){}getPattern(){return this.pattern}setPattern(u){this.pattern=u}isError(){return!1}isAsyncObject(){return!1}isAsyncArrayObject(){return!1}isReferenceObject(){return!1}isArray(){return!1}isValueObject(){return!1}isEqualType(u){return!1}}class Et extends nn{constructor(e){super();A(this,"_customData");this._rawValue=e}isValueObject(){return!0}toUnitRange(){return{range:{startColumn:-1,startRow:-1,endRow:-1,endColumn:-1},sheetId:"",unitId:""}}getValue(){return 0}getArrayValue(){return[]}setValue(e){}setArrayValue(e){}withCustomData(e){return this._customData=e,this}getCustomData(){return this._customData}isCube(){return!1}isString(){return!1}isNumber(){return!1}isBoolean(){return!1}isLambda(){return!1}isError(){return!1}isNull(){return!1}isHyperlink(){return!1}sum(){return g.create(h.VALUE)}max(){return g.create(h.VALUE)}min(){return g.create(h.VALUE)}count(){return g.create(h.VALUE)}countA(){return g.create(h.VALUE)}countBlank(){return g.create(h.VALUE)}getNegative(){return g.create(h.VALUE)}getReciprocal(){return g.create(h.VALUE)}plus(e){return g.create(h.VALUE)}minus(e){return g.create(h.VALUE)}multiply(e){return g.create(h.VALUE)}divided(e){return g.create(h.VALUE)}mod(e){return g.create(h.VALUE)}map(e){return g.create(h.NAME)}mapValue(e){return g.create(h.NAME)}compare(e,t,r=!1){return g.create(h.NAME)}isEqual(e){return this.compare(e,$.EQUALS)}isNotEqual(e){return this.compare(e,$.NOT_EQUAL)}isGreaterThanOrEqual(e){return this.compare(e,$.GREATER_THAN_OR_EQUAL)}isLessThanOrEqual(e){return this.compare(e,$.LESS_THAN_OR_EQUAL)}isLessThan(e){return this.compare(e,$.LESS_THAN)}isGreaterThan(e){return this.compare(e,$.GREATER_THAN)}concatenateFront(e){return g.create(h.NAME)}concatenateBack(e){return g.create(h.NAME)}plusBy(e){return g.create(h.VALUE)}minusBy(e){return g.create(h.VALUE)}multiplyBy(e){return g.create(h.VALUE)}dividedBy(e){return g.create(h.VALUE)}modInverse(e){return g.create(h.VALUE)}compareBy(e,t){return g.create(h.NAME)}concatenate(e,t=et.FRONT){let r=this.getValue().toString();if(typeof e=="string")t===et.FRONT?r=e+r:r+=e;else if(typeof e=="number")t===et.FRONT?r=e.toString()+r:r+=e.toString();else if(typeof e=="boolean"){const n=e?"TRUE":"FALSE";t===et.FRONT?r=n+r:r+=n}return r}pow(e){return g.create(h.VALUE)}powInverse(e){return g.create(h.VALUE)}sqrt(){return g.create(h.VALUE)}cbrt(){return g.create(h.VALUE)}cos(){return g.create(h.VALUE)}cosh(){return g.create(h.VALUE)}acos(){return g.create(h.VALUE)}acosh(){return g.create(h.VALUE)}sin(){return g.create(h.VALUE)}sinh(){return g.create(h.VALUE)}asin(){return g.create(h.VALUE)}asinh(){return g.create(h.VALUE)}tan(){return g.create(h.VALUE)}tanh(){return g.create(h.VALUE)}atan(){return g.create(h.VALUE)}atan2(e){return g.create(h.VALUE)}atan2Inverse(e){return g.create(h.VALUE)}atanh(){return g.create(h.VALUE)}mean(){return this}median(){return this}var(){return this}std(){return this}log(){return g.create(h.VALUE)}log10(){return g.create(h.VALUE)}exp(){return g.create(h.VALUE)}abs(){return g.create(h.VALUE)}round(e){return g.create(h.VALUE)}roundInverse(e){return g.create(h.VALUE)}floor(e){return g.create(h.VALUE)}floorInverse(e){return g.create(h.VALUE)}ceil(e){return g.create(h.VALUE)}ceilInverse(e){return g.create(h.VALUE)}convertToNumberObjectValue(){return g.create(h.VALUE)}convertToBooleanObjectValue(){return g.create(h.VALUE)}}const Xc=1e3,ps=new _t(Xc);class g extends Et{constructor(u,e=""){super(u),this._errorType=u,this._errorContent=e}static create(u,e=""){const t=`${u}-${e}`,r=ps.get(t);if(r)return r;const n=new g(u,e);return ps.set(t,n),n}getValue(){return this._errorType}getErrorType(){return this._errorType}getErrorContent(){return this._errorContent}isEqualType(u){return u.getErrorType()===this.getErrorType()}isError(){return!0}}function Zc(i){var u;if(i===null)return 0;if(i!=null&&i.p){const e=i==null?void 0:i.p.body;if(e==null)return 0;const t=e.dataStream;return M.BuildTextUtils.transform.getPlainText(t)}return(u=i==null?void 0:i.v)!=null?u:0}function zc(i,u=!1){return!u&&!(M.isFormulaString(i==null?void 0:i.f)||M.isFormulaId(i==null?void 0:i.si))?null:typeof(i==null?void 0:i.v)=="string"&&pt.has(i.v)?i.v:null}var Bt=(i=>(i[i.MIN=0]="MIN",i[i.MAX=1]="MAX",i))(Bt||{}),Se=(i=>(i[i.NORMAL=0]="NORMAL",i[i.MIN=1]="MIN",i[i.MAX=2]="MAX",i))(Se||{});function sn(){return Intl&&Intl.Collator?new Intl.Collator(void 0,{numeric:!1}).compare:(i,u)=>i.localeCompare(u)}function Jc(i){return i.indexOf("*")>-1||i.indexOf("?")>-1}function Vs(i,u){const e=tl(u).replace(/~?[*?]/g,r=>r.startsWith("~")?`\\${r.substring(1)}`:r==="*"?".*":r==="?"?".":r);return new RegExp(`^${e}$`).test(i)}function Ai(i){return i.replace(/~?[*?]/g,u=>u.startsWith("~")?u.substring(1):" ")}function el(i,u,e){let t=!1;switch(e){case $.EQUALS:t=Vs(i,u);break;case $.NOT_EQUAL:t=!Vs(i,u);break;case $.GREATER_THAN:case $.GREATER_THAN_OR_EQUAL:t=Vs(i,u)||i>Ai(u);break;case $.LESS_THAN:case $.LESS_THAN_OR_EQUAL:t=i<Ai(u);break}return t}function tl(i){return i.replace(/[.+^${}()|[\]\\]/g,"\\$&")}function Os(i){switch(i){case 1:return 2;case 0:return 0;case-1:return 1;default:return 0}}function Ms(i){return i===-2?1:0}function Ss(i){let u;switch(i){case $.EQUALS:u=$.EQUALS;break;case $.GREATER_THAN:u=$.LESS_THAN;break;case $.GREATER_THAN_OR_EQUAL:u=$.LESS_THAN_OR_EQUAL;break;case $.LESS_THAN:u=$.GREATER_THAN;break;case $.LESS_THAN_OR_EQUAL:u=$.GREATER_THAN_OR_EQUAL;break;case $.NOT_EQUAL:u=$.NOT_EQUAL;break}return u}/*!
1
+ (function(w,M){typeof exports=="object"&&typeof module<"u"?M(exports,require("@univerjs/core"),require("rxjs"),require("@univerjs/rpc")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","rxjs","@univerjs/rpc"],M):(w=typeof globalThis<"u"?globalThis:w||self,M(w.UniverEngineFormula={},w.UniverCore,w.rxjs,w.UniverRpc))})(this,(function(w,M,De,Qu){"use strict";var kE=Object.defineProperty;var $E=(w,M,De)=>M in w?kE(w,M,{enumerable:!0,configurable:!0,writable:!0,value:De}):w[M]=De;var A=(w,M,De)=>$E(w,typeof M!="symbol"?M+"":M,De);var ja;var Rt=(i=>(i.FALSE="FALSE",i.TRUE="TRUE",i))(Rt||{}),Nt=(i=>(i[i.SUCCESS=0]="SUCCESS",i[i.ERROR=1]="ERROR",i))(Nt||{}),et=(i=>(i[i.FRONT=0]="FRONT",i[i.BACK=1]="BACK",i))(et||{});function Ua(i,u,e,t,r){for(let n=0,s=i.length;n<s;n++){const a=i[n];if(u!==a.unitId||e!==a.sheetId)continue;const{startRow:o,startColumn:c,endRow:l,endColumn:f}=a.range;if(t>=o&&t<=l&&r>=c&&r<=f)return!0}return!1}var h=(i=>(i.DIV_BY_ZERO="#DIV/0!",i.NAME="#NAME?",i.VALUE="#VALUE!",i.NUM="#NUM!",i.NA="#N/A",i.CYCLE="#CYCLE!",i.REF="#REF!",i.SPILL="#SPILL!",i.CALC="#CALC!",i.ERROR="#ERROR!",i.CONNECT="#GETTING_DATA",i.NULL="#NULL!",i))(h||{});const pt=new Set(Object.values(h)),va=[...new Set(Object.values(h).map(i=>i.length))];var Ta=(i=>(i[i.Financial=0]="Financial",i[i.Date=1]="Date",i[i.Math=2]="Math",i[i.Statistical=3]="Statistical",i[i.Lookup=4]="Lookup",i[i.Database=5]="Database",i[i.Text=6]="Text",i[i.Logical=7]="Logical",i[i.Information=8]="Information",i[i.Engineering=9]="Engineering",i[i.Cube=10]="Cube",i[i.Compatibility=11]="Compatibility",i[i.Web=12]="Web",i[i.Array=13]="Array",i[i.Univer=14]="Univer",i[i.User=15]="User",i[i.DefinedName=16]="DefinedName",i))(Ta||{}),Z=(i=>(i.PLUS="+",i.MINUS="-",i.MULTIPLY="*",i.DIVIDED="/",i.CONCATENATE="&",i.POWER="^",i.EQUALS="=",i.NOT_EQUAL="<>",i.GREATER_THAN=">",i.GREATER_THAN_OR_EQUAL=">=",i.LESS_THAN="<",i.LESS_THAN_OR_EQUAL="<=",i))(Z||{}),$=(i=>(i.EQUALS="=",i.NOT_EQUAL="<>",i.GREATER_THAN=">",i.GREATER_THAN_OR_EQUAL=">=",i.LESS_THAN="<",i.LESS_THAN_OR_EQUAL="<=",i))($||{});const Ir=new Map([["<>",4],["<",4],[">=",4],["=",4],[">",4],["<=",4],["&",3],["+",2],["-",2],["/",1],["*",1],["^",0]]),Vt=new Set(Ir.keys()),qu=new Set(["=","<>",">",">=","<","<="]);var X=(i=>(i.OPEN_BRACKET="(",i.CLOSE_BRACKET=")",i.COMMA=",",i.SINGLE_QUOTATION="'",i.DOUBLE_QUOTATION='"',i.OPEN_BRACES="{",i.CLOSE_BRACES="}",i.COLON=":",i.OPEN_SQUARE_BRACKET="[",i.CLOSE_SQUARE_BRACKET="]",i))(X||{}),qe=(i=>(i.PERCENTAGE="%",i.POUND="#",i))(qe||{});const rs=new Set(["%","#"]);var Ve=(i=>(i.AT="@",i.MINUS="-",i.PLUS="+",i))(Ve||{});const Ku=" ",Ia=[...Object.values($),...Object.values(Z),...Object.values(X),...Object.values(qe),...Object.values(Ve)];function hr(i){return Ia.includes(i)}function Fa(i){for(const u of Ia)if(i.indexOf(u)>-1)return!0;return!1}function Xu(i){return i[0]==="'"&&i[i.length-1]==="'"?i.substring(1,i.length-1):i}function Zu(i){return hr(i)&&i!==X.CLOSE_BRACES&&i!==X.CLOSE_BRACKET&&i!==X.SINGLE_QUOTATION&&i!==X.DOUBLE_QUOTATION||i===" "}const zu=new Set([Z.PLUS,Z.MINUS,Z.MULTIPLY,Z.DIVIDED,Z.CONCATENATE,Z.POWER,Z.EQUALS,Z.NOT_EQUAL,Z.GREATER_THAN,Z.GREATER_THAN_OR_EQUAL,Z.LESS_THAN,Z.LESS_THAN_OR_EQUAL,X.OPEN_BRACKET,X.COMMA,X.COLON,X.OPEN_BRACES,X.OPEN_SQUARE_BRACKET]);function Ju(i){return zu.has(i)}const ec=new Set([Z.PLUS,Z.MINUS,Z.MULTIPLY,Z.DIVIDED,Z.CONCATENATE,Z.POWER,Z.EQUALS,Z.NOT_EQUAL,Z.GREATER_THAN,Z.GREATER_THAN_OR_EQUAL,Z.LESS_THAN,Z.LESS_THAN_OR_EQUAL,X.OPEN_BRACKET,X.COMMA,X.COLON,X.OPEN_BRACES,X.OPEN_SQUARE_BRACKET,qe.PERCENTAGE,qe.POUND]);function tc(i){return ec.has(i)}const Ba='\\[([^\\[\\]\\/?:"<>|*\\\\]+)\\]',ka=new RegExp(Ba),rc="((?![\\[\\]\\/?*\\\\]).)*!",Ot="$",ns="\\s*?:\\s*?",Fr="[A-Za-z]+",Br="[1-9][0-9]*",zt=`'?(${Ba})?(${rc})?'?`,ss=`\\${Ot}?${Fr}\\${Ot}?${Br}`,nc=`^(${Ve.AT})?${zt}${ss}${ns}${ss}$`,$a=new RegExp(nc),sc=`^${zt}\\s*?${ss}(${qe.POUND})?$`,Ya=new RegExp(sc),ac=`^${zt}\\${Ot}?${Br}${ns}\\${Ot}?${Br}$`,Ha=new RegExp(ac),ic=`^${zt}\\${Ot}?${Fr}${ns}\\${Ot}?${Fr}$`,Ga=new RegExp(ic),oc=`^${zt}\\s*?\\${Ot}?${Br}$`,Wa=new RegExp(oc),uc=`^${zt}\\s*?\\${Ot}?${Fr}$`,Qa=new RegExp(uc),cc="{.*?}",qa=new RegExp(cc,"g");function gr(i){return Ya.lastIndex=0,Ya.test(i)}function lc(i){return $a.lastIndex=0,$a.test(i)}function Ka(i){return Ha.lastIndex=0,Ha.test(i)}function Xa(i){return Ga.lastIndex=0,Ga.test(i)}function Za(i){return Wa.lastIndex=0,Wa.test(i)}function za(i){return Qa.lastIndex=0,Qa.test(i)}function fc(i){return qa.lastIndex=0,qa.test(i)}function as(i){return gr(i)||lc(i)||Ka(i)||Xa(i)}function Ja(i){const u={};return Object.keys(i).forEach(e=>{const t=i[e];if(t==null)return!0;u[e]==null&&(u[e]={}),Object.keys(t).forEach(r=>{const n=t[r];u[e][r]=new M.ObjectMatrix(n)})}),u}function mc(i){const u={};for(const e in i){const t=i[e];if(t!=null){u[e]==null&&(u[e]={});for(const r in t){const n=t[r],s={};n.forValue((a,o,c)=>{s[a]===void 0&&(s[a]={}),s[a][o]=c}),u[e][r]=s}}}return u}const ei={id:"formula.mutation.register-function",type:M.CommandType.MUTATION,handler:()=>!0},kr={id:"formula.mutation.set-array-formula-data",type:M.CommandType.MUTATION,handler:(i,u)=>!0},hc=/[^0-9]/g,gc=/[^A-Za-z]/g;function dr(i){let u=i[0]==="$";const e=i.substring(1);let t=e.indexOf("$")>-1;return M.Tools.isStringNumber(e)&&u&&!t&&(u=!1,t=!0),u&&t?M.AbsoluteRefType.ALL:u?M.AbsoluteRefType.COLUMN:t?M.AbsoluteRefType.ROW:M.AbsoluteRefType.NONE}function dc(i){const u=i.split("!");u.length>1&&(i=u[u.length-1]);const e=i.split(":");return e.length>1?{startAbsoluteRefType:dr(e[0]),endAbsoluteRefType:dr(e[1])}:{startAbsoluteRefType:dr(e[0])}}function ti(i=M.AbsoluteRefType.NONE){let u="",e="";return i===M.AbsoluteRefType.ROW?u="$":i===M.AbsoluteRefType.COLUMN?e="$":i===M.AbsoluteRefType.ALL&&(u="$",e="$"),{rowAbsoluteString:u,columnAbsoluteString:e}}function Ct(i){const{startColumn:u,startRow:e,endColumn:t,endRow:r,startAbsoluteRefType:n,endAbsoluteRefType:s,rangeType:a=M.RANGE_TYPE.NORMAL}=i,o=ti(n),c=ti(s);if(a===M.RANGE_TYPE.ROW||a===M.RANGE_TYPE.ALL){const m=`${o.rowAbsoluteString}${e+1}`,d=`${c.rowAbsoluteString}${r+1}`;return`${m}:${d}`}if(a===M.RANGE_TYPE.COLUMN){const m=`${o.columnAbsoluteString}${M.Tools.chatAtABC(u)}`,d=`${c.columnAbsoluteString}${M.Tools.chatAtABC(t)}`;return`${m}:${d}`}const l=`${o.columnAbsoluteString}${M.Tools.chatAtABC(u)}${o.rowAbsoluteString}${e+1}`,f=`${c.columnAbsoluteString}${M.Tools.chatAtABC(t)}${c.rowAbsoluteString}${r+1}`;return l===f?l:`${l}:${f}`}function ri(i,u){return`${ii(i)}!${Ct(u)}`}function ni(i,u,e){return Gr(i)||Gr(u)?`'[${Wr(i)}]${Wr(u)}'!${Ct(e)}`:`[${i}]${u}!${Ct(e)}`}function $r(i){const{unitId:u,sheetName:e,range:t}=i;return u!=null&&u.length>0&&e!=null&&e.length>0?ni(u,e,t):e!=null&&e.length>0?ri(e,t):Ct(t)}function Yr(i){const u=Number.parseInt(i.replace(hc,""))-1,e=M.Tools.ABCatNum(i.replace(gc,"")),t=dr(i);return{row:u,column:e,absoluteRefType:t}}function Hr(i){const u=ka.exec(i);let e="";u!=null&&(e=u[0].trim(),e=is(e.slice(1,e.length-1)),i=i.replace(ka,""));const t=i.indexOf("!");let r="",n="";return t>-1?(r=i.substring(0,t),r[0]==="'"&&r[r.length-1]==="'"&&(r=r.substring(1,r.length-1)),r=is(r),n=i.substring(t+1)):n=i,{refBody:n,sheetName:r,unitId:e}}function Jt(i){const{refBody:u,sheetName:e,unitId:t}=Hr(i),r=u.indexOf(":");if(r===-1){const _=Yr(u),C=_.row,E=_.column,b=_.absoluteRefType;return{unitId:t,sheetName:e,range:{startRow:C,startColumn:E,endRow:C,endColumn:E,startAbsoluteRefType:b,endAbsoluteRefType:b}}}const n=u.substring(0,r),s=u.substring(r+1),a=Yr(n),o=Yr(s),c=a.row>o.row?o.row:a.row,l=a.column>o.column?o.column:a.column,f=a.row>o.row?a.row:o.row,m=a.column>o.column?a.column:o.column;let d=M.RANGE_TYPE.NORMAL;return Number.isNaN(c)&&Number.isNaN(f)?d=M.RANGE_TYPE.COLUMN:Number.isNaN(l)&&Number.isNaN(m)&&(d=M.RANGE_TYPE.ROW),{unitId:t,sheetName:e,range:{startRow:c,startColumn:l,endRow:f,endColumn:m,startAbsoluteRefType:a.absoluteRefType,endAbsoluteRefType:o.absoluteRefType,rangeType:d}}}const Cc=["LOG10"];function Cr(i){const u=si(i);if(!as(u)||Cc.includes(u.toUpperCase().trim()))return!1;const{range:e}=Jt(u);return!(e.endColumn>=16384)}function si(i){const u=[];let e=!1;for(let t=0,r=i.length;t<r;t++){const n=i[t];if(n===Ku&&!e)u.push(n);else{if(!e&&(n===Ve.AT||n===Ve.MINUS||n===Ve.PLUS))continue;u.push(n),e=!0}}return u.join("")}function _c(i,u){const e=(i==null?void 0:i.split(","))||[];return i===""||e.length===0?[]:ai(i)?e.map(n=>{const s=Jt(n);return{unitId:s.unitId,sheetId:u(s.sheetName),range:s.range,sheetName:s.sheetName}}):[]}function ai(i){return((i==null?void 0:i.split(","))||[]).every(e=>Cr(e.trim()))}function Gr(i){return i.length===0?!1:!!(Fa(i)||Ac(i)||Ec(i)||bc(i)||/[\s!$%^&*()+\-=\[\]{};':"\\|,.<>\/?()]/.test(i))}function ii(i){return Gr(i)?`'${Wr(i)}'`:i}function Wr(i){return i.replace(/'/g,"''")}function is(i){return i.replace(/''/g,"'")}function Ac(i){const u=i.match(/[1-9][0-9]{0,6}/);return/^[A-Z]+[1-9][0-9]{0,6}$/.test(i)&&u!==null}function Ec(i){return/^(R(-?[0-9]+)?C(-?[0-9]+)?|C(-?[0-9]+)?|R(-?[0-9]+)?)$/.test(i)}function bc(i){return!new RegExp("^\\p{Letter}","u").test(i.charAt(0))}var yc=Object.getOwnPropertyDescriptor,Rc=(i,u,e,t)=>{for(var r=t>1?void 0:t?yc(u,e):u,n=i.length-1,s;n>=0;n--)(s=i[n])&&(r=s(r)||r);return r},Nc=(i,u)=>(e,t)=>u(e,t,i);w.DefinedNamesService=class extends M.Disposable{constructor(e){super();A(this,"_definedNameMap",{});A(this,"_nameCacheMap",{});A(this,"_update$",new De.Subject);A(this,"update$",this._update$.asObservable());A(this,"_currentRange",{unitId:"",sheetId:"",range:{startRow:0,endRow:0,startColumn:0,endColumn:0}});A(this,"_currentRange$",new De.Subject);A(this,"currentRange$",this._currentRange$.asObservable());A(this,"_focusRange$",new De.Subject);A(this,"focusRange$",this._focusRange$.asObservable());this._univerInstanceService=e}dispose(){super.dispose(),this._definedNameMap={},this._nameCacheMap={},this._update$.complete(),this._currentRange$.complete(),this._focusRange$.complete()}getWorksheetByRef(e,t){var n;const{sheetName:r}=Hr(t);return(n=this._univerInstanceService.getUnit(e))==null?void 0:n.getSheetBySheetName(r)}focusRange(e,t){const r=this.getValueById(e,t);r!==void 0&&this._focusRange$.next({...r,unitId:e})}setCurrentRange(e){this._currentRange=e,this._currentRange$.next(e)}getCurrentRange(){return this._currentRange}getCurrentRangeForString(){return Ct(this._currentRange.range)}registerDefinedNames(e,t){this._definedNameMap[e]=t,this._updateCache(e),this._update()}registerDefinedName(e,t){this._definedNameMap[e]===void 0&&(this._definedNameMap[e]={}),this._definedNameMap[e][t.id]=t,this._updateCache(e),this._update()}removeDefinedName(e,t){var r;(r=this._definedNameMap[e])==null||delete r[t],this._updateCache(e),this._update()}removeUnitDefinedName(e){delete this._definedNameMap[e],this._updateCache(e),this._update()}getDefinedNameMap(e){return this._definedNameMap[e]}getValueByName(e,t){const r=this._nameCacheMap[e];if(r)return r[t]||null;const n=this._definedNameMap[e];if(n===void 0)return null;let s=null;for(const a of Object.values(n))if(a.name===t){s=a;break}return s&&(this._nameCacheMap[e]=this._nameCacheMap[e]||{},this._nameCacheMap[e][t]=s),s}getValueById(e,t){var r;return(r=this._definedNameMap[e])==null?void 0:r[t]}hasDefinedName(e){return this._definedNameMap[e]===void 0?!1:(Array.from(Object.values(this._definedNameMap[e])).length||0)!==0}_update(){this._update$.next(null)}_updateCache(e){const t=this._definedNameMap[e];if(t===void 0){delete this._nameCacheMap[e];return}this._nameCacheMap[e]={};for(const r of Object.values(t))this._nameCacheMap[e][r.name]=r}},w.DefinedNamesService=Rc([Nc(0,M.IUniverInstanceService)],w.DefinedNamesService);const Tt=M.createIdentifier("univer.formula.defined-names.service"),pc=(i,u)=>{const{unitId:e,id:t}=u;return{...i.get(Tt).getValueById(e,t),unitId:e}},Qr={id:"formula.mutation.set-defined-name",type:M.CommandType.MUTATION,handler:(i,u)=>{if(u==null)return!1;const e=i.get(Tt),{id:t,unitId:r,name:n,formulaOrRefString:s,comment:a,hidden:o,localSheetId:c}=u;return e.registerDefinedName(r,{id:t,name:n.trim(),formulaOrRefString:s.trim(),comment:a==null?void 0:a.trim(),hidden:o,localSheetId:c}),!0}},os={id:"formula.mutation.remove-defined-name",type:M.CommandType.MUTATION,handler:(i,u)=>{if(u==null)return!1;const e=i.get(Tt),{unitId:t,id:r}=u;return e.removeDefinedName(t,r),!0}},qr={id:"formula.mutation.set-feature-calculation",type:M.CommandType.MUTATION,handler:()=>!0},Kr={id:"formula.mutation.remove-feature-calculation",type:M.CommandType.MUTATION,handler:()=>!0},us={id:"formula.mutation.set-formula-calculation-start",type:M.CommandType.MUTATION,handler:()=>!0},cs={id:"formula.mutation.set-formula-calculation-stop",type:M.CommandType.MUTATION,handler:()=>!0},_r={id:"formula.mutation.set-formula-calculation-notification",type:M.CommandType.MUTATION,handler:()=>!0},ls={id:"formula.mutation.set-formula-calculation-result",type:M.CommandType.MUTATION,handler:()=>!0},fs={id:"formula.mutation.set-formula-data",type:M.CommandType.MUTATION,handler:(i,u)=>!0},Xr={id:"formula.mutation.set-other-formula",type:M.CommandType.MUTATION,handler:()=>!0},Zr={id:"formula.mutation.remove-other-formula",type:M.CommandType.MUTATION,handler:()=>!0},oi={id:"formula.mutation.set-super-table",type:M.CommandType.MUTATION,handler:()=>!0},ui={id:"formula.mutation.remove-super-table",type:M.CommandType.MUTATION,handler:()=>!0},ms={id:"formula.mutation.set-super-table-option",type:M.CommandType.MUTATION,handler:()=>!0};class _t{constructor(u){A(this,"_cache");this._cache=new M.LRUMap(u)}set(u,e){const t=this._hash(u);this._cache.set(t,e)}get(u){const e=this._hash(u);return this._cache.get(e)}clear(){this._cache.clear()}delete(u){this._cache.delete(this._hash(u))}forEach(u,e){this._cache.forEach(u,e)}_hash(u){return u.length<=64?u:M.hashAlgorithm(u).toString()}}const at="P_1",Mt="R_1",er="L_1",Ar="LR_1",ci="LO_1",Vc="LET",Er="LAMBDA",Oc="CUBE",Mc=new Set(["RAND","RANDBETWEEN","NOW","TODAY"]),hs=new _t(1e5);function It(i){const u=hs.get(i);if(u)return u;const e=Jt(i);return hs.set(i,e),Jt(i)}function Sc(){hs.clear()}var Ie=(i=>(i[i.NORMAL=0]="NORMAL",i[i.NUMBER=1]="NUMBER",i[i.STRING=2]="STRING",i[i.FUNCTION=3]="FUNCTION",i[i.REFERENCE=4]="REFERENCE",i[i.ARRAY=5]="ARRAY",i[i.DEFINED_NAME=6]="DEFINED_NAME",i))(Ie||{});function gs(i){let u="";for(const e of i)typeof e=="string"?u+=e:u+=e.token;return u}class ie{constructor(){A(this,"_parent");A(this,"_token",Mt);A(this,"_children",[]);A(this,"_lambdaId");A(this,"_functionDefinitionPrivacyVar");A(this,"_lambdaParameter","");A(this,"_startIndex",-1);A(this,"_endIndex",-1);A(this,"_definedNames",[])}dispose(){var u;this._children.forEach(e=>{typeof e!="string"&&e.dispose()}),(u=this._functionDefinitionPrivacyVar)==null||u.clear(),this._functionDefinitionPrivacyVar=null,this._children=[],this._parent=null,this._definedNames=[]}getDefinedNames(){return this._definedNames}getStartIndex(){return this._startIndex}getLambdaId(){return this._lambdaId}setLambdaId(u){this._lambdaId=u}getFunctionDefinitionPrivacyVar(){return this._functionDefinitionPrivacyVar}setLambdaPrivacyVar(u){this._functionDefinitionPrivacyVar=u}getLambdaParameter(){return this._lambdaParameter}setLambdaParameter(u){this._lambdaParameter=u}getParent(){return this._parent}setParent(u){this._parent=u}getChildren(){return this._children}setChildren(u){this._children=u}addChildren(u){this._children.push(u)}addChildrenFirst(u){this._children.unshift(u)}getToken(){return this._token}setToken(u){this._token=u}setIndex(u,e){this._startIndex=u,this._endIndex=e}setDefinedNames(u){this._definedNames=u}hasDefinedNames(){return this._definedNames.length>0}replaceChild(u,e){const t=this._getIndexInParent(u);t!=null&&(this.getChildren().splice(t,1,e),e.setParent(this))}changeToParent(u){const e=this.getParent();e&&e.removeChild(this),this.setParent(u),u.getChildren().push(this)}removeChild(u){const e=this._getIndexInParent(u);e!=null&&this.getChildren().splice(e,1)}serialize(){const u=this.getToken(),e=this.getChildren(),t=[],r=e.length;for(let n=0;n<r;n++){const s=e[n];s instanceof ie?t.push(s.serialize()):t.push(s)}return{token:u,st:this._startIndex,ed:this._endIndex,children:t}}_getIndexInParent(u){const e=this.getChildren(),t=e.length;for(let r=0;r<t;r++)if(e[r]===u)return r}}const li=2e3,ds=new _t(li),Cs=new _t(li);class zr extends M.Disposable{constructor(){super(...arguments);A(this,"_currentLexerNode",new ie);A(this,"_upLevel",0);A(this,"_segment","");A(this,"_bracketState",[]);A(this,"_squareBracketState",0);A(this,"_bracesState",0);A(this,"_singleQuotationState",0);A(this,"_doubleQuotationState",0);A(this,"_lambdaState",!1);A(this,"_colonState",!1);A(this,"_formulaErrorCount",0);A(this,"_tableBracketState",!1)}dispose(){this._resetTemp(),this._currentLexerNode.dispose(),ds.clear(),Cs.clear()}getUpLevel(){return this._upLevel}isColonClose(){return this._colonState===!1}isColonOpen(){return this._colonState===!0}isDoubleQuotationClose(){return this._doubleQuotationState===0}isLambdaOpen(){return this._lambdaState===!0}isLambdaClose(){return this._lambdaState===!1}isSingleQuotationClose(){return this._singleQuotationState===0}isBracesClose(){return this._bracesState===0}isBracketClose(){return this._bracketState.length===0}isSquareBracketClose(){return this._squareBracketState===0}getCurrentLexerNode(){return this._currentLexerNode}getFunctionAndParameter(e,t){const r=this._getCurrentParamIndex(e,t);if(r==null||r===h.VALUE)return;const n=r[0];if(typeof n=="string")return;let s=n.getParent(),a=n;for(;s;){const o=s.getToken();if(o!==at&&!hr(o)&&s.getStartIndex()!==-1){const c=s.getChildren().indexOf(a);return{functionName:o,paramIndex:c}}a=s,s=s.getParent()}}checkIfAddBracket(e){let t=0,r=e.length-1,n=e[r];for(;(n===X.CLOSE_BRACKET||n===" ")&&r>=0;)n===X.CLOSE_BRACKET&&t++,n=e[--r];const s=this._getCurrentParamIndex(e,e.length-2);if(s==null||s===h.VALUE)return 0;const a=s[0];if(typeof a=="string")return 0;let o=a.getParent(),c=0;for(s[1]===X.OPEN_BRACKET&&c++;o;){const l=o.getToken();l!==at&&l!==X.COLON&&o.getStartIndex()!==-1&&l.toUpperCase()!==Er&&(t===0?c+=1:t--),o=o.getParent()}return c}sequenceNodesBuilder(e){const t=Cs.get(e);if(t)return[...t];const r=this._getSequenceArray(e);if(r.length===0)return;const n=this.getSequenceNode(r);return Cs.set(e,[...n]),n}convertRefersToAbsolute(e,t,r,n=""){const s=this.sequenceNodesBuilder(e);if(s==null)return e;let a="";e.substring(0,1)===Z.EQUALS&&(a=Z.EQUALS);for(let o=0,c=s.length;o<c;o++){const l=s[o];if(typeof l!="string"&&l.nodeType===Ie.REFERENCE){const{token:f,endIndex:m}=l,d=It(f);if(d==null)continue;const{range:_,sheetName:C,unitId:E}=d,b={..._,startAbsoluteRefType:t,endAbsoluteRefType:r},y=$r({range:b,unitId:E,sheetName:C||n}),N=y.length-f.length;s[o]={...l,token:y,endIndex:m+N};for(let V=o+1;V<c;V++){const S=s[V];typeof S!="string"&&(S.startIndex+=N,S.endIndex+=N)}}}return`${a}${gs(s)}`}moveFormulaRefOffset(e,t,r,n=!1){const s=this.sequenceNodesBuilder(e);if(s==null)return e;const a=[];for(let o=0,c=s.length;o<c;o++){const l=s[o];if(typeof l=="string"||l.nodeType!==Ie.REFERENCE){a.push(l);continue}const{token:f}=l,m=It(f),{sheetName:d,unitId:_}=m;let C=m.range;if(!n&&C.startAbsoluteRefType===M.AbsoluteRefType.ALL&&C.endAbsoluteRefType===M.AbsoluteRefType.ALL){a.push(l);continue}else C=M.moveRangeByOffset(C,t,r,n);let E="";M.isValidRange(C)?E=$r({range:C,unitId:_,sheetName:d}):E=h.REF,a.push({...l,token:E})}return`=${gs(a)}`}_formulaSpellCheck(){if(this._currentLexerNode.getChildren().length===0)return!0;const e=this._currentLexerNode.getChildren()[0];return!!(e instanceof ie||Vt.has(e)||rs.has(e)||e===Ve.AT||e===X.COMMA||e===X.COLON||e===X.OPEN_BRACKET)}getSequenceNode(e){const t=[];let r=!1;for(let n=0,s=e.length;n<s;n++){const a=e[n],o=e[n-1],{segment:c,currentString:l}=a;if(l===X.DOUBLE_QUOTATION&&(r=!0),(c!==""||n===0)&&n!==s-1){t.push(l);continue}let f=(o==null?void 0:o.segment)||"";const m=n-f.length;let d=n-1;const _=n-1;if(n===s-1&&this._isLastMergeString(l)&&(f+=l,d+=1),f===""||Ir.has(f)){t.push(l);continue}const C=f.trim(),E=si(C);r===!0&&C[C.length-1]===X.DOUBLE_QUOTATION&&C[0]!==X.OPEN_BRACES?(r=!1,this._processPushSequenceNode(t,Ie.STRING,f,m,d,_)):gr(E)&&Cr(E)?this._processPushSequenceNode(t,Ie.REFERENCE,f,m,d,_):M.Tools.isStringNumber(C)?this._processPushSequenceNode(t,Ie.NUMBER,f,m,d,_):C.length>0&&this._processPushSequenceNode(t,Ie.FUNCTION,f,m,d,_),(n!==s-1||!this._isLastMergeString(l))&&t.push(l)}return this._mergeSequenceNodeReference(t)}_processPushSequenceNode(e,t,r,n,s,a){this._pushSequenceNode(e,{nodeType:t,token:r,startIndex:n,endIndex:s},a)}_getCurrentParamIndex(e,t){return this._nodeMaker(e,void 0,t)}_isLastMergeString(e){return e===X.DOUBLE_QUOTATION||M.Tools.isStringNumber(e)||!hr(e)}_mergeSequenceNodeReference(e){const t=[],r=e.length;let n=0;for(;n<r;){const s=e[n];if(typeof s=="string"){const a=e[n-1];if(s.trim()===X.CLOSE_BRACES&&a!=null&&typeof a!="string"&&a.nodeType===Ie.FUNCTION&&a.token.trim().substring(0,1)===X.OPEN_BRACES){a.nodeType=Ie.ARRAY,a.token+=s,a.endIndex+=s.length,n++;continue}t.push(s)}else{const a=e[n+1],o=e[n+2];a===X.COLON&&typeof s!="string"&&o!=null&&typeof o!="string"&&Cr((s.token+a+o.token).trim())&&(s.nodeType=Ie.REFERENCE,s.token+=a+o.token,s.endIndex=o.endIndex,n+=2),t.push(s)}n++}return this._minusSplitSequenceNode(t)}_minusSplitSequenceNode(e){const t=[];for(const r of e){if(typeof r!="string"){const s=r.token.match(/^(\s*([-@+]\s*)+)(.*)$/);if(s){const a=s[1],o=s[3];if(Cr(o.trim())){const c=a.length,l=r.startIndex,f=r.startIndex+c-1,m={nodeType:Ie.NORMAL,token:a,startIndex:l,endIndex:f},d={nodeType:Ie.REFERENCE,token:o,startIndex:f+1,endIndex:r.endIndex};t.push(m),t.push(d);continue}}}t.push(r)}return t}_pushSequenceNode(e,t,r){const n=r-t.startIndex+1;e.splice(e.length-n,n,t)}nodeMakerTest(e){return this._nodeMaker(e)}treeBuilder(e,t=!0,r){var f;if(t===!0){const m=ds.get(e),d=r&&((f=this._simpleCheckDefinedName)==null?void 0:f.call(this,e,r));if(m&&!d)return m}this._resetCurrentLexerNode(),this._currentLexerNode.setToken(Mt);const n=[];let s=this._nodeMaker(e,n);if(s===h.VALUE||n.length===0)return s;let a=!1,o="",c=[];if(r){const{hasDefinedName:m,sequenceString:d,definedNames:_}=this._handleDefinedName(n,r);a=m,o=d,c=_}if(a&&(this._resetCurrentLexerNode(),this._currentLexerNode.setToken(Mt),s=this._nodeMaker(`=${o}`),s===h.VALUE))return s;const l=this._getTopNode(this._currentLexerNode);if(l&&(this._currentLexerNode=l),t){if(!this._suffixExpressionHandler(this._currentLexerNode))return h.VALUE;ds.set(e,this._currentLexerNode)}return a&&this._currentLexerNode.setDefinedNames(c),this._currentLexerNode}_handleDefinedName(e,t){const{unitId:r,getValueByName:n}=t;if(r==null)return{sequenceString:"",hasDefinedName:!1,definedNames:[]};const s=this.getSequenceNode(e);let a="",o=!1;const c=[];for(let l=0,f=s.length;l<f;l++){const m=s[l];if(typeof m=="string"){a+=m;continue}const{nodeType:d,token:_}=m;if(d===Ie.REFERENCE||d===Ie.FUNCTION){const C=n(r,_);if(C){const E=C.formulaOrRefString,b=this._handleNestedDefinedName(E,t);if(b==null||typeof b!="object")a+=b||h.NAME,o=!0,c.push(_);else if(typeof b=="object"){const{sequenceString:y,definedNames:N}=b;a+=y,N.forEach(V=>{c.push(V)}),o=!0}}else this._checkDefinedNameDirty(_,t)?(a+=h.NAME,o=!0,c.push(_)):a+=_}else a+=_}return{sequenceString:a,hasDefinedName:o,definedNames:c}}_handleNestedDefinedName(e,t){const r=[],n=this._nodeMaker(e,r);return n===h.VALUE||r.length===0?n:this._handleDefinedName(r,t)}_simpleCheckDefinedName(e,t){const{getDirtyDefinedNameMap:r,unitId:n}=t,s=r(),a=n;if(a!=null&&s[a]!=null){const o=Object.keys(s[a]);for(let c=0,l=o.length;c<l;c++){const f=o[c];if(e.indexOf(f)>-1)return!0}}return!1}_checkDefinedNameDirty(e,t){const{getDirtyDefinedNameMap:r,unitId:n}=t,s=r(),a=n;if(a!=null&&s[a]!=null){const o=Object.keys(s[a]);for(let c=0,l=o.length;c<l;c++)if(o[c]===e)return!0}return!1}_suffixExpressionHandler(e){var o;const t=e.getChildren();if(!t)return!1;const r=t.length,n=[],s=[];let a=!0;for(let c=0;c<r;c++){const l=t[c];if(l instanceof ie)a=this._suffixExpressionHandler(l),n.push(l);else{const f=l.trim();if(f==="")continue;if(Vt.has(f)){if(f===Z.PLUS&&this._deletePlusForPreNode(t[c-1]))continue;if(f!==Z.PLUS&&f!==Z.MINUS&&this._deletePlusForPreNode(t[c-1]))return!1;for(;s.length>0;){const m=(o=s[s.length-1])==null?void 0:o.trim();if(!m||m===X.OPEN_BRACKET)break;const d=Ir.get(m),_=Ir.get(f);if(d===void 0||_===void 0)break;if(_>=d)n.push(s.pop());else break}s.push(l)}else if(f===X.OPEN_BRACKET)s.push(l);else if(f===X.CLOSE_BRACKET)this._processSuffixExpressionCloseBracket(n,s,t,c);else{if(this._checkCloseBracket(t[c-1]))return!1;n.push(l)}}}return a?this._processSuffixExpressionRemain(n,s,e):!1}_processSuffixExpressionRemain(e,t,r){const n=e.length,s=e[n-1];for(;t.length>0;){const a=t.pop();if(!(s instanceof ie)&&(a===X.OPEN_BRACKET||a===X.CLOSE_BRACKET))return!1;e.push(a)}return r.setChildren(e),!0}_processSuffixExpressionCloseBracket(e,t,r,n){var s;if(this._checkOpenBracket(r[n-1])||this._checkOperator(r[n-1]))return!1;for(;t.length>0;){const a=(s=t[t.length-1])==null?void 0:s.trim();if(!a)break;if(a===X.OPEN_BRACKET){t.pop();break}e.push(t.pop())}}_checkCloseBracket(e){return e===X.CLOSE_BRACKET||e instanceof ie}_checkOpenBracket(e){return e===X.OPEN_BRACKET}_checkOperator(e){return e==null||e instanceof ie?!1:Vt.has(e)}_deletePlusForPreNode(e){if(e==null)return!0;if(!(e instanceof ie)){const t=e.trim();if(Vt.has(t)||t===X.OPEN_BRACKET)return!0}return!1}_resetCurrentLexerNode(){this._currentLexerNode=new ie}_resetSegment(){this._segment=""}_openBracket(e=0){this._bracketState.push(e)}_closeBracket(){this._bracketState.pop()}_openSquareBracket(){this._squareBracketState+=1}_closeSquareBracket(){this._squareBracketState-=1}_getCurrentBracket(){const e=this._bracketState;return e[e.length-1]}_changeCurrentBracket(e){const t=this._bracketState;t[t.length-1]=e}_openBraces(){this._bracesState+=1}_closeBraces(){this._bracesState-=1}_openSingleQuotation(){this._singleQuotationState+=1}_closeSingleQuotation(){this._singleQuotationState-=1}_openDoubleQuotation(){this._doubleQuotationState+=1}_closeDoubleQuotation(){this._doubleQuotationState-=1}_openLambda(){this._lambdaState=!0}_closeLambda(){this._lambdaState=!1}_openColon(e){this._upLevel=e,this._colonState=!0}_closeColon(){this._upLevel=0,this._colonState=!1}_isTableBracket(){return this._tableBracketState}_openTableBracket(){this._tableBracketState=!0}_closeTableBracket(){this._tableBracketState=!1}_formalErrorOccurred(){this._formulaErrorCount+=1}_hasFormalError(){return this._formulaErrorCount>0}_getLastChildCurrentLexerNode(){const e=this._currentLexerNode.getChildren();if(e&&e.length>0){const t=e[e.length-1];if(t instanceof ie)return t}return!1}_getLastChildCurrent(){const e=this._currentLexerNode.getChildren();return e&&e.length>0?e[e.length-1]:!1}_setParentCurrentLexerNode(){const e=this._currentLexerNode.getParent();return e?(this._currentLexerNode=e,!0):!1}_setAncestorCurrentLexerNode(){var r,n,s,a,o,c;const e=(r=this._currentLexerNode)==null?void 0:r.getParent();let t=!1;if(e&&e.getToken()===er){if((n=e==null?void 0:e.getParent())!=null&&n.getParent()){const l=(a=(s=this._currentLexerNode.getParent())==null?void 0:s.getParent())==null?void 0:a.getParent();l&&(this._currentLexerNode=l),t=!0}}else if(e!=null&&e.getParent()){const l=(o=this._currentLexerNode.getParent())==null?void 0:o.getParent();l&&(this._currentLexerNode=l),t=!0}for(let l=0;l<this._upLevel;l++){const f=(c=this._currentLexerNode)==null?void 0:c.getParent();f&&(this._currentLexerNode=f),this._currentLexerNode?t=!0:t=!1}return t}_segmentCount(){return this._segment.trim().length}_pushSegment(e){this._segment+=e}_pushNodeToChildren(e,t=!1){let r=e;if(r!==""){const n=this._currentLexerNode.getChildren();if(!(r instanceof ie)&&this.isColonOpen()){const s=new ie;s.setToken(r),s.setParent(this._currentLexerNode),r=s}t?n.unshift(r):n.push(r)}this.isColonOpen()&&(this._setAncestorCurrentLexerNode(),this._closeColon())}_setCurrentLexerNode(e,t=!1){this._pushNodeToChildren(e,t),e.setParent(this._currentLexerNode),this._currentLexerNode=e}_newAndPushCurrentLexerNode(e,t,r=!1){const n=new ie;n.setToken(e),n.setIndex(t-e.length,t-1),this._setCurrentLexerNode(n,r)}_getTopNode(e){let t=e;for(;t!=null&&t.getParent();)t=t.getParent();return t}_removeLastChild(){this._currentLexerNode.getChildren().splice(-1)}_formulaErrorLastTokenCheck(e,t){const r=this._findPreviousToken(e,t)||"",n=e.length-1===t;if(!n&&this._isOperatorToken(r)||n&&Ju(r))return!0;if(rs.has(r)){const s=this._findSecondLastNonSpaceToken(e,t);if(s==null||tc(s))return!0}return!1}_findPreviousToken(e,t){let r=t;for(;r>=0;){const n=e[r];if(n!==" ")return n;r--}}_findSecondLastNonSpaceToken(e,t){let r=t,n=0;for(;r>=0;){const s=e[r];if(s!==" "&&(n++,n===2))return s;r--}return null}_findNextToken(e,t){let r=t;for(;r>=0;){const n=e[r];if(n!==" ")return n;r++}}_unexpectedEndingTokenExcludeOperator(e){return e===X.OPEN_BRACKET||e===X.COMMA||e===Z.EQUALS||e===""}_unexpectedEndingToken(e){return!!(this._isOperatorToken(e)||this._unexpectedEndingTokenExcludeOperator(e))}_isOperatorToken(e){return!!Vt.has(e)}_getSequenceArray(e){const t=[];return this._nodeMaker(e,t),t}_resetTemp(){this._currentLexerNode=new ie,this._upLevel=0,this._segment="",this._bracketState=[],this._bracesState=0,this._singleQuotationState=0,this._doubleQuotationState=0,this._lambdaState=!1,this._colonState=!1,this._formulaErrorCount=0}_checkErrorState(){return!!(this._bracketState.length>0||this._bracesState>0||this._singleQuotationState>0||this._doubleQuotationState>0||this._hasFormalError())}_checkSimilarErrorToken(e,t,r){let n=t;if(e!==qe.POUND)return!0;let s=r[++n];for(;s===" ";)s=r[++n];return!!hr(s)}_checkIfErrorObject(e,t){return!!this._findErrorObject(e,t)}_findErrorObject(e,t){for(let r=0;r<va.length;r++){const n=va[r],s=t.slice(e,e+n).join("").toUpperCase();if(pt.has(s))return s}}_nodeMaker(e,t,r){let n=e.replace(/\r\n$|\r|\n/g," ");n.substring(0,1)===Z.EQUALS&&(n=n.substring(1));const s=n.split(""),a=s.length;this._resetTemp(),this._formulaErrorLastTokenCheck(s,a-1)&&this._formalErrorOccurred();let o=0;for(;o<a;){const c=s[o];if(r===o)return[this._currentLexerNode,c];if(c===qe.POUND&&this.isSingleQuotationClose()&&this.isDoubleQuotationClose()&&this.isBracesClose()&&this.isSquareBracketClose()&&this._checkIfErrorObject(o,s)){const l=this._findErrorObject(o,s);if(l==null)return h.VALUE;this._pushNodeToChildren(l);for(let f=0;f<l.length;f++){const m=s[o];this._pushSegment(m),this._addSequenceArray(t,m,o),o++}this._resetSegment();continue}else if(c===X.OPEN_BRACKET&&this.isSingleQuotationClose()&&this.isDoubleQuotationClose())if(this._segmentCount()>0||this.isLambdaOpen()){this.isLambdaClose()&&(this._newAndPushCurrentLexerNode(this._segment,o),this._resetSegment()),this._openBracket(1),this._closeLambda();const l=s[o+1];if(l&&l===X.CLOSE_BRACKET){if(!this._setParentCurrentLexerNode()&&o!==a-1)return h.VALUE;this._addSequenceArray(t,c,o),o++,this._addSequenceArray(t,l,o),o++,this._closeBracket();continue}else l&&this._newAndPushCurrentLexerNode(at,o)}else this._pushNodeToChildren(c),this._openBracket(0),this._resetSegment();else if(c===X.CLOSE_BRACKET&&this.isSingleQuotationClose()&&this.isDoubleQuotationClose()){this._formulaErrorLastTokenCheck(s,o-1)&&this._formalErrorOccurred(),this._pushNodeToChildren(this._segment),this._resetSegment();const l=this._getCurrentBracket();if(l===0)this._pushNodeToChildren(c);else if(l===1){const f=s[o+1];if(f&&f===X.OPEN_BRACKET){if(!this._setParentCurrentLexerNode()&&o!==a-1)return h.VALUE;this._newAndPushCurrentLexerNode(er,o,!0),this._openLambda()}else if(!this._setAncestorCurrentLexerNode()&&o!==a-1)return h.VALUE}else return h.VALUE;this._closeBracket()}else if(c===X.OPEN_BRACES&&this.isSingleQuotationClose()&&this.isDoubleQuotationClose()){if(this._pushSegment(c),this._openBraces(),!this._formulaSpellCheck())return h.VALUE}else if(c===X.CLOSE_BRACES&&this.isSingleQuotationClose()&&this.isDoubleQuotationClose())this._pushSegment(c),this._pushNodeToChildren(this._segment),this._resetSegment(),this._closeBraces();else if(c===X.OPEN_SQUARE_BRACKET&&this.isSingleQuotationClose()&&this.isDoubleQuotationClose())this._segment.length>0&&this._openTableBracket(),this._pushSegment(c),this._openSquareBracket();else if(c===X.CLOSE_SQUARE_BRACKET&&this.isSingleQuotationClose()&&this.isDoubleQuotationClose())this._closeSquareBracket(),this.isSquareBracketClose()?(this._pushSegment(c),this._isTableBracket()&&(this._pushNodeToChildren(this._segment),this._resetSegment()),this._closeTableBracket()):this._pushSegment(c);else if(c===X.DOUBLE_QUOTATION&&this.isSingleQuotationClose()&&this.isSquareBracketClose()){if(this.isDoubleQuotationClose())this._openDoubleQuotation();else{const l=s[o+1];l&&l===X.DOUBLE_QUOTATION?o++:this._closeDoubleQuotation()}this._pushSegment(c)}else if(c===X.SINGLE_QUOTATION&&this.isDoubleQuotationClose()){if(this.isSingleQuotationClose())this._openSingleQuotation(),this._segmentCount()===0&&this._resetSegment();else{const l=s[o+1];if(l&&l===X.SINGLE_QUOTATION){this._pushSegment(c),this._addSequenceArray(t,c,o),o++,this._pushSegment(l),this._addSequenceArray(t,l,o),o++;continue}else this._closeSingleQuotation()}this._pushSegment(c)}else if(c===X.COMMA&&this.isSingleQuotationClose()&&this.isDoubleQuotationClose()&&this.isBracesClose()&&this.isSquareBracketClose()){this._formulaErrorLastTokenCheck(s,o-1)&&this._formalErrorOccurred();const l=this._getCurrentBracket();if(l===1||l==null){if(this._pushNodeToChildren(this._segment),this._resetSegment(),!this._setParentCurrentLexerNode()&&o!==a-1&&l!=null)return h.VALUE;this._newAndPushCurrentLexerNode(at,o)}else{const f=new ie;f.setToken(Oc);const m=new ie;m.setToken(at),m.changeToParent(f);const d=this._currentLexerNode.getParent();if(d&&d.getToken()===X.COLON){const _=d.getParent();if(!_)return h.VALUE;d.changeToParent(m),_.setChildren([]),f.changeToParent(_)}else return h.VALUE;this._changeCurrentBracket(1),this._pushNodeToChildren(this._segment),this._resetSegment(),this._currentLexerNode=f,this._newAndPushCurrentLexerNode(at,o)}}else if(c===X.COLON&&this.isSingleQuotationClose()&&this.isDoubleQuotationClose()&&this.isBracesClose()&&this.isSquareBracketClose()){const l=new ie;l.setToken(c);const f=new ie;f.setToken(at),f.setParent(l);const m=new ie;m.setToken(at),m.setParent(l),l.getChildren().push(f,m);let d=l,_=0;if(this._segmentCount()>0){let C,E,b=0;const y=this._segment.trim(),N=y[0],V=y[1];if(N===Ve.MINUS&&(C=new ie,C.setToken(Ve.MINUS),b++),(N===Ve.AT||V===Ve.AT)&&(E=new ie,E.setToken(Ve.AT),C&&(C.addChildren(E),E.setParent(C)),b++),b>0&&(this._segment=y.slice(b)),_=b,E)if(E.addChildren(l),l.setParent(E),E.getParent()){const D=E.getParent();D&&(d=D)}else d=E;else C&&(d=C,C.addChildren(l),l.setParent(C));const S=new ie;S.setToken(this._segment),S.setParent(f),f.getChildren().push(S),this._resetSegment()}else{const C=this._getLastChildCurrentLexerNode();C&&C.changeToParent(f)}this._setCurrentLexerNode(d),this._currentLexerNode=m,this._openColon(_)}else if(rs.has(c)&&this._checkSimilarErrorToken(c,o,s)&&this.isSingleQuotationClose()&&this.isDoubleQuotationClose()&&this.isSquareBracketClose()&&this.isBracesClose()){this._pushNodeToChildren(this._segment);const l=new ie;l.setToken(c);const f=this._getLastChildCurrent();f instanceof ie?f.changeToParent(l):f!==!1&&(l.getChildren().push(f),this._removeLastChild()),this._pushNodeToChildren(l),l.setParent(this._currentLexerNode),this._resetSegment()}else if(Vt.has(c)&&this.isSingleQuotationClose()&&this.isDoubleQuotationClose()&&this.isSquareBracketClose()&&this.isBracesClose()){let l=this._segment.trim();if(c===Z.MINUS&&l===""){const f=this._findPreviousToken(s,o-1)||"",m=this._findNextToken(s,o+1)||"";if(this._unexpectedEndingTokenExcludeOperator(f)&&this._isOperatorToken(m)){this._pushNodeToChildren("0"),this._pushNodeToChildren(Z.MINUS),this._addSequenceArray(t,c,o),this._resetSegment(),o++;continue}else if(this._unexpectedEndingToken(f))if(m===Z.PLUS){this._pushSegment(Z.MINUS),this._addSequenceArray(t,c,o),this._addSequenceArray(t,Z.PLUS,o+1),o+=2;continue}else{this._pushSegment(Z.MINUS),this._addSequenceArray(t,c,o),o++;continue}}else if(this._segment.length>0&&this._isScientificNotation(s,o,c)){this._pushSegment(c),this._addSequenceArray(t,c,o),o++;continue}else this._segment.length>0&&l===""?l=this._segment:(this._pushNodeToChildren(this._segment),l="");if(c===Z.LESS_THAN||c===Z.GREATER_THAN){const f=s[o+1];if(f&&Vt.has(c+f)){this._pushNodeToChildren(l+c+f),this._resetSegment(),this._addSequenceArray(t,c,o),o++,this._addSequenceArray(t,f,o),o++;continue}else this._pushNodeToChildren(l+c)}else this._pushNodeToChildren(l+c);this._resetSegment()}else(this._segment!==""||c!==" ")&&this._pushSegment(c);this._addSequenceArray(t,c,o),o++}if(this._pushNodeToChildren(this._segment),this._checkErrorState())return h.VALUE}_isScientificNotation(e,t,r){const n=e[t-2];if(n&&Number.isNaN(Number(n))||!(r===Z.MINUS||r===Z.PLUS))return!1;const s=e[t+1];if(s&&Number.isNaN(Number(s)))return!1;const a=e[t-1];return a&&a.toUpperCase()==="E"}_addSequenceArray(e,t,r){e==null||e.push({segment:this._segment,currentString:t,cur:r,currentLexerNode:this._currentLexerNode})}}function Dc(i,u,e,t,r,n,s){const a=(s==null?void 0:s.f)||"",o=(s==null?void 0:s.si)||"",c=M.isFormulaString(a),l=M.isFormulaId(o),f=i.getValue(r,n),m=(f==null?void 0:f.f)||"",d=(f==null?void 0:f.si)||"";function _(){var C;if(M.isFormulaString(m)&&M.isFormulaId(d)){const E=(C=e==null?void 0:e[d])==null?void 0:C.f;E?t.set(d,E):t.set(d,m)}}c&&l?(d!==o&&_(),i.setValue(r,n,{f:a,si:o}),e[o]={f:a,r,c:n},u.setValue(r,n,{f:a,si:o})):c&&!l?(m!==a&&_(),i.setValue(r,n,{f:a}),u.setValue(r,n,{f:a})):!c&&l?(d!==o&&_(),i.setValue(r,n,{f:"",si:o})):!c&&!l&&i.getValue(r,n)&&(_(),i.realDeleteValue(r,n),u.setValue(r,n,null))}function wc(i,u,e,t){const r=i==null?void 0:i.getValue(e,t);if(r==null)return!0;const n=[];i.forValue((l,f,m)=>{l===e&&f===t||M.Rectangle.intersects(m,r)&&n.push(m)});const{startRow:s,startColumn:a,endRow:o,endColumn:c}=r;for(let l=s;l<=o;l++)for(let f=a;f<=c;f++){let m=!1;const d=M.cellToRange(l,f);n.some(_=>M.Rectangle.contains(_,d)?(m=!0,!0):!1),m||u.realDeleteValue(l,f)}}var Lc=Object.getOwnPropertyDescriptor,Pc=(i,u,e,t)=>{for(var r=t>1?void 0:t?Lc(u,e):u,n=i.length-1,s;n>=0;n--)(s=i[n])&&(r=s(r)||r);return r},fi=(i,u)=>(e,t)=>u(e,t,i);w.FormulaDataModel=class extends M.Disposable{constructor(e,t){super();A(this,"_arrayFormulaRange",{});A(this,"_arrayFormulaCellData",{});this._univerInstanceService=e,this._lexerTreeBuilder=t}dispose(){super.dispose(),this._arrayFormulaRange={},this._arrayFormulaCellData={}}clearPreviousArrayFormulaCellData(e){Object.keys(e).forEach(t=>{const r=e[t];if(r==null)return!0;Object.keys(r).forEach(n=>{var l,f,m,d;const s=r[n],a=(f=(l=this._arrayFormulaRange)==null?void 0:l[t])==null?void 0:f[n];if(a==null)return!0;const o=new M.ObjectMatrix(a);let c=new M.ObjectMatrix;((m=this._arrayFormulaCellData[t])==null?void 0:m[n])!=null&&(c=new M.ObjectMatrix((d=this._arrayFormulaCellData[t])==null?void 0:d[n])),s.forValue((_,C)=>{const E=o.getValue(_,C);if(E==null)return!0;const{startRow:b,startColumn:y,endRow:N,endColumn:V}=E;for(let S=b;S<=N;S++)for(let D=y;D<=V;D++)c.setValue(S,D,null);o.realDeleteValue(_,C)}),this._arrayFormulaCellData[t]&&(this._arrayFormulaCellData[t][n]=c.getData())})})}mergeArrayFormulaCellData(e){Object.keys(e).forEach(t=>{const r=e[t];if(r==null)return!0;this._arrayFormulaRange[t]==null&&(this._arrayFormulaRange[t]={}),this._arrayFormulaCellData[t]==null&&(this._arrayFormulaCellData[t]={}),Object.keys(r).forEach(n=>{var c,l;const s=r[n],a=new M.ObjectMatrix((c=this._arrayFormulaRange[t])==null?void 0:c[n]),o=new M.ObjectMatrix((l=this._arrayFormulaCellData[t])==null?void 0:l[n]);s.forValue((f,m)=>{const d=a==null?void 0:a.getValue(f,m);if(d==null)return!0;const{startRow:_,startColumn:C,endRow:E,endColumn:b}=d;for(let y=_;y<=E;y++)for(let N=C;N<=b;N++)o.setValue(y,N,null)}),s.forValue((f,m,d)=>{o.setValue(f,m,d)}),this._arrayFormulaCellData[t]&&(this._arrayFormulaCellData[t][n]=o.getData())})})}getFormulaData(){const e={},t=this._univerInstanceService.getAllUnitsForType(M.UniverInstanceType.UNIVER_SHEET);return t.length===0||t.forEach(r=>{const n=r.getUnitId();e[n]={},r.getSheets().forEach(a=>{const o=a.getCellMatrix(),c=a.getSheetId();_s(e,n,c,o)})}),e}getSheetFormulaData(e,t){const r={},n=this._univerInstanceService.getUnit(e);if(n==null)return{};r[e]={};const s=n.getSheetBySheetId(t);if(s==null)return{};const a=s.getCellMatrix();return _s(r,e,t,a),r[e][t]}getArrayFormulaRange(){return this._arrayFormulaRange}setArrayFormulaRange(e){this._arrayFormulaRange=e}getArrayFormulaCellData(){return this._arrayFormulaCellData}setArrayFormulaCellData(e){this._arrayFormulaCellData=e}mergeArrayFormulaRange(e){Object.keys(e).forEach(t=>{const r=e[t];if(r==null)return!0;this._arrayFormulaRange[t]||(this._arrayFormulaRange[t]={}),Object.keys(r).forEach(n=>{var o;const s=new M.ObjectMatrix(r[n]),a=new M.ObjectMatrix((o=this._arrayFormulaRange[t])==null?void 0:o[n]);s.forValue((c,l,f)=>{a.setValue(c,l,f)}),this._arrayFormulaRange[t]&&(this._arrayFormulaRange[t][n]=a.getData())})})}deleteArrayFormulaRange(e,t,r,n){var o;const s=(o=this._arrayFormulaRange[e])==null?void 0:o[t];if(s==null)return;const a=new M.ObjectMatrix(s);a.getValue(r,n)&&(a.realDeleteValue(r,n),this._arrayFormulaRange[e]&&(this._arrayFormulaRange[e][t]=a.getData()))}getCalculateData(){const e=this._univerInstanceService.getAllUnitsForType(M.UniverInstanceType.UNIVER_SHEET),t={},r={},n={};for(const s of e){const a=s.getUnitId(),o=s.getSheets(),c={},l={};for(const f of o){const m=f.getSheetId(),d=f.getConfig();c[m]={cellData:new M.ObjectMatrix(d.cellData),rowCount:d.rowCount,columnCount:d.columnCount,rowData:d.rowData,columnData:d.columnData,defaultRowHeight:d.defaultRowHeight,defaultColumnWidth:d.defaultColumnWidth},l[f.getName()]=f.getSheetId()}t[a]=c,r[a]=s.getStyles(),n[a]=l}return{allUnitData:t,unitStylesData:r,unitSheetNameMap:n}}getHiddenRowsFiltered(){const e=this._univerInstanceService.getAllUnitsForType(M.UniverInstanceType.UNIVER_SHEET),t={};for(const r of e){const n=r.getUnitId(),s=r.getSheets();t[n]={};for(const a of s){const o=a.getSheetId();t[n][o]={};const c=0,l=a.getRowCount()-1,f={};for(let m=c;m<=l;m++)a.getRowVisible(m)||(f[m]={hd:M.BooleanNumber.TRUE});t[n][o]=f}}return t}updateFormulaData(e,t,r){const n=new M.ObjectMatrix(r),s=this._getSheetFormulaIdMap(e,t),a=new Map,o=this.getFormulaData();o[e]==null&&(o[e]={});const c=o[e];c[t]==null&&(c[t]={});const l=new M.ObjectMatrix(c[t]||{}),f=new M.ObjectMatrix;return n.forValue((m,d,_)=>{Dc(l,f,s,a,m,d,_)}),l.forValue((m,d,_)=>{const C=(_==null?void 0:_.f)||"",E=(_==null?void 0:_.si)||"";if(M.isFormulaId(E)){const b=s==null?void 0:s[E],y=a.get(E);if(b&&!M.isFormulaString(C)){const N=b.f,V=d-b.c,S=m-b.r;l.setValue(m,d,{f:N,si:E,x:V,y:S}),f.setValue(m,d,{f:N,si:E,x:V,y:S})}else if(typeof y=="string"){const N=(_==null?void 0:_.x)||0,V=(_==null?void 0:_.y)||0,S=this._lexerTreeBuilder.moveFormulaRefOffset(y,N,V);a.set(E,{r:m,c:d,f:S}),l.setValue(m,d,{f:S,si:E}),f.setValue(m,d,{f:S,si:E})}else if(typeof y=="object"){const N=d-y.c,V=m-y.r;l.setValue(m,d,{f:y.f,si:E,x:N,y:V}),f.setValue(m,d,{f:y.f,si:E,x:N,y:V})}}}),f.getMatrix()}updateArrayFormulaRange(e,t,r){var o;const n=(o=this._arrayFormulaRange[e])==null?void 0:o[t];if(!n)return;const s=new M.ObjectMatrix(n);new M.ObjectMatrix(r).forValue((c,l,f)=>{s.realDeleteValue(c,l)})}updateArrayFormulaCellData(e,t,r){var l,f;const n=(l=this._arrayFormulaRange[e])==null?void 0:l[t];if(!n)return;const s=new M.ObjectMatrix(n),a=(f=this._arrayFormulaCellData[e])==null?void 0:f[t];if(!a)return;const o=new M.ObjectMatrix(a);new M.ObjectMatrix(r).forValue((m,d,_)=>{wc(s,o,m,d)})}getFormulaStringByCell(e,t,r,n){const s=this._univerInstanceService.getUnit(n);if(s==null)return null;const a=s.getSheetBySheetId(r);if(a==null)return null;const o=a.getCellMatrix(),c=o.getValue(e,t);if(c==null)return null;const{f:l,si:f}=c;if(M.isFormulaString(l))return l;if(M.isFormulaId(f)){let m=null;return o.forValue((d,_,C)=>{if(C==null)return!0;const{f:E,si:b}=C;if(M.isFormulaString(E)&&f===b)return m=this._lexerTreeBuilder.moveFormulaRefOffset(E,t-_,e-d),!1}),m}return null}getFormulaDirtyRanges(){const e=this.getFormulaData(),t=[];for(const r in e){const n=e[r];if(!n)continue;const s=this._univerInstanceService.getUnit(r);if(s)for(const a in n){const o=n[a];if(!o)continue;const c=s.getSheetBySheetId(a);if(!c)continue;const l={};for(const f of Object.keys(o)){const m=Number(f);for(const d in o[m]){const _=Number(d),C=c.getCellRaw(m,_),E=M.isFormulaString(C==null?void 0:C.f)||M.isFormulaId(C==null?void 0:C.si),b=(C==null?void 0:C.v)===void 0;if(!(E&&b))continue;l[_]||(l[_]=[]);const y=l[_].slice(-1)[0];y&&y.endRow===m-1?y.endRow=m:l[_].push({startRow:m,endRow:m})}}for(const f in l){const m=l[f];for(let d=0;d<m.length;d++){const _=m[d];t.push({unitId:r,sheetId:a,range:{rangeType:M.RANGE_TYPE.NORMAL,startRow:_.startRow,endRow:_.endRow,startColumn:Number(f),endColumn:Number(f)}})}}}}return t}_getSheetFormulaIdMap(e,t){const r={},n=this._univerInstanceService.getUnit(e);if(n==null)return r;const s=n.getSheetBySheetId(t);return s==null||s.getCellMatrix().forValue((o,c,l)=>{if(l==null)return!0;const{f,si:m}=l;M.isFormulaString(f)&&M.isFormulaId(m)&&(r[m]={f,r:o,c})}),r}},w.FormulaDataModel=Pc([fi(0,M.IUniverInstanceService),fi(1,M.Inject(zr))],w.FormulaDataModel);function _s(i,u,e,t){i[u]||(i[u]={}),i[u][e]||(i[u][e]={});const r=new Map,n=new M.ObjectMatrix(i[u][e]);t.forValue((a,o,c)=>{const l=(c==null?void 0:c.f)||"",f=(c==null?void 0:c.si)||"",m=M.isFormulaString(l),d=M.isFormulaId(f);m&&d?(n.setValue(a,o,{f:l,si:f}),r.set(f,{f:l,r:a,c:o})):m&&!d?n.setValue(a,o,{f:l}):!m&&d&&n.setValue(a,o,{f:"",si:f})}),n.forValue((a,o,c)=>{const l=(c==null?void 0:c.f)||"",f=(c==null?void 0:c.si)||"";if(M.isFormulaId(f)&&!M.isFormulaString(l)){const m=r.get(f);if(m){const d=m.f,_=o-m.c,C=a-m.r;n.setValue(a,o,{f:d,si:f,x:_,y:C})}else n.realDeleteValue(a,o)}});const s=n.getMatrix();return{[u]:{[e]:s}}}const jc=class xa{constructor(u,e){this.low=u,this.high=e}clone(){return new xa(this.low,this.high)}get max(){return this.clone()}less_than(u){return this.low<u.low||this.low===u.low&&this.high<u.high}equal_to(u){return this.low===u.low&&this.high===u.high}intersect(u){return!this.not_intersect(u)}not_intersect(u){return this.high<u.low||u.high<this.low}merge(u){return new xa(this.low===void 0?u.low:this.low<u.low?this.low:u.low,this.high===void 0?u.high:this.high>u.high?this.high:u.high)}output(){return[this.low,this.high]}static comparable_max(u,e){return u.merge(e)}static comparable_less_than(u,e){return u<e}},Pe=0,Ae=1;class Ft{constructor(u=void 0,e=void 0,t=null,r=null,n=null,s=Ae){if(this.left=t,this.right=r,this.parent=n,this.color=s,this.item={key:u,value:e},u&&u instanceof Array&&u.length===2&&!Number.isNaN(u[0])&&!Number.isNaN(u[1])){let[a,o]=u;a>o&&([a,o]=[o,a]),this.item.key=new jc(a,o)}this.max=this.item.key?this.item.key.max:void 0}isNil(){return this.item.key===void 0&&this.item.value===void 0&&this.left===null&&this.right===null&&this.color===Ae}_value_less_than(u){return this.item.value&&u.item.value&&this.item.value.less_than?this.item.value.less_than(u.item.value):this.item.value<u.item.value}less_than(u){return this.item.value===this.item.key&&u.item.value===u.item.key?this.item.key.less_than(u.item.key):this.item.key.less_than(u.item.key)||this.item.key.equal_to(u.item.key)&&this._value_less_than(u)}_value_equal(u){return this.item.value&&u.item.value&&this.item.value.equal_to?this.item.value.equal_to(u.item.value):this.item.value===u.item.value}equal_to(u){return this.item.value===this.item.key&&u.item.value===u.item.key?this.item.key.equal_to(u.item.key):this.item.key.equal_to(u.item.key)&&this._value_equal(u)}intersect(u){return this.item.key.intersect(u.item.key)}copy_data(u){this.item.key=u.item.key,this.item.value=u.item.value}update_max(){if(this.max=this.item.key?this.item.key.max:void 0,this.right&&this.right.max){const u=this.item.key.constructor.comparable_max;this.max=u(this.max,this.right.max)}if(this.left&&this.left.max){const u=this.item.key.constructor.comparable_max;this.max=u(this.max,this.left.max)}}not_intersect_left_subtree(u){const e=this.item.key.constructor.comparable_less_than;let t=this.left.max.high!==void 0?this.left.max.high:this.left.max;return e(t,u.item.key.low)}not_intersect_right_subtree(u){const e=this.item.key.constructor.comparable_less_than;let t=this.right.max.low!==void 0?this.right.max.low:this.right.item.key.low;return e(u.item.key.high,t)}}class As{constructor(){this.root=null,this.nil_node=new Ft}get size(){let u=0;return this.tree_walk(this.root,()=>u++),u}get keys(){let u=[];return this.tree_walk(this.root,e=>u.push(e.item.key.output?e.item.key.output():e.item.key)),u}get values(){let u=[];return this.tree_walk(this.root,e=>u.push(e.item.value)),u}get items(){let u=[];return this.tree_walk(this.root,e=>u.push({key:e.item.key.output?e.item.key.output():e.item.key,value:e.item.value})),u}isEmpty(){return this.root==null||this.root===this.nil_node}clear(){this.root=null}insert(u,e=u){if(u===void 0)return;let t=new Ft(u,e,this.nil_node,this.nil_node,null,Pe);return this.tree_insert(t),this.recalc_max(t),t}exist(u,e=u){let t=new Ft(u,e);return!!this.tree_search(this.root,t)}remove(u,e=u){let t=new Ft(u,e),r=this.tree_search(this.root,t);return r&&this.tree_delete(r),r}search(u,e=(t,r)=>t===r?r.output():t){let t=new Ft(u),r=[];return this.tree_search_interval(this.root,t,r),r.map(n=>e(n.item.value,n.item.key))}intersect_any(u){let e=new Ft(u);return this.tree_find_any_interval(this.root,e)}forEach(u){this.tree_walk(this.root,e=>u(e.item.key,e.item.value))}map(u){const e=new As;return this.tree_walk(this.root,t=>e.insert(t.item.key,u(t.item.value,t.item.key))),e}*iterate(u,e=(t,r)=>t===r?r.output():t){let t;for(u?t=this.tree_search_nearest_forward(this.root,new Ft(u)):this.root&&(t=this.local_minimum(this.root));t;)yield e(t.item.value,t.item.key),t=this.tree_successor(t)}recalc_max(u){let e=u;for(;e.parent!=null;)e.parent.update_max(),e=e.parent}tree_insert(u){let e=this.root,t=null;if(this.root==null||this.root===this.nil_node)this.root=u;else{for(;e!==this.nil_node;)t=e,u.less_than(e)?e=e.left:e=e.right;u.parent=t,u.less_than(t)?t.left=u:t.right=u}this.insert_fixup(u)}insert_fixup(u){let e,t;for(e=u;e!==this.root&&e.parent.color===Pe;)e.parent===e.parent.parent.left?(t=e.parent.parent.right,t.color===Pe?(e.parent.color=Ae,t.color=Ae,e.parent.parent.color=Pe,e=e.parent.parent):(e===e.parent.right&&(e=e.parent,this.rotate_left(e)),e.parent.color=Ae,e.parent.parent.color=Pe,this.rotate_right(e.parent.parent))):(t=e.parent.parent.left,t.color===Pe?(e.parent.color=Ae,t.color=Ae,e.parent.parent.color=Pe,e=e.parent.parent):(e===e.parent.left&&(e=e.parent,this.rotate_right(e)),e.parent.color=Ae,e.parent.parent.color=Pe,this.rotate_left(e.parent.parent)));this.root.color=Ae}tree_delete(u){let e,t;u.left===this.nil_node||u.right===this.nil_node?e=u:e=this.tree_successor(u),e.left!==this.nil_node?t=e.left:t=e.right,t.parent=e.parent,e===this.root?this.root=t:(e===e.parent.left?e.parent.left=t:e.parent.right=t,e.parent.update_max()),this.recalc_max(t),e!==u&&(u.copy_data(e),u.update_max(),this.recalc_max(u)),e.color===Ae&&this.delete_fixup(t)}delete_fixup(u){let e=u,t;for(;e!==this.root&&e.parent!=null&&e.color===Ae;)e===e.parent.left?(t=e.parent.right,t.color===Pe&&(t.color=Ae,e.parent.color=Pe,this.rotate_left(e.parent),t=e.parent.right),t.left.color===Ae&&t.right.color===Ae?(t.color=Pe,e=e.parent):(t.right.color===Ae&&(t.color=Pe,t.left.color=Ae,this.rotate_right(t),t=e.parent.right),t.color=e.parent.color,e.parent.color=Ae,t.right.color=Ae,this.rotate_left(e.parent),e=this.root)):(t=e.parent.left,t.color===Pe&&(t.color=Ae,e.parent.color=Pe,this.rotate_right(e.parent),t=e.parent.left),t.left.color===Ae&&t.right.color===Ae?(t.color=Pe,e=e.parent):(t.left.color===Ae&&(t.color=Pe,t.right.color=Ae,this.rotate_left(t),t=e.parent.left),t.color=e.parent.color,e.parent.color=Ae,t.left.color=Ae,this.rotate_right(e.parent),e=this.root));e.color=Ae}tree_search(u,e){if(!(u==null||u===this.nil_node))return e.equal_to(u)?u:e.less_than(u)?this.tree_search(u.left,e):this.tree_search(u.right,e)}tree_search_nearest_forward(u,e){let t,r=u;for(;r&&r!==this.nil_node;)r.less_than(e)?r.intersect(e)?(t=r,r=r.left):r=r.right:((!t||r.less_than(t))&&(t=r),r=r.left);return t||null}tree_search_interval(u,e,t){u!=null&&u!==this.nil_node&&(u.left!==this.nil_node&&!u.not_intersect_left_subtree(e)&&this.tree_search_interval(u.left,e,t),u.intersect(e)&&t.push(u),u.right!==this.nil_node&&!u.not_intersect_right_subtree(e)&&this.tree_search_interval(u.right,e,t))}tree_find_any_interval(u,e){let t=!1;return u!=null&&u!==this.nil_node&&(u.left!==this.nil_node&&!u.not_intersect_left_subtree(e)&&(t=this.tree_find_any_interval(u.left,e)),t||(t=u.intersect(e)),!t&&u.right!==this.nil_node&&!u.not_intersect_right_subtree(e)&&(t=this.tree_find_any_interval(u.right,e))),t}local_minimum(u){let e=u;for(;e.left!=null&&e.left!==this.nil_node;)e=e.left;return e}local_maximum(u){let e=u;for(;e.right!=null&&e.right!==this.nil_node;)e=e.right;return e}tree_successor(u){let e,t,r;if(u.right!==this.nil_node)e=this.local_minimum(u.right);else{for(t=u,r=u.parent;r!=null&&r.right===t;)t=r,r=r.parent;e=r}return e}rotate_left(u){let e=u.right;u.right=e.left,e.left!==this.nil_node&&(e.left.parent=u),e.parent=u.parent,u===this.root?this.root=e:u===u.parent.left?u.parent.left=e:u.parent.right=e,e.left=u,u.parent=e,u!=null&&u!==this.nil_node&&u.update_max(),e=u.parent,e!=null&&e!==this.nil_node&&e.update_max()}rotate_right(u){let e=u.left;u.left=e.right,e.right!==this.nil_node&&(e.right.parent=u),e.parent=u.parent,u===this.root?this.root=e:u===u.parent.left?u.parent.left=e:u.parent.right=e,e.right=u,u.parent=e,u!==null&&u!==this.nil_node&&u.update_max(),e=u.parent,e!=null&&e!==this.nil_node&&e.update_max()}tree_walk(u,e){u!=null&&u!==this.nil_node&&(this.tree_walk(u.left,e),e(u),this.tree_walk(u.right,e))}testRedBlackProperty(){let u=!0;return this.tree_walk(this.root,function(e){e.color===Pe&&(e.left.color===Ae&&e.right.color===Ae||(u=!1))}),u}testBlackHeightProperty(u){let e=0,t=0,r=0;if(u.color===Ae&&e++,u.left!==this.nil_node?t=this.testBlackHeightProperty(u.left):t=1,u.right!==this.nil_node?r=this.testBlackHeightProperty(u.right):r=1,t!==r)throw new Error("Red-black height property violated");return e+=t,e}}class xc{constructor(){A(this,"_cache",new Map);A(this,"_continueBuildingCache",new Map)}set(u,e,t,r,n,s=!1){if(!this.shouldContinueBuildingCache(u,e,t,n)&&!s)return;let a=this._cache.get(u);a==null&&(a=new Map,this._cache.set(u,a));let o=a.get(e);o==null&&(o=new Map,a.set(e,o));let c=o.get(t);if(c==null&&(c=new Map,o.set(t,c)),s){for(const[f,m]of c)if(m.has(n)){m.delete(n);break}}let l=c.get(r);l==null&&(l=new Set,c.set(r,l)),l.add(n)}getCellValuePositions(u,e,t){var r,n;return(n=(r=this._cache.get(u))==null?void 0:r.get(e))==null?void 0:n.get(t)}getCellPositions(u,e,t,r,n){var a,o,c;const s=(c=(o=(a=this._cache.get(u))==null?void 0:a.get(e))==null?void 0:o.get(t))==null?void 0:c.get(r);return s&&[...s].filter(l=>n.some(([f,m])=>l>=f&&l<=m))}setContinueBuildingCache(u,e,t,r,n){if(t===-1||r===-1||n===-1)return;let s=this._continueBuildingCache.get(u);s==null&&(s=new Map,this._continueBuildingCache.set(u,s));let a=s.get(e);a==null&&(a=new Map,s.set(e,a));let o=a.get(t);if(o==null){o=new As,o.insert([r,n]),a.set(t,o);return}this._handleNewInterval(o,r,n)}shouldContinueBuildingCache(u,e,t,r){var a,o;if(t===-1||r===-1)return!1;const n=(o=(a=this._continueBuildingCache.get(u))==null?void 0:a.get(e))==null?void 0:o.get(t);return n?n.search([r,r]).length===0:!0}canUseCache(u,e,t,r,n){var f,m;const s=(m=(f=this._continueBuildingCache.get(u))==null?void 0:f.get(e))==null?void 0:m.get(t);if(t===-1||r===-1||n===-1||!s)return{rowsInCache:[],rowsNotInCache:[]};const a=s.search([r,n]);if(a.length===0)return{rowsInCache:[],rowsNotInCache:[]};a.sort((d,_)=>d[0]-_[0]);const o=[],c=[];let l=r;for(let d=0;d<a.length;d++){const[_,C]=a[d];if(l>=_){if(n<=C){o.push([l,n]);break}o.push([l,C]),l=C+1,d===a.length-1&&l<=n&&c.push([l,n])}else{if(n>C){o.push([_,C]),c.push([l,_-1]),l=C+1,d===a.length-1&&l<=n&&c.push([l,n]);continue}o.push([_,n]),c.push([l,_-1])}}return{rowsInCache:o,rowsNotInCache:c}}clear(){this._cache.clear(),this._continueBuildingCache.clear()}_handleNewInterval(u,e,t){let r=u.search([e,t]);if(r.length===0){const a=[e-1<0?0:e-1,t+1];if(r=u.search(a),r.length===0){u.insert([e,t]);return}}let n=e,s=t;for(const a of r)n=Math.min(n,a[0]),s=Math.max(s,a[1]),u.remove(a);u.insert([n,s])}}const Ye=new xc,Jr="engine-formula.config",mi=1,Uc="CYCLE_REFERENCE_COUNT",hi={};class gi extends M.Disposable{constructor(){super(...arguments);A(this,"_getRowFilteredCallback")}register(e){this._getRowFilteredCallback=e}getRowFiltered(e,t,r){var n;return this._getRowFilteredCallback&&(n=this._getRowFilteredCallback(e,t,r))!=null?n:!1}}const Es=M.createIdentifier("univer.formula.sheet-row-filtered.service");var vc=Object.getOwnPropertyDescriptor,Tc=(i,u,e,t)=>{for(var r=t>1?void 0:t?vc(u,e):u,n=i.length-1,s;n>=0;n--)(s=i[n])&&(r=s(r)||r);return r},en=(i,u)=>(e,t)=>u(e,t,i);w.FormulaCurrentConfigService=class extends M.Disposable{constructor(e,t,r,n){super();A(this,"_unitData",{});A(this,"_unitStylesData",{});A(this,"_arrayFormulaCellData",{});A(this,"_arrayFormulaRange",{});A(this,"_formulaData",{});A(this,"_sheetNameMap",{});A(this,"_forceCalculate",!1);A(this,"_clearDependencyTreeCache",{});A(this,"_dirtyRanges",[]);A(this,"_dirtyNameMap",{});A(this,"_dirtyDefinedNameMap",{});A(this,"_dirtyUnitFeatureMap",{});A(this,"_dirtyUnitOtherFormulaMap",{});A(this,"_excludedCell");A(this,"_sheetIdToNameMap",{});A(this,"_executeUnitId","");A(this,"_executeSubUnitId","");this._univerInstanceService=e,this._localeService=t,this._formulaDataModel=r,this._sheetRowFilteredService=n}dispose(){super.dispose(),this._unitData={},this._unitStylesData={},this._arrayFormulaCellData={},this._arrayFormulaRange={},this._formulaData={},this._sheetNameMap={},this._clearDependencyTreeCache={},this._dirtyRanges=[],this._dirtyNameMap={},this._dirtyDefinedNameMap={},this._dirtyUnitFeatureMap={},this._dirtyUnitOtherFormulaMap={},this._excludedCell={},this._sheetIdToNameMap={}}getExecuteUnitId(){return this._executeUnitId}getExecuteSubUnitId(){return this._executeSubUnitId}setExecuteUnitId(e){this._executeUnitId=e}setExecuteSubUnitId(e){this._executeSubUnitId=e}getExcludedRange(){return this._excludedCell}getUnitData(){return this._unitData}getUnitStylesData(){return this._unitStylesData}getFormulaData(){return this._formulaData}getArrayFormulaCellData(){return this._arrayFormulaCellData}getArrayFormulaRange(){return this._arrayFormulaRange}getSheetNameMap(){return this._sheetNameMap}isForceCalculate(){return this._forceCalculate}getDirtyRanges(){return this._dirtyRanges}getDirtyNameMap(){return this._dirtyNameMap}getDirtyDefinedNameMap(){return this._dirtyDefinedNameMap}getDirtyUnitFeatureMap(){return this._dirtyUnitFeatureMap}getDirtyUnitOtherFormulaMap(){return this._dirtyUnitOtherFormulaMap}getSheetName(e,t){return this._sheetIdToNameMap[e]==null?"":this._sheetIdToNameMap[e][t]||""}getClearDependencyTreeCache(){return this._clearDependencyTreeCache}getLocale(){return this._localeService.getCurrentLocale()}getSheetsInfo(){const e=this._univerInstanceService.getCurrentUnitForType(M.UniverInstanceType.UNIVER_SHEET),{id:t,sheetOrder:r}=e.getSnapshot();return{sheetOrder:r,sheetNameMap:this._sheetIdToNameMap[t]}}getSheetRowColumnCount(e,t){const r=this._univerInstanceService.getUnit(e),n=r==null?void 0:r.getSheetBySheetId(t),s=n==null?void 0:n.getSnapshot();if(!s)return{rowCount:0,columnCount:0};const{rowCount:a,columnCount:o}=s;return{rowCount:a,columnCount:o}}getFilteredOutRows(e,t,r,n){const s=[];for(let a=r;a<=n;a++)this._sheetRowFilteredService.getRowFiltered(e,t,a)&&s.push(a);return s}load(e){if(e.allUnitData&&e.unitSheetNameMap&&e.unitStylesData)this._unitData=e.allUnitData,this._unitStylesData=e.unitStylesData,this._sheetNameMap=e.unitSheetNameMap;else{const{allUnitData:t,unitSheetNameMap:r,unitStylesData:n}=this._loadSheetData();this._unitData=t,this._unitStylesData=n,this._sheetNameMap=r}e.rowData&&this._applyUnitRowData(e.rowData),this._formulaData=e.formulaData,this._arrayFormulaCellData=Ja(e.arrayFormulaCellData),this._arrayFormulaRange=e.arrayFormulaRange,this._forceCalculate=e.forceCalculate,this._clearDependencyTreeCache=e.clearDependencyTreeCache||{},this._dirtyRanges=e.dirtyRanges,this._dirtyNameMap=e.dirtyNameMap,this._dirtyDefinedNameMap=e.dirtyDefinedNameMap,this._dirtyUnitFeatureMap=e.dirtyUnitFeatureMap,this._dirtyUnitOtherFormulaMap=e.dirtyUnitOtherFormulaMap,this._excludedCell=e.excludedCell,this._mergeNameMap(this._sheetNameMap,this._dirtyNameMap)}getDirtyData(){return{forceCalculation:this._forceCalculate,dirtyRanges:this._dirtyRanges,dirtyNameMap:this._dirtyNameMap,dirtyDefinedNameMap:this._dirtyDefinedNameMap,dirtyUnitFeatureMap:this._dirtyUnitFeatureMap,dirtyUnitOtherFormulaMap:this._dirtyUnitOtherFormulaMap,clearDependencyTreeCache:this._clearDependencyTreeCache}}loadDirtyRangesAndExcludedCell(e,t){this._dirtyRanges=e,this._excludedCell=t,this._dirtyNameMap={}}registerUnitData(e){this._unitData=e}registerFormulaData(e){this._formulaData=e}registerSheetNameMap(e){this._sheetNameMap=e}_mergeNameMap(e,t){Object.keys(t).forEach(r=>{t[r]&&Object.keys(t[r]).forEach(n=>{e[r]==null&&(e[r]={}),e[r][t[r][n]]=n})}),this._sheetIdToNameMap={},Object.keys(e).forEach(r=>{Object.keys(e[r]).forEach(n=>{this._sheetIdToNameMap[r]==null&&(this._sheetIdToNameMap[r]={}),this._sheetIdToNameMap[r][e[r][n]]=n})})}_loadSheetData(){const e=this._univerInstanceService.getCurrentUnitForType(M.UniverInstanceType.UNIVER_SHEET),t=e==null?void 0:e.getActiveSheet();return this._executeUnitId=e==null?void 0:e.getUnitId(),this._executeSubUnitId=t==null?void 0:t.getSheetId(),this._formulaDataModel.getCalculateData()}_applyUnitRowData(e){for(const t in e)if(e[t]!=null)for(const r in e[t])e[t][r]!=null&&(this._unitData[t]==null&&(this._unitData[t]={}),this._unitData[t][r]==null&&(this._unitData[t][r]={cellData:new M.ObjectMatrix({}),rowCount:0,columnCount:0,rowData:{},columnData:{}}),this._unitData[t][r].rowData=e[t][r])}},w.FormulaCurrentConfigService=Tc([en(0,M.IUniverInstanceService),en(1,M.Inject(M.LocaleService)),en(2,M.Inject(w.FormulaDataModel)),en(3,M.Inject(Es))],w.FormulaCurrentConfigService);const At=M.createIdentifier("univer.formula.current-data.service");var Ic=Object.getOwnPropertyDescriptor,Fc=(i,u,e,t)=>{for(var r=t>1?void 0:t?Ic(u,e):u,n=i.length-1,s;n>=0;n--)(s=i[n])&&(r=s(r)||r);return r},bs=(i,u)=>(e,t)=>u(e,t,i);w.Lexer=class extends M.Disposable{constructor(u,e,t){super(),this._definedNamesService=u,this._lexerTreeBuilder=e,this._formulaCurrentConfigService=t}treeBuilder(u,e=!0){return this._lexerTreeBuilder.treeBuilder(u,e,{unitId:this._formulaCurrentConfigService.getExecuteUnitId(),getValueByName:this._definedNamesService.getValueByName.bind(this._definedNamesService),getDirtyDefinedNameMap:this._formulaCurrentConfigService.getDirtyDefinedNameMap.bind(this._formulaCurrentConfigService)})}},w.Lexer=Fc([bs(0,Tt),bs(1,M.Inject(zr)),bs(2,At)],w.Lexer);function tn(i){if(i==null)return!0;const{v:u,f:e,si:t,p:r}=i;return!(!(u==null||typeof u=="string"&&u.length===0)||e!=null&&e.length>0||t!=null&&t.length>0||r!=null)}function di(i,u,e,t,r){var s;const n=Object.keys(r);for(const a of n){const o=r[a],c=(s=o==null?void 0:o[t])==null?void 0:s[e];if(c==null)continue;const l=c.getValue(i,u);if(l!=null)return l}}const Bc=["$","£","¥","¤","֏","؋","৳","฿","៛","₡","₦","₩","₪","₫","€","₭","₮","₱","₲","₴","₸","₹","₺","₼","₽","₾","₿"],kc={currency:2,date:4,datetime:5,error:11,fraction:7,general:0,grouped:11,number:1,percent:6,scientific:8,text:9,time:5,unknown:11};function $c(i,u,e){var o,c;if(i==null||u==null)return u;const t=(e==null?void 0:e.getStyleByCell(i))||i.s,r=(e==null?void 0:e.getStyleByCell(u))||u.s;if(t==null||r==null||typeof t!="object"||typeof r!="object")return u;const n=(o=t==null?void 0:t.n)==null?void 0:o.pattern,s=(c=r==null?void 0:r.n)==null?void 0:c.pattern;if(n==null||s==null)return u;const a=n||s;return r.n.pattern=a,u}const ys=new _t(1e5);function Ci(i){const u=ys.get(i);if(u!==void 0)return u;const e=Hc(i);return ys.set(i,e),e}function Yc(){ys.clear()}function Hc(i){if(Gc(i))return 3;const u=M.numfmt.getFormatInfo(i).type||"unknown";return kc[u]}function Gc(i){return!!Bc.find(u=>i.includes(u))&&i.startsWith("_(")}function rn(i,u,e){if(i==="")return u;if(u==="")return i;const t=Ci(i),r=Ci(u);return e===Z.PLUS||e===Z.MINUS?t===4&&r===4||t===11&&r===11?"":u:e===Z.MULTIPLY||e===Z.DIVIDED?t===6&&r===6||t===7&&r===7||t===8&&r===8||t===9&&r===9?u:"":i||u}const Wc=new Map([[M.LocaleType.EN_US,"$"],[M.LocaleType.RU_RU,"₽"],[M.LocaleType.VI_VN,"₫"],[M.LocaleType.ZH_CN,"¥"],[M.LocaleType.ZH_TW,"NT$"],[M.LocaleType.FR_FR,"€"],[M.LocaleType.FA_IR,"﷼"],[M.LocaleType.KO_KR,"₩"],[M.LocaleType.ES_ES,"€"],[M.LocaleType.CA_ES,"€"]]);function _i(i){return Wc.get(i)||"$"}function He(i,u=2){let e=u;u>127&&(e=127);let t="";return e>0&&(t=`.${"0".repeat(e)}`),`"${_i(i)}"#,##0${t}_);[Red]("${_i(i)}"#,##0${t})`}function Qc(i,u,e=2){return M.numfmt.format(He(i,e),u)}const Rs=new _t(1e5);function qc(i){let u=i;u.startsWith('"')&&u.endsWith('"')&&(u=u.slice(1,-1));const e=Rs.get(u);if(e)return{isNumberPattern:!0,value:e.value,pattern:e.pattern};const t=M.numfmt.parseNumber(u);if(t&&t.z)return Ns(u,t.v,t.z);const r=M.numfmt.parseDate(u);if(r&&r.z)return Ns(u,r.v,r.z);const n=M.numfmt.parseTime(u);return n&&n.z?Ns(u,n.v,n.z):{isNumberPattern:!1}}function Ns(i,u,e){return Rs.set(i,{value:u,pattern:e}),{isNumberPattern:!0,value:u,pattern:e}}function Kc(){Rs.clear()}class nn{constructor(){A(this,"pattern","")}dispose(){}getPattern(){return this.pattern}setPattern(u){this.pattern=u}isError(){return!1}isAsyncObject(){return!1}isAsyncArrayObject(){return!1}isReferenceObject(){return!1}isArray(){return!1}isValueObject(){return!1}isEqualType(u){return!1}}class Et extends nn{constructor(e){super();A(this,"_customData");this._rawValue=e}isValueObject(){return!0}toUnitRange(){return{range:{startColumn:-1,startRow:-1,endRow:-1,endColumn:-1},sheetId:"",unitId:""}}getValue(){return 0}getArrayValue(){return[]}setValue(e){}setArrayValue(e){}withCustomData(e){return this._customData=e,this}getCustomData(){return this._customData}isCube(){return!1}isString(){return!1}isNumber(){return!1}isBoolean(){return!1}isLambda(){return!1}isError(){return!1}isNull(){return!1}isHyperlink(){return!1}sum(){return g.create(h.VALUE)}max(){return g.create(h.VALUE)}min(){return g.create(h.VALUE)}count(){return g.create(h.VALUE)}countA(){return g.create(h.VALUE)}countBlank(){return g.create(h.VALUE)}getNegative(){return g.create(h.VALUE)}getReciprocal(){return g.create(h.VALUE)}plus(e){return g.create(h.VALUE)}minus(e){return g.create(h.VALUE)}multiply(e){return g.create(h.VALUE)}divided(e){return g.create(h.VALUE)}mod(e){return g.create(h.VALUE)}map(e){return g.create(h.NAME)}mapValue(e){return g.create(h.NAME)}compare(e,t,r=!1){return g.create(h.NAME)}isEqual(e){return this.compare(e,$.EQUALS)}isNotEqual(e){return this.compare(e,$.NOT_EQUAL)}isGreaterThanOrEqual(e){return this.compare(e,$.GREATER_THAN_OR_EQUAL)}isLessThanOrEqual(e){return this.compare(e,$.LESS_THAN_OR_EQUAL)}isLessThan(e){return this.compare(e,$.LESS_THAN)}isGreaterThan(e){return this.compare(e,$.GREATER_THAN)}concatenateFront(e){return g.create(h.NAME)}concatenateBack(e){return g.create(h.NAME)}plusBy(e){return g.create(h.VALUE)}minusBy(e){return g.create(h.VALUE)}multiplyBy(e){return g.create(h.VALUE)}dividedBy(e){return g.create(h.VALUE)}modInverse(e){return g.create(h.VALUE)}compareBy(e,t){return g.create(h.NAME)}concatenate(e,t=et.FRONT){let r=this.getValue().toString();if(typeof e=="string")t===et.FRONT?r=e+r:r+=e;else if(typeof e=="number")t===et.FRONT?r=e.toString()+r:r+=e.toString();else if(typeof e=="boolean"){const n=e?"TRUE":"FALSE";t===et.FRONT?r=n+r:r+=n}return r}pow(e){return g.create(h.VALUE)}powInverse(e){return g.create(h.VALUE)}sqrt(){return g.create(h.VALUE)}cbrt(){return g.create(h.VALUE)}cos(){return g.create(h.VALUE)}cosh(){return g.create(h.VALUE)}acos(){return g.create(h.VALUE)}acosh(){return g.create(h.VALUE)}sin(){return g.create(h.VALUE)}sinh(){return g.create(h.VALUE)}asin(){return g.create(h.VALUE)}asinh(){return g.create(h.VALUE)}tan(){return g.create(h.VALUE)}tanh(){return g.create(h.VALUE)}atan(){return g.create(h.VALUE)}atan2(e){return g.create(h.VALUE)}atan2Inverse(e){return g.create(h.VALUE)}atanh(){return g.create(h.VALUE)}mean(){return this}median(){return this}var(){return this}std(){return this}log(){return g.create(h.VALUE)}log10(){return g.create(h.VALUE)}exp(){return g.create(h.VALUE)}abs(){return g.create(h.VALUE)}round(e){return g.create(h.VALUE)}roundInverse(e){return g.create(h.VALUE)}floor(e){return g.create(h.VALUE)}floorInverse(e){return g.create(h.VALUE)}ceil(e){return g.create(h.VALUE)}ceilInverse(e){return g.create(h.VALUE)}convertToNumberObjectValue(){return g.create(h.VALUE)}convertToBooleanObjectValue(){return g.create(h.VALUE)}}const Xc=1e3,ps=new _t(Xc);class g extends Et{constructor(u,e=""){super(u),this._errorType=u,this._errorContent=e}static create(u,e=""){const t=`${u}-${e}`,r=ps.get(t);if(r)return r;const n=new g(u,e);return ps.set(t,n),n}getValue(){return this._errorType}getErrorType(){return this._errorType}getErrorContent(){return this._errorContent}isEqualType(u){return u.getErrorType()===this.getErrorType()}isError(){return!0}}function Zc(i){var u;if(i===null)return 0;if(i!=null&&i.p){const e=i==null?void 0:i.p.body;if(e==null)return 0;const t=e.dataStream;return M.BuildTextUtils.transform.getPlainText(t)}return(u=i==null?void 0:i.v)!=null?u:0}function zc(i,u=!1){return!u&&!(M.isFormulaString(i==null?void 0:i.f)||M.isFormulaId(i==null?void 0:i.si))?null:typeof(i==null?void 0:i.v)=="string"&&pt.has(i.v)?i.v:null}var Bt=(i=>(i[i.MIN=0]="MIN",i[i.MAX=1]="MAX",i))(Bt||{}),Se=(i=>(i[i.NORMAL=0]="NORMAL",i[i.MIN=1]="MIN",i[i.MAX=2]="MAX",i))(Se||{});function sn(){return Intl&&Intl.Collator?new Intl.Collator(void 0,{numeric:!1}).compare:(i,u)=>i.localeCompare(u)}function Jc(i){return i.indexOf("*")>-1||i.indexOf("?")>-1}function Vs(i,u){const e=tl(u).replace(/~?[*?]/g,r=>r.startsWith("~")?`\\${r.substring(1)}`:r==="*"?".*":r==="?"?".":r);return new RegExp(`^${e}$`).test(i)}function Ai(i){return i.replace(/~?[*?]/g,u=>u.startsWith("~")?u.substring(1):" ")}function el(i,u,e){let t=!1;switch(e){case $.EQUALS:t=Vs(i,u);break;case $.NOT_EQUAL:t=!Vs(i,u);break;case $.GREATER_THAN:case $.GREATER_THAN_OR_EQUAL:t=Vs(i,u)||i>Ai(u);break;case $.LESS_THAN:case $.LESS_THAN_OR_EQUAL:t=i<Ai(u);break}return t}function tl(i){return i.replace(/[.+^${}()|[\]\\]/g,"\\$&")}function Os(i){switch(i){case 1:return 2;case 0:return 0;case-1:return 1;default:return 0}}function Ms(i){return i===-2?1:0}function Ss(i){let u;switch(i){case $.EQUALS:u=$.EQUALS;break;case $.GREATER_THAN:u=$.LESS_THAN;break;case $.GREATER_THAN_OR_EQUAL:u=$.LESS_THAN_OR_EQUAL;break;case $.LESS_THAN:u=$.GREATER_THAN;break;case $.LESS_THAN_OR_EQUAL:u=$.GREATER_THAN_OR_EQUAL;break;case $.NOT_EQUAL:u=$.NOT_EQUAL;break}return u}/*!
2
2
  * decimal.js v10.6.0
3
3
  * An arbitrary-precision Decimal type for JavaScript.
4
4
  * https://github.com/MikeMcl/decimal.js
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@univerjs/engine-formula",
3
- "version": "0.10.14-nightly.202511070628",
3
+ "version": "0.10.14-nightly.202511070640",
4
4
  "private": false,
5
5
  "description": "UniverSheet normal base-formula-engine",
6
6
  "author": "DreamNum <developer@univer.ai>",
@@ -60,8 +60,8 @@
60
60
  "dependencies": {
61
61
  "@flatten-js/interval-tree": "^1.1.3",
62
62
  "decimal.js": "^10.6.0",
63
- "@univerjs/core": "0.10.14-nightly.202511070628",
64
- "@univerjs/rpc": "0.10.14-nightly.202511070628"
63
+ "@univerjs/core": "0.10.14-nightly.202511070640",
64
+ "@univerjs/rpc": "0.10.14-nightly.202511070640"
65
65
  },
66
66
  "devDependencies": {
67
67
  "rxjs": "^7.8.2",