@univerjs/sheets-data-validation 1.1.0 → 1.3.0

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/cjs/index.js CHANGED
@@ -2352,7 +2352,7 @@ DataValidationFormulaController = __decorate([
2352
2352
  //#endregion
2353
2353
  //#region package.json
2354
2354
  var name = "@univerjs/sheets-data-validation";
2355
- var version = "1.1.0";
2355
+ var version = "1.3.0";
2356
2356
 
2357
2357
  //#endregion
2358
2358
  //#region src/config/config.ts
package/lib/es/index.js CHANGED
@@ -2351,7 +2351,7 @@ DataValidationFormulaController = __decorate([
2351
2351
  //#endregion
2352
2352
  //#region package.json
2353
2353
  var name = "@univerjs/sheets-data-validation";
2354
- var version = "1.1.0";
2354
+ var version = "1.3.0";
2355
2355
 
2356
2356
  //#endregion
2357
2357
  //#region src/config/config.ts
package/lib/index.js CHANGED
@@ -2351,7 +2351,7 @@ DataValidationFormulaController = __decorate([
2351
2351
  //#endregion
2352
2352
  //#region package.json
2353
2353
  var name = "@univerjs/sheets-data-validation";
2354
- var version = "1.1.0";
2354
+ var version = "1.3.0";
2355
2355
 
2356
2356
  //#endregion
2357
2357
  //#region src/config/config.ts
package/lib/umd/index.js CHANGED
@@ -1 +1 @@
1
- (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("@univerjs/data-validation"),require("@univerjs/engine-formula"),require("@univerjs/sheets"),require("rxjs"),require("@univerjs/protocol"),require("@univerjs/sheets-formula")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`@univerjs/data-validation`,`@univerjs/engine-formula`,`@univerjs/sheets`,`rxjs`,`@univerjs/protocol`,`@univerjs/sheets-formula`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverSheetsDataValidation={},e.UniverCore,e.UniverDataValidation,e.UniverEngineFormula,e.UniverSheets,e.rxjs,e.UniverProtocol,e.UniverSheetsFormula))})(this,function(e,t,n,r,i,a,o,s){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});function c(e,n){let r=n.getMaxRows(),i=n.getMaxColumns();if(r<=0||i<=0)return null;let a=(0,t.getIntersectRange)(t.Range.transformRange(e,n),{startRow:0,endRow:r-1,startColumn:0,endColumn:i-1});return a?{startRow:a.startRow,endRow:a.endRow,startColumn:a.startColumn,endColumn:a.endColumn}:null}function l(e,t){let n=[];return e.forEach(e=>{let r=c(e,t);r&&n.push(r)}),n}function u(e){"@babel/helpers - typeof";return u=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},u(e)}function d(e,t){if(u(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(u(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function f(e){var t=d(e,`string`);return u(t)==`symbol`?t:t+``}function p(e,t,n){return(t=f(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function m(e,t){return function(n,r){t(n,r,e)}}function h(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let g=class extends t.Disposable{constructor(e,t,n){super(),this._commandService=e,this._univerInstanceService=t,this._sheetDataValidationModel=n,p(this,`_cacheMatrix`,/* @__PURE__ */ new Map),p(this,`_dirtyRanges$`,new a.Subject),p(this,`dirtyRanges$`,this._dirtyRanges$.asObservable()),this._initDirtyRanges(),this._initSheetRemove()}_initDirtyRanges(){this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{if(e.id===i.SetRangeValuesMutation.id&&!(n!=null&&n.onlyLocal)){let{cellValue:n,unitId:r,subUnitId:i}=e.params;if(n){let e=new t.ObjectMatrix(n).getDataRange();if(e.endRow===-1)return;let a=this._sheetDataValidationModel.getRules(r,i),o=[];for(let e of a)o.push(...e.ranges);let s=[];for(let n of o){let r=(0,t.getIntersectRange)(n,e);r&&s.push(r)}s.length&&this.markRangeDirty(r,i,s,!0)}}}))}_initSheetRemove(){this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(e.id===i.RemoveSheetMutation.id){var t;let{unitId:n,subUnitId:r}=e.params;(t=this._cacheMatrix.get(n))==null||t.delete(r)}})),this.disposeWithMe(this._univerInstanceService.unitDisposed$.subscribe(e=>{e.type===t.UniverInstanceType.UNIVER_SHEET&&this._cacheMatrix.delete(e.getUnitId())}))}_ensureCache(e,n){let r=this._cacheMatrix.get(e);r||(r=/* @__PURE__ */ new Map,this._cacheMatrix.set(e,r));let i=r.get(n);return i||(i=new t.ObjectMatrix,r.set(n,i)),i}ensureCache(e,t){return this._ensureCache(e,t)}addRule(e,t,n){this.markRangeDirty(e,t,n.ranges)}removeRule(e,t,n){this._deleteRange(e,t,n.ranges)}_getEffectiveRanges(e,n,r){var i;let a=(i=this._univerInstanceService.getUnit(e,t.UniverInstanceType.UNIVER_SHEET))==null?void 0:i.getSheetBySheetId(n);return a?l(r,a):[]}markRangeDirty(e,n,r,i){let a=this._ensureCache(e,n);this._getEffectiveRanges(e,n,r).forEach(e=>{t.Range.foreach(e,(e,t)=>{a.getValue(e,t)!==void 0&&a.setValue(e,t,void 0)})}),this._dirtyRanges$.next({unitId:e,subUnitId:n,ranges:r,isSetRange:i})}_deleteRange(e,n,r){let i=this._ensureCache(e,n);this._getEffectiveRanges(e,n,r).forEach(e=>{t.Range.foreach(e,(e,t)=>{i.realDeleteValue(e,t)})}),this._dirtyRanges$.next({unitId:e,subUnitId:n,ranges:r})}getValue(e,t,n,r){return this._ensureCache(e,t).getValue(n,r)}};g=h([m(0,(0,t.Inject)(t.ICommandService)),m(1,(0,t.Inject)(t.IUniverInstanceService)),m(2,(0,t.Inject)(n.DataValidationModel))],g);function _(e){var t;return e==null||(t=e[0])==null||(t=t[0])==null?void 0:t.v}function v(e){var t;return e==null||(t=e[0])==null?void 0:t[0]}function y(e){return!r.ERROR_TYPE_SET.has(e)}function b(e,t){let n=t.getValidatorItem(e);return(n==null?void 0:n.offsetFormulaByRange)??!1}let x=class extends t.Disposable{constructor(e,t,n,r,i){super(),this._instanceSrv=e,this._registerOtherFormulaService=t,this._dataValidationModel=n,this._dataValidationCacheService=r,this._validatorRegistryService=i,p(this,`_ruleFormulaMap`,/* @__PURE__ */ new Map),p(this,`_ruleFormulaMap2`,/* @__PURE__ */ new Map),this._initFormulaResultHandler(),this._initDirtyRanges()}dispose(){super.dispose(),this._ruleFormulaMap.clear(),this._ruleFormulaMap2.clear()}_initFormulaResultHandler(){this.disposeWithMe(this._registerOtherFormulaService.formulaResult$.subscribe(e=>{for(let n in e){let r=e[n];if(this._instanceSrv.getUnitType(n)===t.UniverInstanceType.UNIVER_SHEET)for(let e in r){let t=r[e],{ruleFormulaMap:i}=this._ensureMaps(n,e);t.forEach(t=>{var r,a;let o=i.get((r=t.extra)==null?void 0:r.ruleId),s=this._dataValidationModel.getRuleById(n,e,(a=t.extra)==null?void 0:a.ruleId);s&&o&&this._dataValidationCacheService.markRangeDirty(n,e,s.ranges)})}}}))}_ensureMaps(e,t){let n=this._ruleFormulaMap.get(e),r=this._ruleFormulaMap2.get(e);n||(n=/* @__PURE__ */ new Map,this._ruleFormulaMap.set(e,n)),r||(r=/* @__PURE__ */ new Map,this._ruleFormulaMap2.set(e,r));let i=n.get(t);i||(i=/* @__PURE__ */ new Map,n.set(t,i));let a=r.get(t);return a||(a=/* @__PURE__ */ new Map,r.set(t,a)),{ruleFormulaMap:i,ruleFormulaMap2:a}}_registerFormula(e,t,n,i,a){return this._registerOtherFormulaService.registerFormulaWithRange(e,t,i,a,{ruleId:n},r.OtherFormulaBizType.DATA_VALIDATION_CUSTOM,n)}_handleDirtyRanges(e,n,r){this._dataValidationModel.getRules(e,n).forEach(i=>{let a=i.ranges;t.Rectangle.doAnyRangesIntersect(a,r)&&this.makeRuleDirty(e,n,i.uid)})}_initDirtyRanges(){this.disposeWithMe(this._dataValidationCacheService.dirtyRanges$.subscribe(e=>{e.isSetRange&&this._handleDirtyRanges(e.unitId,e.subUnitId,e.ranges)}))}deleteByRuleId(e,t,n){let{ruleFormulaMap:r,ruleFormulaMap2:i}=this._ensureMaps(e,t),a=this._dataValidationModel.getRuleById(e,t,n),o=r.get(n);if(!a||!o)return;let s=r.get(n);s&&(r.delete(n),this._registerOtherFormulaService.deleteFormula(e,t,[s.formulaId]));let c=i.get(n);c&&(i.delete(n),this._registerOtherFormulaService.deleteFormula(e,t,[c.formulaId]))}_addFormulaByRange(e,n,r,i,a,o){var s;let{ruleFormulaMap:c,ruleFormulaMap2:u}=this._ensureMaps(e,n),d=(s=this._instanceSrv.getUnit(e,t.UniverInstanceType.UNIVER_SHEET))==null?void 0:s.getSheetBySheetId(n);if(!d)return;let f=l(o,d);if(!f.length)return;let p=f[0].startRow,m=f[0].startColumn;if(i&&(0,t.isFormulaString)(i)){let t=this._registerFormula(e,n,r,i,f);c.set(r,{formula:i,originCol:m,originRow:p,formulaId:t})}if(a&&(0,t.isFormulaString)(a)){let t=this._registerFormula(e,n,r,a,f);u.set(r,{formula:a,originCol:m,originRow:p,formulaId:t})}}addRule(e,t,n){if(b(n.type,this._validatorRegistryService)){let{ranges:r,formula1:i,formula2:a,uid:o}=n;this._addFormulaByRange(e,t,o,i,a,r)}}async getCellFormulaValue(e,t,n,r,i){var a;let{ruleFormulaMap:o}=this._ensureMaps(e,t),s=o.get(n);if(!s)return Promise.resolve(void 0);let c=await this._registerOtherFormulaService.getFormulaValue(e,t,s.formulaId),{originRow:l,originCol:u}=s,d=r-l,f=i-u;return v(c==null||(a=c.result)==null||(a=a[d])==null?void 0:a[f])}async getCellFormula2Value(e,t,n,r,i){var a;let{ruleFormulaMap2:o}=this._ensureMaps(e,t),s=o.get(n);if(!s)return Promise.resolve(void 0);let c=await this._registerOtherFormulaService.getFormulaValue(e,t,s.formulaId),{originRow:l,originCol:u}=s,d=r-l,f=i-u;return v(c==null||(a=c.result)==null||(a=a[d])==null?void 0:a[f])}getCellFormulaValueSync(e,t,n,r,i){var a;let{ruleFormulaMap:o}=this._ensureMaps(e,t),s=o.get(n);if(!s)return;let c=this._registerOtherFormulaService.getFormulaValueSync(e,t,s.formulaId),{originRow:l,originCol:u}=s,d=r-l,f=i-u;return v(c==null||(a=c.result)==null||(a=a[d])==null?void 0:a[f])}getCellFormula2ValueSync(e,t,n,r,i){var a;let{ruleFormulaMap2:o}=this._ensureMaps(e,t),s=o.get(n);if(!s)return;let c=this._registerOtherFormulaService.getFormulaValueSync(e,t,s.formulaId),{originRow:l,originCol:u}=s,d=r-l,f=i-u;return v(c==null||(a=c.result)==null||(a=a[d])==null?void 0:a[f])}getRuleFormulaInfo(e,t,n){let{ruleFormulaMap:r}=this._ensureMaps(e,t);return r.get(n)}makeRuleDirty(e,t,n){var r,i;let a=(r=this._ruleFormulaMap.get(e))==null||(r=r.get(t))==null?void 0:r.get(n),o=(i=this._ruleFormulaMap2.get(e))==null||(i=i.get(t))==null?void 0:i.get(n);a&&this._registerOtherFormulaService.markFormulaDirty(e,t,a.formulaId),o&&this._registerOtherFormulaService.markFormulaDirty(e,t,o.formulaId)}};x=h([m(0,t.IUniverInstanceService),m(1,(0,t.Inject)(r.RegisterOtherFormulaService)),m(2,(0,t.Inject)(n.DataValidationModel)),m(3,(0,t.Inject)(g)),m(4,(0,t.Inject)(n.DataValidatorRegistryService))],x);function S(e){return(0,t.getOriginCellValue)(e)}function C(e){return String(S(e)??``)}let w=class extends t.Disposable{constructor(e,t){super(),this._injector=e,this._dataValidationModel=t,p(this,`_cache`,/* @__PURE__ */ new Map),this._initRuleChangeListener()}_initRuleChangeListener(){this.disposeWithMe(this._dataValidationModel.ruleChange$.subscribe(e=>{(e.type===`remove`||e.type===`update`)&&this.markRuleDirty(e.unitId,e.subUnitId,e.rule.uid)}))}getOrCompute(e,t,n){let r=this.getCache(e,t,n.uid);if(r)return r;let i=this._injector.get(T).getRuleFormulaResultSync(e,t,n.uid);return this.computeAndCache(e,t,n,i)}_ensureCache(e,t){let n=this._cache.get(e);n||(n=/* @__PURE__ */ new Map,this._cache.set(e,n));let r=n.get(t);return r||(r=/* @__PURE__ */ new Map,n.set(t,r)),r}getCache(e,t,n){var r;return(r=this._cache.get(e))==null||(r=r.get(t))==null?void 0:r.get(n)}setCache(e,t,n,r){this._ensureCache(e,t).set(n,r)}markRuleDirty(e,t,n){var r;(r=this._cache.get(e))==null||(r=r.get(t))==null||r.delete(n)}clear(){this._cache.clear()}computeAndCache(e,n,r,a){var o;let{formula1:s=``,formula2:c=``}=r,l=(0,t.isFormulaString)(s)?this._getRuleFormulaResultSet(a==null||(o=a[0])==null||(o=o.result)==null||(o=o[0])==null?void 0:o[0]):(0,i.deserializeListOptions)(s),u=c.split(`,`),d=l.map((e,t)=>({label:e,color:u[t]||``})),f={};for(let e of d)e.color&&(f[e.label]=e.color);let p={list:l,listWithColor:d,colorMap:f,set:new Set(l)};return this.setCache(e,n,r.uid,p),p}_getRuleFormulaResultSet(e){if(!e)return[];let n=/* @__PURE__ */ new Set;for(let i=0,a=e.length;i<a;i++){let a=e[i];if(a)for(let e=0,i=a.length;e<i;e++){let i=a[e],o=S(i);if(o!=null){var r;if(typeof o!=`string`&&typeof(i==null?void 0:i.s)==`object`&&(r=i.s)!=null&&(r=r.n)!=null&&r.pattern){n.add(t.numfmt.format(i.s.n.pattern,o,{throws:!1}));continue}let e=typeof o==`string`?o:String(o);y(e)&&n.add(e)}}}return[...n]}};w=h([m(0,(0,t.Inject)(t.Injector)),m(1,(0,t.Inject)(n.DataValidationModel))],w);let T=class extends t.Disposable{constructor(e,t,n,r,i,a){super(),this._instanceService=e,this._registerOtherFormulaService=t,this._dataValidationCacheService=n,this._dataValidationModel=r,this._validatorRegistryService=i,this._listCacheService=a,p(this,`_formulaRuleMap`,/* @__PURE__ */ new Map),this._initFormulaResultHandler()}_initFormulaResultHandler(){this.disposeWithMe(this._registerOtherFormulaService.formulaResult$.subscribe(e=>{for(let n in e){let r=e[n];if(this._instanceService.getUnitType(n)===t.UniverInstanceType.UNIVER_SHEET)for(let e in r){let t=r[e],i=this._ensureRuleFormulaMap(n,e);t.forEach(t=>{var r;let a=(r=t.extra)==null?void 0:r.ruleId;if(a&&i.get(a)){let t=this._dataValidationModel.getRuleById(n,e,a);t&&(this._listCacheService.markRuleDirty(n,e,a),this._dataValidationCacheService.markRangeDirty(n,e,t.ranges))}})}}}))}_ensureRuleFormulaMap(e,t){let n=this._formulaRuleMap.get(e);n||(n=/* @__PURE__ */ new Map,this._formulaRuleMap.set(e,n));let r=n.get(t);return r||(r=/* @__PURE__ */ new Map,n.set(t,r)),r}_registerSingleFormula(e,t,n,i){return this._registerOtherFormulaService.registerFormulaWithRange(e,t,n,[{startColumn:0,endColumn:0,startRow:0,endRow:0}],{ruleId:i},r.OtherFormulaBizType.DATA_VALIDATION,i)}addRule(e,n,r){if(!b(r.type,this._validatorRegistryService)&&r.type!==t.DataValidationType.CHECKBOX){let{formula1:i,formula2:a,uid:o}=r,s=(0,t.isFormulaString)(i),c=(0,t.isFormulaString)(a);if(!s&&!c)return;let l=this._ensureRuleFormulaMap(e,n),u=[void 0,void 0];s&&(u[0]={id:this._registerSingleFormula(e,n,i,o),text:i}),c&&(u[1]={id:this._registerSingleFormula(e,n,a,o),text:a}),l.set(o,u)}}removeRule(e,t,n){let r=this._ensureRuleFormulaMap(e,t).get(n);if(!r)return;let[i,a]=r,o=[i==null?void 0:i.id,a==null?void 0:a.id].filter(Boolean);o.length&&this._registerOtherFormulaService.deleteFormula(e,t,o)}getRuleFormulaResult(e,t,n){let r=this._ensureRuleFormulaMap(e,t).get(n);if(!r)return Promise.resolve(null);let i=async n=>n&&this._registerOtherFormulaService.getFormulaValue(e,t,n.id);return Promise.all([i(r[0]),i(r[1])])}getRuleFormulaResultSync(e,t,n){let r=this._ensureRuleFormulaMap(e,t).get(n);if(r)return r.map(n=>{if(n)return this._registerOtherFormulaService.getFormulaValueSync(e,t,n.id)})}getRuleFormulaInfo(e,t,n){return this._ensureRuleFormulaMap(e,t).get(n)}};T=h([m(0,t.IUniverInstanceService),m(1,(0,t.Inject)(r.RegisterOtherFormulaService)),m(2,(0,t.Inject)(g)),m(3,(0,t.Inject)(n.DataValidationModel)),m(4,(0,t.Inject)(n.DataValidatorRegistryService)),m(5,(0,t.Inject)(w))],T);var ee=class e{constructor(e,n,r,i,a=!1){this._unitId=n,this._subUnitId=r,this._univerInstanceService=i,this._disableTree=a,p(this,`_map`,void 0),p(this,`_tree`,new t.RBush),p(this,`_dirty`,!0),p(this,`_buildTree`,()=>{if(!this._dirty||this._disableTree)return;this._tree.clear();let e=[];this._map.forEach((t,n)=>{t.forEach(t=>{e.push({minX:t.startRow,maxX:t.endRow,minY:t.startColumn,maxY:t.endColumn,ruleId:n})})}),this._tree.load(e),this._dirty=!1}),p(this,`_debonceBuildTree`,(0,t.debounce)(this._buildTree,0)),this._map=e,this._buildTree()}get _worksheet(){var e;return(e=this._univerInstanceService.getUnit(this._unitId,t.UniverInstanceType.UNIVER_SHEET))==null?void 0:e.getSheetBySheetId(this._subUnitId)}_addRule(e,n){if(!this._worksheet)return;let r=t.Rectangle.mergeRanges(n.map(e=>t.Range.transformRange(e,this._worksheet)));this._map.forEach((e,n)=>{let i=t.Rectangle.subtractMulti(e,r);i.length===0?this._map.delete(n):this._map.set(n,i)}),this._dirty=!0,this._map.set(e,r),this._debonceBuildTree()}addRule(e){this._addRule(e.uid,e.ranges)}removeRange(e,n){if(!this._worksheet)return;let r=e.map(e=>t.Range.transformRange(e,this._worksheet));this._map.forEach((e,i)=>{if(n&&!n.has(i))return;let a=t.Rectangle.subtractMulti(e,r);a.length===0?this._map.delete(i):this._map.set(i,a)}),this._dirty=!0,this._debonceBuildTree()}_removeRule(e){this._map.delete(e),this._dirty=!0,this._debonceBuildTree()}removeRule(e){this._removeRule(e.uid)}updateRange(e,t){this._removeRule(e),this._addRule(e,t)}addRangeRules(e){e.forEach(({id:e,ranges:n})=>{if(!n.length)return;let r=this._map.get(e);r?(this._map.set(e,t.Rectangle.mergeRanges([...r,...n])),r=this._map.get(e)):(r=n,this._map.set(e,r)),this._map.forEach((r,i)=>{if(i===e)return;let a=t.Rectangle.subtractMulti(r,n);a.length===0?this._map.delete(i):this._map.set(i,a)})}),this._dirty=!0,this._debonceBuildTree()}diff(e){let n=[],r=0;return e.forEach((e,i)=>{let a=this._map.get(e.uid)??[],o=e.ranges;a.length!==0&&(a.length!==o.length||a.some((e,n)=>!t.Rectangle.equals(e,o[n])))&&n.push({type:`update`,ruleId:e.uid,oldRanges:o,newRanges:t.Rectangle.sort(a),rule:e}),a.length===0&&(n.push({type:`delete`,rule:e,index:i-r}),r++)}),n}diffWithAddition(e,n){let r=[],i=0;return e.forEach((e,n)=>{let a=this._map.get(e.uid)??[],o=e.ranges;a.length!==0&&(a.length!==o.length||a.some((e,n)=>!t.Rectangle.equals(e,o[n])))&&r.push({type:`update`,ruleId:e.uid,oldRanges:o,newRanges:t.Rectangle.sort(a),rule:e}),a.length===0&&(r.push({type:`delete`,rule:e,index:n-i}),i++)}),Array.from(n).forEach(e=>{let n=this._map.get(e.uid)??[];r.push({type:`add`,rule:{...e,ranges:t.Rectangle.sort(n)}})}),r}clone(){return new e(new Map(t.Tools.deepClone(Array.from(this._map.entries()))),this._unitId,this._subUnitId,this._univerInstanceService,!0)}getValue(e,t){this._dirty&&this._buildTree();let n=this._tree.search({minX:e,maxX:e,minY:t,maxY:t});return n.length>0?n[0].ruleId:void 0}};let E=class extends t.Disposable{constructor(e,t,n,r,i,o,s){super(),this._dataValidationModel=e,this._univerInstanceService=t,this._dataValidatorRegistryService=n,this._dataValidationCacheService=r,this._dataValidationFormulaService=i,this._dataValidationCustomFormulaService=o,this._commandService=s,p(this,`_ruleMatrixMap`,/* @__PURE__ */ new Map),p(this,`_validStatusChange$`,new a.Subject),p(this,`_ruleChange$`,new a.Subject),p(this,`ruleChange$`,this._ruleChange$.asObservable()),p(this,`validStatusChange$`,this._validStatusChange$.asObservable()),this._initRuleUpdateListener(),this.disposeWithMe(()=>{this._ruleChange$.complete(),this._validStatusChange$.complete()}),this._initUniverInstanceListener()}_initUniverInstanceListener(){this.disposeWithMe(this._univerInstanceService.unitDisposed$.subscribe(e=>{this._ruleMatrixMap.delete(e.getUnitId())})),this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(e.id===i.RemoveSheetMutation.id){let{unitId:t,subUnitId:n}=e.params,r=this._ruleMatrixMap.get(t);r&&r.delete(n)}}))}_initRuleUpdateListener(){let e=this._dataValidationModel.getAll();for(let[t,n]of e)for(let[e,r]of n)for(let n of r)this._addRule(t,e,n),this._ruleChange$.next({type:`add`,unitId:t,subUnitId:e,rule:n,source:`patched`});this.disposeWithMe(this._dataValidationModel.ruleChange$.subscribe(e=>{switch(e.type){case`add`:this._addRule(e.unitId,e.subUnitId,e.rule);break;case`update`:this._updateRule(e.unitId,e.subUnitId,e.rule.uid,e.oldRule,e.updatePayload);break;case`remove`:this._removeRule(e.unitId,e.subUnitId,e.rule)}this._ruleChange$.next(e)}))}_ensureRuleMatrix(e,t){let n=this._ruleMatrixMap.get(e);n||(n=/* @__PURE__ */ new Map,this._ruleMatrixMap.set(e,n));let r=n.get(t);return r||(r=new ee(/* @__PURE__ */ new Map,e,t,this._univerInstanceService),n.set(t,r)),r}_addRuleSideEffect(e,t,n){this._ensureRuleMatrix(e,t).addRule(n),this._dataValidationCacheService.addRule(e,t,n),this._dataValidationFormulaService.addRule(e,t,n),this._dataValidationCustomFormulaService.addRule(e,t,n)}_addRule(e,t,n){(Array.isArray(n)?n:[n]).forEach(n=>{this._addRuleSideEffect(e,t,n)})}_updateRule(e,t,r,i,a){let o=this._ensureRuleMatrix(e,t),s={...i,...a.payload};a.type===n.UpdateRuleType.RANGE?o.updateRange(r,a.payload):a.type===n.UpdateRuleType.ALL&&o.updateRange(r,a.payload.ranges),this._dataValidationCacheService.removeRule(e,t,i),this._dataValidationCacheService.addRule(e,t,s),this._dataValidationFormulaService.removeRule(e,t,i.uid),this._dataValidationFormulaService.addRule(e,t,s),this._dataValidationCustomFormulaService.deleteByRuleId(e,t,r),this._dataValidationCustomFormulaService.addRule(e,t,s)}_removeRule(e,t,n){this._ensureRuleMatrix(e,t).removeRule(n),this._dataValidationCacheService.removeRule(e,t,n),this._dataValidationCustomFormulaService.deleteByRuleId(e,t,n.uid)}getValidator(e){return this._dataValidatorRegistryService.getValidatorItem(e)}getRuleIdByLocation(e,t,n,r){return this._ensureRuleMatrix(e,t).getValue(n,r)}getRuleByLocation(e,t,n,r){let i=this.getRuleIdByLocation(e,t,n,r);if(i)return this._dataValidationModel.getRuleById(e,t,i)}validator(e,n,r){let{col:i,row:a,unitId:o,subUnitId:s,worksheet:c}=n,l=(t,n)=>{r&&r(t,n),n&&this._validStatusChange$.next({unitId:o,subUnitId:s,ruleId:e.uid,status:t,row:a,col:i})},u=c.getCellValueOnly(a,i),d=this.getValidator(e.type),f=c.getCellRaw(a,i),p=S(f);if(d){let r=this._dataValidationCacheService.ensureCache(o,s),c=r.getValue(a,i);return c==null?(r.setValue(a,i,t.DataValidationStatus.VALIDATING),d.validator({value:p,unitId:o,subUnitId:s,row:a,column:i,worksheet:n.worksheet,workbook:n.workbook,interceptValue:S(u),t:f==null?void 0:f.t},e).then(e=>{let n=e?t.DataValidationStatus.VALID:t.DataValidationStatus.INVALID,o=r.getValue(a,i);n===t.DataValidationStatus.VALID?r.realDeleteValue(a,i):r.setValue(a,i,n),l(n,c!==o)}),t.DataValidationStatus.VALIDATING):(l(c??t.DataValidationStatus.VALID,!1),c??t.DataValidationStatus.VALID)}return l(t.DataValidationStatus.VALID,!1),t.DataValidationStatus.VALID}getRuleObjectMatrix(e,t){return this._ensureRuleMatrix(e,t)}getRuleById(e,t,n){return this._dataValidationModel.getRuleById(e,t,n)}getRuleIndex(e,t,n){return this._dataValidationModel.getRuleIndex(e,t,n)}getRules(e,t){return[...this._dataValidationModel.getRules(e,t)]}getUnitRules(e){return this._dataValidationModel.getUnitRules(e)}deleteUnitRules(e){return this._dataValidationModel.deleteUnitRules(e)}getSubUnitIds(e){return this._dataValidationModel.getSubUnitIds(e)}getAll(){return this._dataValidationModel.getAll()}};E=h([m(0,(0,t.Inject)(n.DataValidationModel)),m(1,t.IUniverInstanceService),m(2,(0,t.Inject)(n.DataValidatorRegistryService)),m(3,(0,t.Inject)(g)),m(4,(0,t.Inject)(T)),m(5,(0,t.Inject)(x)),m(6,t.ICommandService)],E);let te={[t.DataValidationOperator.BETWEEN]:`sheets-data-validation.operators.between`,[t.DataValidationOperator.EQUAL]:`sheets-data-validation.operators.equal`,[t.DataValidationOperator.GREATER_THAN]:`sheets-data-validation.operators.greaterThan`,[t.DataValidationOperator.GREATER_THAN_OR_EQUAL]:`sheets-data-validation.operators.greaterThanOrEqual`,[t.DataValidationOperator.LESS_THAN]:`sheets-data-validation.operators.lessThan`,[t.DataValidationOperator.LESS_THAN_OR_EQUAL]:`sheets-data-validation.operators.lessThanOrEqual`,[t.DataValidationOperator.NOT_BETWEEN]:`sheets-data-validation.operators.notBetween`,[t.DataValidationOperator.NOT_EQUAL]:`sheets-data-validation.operators.notEqual`},D={[t.DataValidationOperator.BETWEEN]:`sheets-data-validation.ruleName.between`,[t.DataValidationOperator.EQUAL]:`sheets-data-validation.ruleName.equal`,[t.DataValidationOperator.GREATER_THAN]:`sheets-data-validation.ruleName.greaterThan`,[t.DataValidationOperator.GREATER_THAN_OR_EQUAL]:`sheets-data-validation.ruleName.greaterThanOrEqual`,[t.DataValidationOperator.LESS_THAN]:`sheets-data-validation.ruleName.lessThan`,[t.DataValidationOperator.LESS_THAN_OR_EQUAL]:`sheets-data-validation.ruleName.lessThanOrEqual`,[t.DataValidationOperator.NOT_BETWEEN]:`sheets-data-validation.ruleName.notBetween`,[t.DataValidationOperator.NOT_EQUAL]:`sheets-data-validation.ruleName.notEqual`,NONE:`sheets-data-validation.ruleName.legal`},O={[t.DataValidationOperator.BETWEEN]:`sheets-data-validation.errorMsg.between`,[t.DataValidationOperator.EQUAL]:`sheets-data-validation.errorMsg.equal`,[t.DataValidationOperator.GREATER_THAN]:`sheets-data-validation.errorMsg.greaterThan`,[t.DataValidationOperator.GREATER_THAN_OR_EQUAL]:`sheets-data-validation.errorMsg.greaterThanOrEqual`,[t.DataValidationOperator.LESS_THAN]:`sheets-data-validation.errorMsg.lessThan`,[t.DataValidationOperator.LESS_THAN_OR_EQUAL]:`sheets-data-validation.errorMsg.lessThanOrEqual`,[t.DataValidationOperator.NOT_BETWEEN]:`sheets-data-validation.errorMsg.notBetween`,[t.DataValidationOperator.NOT_EQUAL]:`sheets-data-validation.errorMsg.notEqual`,NONE:`sheets-data-validation.errorMsg.legal`},ne={[t.DataValidationOperator.BETWEEN]:`sheets-data-validation.textLength.errorMsg.between`,[t.DataValidationOperator.EQUAL]:`sheets-data-validation.textLength.errorMsg.equal`,[t.DataValidationOperator.GREATER_THAN]:`sheets-data-validation.textLength.errorMsg.greaterThan`,[t.DataValidationOperator.GREATER_THAN_OR_EQUAL]:`sheets-data-validation.textLength.errorMsg.greaterThanOrEqual`,[t.DataValidationOperator.LESS_THAN]:`sheets-data-validation.textLength.errorMsg.lessThan`,[t.DataValidationOperator.LESS_THAN_OR_EQUAL]:`sheets-data-validation.textLength.errorMsg.lessThanOrEqual`,[t.DataValidationOperator.NOT_BETWEEN]:`sheets-data-validation.textLength.errorMsg.notBetween`,[t.DataValidationOperator.NOT_EQUAL]:`sheets-data-validation.textLength.errorMsg.notEqual`},k=`{FORMULA1}`,A=`{FORMULA2}`,j=`{TYPE}`;var M=class extends n.BaseDataValidator{get operatorNames(){return this.operators.map(e=>this.localeService.t(te[e]))}generateRuleName(e){if(!e.operator)return this.localeService.t(D.NONE).replace(j,this.titleStr);let t=this.localeService.t(D[e.operator]).replace(k,e.formula1??``).replace(A,e.formula2??``);return`${this.titleStr} ${t}`}generateRuleErrorMessage(e,t){return e.operator?`${this.localeService.t(O[e.operator]).replace(k,e.formula1??``).replace(A,e.formula2??``)}`:this.localeService.t(O.NONE).replace(j,this.titleStr)}};function N(e,n){return t.Tools.isBlank(e)?n.t(`sheets-data-validation.validFail.value`):(0,t.isFormulaString)(e)?n.t(`sheets-data-validation.validFail.primitive`):``}let P=e=>t.Tools.isDefine(e)&&String(e).toLowerCase()===`true`?`1`:String(e).toLowerCase()===`false`?`0`:e;var re=class extends M{constructor(...e){super(...e),p(this,`id`,t.DataValidationType.CHECKBOX),p(this,`title`,`sheets-data-validation.checkbox.title`),p(this,`operators`,[]),p(this,`scopes`,[`sheet`]),p(this,`order`,41),p(this,`offsetFormulaByRange`,!1),p(this,`_formulaService`,this.injector.get(T)),p(this,`skipDefaultFontRender`,(e,t,n)=>{let{unitId:r,subUnitId:i}=n,{formula1:a,formula2:o}=this.parseFormulaSync(e,r,i),s=`${t??``}`;return!s||s===`${a}`||s===`${o}`})}validatorFormula(e,n,r){let{formula1:i,formula2:a}=e,o=i===a;if(t.Tools.isBlank(i)&&t.Tools.isBlank(a))return{success:!0};if(o)return{success:!1,formula1:this.localeService.t(`sheets-data-validation.validFail.checkboxEqual`),formula2:this.localeService.t(`sheets-data-validation.validFail.checkboxEqual`)};let s=N(i,this.localeService),c=N(a,this.localeService);return{success:!s&&!c,formula1:s,formula2:c}}async parseFormula(e,n,r){var i,a;let{formula1:o=1,formula2:s=0}=e,c=await this._formulaService.getRuleFormulaResult(n,r,e.uid),l=(0,t.isFormulaString)(o)?_(c==null||(i=c[0])==null||(i=i.result)==null?void 0:i[0][0]):o,u=(0,t.isFormulaString)(s)?_(c==null||(a=c[1])==null||(a=a.result)==null?void 0:a[0][0]):s,d=y(String(l))&&y(String(u));return{formula1:P(l),formula2:P(u),originFormula1:l,originFormula2:u,isFormulaValid:d}}getExtraStyle(e,n){return{tb:t.WrapStrategy.CLIP}}parseFormulaSync(e,n,r){var i,a;let{formula1:o=1,formula2:s=0}=e,c=this._formulaService.getRuleFormulaResultSync(n,r,e.uid),l=(0,t.isFormulaString)(o)?_(c==null||(i=c[0])==null||(i=i.result)==null?void 0:i[0][0]):o,u=(0,t.isFormulaString)(s)?_(c==null||(a=c[1])==null||(a=a.result)==null?void 0:a[0][0]):s,d=y(String(l))&&y(String(u));return{formula1:P(l),formula2:P(u),originFormula1:l,originFormula2:u,isFormulaValid:d}}async isValidType(e,n,r){let{value:i,unitId:a,subUnitId:o}=e,{formula1:s,formula2:c,originFormula1:l,originFormula2:u}=await this.parseFormula(r,a,o);return!t.Tools.isDefine(s)||!t.Tools.isDefine(c)||t.Tools.isDefine(i)&&(String(i)===String(s)||String(i)===String(c)||String(i)===String(l??``)||String(i)===String(u??``))}generateRuleErrorMessage(e){return this.localeService.t(`sheets-data-validation.checkbox.error`)}generateRuleName(e){return this.titleStr}};let ie={[t.DataValidationOperator.BETWEEN]:`sheets-data-validation.date.operators.between`,[t.DataValidationOperator.EQUAL]:`sheets-data-validation.date.operators.equal`,[t.DataValidationOperator.GREATER_THAN]:`sheets-data-validation.date.operators.greaterThan`,[t.DataValidationOperator.GREATER_THAN_OR_EQUAL]:`sheets-data-validation.date.operators.greaterThanOrEqual`,[t.DataValidationOperator.LESS_THAN]:`sheets-data-validation.date.operators.lessThan`,[t.DataValidationOperator.LESS_THAN_OR_EQUAL]:`sheets-data-validation.date.operators.lessThanOrEqual`,[t.DataValidationOperator.NOT_BETWEEN]:`sheets-data-validation.date.operators.notBetween`,[t.DataValidationOperator.NOT_EQUAL]:`sheets-data-validation.date.operators.notEqual`};t.DataValidationOperator.BETWEEN,t.DataValidationOperator.EQUAL,t.DataValidationOperator.GREATER_THAN,t.DataValidationOperator.GREATER_THAN_OR_EQUAL,t.DataValidationOperator.LESS_THAN,t.DataValidationOperator.LESS_THAN_OR_EQUAL,t.DataValidationOperator.NOT_BETWEEN,t.DataValidationOperator.NOT_EQUAL;let ae={[t.DataValidationOperator.BETWEEN]:`sheets-data-validation.date.ruleName.between`,[t.DataValidationOperator.EQUAL]:`sheets-data-validation.date.ruleName.equal`,[t.DataValidationOperator.GREATER_THAN]:`sheets-data-validation.date.ruleName.greaterThan`,[t.DataValidationOperator.GREATER_THAN_OR_EQUAL]:`sheets-data-validation.date.ruleName.greaterThanOrEqual`,[t.DataValidationOperator.LESS_THAN]:`sheets-data-validation.date.ruleName.lessThan`,[t.DataValidationOperator.LESS_THAN_OR_EQUAL]:`sheets-data-validation.date.ruleName.lessThanOrEqual`,[t.DataValidationOperator.NOT_BETWEEN]:`sheets-data-validation.date.ruleName.notBetween`,[t.DataValidationOperator.NOT_EQUAL]:`sheets-data-validation.date.ruleName.notEqual`,NONE:`sheets-data-validation.date.ruleName.legal`},oe={[t.DataValidationOperator.BETWEEN]:`sheets-data-validation.date.errorMsg.between`,[t.DataValidationOperator.EQUAL]:`sheets-data-validation.date.errorMsg.equal`,[t.DataValidationOperator.GREATER_THAN]:`sheets-data-validation.date.errorMsg.greaterThan`,[t.DataValidationOperator.GREATER_THAN_OR_EQUAL]:`sheets-data-validation.date.errorMsg.greaterThanOrEqual`,[t.DataValidationOperator.LESS_THAN]:`sheets-data-validation.date.errorMsg.lessThan`,[t.DataValidationOperator.LESS_THAN_OR_EQUAL]:`sheets-data-validation.date.errorMsg.lessThanOrEqual`,[t.DataValidationOperator.NOT_BETWEEN]:`sheets-data-validation.date.errorMsg.notBetween`,[t.DataValidationOperator.NOT_EQUAL]:`sheets-data-validation.date.errorMsg.notEqual`,NONE:`sheets-data-validation.date.errorMsg.legal`},F=[t.DataValidationOperator.BETWEEN,t.DataValidationOperator.NOT_BETWEEN],I=`{FORMULA1}`,L=`{FORMULA2}`;function se(e){let t=S(e);return t==null?``:t.toString()}function R(e,n,r){let{formula1:i,formula2:a}=n,o=n.ranges[0].startRow,s=n.ranges[0].startColumn,c=r.row-o,l=r.col-s;return{transformedFormula1:(0,t.isFormulaString)(i)?e.moveFormulaRefOffset(i,l,c,!0):i,transformedFormula2:(0,t.isFormulaString)(a)?e.moveFormulaRefOffset(a,l,c,!0):a}}let z=(e,n={})=>{var r;if(e!=null&&typeof e!=`boolean`)return typeof e==`number`||!Number.isNaN(+e)?+e:(r=t.numfmt.parseDate(e,n))==null?void 0:r.v};function B(e){return String(e).padStart(2,`0`)}var V=class extends M{constructor(...e){super(...e),p(this,`id`,t.DataValidationType.DATE),p(this,`title`,`sheets-data-validation.date.title`),p(this,`order`,40),p(this,`operators`,[t.DataValidationOperator.BETWEEN,t.DataValidationOperator.EQUAL,t.DataValidationOperator.GREATER_THAN,t.DataValidationOperator.GREATER_THAN_OR_EQUAL,t.DataValidationOperator.LESS_THAN,t.DataValidationOperator.LESS_THAN_OR_EQUAL,t.DataValidationOperator.NOT_BETWEEN,t.DataValidationOperator.NOT_EQUAL]),p(this,`scopes`,[`sheet`]),p(this,`_customFormulaService`,this.injector.get(x)),p(this,`_lexerTreeBuilder`,this.injector.get(r.LexerTreeBuilder)),p(this,`_univerInstanceService`,this.injector.get(t.IUniverInstanceService))}_getParseOptions(e,n){let r=n??(e?this._univerInstanceService.getUnit(e,t.UniverInstanceType.UNIVER_SHEET):void 0);return r?{locale:(0,t.getNumfmtLocaleTag)(r.getSnapshot().locale),dateSystem:r.getDateSystem()}:{dateSystem:t.DateSystem.Date1900}}async parseFormula(e,n,r,i,a){let o=await this._customFormulaService.getCellFormulaValue(n,r,e.uid,i,a),s=await this._customFormulaService.getCellFormula2Value(n,r,e.uid,i,a),{formula1:c,formula2:l}=e,u=y(String(o==null?void 0:o.v))&&y(String(s==null?void 0:s.v));return{formula1:z((0,t.isFormulaString)(c)?o==null?void 0:o.v:c,this._getParseOptions(n)),formula2:z((0,t.isFormulaString)(l)?s==null?void 0:s.v:l,this._getParseOptions(n)),isFormulaValid:u}}async isValidType(e){let{interceptValue:n,value:r}=e,i=this._getParseOptions(e.unitId,e.workbook);return typeof r==`number`&&typeof n==`string`||typeof n==`string`?!!t.numfmt.parseDate(n,i):!1}_validatorSingleFormula(e,n){return!t.Tools.isBlank(e)&&((0,t.isFormulaString)(e)||!Number.isNaN(+e)||!!(e&&t.numfmt.parseDate(e,n)))}validatorFormula(e,t,n){let r=e.operator;if(!r)return{success:!0};let i=this._getParseOptions(t),a=this._validatorSingleFormula(e.formula1,i),o=this.localeService.t(`sheets-data-validation.validFail.date`);if(F.includes(r)){let t=this._validatorSingleFormula(e.formula2,i);return{success:a&&t,formula1:a?void 0:o,formula2:t?void 0:o}}return{success:a,formula1:a?void 0:o}}normalizeFormula(e,n,r){let{formula1:i,formula2:a,bizInfo:o}=e,s=this._getParseOptions(n),c=e=>{var n;if(!e)return e;let r=Number.isNaN(+e)?(n=t.numfmt.parseDate(e,s))==null?void 0:n.v:+e;if(typeof r!=`number`)return``;let i=(0,t.excelSerialToDateTimeParts)(r,{dateSystem:s.dateSystem??t.DateSystem.Date1900});if(!i)return``;let a=`${String(i.year).padStart(4,`0`)}-${B(i.month)}-${B(i.day)}`;return o!=null&&o.showTime?`${a} ${B(i.hours)}:${B(i.minutes)}:${B(i.seconds)}`:a};return{formula1:(0,t.isFormulaString)(i)?i:c(`${i}`),formula2:(0,t.isFormulaString)(a)?a:c(`${a}`)}}transform(e,t,n){let{value:r}=e;return{...e,value:z(r,this._getParseOptions(e.unitId,e.workbook))}}get operatorNames(){return this.operators.map(e=>this.localeService.t(ie[e]))}generateRuleName(e){if(!e.operator)return this.localeService.t(ae.NONE);let t=this.localeService.t(ae[e.operator]).replace(I,e.formula1??``).replace(L,e.formula2??``);return`${this.titleStr} ${t}`}generateRuleErrorMessage(e,t){if(!e.operator)return this.titleStr;let{transformedFormula1:n,transformedFormula2:r}=R(this._lexerTreeBuilder,e,t);return`${this.localeService.t(oe[e.operator]).replace(I,n??``).replace(L,r??``)}`}};function H(e){return+e}var ce=class extends M{constructor(...e){super(...e),p(this,`_customFormulaService`,this.injector.get(x)),p(this,`id`,t.DataValidationType.DECIMAL),p(this,`_lexerTreeBuilder`,this.injector.get(r.LexerTreeBuilder)),p(this,`title`,`sheets-data-validation.decimal.title`),p(this,`order`,20),p(this,`operators`,[t.DataValidationOperator.BETWEEN,t.DataValidationOperator.EQUAL,t.DataValidationOperator.GREATER_THAN,t.DataValidationOperator.GREATER_THAN_OR_EQUAL,t.DataValidationOperator.LESS_THAN,t.DataValidationOperator.LESS_THAN_OR_EQUAL,t.DataValidationOperator.NOT_BETWEEN,t.DataValidationOperator.NOT_EQUAL]),p(this,`scopes`,[`sheet`])}_isFormulaOrNumber(e){return!t.Tools.isBlank(e)&&((0,t.isFormulaString)(e)||!Number.isNaN(+e))}async isValidType(e,t,n){let{value:r}=e;return!Number.isNaN(H(r))}transform(e,t,n){let{value:r}=e;return{...e,value:H(r)}}_parseNumber(e){return e==null?NaN:+e}async parseFormula(e,n,r,i,a){let o=await this._customFormulaService.getCellFormulaValue(n,r,e.uid,i,a),s=await this._customFormulaService.getCellFormula2Value(n,r,e.uid,i,a),{formula1:c,formula2:l}=e,u=y(String(o==null?void 0:o.v))&&y(String(s==null?void 0:s.v));return{formula1:this._parseNumber((0,t.isFormulaString)(c)?o==null?void 0:o.v:c),formula2:this._parseNumber((0,t.isFormulaString)(l)?s==null?void 0:s.v:l),isFormulaValid:u}}validatorFormula(e,n,r){let i=e.operator;if(!i)return{success:!0};let a=t.Tools.isDefine(e.formula1)&&this._isFormulaOrNumber(e.formula1),o=t.Tools.isDefine(e.formula2)&&this._isFormulaOrNumber(e.formula2),s=F.includes(i),c=this.localeService.t(`sheets-data-validation.validFail.number`);return s?{success:a&&o,formula1:a?void 0:c,formula2:o?void 0:c}:{success:a,formula1:a?``:c}}generateRuleErrorMessage(e,t){if(!e.operator)return this.localeService.t(O.NONE).replace(`{TYPE}`,this.titleStr);let{transformedFormula1:n,transformedFormula2:r}=R(this._lexerTreeBuilder,e,t);return`${this.localeService.t(O[e.operator]).replace(I,n??``).replace(L,r??``)}`}};function le(e){if(!e)return[];let n=/* @__PURE__ */ new Set;for(let i=0,a=e.length;i<a;i++){let a=e[i];if(a)for(let e=0,i=a.length;e<i;e++){let i=a[e],o=S(i);if(o!=null){var r;if(typeof o!=`string`&&typeof(i==null?void 0:i.s)==`object`&&(r=i.s)!=null&&(r=r.n)!=null&&r.pattern){n.add(t.numfmt.format(i.s.n.pattern,o,{throws:!1}));continue}let e=typeof o==`string`?o:String(o);y(e)&&n.add(e)}}}return[...n]}let ue=[`if`,`indirect`,`choose`,`offset`];function de(e,n){if(!(0,t.isFormulaString)(e)||(0,r.isReferenceString)(e.slice(1)))return!0;let i=n.sequenceNodesBuilder(e);return i&&i.some(e=>typeof e==`object`&&e.nodeType===r.sequenceNodeType.FUNCTION&&ue.indexOf(e.token.toLowerCase())>-1)}function fe(e,n){let{formula1:i=``,ranges:a}=e;if((0,r.isReferenceString)(i.slice(1))){let e=(0,r.deserializeRangeWithSheet)(i.slice(1));if((!e.sheetName||e.sheetName===n)&&a.some(n=>t.Rectangle.intersects(n,e.range)))return!0}return!1}var U=class extends M{constructor(...e){super(...e),p(this,`formulaService`,this.injector.get(T)),p(this,`_lexer`,this.injector.get(r.LexerTreeBuilder)),p(this,`_univerInstanceService`,this.injector.get(t.IUniverInstanceService)),p(this,`_listCacheService`,this.injector.get(w)),p(this,`order`,50),p(this,`offsetFormulaByRange`,!1),p(this,`id`,t.DataValidationType.LIST),p(this,`title`,`sheets-data-validation.list.title`),p(this,`operators`,[]),p(this,`scopes`,[`sheet`]),p(this,`skipDefaultFontRender`,e=>e.renderMode!==t.DataValidationRenderMode.TEXT)}validatorFormula(e,n,r){var i;let a=!t.Tools.isBlank(e.formula1),o=de(e.formula1??``,this._lexer),s=fe(e,((i=this._univerInstanceService.getUnit(n,t.UniverInstanceType.UNIVER_SHEET))==null||(i=i.getSheetBySheetId(r))==null?void 0:i.getName())??``);return{success:!!(a&&o&&!s),formula1:a?o?s?this.localeService.t(`sheets-data-validation.validFail.listIntersects`):void 0:this.localeService.t(`sheets-data-validation.validFail.listInvalid`):this.localeService.t(`sheets-data-validation.validFail.list`)}}getExtraStyle(e,n,{style:r}){let i=(r.tb===t.WrapStrategy.OVERFLOW?t.WrapStrategy.CLIP:r.tb)??t.WrapStrategy.WRAP;if(e.type===t.DataValidationType.LIST&&(e.renderMode===t.DataValidationRenderMode.ARROW||e.renderMode===t.DataValidationRenderMode.TEXT)){let t=this.getListWithColorMap(e)[`${n??``}`];if(t)return{bg:{rgb:t},tb:i}}return{tb:i}}parseCellValue(e){let t=e.toString();return(0,i.deserializeListOptions)(t)}async parseFormula(e,t,n){var r;let i=await this.formulaService.getRuleFormulaResult(t,n,e.uid),a=_(i==null||(r=i[0])==null||(r=r.result)==null?void 0:r[0][0]);return{formula1:void 0,formula2:void 0,isFormulaValid:y(String(a))}}async isValidType(e,n,r){let{value:a,unitId:o,subUnitId:s}=e,{formula1:c=``}=r,l=(0,t.isFormulaString)(c)?this._listCacheService.getOrCompute(o,s,r).list:(0,i.deserializeListOptions)(c);return(r.type===t.DataValidationType.LIST?a==null||a===``?[]:[`${a}`]:this.parseCellValue(a)).every(e=>l.includes(e))}generateRuleName(){return this.localeService.t(`sheets-data-validation.list.name`)}generateRuleErrorMessage(){return this.localeService.t(`sheets-data-validation.list.error`)}_getUnitAndSubUnit(e,n){let r=(e?this._univerInstanceService.getUnit(e,t.UniverInstanceType.UNIVER_SHEET):void 0)??this._univerInstanceService.getCurrentUnitOfType(t.UniverInstanceType.UNIVER_SHEET);if(!r)return null;let i=(n?r.getSheetBySheetId(n):void 0)??r.getActiveSheet();return i?{unitId:r.getUnitId(),subUnitId:i.getSheetId()}:null}getList(e,t,n){let r=this._getUnitAndSubUnit(t,n);if(!r)return[];let{unitId:i,subUnitId:a}=r;return this._listCacheService.getOrCompute(i,a,e).list}async getListAsync(e,n,r){var a;let{formula1:o=``}=e,s=this._getUnitAndSubUnit(n,r);if(!s)return[];let{unitId:c,subUnitId:l}=s,u=await this.formulaService.getRuleFormulaResult(c,l,e.uid);return(0,t.isFormulaString)(o)?le(u==null||(a=u[0])==null||(a=a.result)==null?void 0:a[0][0]):(0,i.deserializeListOptions)(o)}getListWithColor(e,t,n){let r=this._getUnitAndSubUnit(t,n);if(!r)return[];let{unitId:i,subUnitId:a}=r;return this._listCacheService.getOrCompute(i,a,e).listWithColor}getListWithColorMap(e,t,n){let r=this._getUnitAndSubUnit(t,n);if(!r)return{};let{unitId:i,subUnitId:a}=r;return this._listCacheService.getOrCompute(i,a,e).colorMap}},pe=class extends M{constructor(...e){super(...e),p(this,`id`,t.DataValidationType.TEXT_LENGTH),p(this,`title`,`sheets-data-validation.textLength.title`),p(this,`_lexerTreeBuilder`,this.injector.get(r.LexerTreeBuilder)),p(this,`order`,30),p(this,`operators`,[t.DataValidationOperator.BETWEEN,t.DataValidationOperator.EQUAL,t.DataValidationOperator.GREATER_THAN,t.DataValidationOperator.GREATER_THAN_OR_EQUAL,t.DataValidationOperator.LESS_THAN,t.DataValidationOperator.LESS_THAN_OR_EQUAL,t.DataValidationOperator.NOT_BETWEEN,t.DataValidationOperator.NOT_EQUAL]),p(this,`scopes`,[`sheet`]),p(this,`_customFormulaService`,this.injector.get(x))}_isFormulaOrInt(e){return!t.Tools.isBlank(e)&&((0,t.isFormulaString)(e)||!Number.isNaN(+e)&&Number.isInteger(+e))}validatorFormula(e,n,r){let i=e.operator;if(!i)return{success:!1};let a=t.Tools.isDefine(e.formula1)&&this._isFormulaOrInt(e.formula1),o=t.Tools.isDefine(e.formula2)&&this._isFormulaOrInt(e.formula2),s=F.includes(i),c=this.localeService.t(`sheets-data-validation.validFail.number`);return s?{success:a&&o,formula1:a?void 0:c,formula2:o?void 0:c}:{success:a,formula1:c}}_parseNumber(e){return e==null?NaN:+e}async parseFormula(e,n,r,i,a){let o=await this._customFormulaService.getCellFormulaValue(n,r,e.uid,i,a),s=await this._customFormulaService.getCellFormula2Value(n,r,e.uid,i,a),{formula1:c,formula2:l}=e,u=y(String(o==null?void 0:o.v))&&y(String(s==null?void 0:s.v));return{formula1:this._parseNumber((0,t.isFormulaString)(c)?o==null?void 0:o.v:c),formula2:this._parseNumber((0,t.isFormulaString)(l)?s==null?void 0:s.v:l),isFormulaValid:u}}transform(e,t,n){return{...e,value:e.value.toString().length}}async isValidType(e,t,n){let{value:r}=e;return typeof r==`string`||typeof r==`number`}generateRuleErrorMessage(e,t){if(!e.operator)return this.titleStr;let{transformedFormula1:n,transformedFormula2:r}=R(this._lexerTreeBuilder,e,t);return`${this.localeService.t(ne[e.operator]).replace(I,n??``).replace(L,r??``)}`}};function W(e){var n;return e?e.p?!(((n=e.p.body)==null?void 0:n.dataStream)??``).slice(0,-2).trim():t.Tools.isBlank(e.v):!0}function G(e,a,o,s,c=`command`,u=!0){let d=s.get(r.LexerTreeBuilder),f=s.get(n.DataValidatorRegistryService),p=[],m=[],h=s.get(E),g=s.get(t.IUniverInstanceService),_=(0,i.getSheetCommandTarget)(g,{unitId:e,subUnitId:a});if(!_)return{redoMutations:p,undoMutations:m};let{worksheet:v}=_,y=new t.ObjectMatrix,x=!1;function S(e,n){u&&l(e,v).forEach(e=>{t.Range.foreach(e,(e,t)=>{let r=v.getCellRaw(e,t),i=C(r);(W(r)||i===n)&&!(r!=null&&r.p)&&(x=!0,y.setValue(e,t,{v:n,p:null}))})})}if(o.forEach(r=>{switch(r.type){case`delete`:p.push({id:n.RemoveDataValidationMutation.id,params:{unitId:e,subUnitId:a,ruleId:r.rule.uid,source:c}}),m.unshift({id:n.AddDataValidationMutation.id,params:{unitId:e,subUnitId:a,rule:r.rule,index:r.index,source:c}});break;case`update`:{if(b(r.rule.type,f)){let i=r.oldRanges[0].startRow,o=r.oldRanges[0].startColumn,s=r.newRanges[0].startRow,l=r.newRanges[0].startColumn,u=s-i,f=l-o,h=(0,t.isFormulaString)(r.rule.formula1)?d.moveFormulaRefOffset(r.rule.formula1,f,u):r.rule.formula1,g=(0,t.isFormulaString)(r.rule.formula2)?d.moveFormulaRefOffset(r.rule.formula2,f,u):r.rule.formula2;h!==r.rule.formula1||g!==r.rule.formula2||!(0,t.isRangesEqual)(r.newRanges,r.oldRanges)?(p.push({id:n.UpdateDataValidationMutation.id,params:{unitId:e,subUnitId:a,ruleId:r.ruleId,payload:{type:n.UpdateRuleType.ALL,payload:{formula1:h,formula2:g,ranges:r.newRanges}}}}),m.unshift({id:n.UpdateDataValidationMutation.id,params:{unitId:e,subUnitId:a,ruleId:r.ruleId,payload:{type:n.UpdateRuleType.ALL,payload:{formula1:r.rule.formula1,formula2:r.rule.formula2,ranges:r.oldRanges}}}})):(p.push({id:n.UpdateDataValidationMutation.id,params:{unitId:e,subUnitId:a,ruleId:r.ruleId,payload:{type:n.UpdateRuleType.RANGE,payload:r.newRanges},source:c}}),m.unshift({id:n.UpdateDataValidationMutation.id,params:{unitId:e,subUnitId:a,ruleId:r.ruleId,payload:{type:n.UpdateRuleType.RANGE,payload:r.oldRanges},source:c}}))}else p.push({id:n.UpdateDataValidationMutation.id,params:{unitId:e,subUnitId:a,ruleId:r.ruleId,payload:{type:n.UpdateRuleType.RANGE,payload:r.newRanges},source:c}}),m.unshift({id:n.UpdateDataValidationMutation.id,params:{unitId:e,subUnitId:a,ruleId:r.ruleId,payload:{type:n.UpdateRuleType.RANGE,payload:r.oldRanges},source:c}});let i=h.getRuleById(e,a,r.ruleId);if(i&&i.type===t.DataValidationType.CHECKBOX){let n=h.getValidator(t.DataValidationType.CHECKBOX).parseFormulaSync(i,e,a);S(r.newRanges,n.formula2)}break}case`add`:if(p.push({id:n.AddDataValidationMutation.id,params:{unitId:e,subUnitId:a,rule:r.rule,source:c}}),m.unshift({id:n.RemoveDataValidationMutation.id,params:{unitId:e,subUnitId:a,ruleId:r.rule.uid,source:c}}),r.rule.type===t.DataValidationType.CHECKBOX){let n=h.getValidator(t.DataValidationType.CHECKBOX).parseFormulaSync(r.rule,e,a);S(r.rule.ranges,n.originFormula2)}}}),x){let t={id:i.SetRangeValuesMutation.id,params:{unitId:e,subUnitId:a,cellValue:y.clone()}},n={id:i.SetRangeValuesMutation.id,params:(0,i.SetRangeValuesUndoMutationFactory)(s,t.params)};p.push(t),m.push(n)}return{redoMutations:p,undoMutations:m}}let K={type:t.CommandType.COMMAND,id:`sheet.command.updateDataValidationRuleRange`,handler(e,n){if(!n)return!1;let{unitId:r,subUnitId:i,ranges:a,ruleId:o}=n,s=e.get(E),c=e.get(t.ICommandService),l=e.get(t.IUndoRedoService);if(!s.getRuleById(r,i,o))return!1;let u=s.getRuleObjectMatrix(r,i).clone();u.updateRange(o,a);let{redoMutations:d,undoMutations:f}=G(r,i,u.diff(s.getRules(r,i)),e);return l.pushUndoRedo({undoMutations:f,redoMutations:d,unitID:r}),(0,t.sequenceExecute)(d,c),!0}},me={type:t.CommandType.COMMAND,id:`sheet.command.addDataValidation`,handler(e,r){if(!r)return!1;let{unitId:i,subUnitId:a,rule:o}=r,s=e.get(E),c=e.get(t.ICommandService),l=e.get(t.IUndoRedoService),u=s.getRuleObjectMatrix(i,a).clone();u.addRule(o);let d=u.diff(s.getRules(i,a)),f=s.getValidator(o.type),p={unitId:i,subUnitId:a,rule:{...o,...f==null?void 0:f.normalizeFormula(o,i,a)}},{redoMutations:m,undoMutations:h}=G(i,a,d,e);return m.push({id:n.AddDataValidationMutation.id,params:p}),h.unshift({id:n.RemoveDataValidationMutation.id,params:{unitId:i,subUnitId:a,ruleId:o.uid}}),l.pushUndoRedo({unitID:i,redoMutations:m,undoMutations:h}),(0,t.sequenceExecute)(m,c),!0}},he={type:t.CommandType.COMMAND,id:`sheets.command.update-data-validation-setting`,handler(e,r){if(!r)return!1;let a=e.get(t.ICommandService),o=e.get(t.IUndoRedoService),s=e.get(E),c=e.get(n.DataValidatorRegistryService),{unitId:u,subUnitId:d,ruleId:f,setting:p}=r,m=c.getValidatorItem(p.type);if(!m)return!1;let h=s.getRuleById(u,d,f);if(!h)return!1;let g={...h,...p};if(!m.validatorFormula(g,u,d).success)return!1;let _={unitId:u,subUnitId:d,ruleId:f,payload:{type:n.UpdateRuleType.SETTING,payload:{...p,...m.normalizeFormula(g,u,d)}}},v=[{id:n.UpdateDataValidationMutation.id,params:_}],y={unitId:u,subUnitId:d,ruleId:f,payload:{type:n.UpdateRuleType.SETTING,payload:(0,n.getRuleSetting)(h)}},b=[{id:n.UpdateDataValidationMutation.id,params:y}];if(p.type===t.DataValidationType.CHECKBOX){let n=h.ranges,r=e.get(t.IUniverInstanceService),a=(0,i.getSheetCommandTarget)(r,{unitId:u,subUnitId:d});if(a){let r=new t.ObjectMatrix,{worksheet:o}=a,{formula2:s=0,formula1:c=1}=h,{formula2:f=0,formula1:m=1}=p,g=!1;if(l(n,o).forEach(e=>{t.Range.foreach(e,(e,t)=>{let n=o.getCellRaw(e,t),i=C(n);(W(n)||i===String(s))&&!(n!=null&&n.p)?(r.setValue(e,t,{v:f,p:null}),g=!0):i===String(c)&&!(n!=null&&n.p)&&(r.setValue(e,t,{v:m,p:null}),g=!0)})}),g){let t={id:i.SetRangeValuesMutation.id,params:{unitId:u,subUnitId:d,cellValue:r.clone()}},n={id:i.SetRangeValuesMutation.id,params:(0,i.SetRangeValuesUndoMutationFactory)(e,t.params)};v.push(t),b.push(n)}}}return(0,t.sequenceExecute)(v,a).result?(o.pushUndoRedo({unitID:u,redoMutations:v,undoMutations:b}),!0):!1}},ge={type:t.CommandType.COMMAND,id:`sheets.command.update-data-validation-options`,handler(e,r){if(!r)return!1;let i=e.get(t.ICommandService),a=e.get(t.IUndoRedoService),o=e.get(E),{unitId:s,subUnitId:c,ruleId:l,options:u}=r,d=o.getRuleById(s,c,l);if(!d)return!1;let f={unitId:s,subUnitId:c,ruleId:l,payload:{type:n.UpdateRuleType.OPTIONS,payload:u}},p=[{id:n.UpdateDataValidationMutation.id,params:f}],m={unitId:s,subUnitId:c,ruleId:l,payload:{type:n.UpdateRuleType.OPTIONS,payload:(0,n.getRuleOptions)(d)}},h=[{id:n.UpdateDataValidationMutation.id,params:m}];return a.pushUndoRedo({unitID:s,redoMutations:p,undoMutations:h}),i.executeCommand(n.UpdateDataValidationMutation.id,f),!0}},_e={type:t.CommandType.COMMAND,id:`sheets.command.clear-range-data-validation`,handler(e,n){if(!n)return!1;let{unitId:r,subUnitId:a,ranges:o,types:s}=n,c=e.get(t.ICommandService),l=e.get(t.IUniverInstanceService),u=(0,i.getSheetCommandTarget)(l,{unitId:r,subUnitId:a}),d=e.get(E);if(!u)return!1;let f=e.get(t.IUndoRedoService),p=d.getRuleObjectMatrix(r,a).clone(),m=s?new Set(d.getRules(r,a).filter(e=>s.includes(e.type)).map(e=>e.uid)):void 0;p.removeRange(o,m);let{redoMutations:h,undoMutations:g}=G(r,a,p.diff(d.getRules(r,a)),e);return f.pushUndoRedo({unitID:r,redoMutations:h,undoMutations:g}),(0,t.sequenceExecute)(h,c).result}},ve={type:t.CommandType.COMMAND,id:`sheet.command.remove-all-data-validation`,handler(e,r){if(!r)return!1;let{unitId:i,subUnitId:a}=r,o=e.get(t.ICommandService),s=e.get(E),c=e.get(t.IUndoRedoService),l=[...s.getRules(i,a)],u={unitId:i,subUnitId:a,ruleId:l.map(e=>e.uid)},d=[{id:n.RemoveDataValidationMutation.id,params:u}],f=[{id:n.AddDataValidationMutation.id,params:{unitId:i,subUnitId:a,rule:l}}];return c.pushUndoRedo({redoMutations:d,undoMutations:f,unitID:i}),o.executeCommand(n.RemoveDataValidationMutation.id,u),!0}},ye=(e,t)=>{let r=e.get(E),{unitId:i,subUnitId:a,ruleId:o,source:s}=t;if(Array.isArray(o)){let e=o.map(e=>r.getRuleById(i,a,e)).filter(Boolean);return[{id:n.AddDataValidationMutation.id,params:{unitId:i,subUnitId:a,rule:e,source:s}}]}return[{id:n.AddDataValidationMutation.id,params:{unitId:i,subUnitId:a,rule:{...r.getRuleById(i,a,o)},index:r.getRuleIndex(i,a,o)}}]},be={type:t.CommandType.COMMAND,id:`sheet.command.remove-data-validation-rule`,handler(e,r){if(!r)return!1;let{unitId:i,subUnitId:a,ruleId:o}=r,s=e.get(t.ICommandService),c=e.get(t.IUndoRedoService),l=e.get(E),u=[{id:n.RemoveDataValidationMutation.id,params:r}],d=[{id:n.AddDataValidationMutation.id,params:{unitId:i,subUnitId:a,rule:{...l.getRuleById(i,a,o)},index:l.getRuleIndex(i,a,o)}}];return c.pushUndoRedo({undoMutations:d,redoMutations:u,unitID:r.unitId}),s.executeCommand(n.RemoveDataValidationMutation.id,r),!0}},xe=`SHEET_DATA_VALIDATION_PLUGIN`,q=class extends t.Disposable{constructor(e,t,n){super(),this._univerInstanceService=e,this._permissionService=t,this._lexerTreeBuilder=n}getFormulaRefCheck(e){let n=this._lexerTreeBuilder.sequenceNodesBuilder(e);if(!n)return!0;for(let e=0;e<n.length;e++){let s=n[e];if(typeof s==`string`)continue;let{token:l}=s,u=(0,r.deserializeRangeWithSheetWithCache)(l),d=this._univerInstanceService.getCurrentUnitOfType(t.UniverInstanceType.UNIVER_SHEET),f=d.getActiveSheet(),p=d.getUnitId();if(u.sheetName){if(f=d.getSheetBySheetName(u.sheetName),!f)return!1;let e=f==null?void 0:f.getSheetId();if(!this._permissionService.getPermissionPoint(new i.WorksheetViewPermission(p,e).id))return!1}if(!f)return!1;let m=c(u.range,f);if(!m)continue;let{startRow:h,endRow:g,startColumn:_,endColumn:v}=m;for(let e=h;e<=g;e++)for(let t=_;t<=v;t++){var a;let n=(a=f.getCell(e,t))==null||(a=a.selectionProtection)==null?void 0:a[0];if((n==null?void 0:n[o.UnitAction.View])===!1)return!1}}return!0}};q=h([m(0,t.IUniverInstanceService),m(1,t.IPermissionService),m(2,(0,t.Inject)(r.LexerTreeBuilder))],q);var Se=`@univerjs/sheets-data-validation`,Ce=`1.1.0`;let we={},J=class extends t.Disposable{constructor(e,t,n){super(),this._dataValidationModel=e,this._formulaRefRangeService=t,this._validatorRegistryService=n,p(this,`_disposableMap`,/* @__PURE__ */ new Map),p(this,`registerRule`,(e,t,n)=>{b(n.type,this._validatorRegistryService)&&this.register(e,t,n)}),this._initRefRange()}_getIdWithUnitId(e,t,n){return`${e}_${t}_${n}`}register(e,r,i){let a=i.ranges,o=i.formula1,s=i.formula2,c=this._formulaRefRangeService.registerRangeFormula(e,r,a,[o??``,s??``],c=>{if(c.length===0)return{undos:[{id:n.AddDataValidationMutation.id,params:{unitId:e,subUnitId:r,rule:i,source:`patched`}}],redos:[{id:n.RemoveDataValidationMutation.id,params:{unitId:e,subUnitId:r,ruleId:i.uid,source:`patched`}}]};let l=[],u=[],d=c[0];l.push({id:n.UpdateDataValidationMutation.id,params:{unitId:e,subUnitId:r,ruleId:i.uid,payload:{type:n.UpdateRuleType.ALL,payload:{ranges:d.ranges,formula1:d.formulas[0],formula2:d.formulas[1]}},source:`patched`}}),u.push({id:n.UpdateDataValidationMutation.id,params:{unitId:e,subUnitId:r,ruleId:i.uid,payload:{type:n.UpdateRuleType.ALL,payload:{ranges:a,formula1:o,formula2:s}},source:`patched`}});for(let a=1;a<c.length;a++){let o=c[a],s=(0,t.generateRandomId)();l.push({id:n.AddDataValidationMutation.id,params:{unitId:e,subUnitId:r,rule:{...i,uid:s,formula1:o.formulas[0],formula2:o.formulas[1],ranges:o.ranges},source:`patched`}}),u.push({id:n.RemoveDataValidationMutation.id,params:{unitId:e,subUnitId:r,ruleId:s,source:`patched`}})}return{undos:u,redos:l}}),l=this._getIdWithUnitId(e,r,i.uid);this._disposableMap.set(l,c)}_initRefRange(){let e=this._dataValidationModel.getAll();for(let[t,n]of e)for(let[e,r]of n)for(let n of r)this.registerRule(t,e,n);this.disposeWithMe(this._dataValidationModel.ruleChange$.subscribe(e=>{let{unitId:t,subUnitId:n,rule:r}=e;switch(e.type){case`add`:{let t=e.rule;this.registerRule(e.unitId,e.subUnitId,t);break}case`remove`:{let e=this._disposableMap.get(this._getIdWithUnitId(t,n,r.uid));e&&e.dispose();break}case`update`:{let r=e.rule,i=this._disposableMap.get(this._getIdWithUnitId(t,n,r.uid));i&&i.dispose(),this.registerRule(e.unitId,e.subUnitId,r);break}}})),this.disposeWithMe((0,t.toDisposable)(()=>{this._disposableMap.forEach(e=>{e.dispose()}),this._disposableMap.clear()}))}};J=h([m(0,(0,t.Inject)(E)),m(1,(0,t.Inject)(s.FormulaRefRangeService)),m(2,(0,t.Inject)(n.DataValidatorRegistryService))],J);let Y=class extends t.Disposable{constructor(e,t,n,r,i,a){super(),this._dataValidationModel=e,this._injector=t,this._refRangeService=n,this._dataValidationFormulaService=r,this._formulaRefRangeService=i,this._validatorRegistryService=a,p(this,`_disposableMap`,/* @__PURE__ */ new Map),p(this,`registerRule`,(e,t,n)=>{b(n.type,this._validatorRegistryService)||(this.register(e,t,n),this.registerFormula(e,t,n))}),this._initRefRange()}_getIdWithUnitId(e,t,n){return`${e}_${t}_${n}`}registerFormula(e,t,r){let i=r.uid,a=this._getIdWithUnitId(e,t,i),o=this._disposableMap.get(a)??/* @__PURE__ */ new Set,s=(a,o)=>{let s=this._dataValidationModel.getRuleById(e,t,i);if(!s)return{redos:[],undos:[]};let c=s[a];if(!c||c===o)return{redos:[],undos:[]};let l={unitId:e,subUnitId:t,ruleId:r.uid,payload:{type:n.UpdateRuleType.SETTING,payload:{type:s.type,formula1:s.formula1,formula2:s.formula2,[a]:o}},source:`patched`},u={unitId:e,subUnitId:t,ruleId:r.uid,payload:{type:n.UpdateRuleType.SETTING,payload:{type:s.type,formula1:s.formula1,formula2:s.formula2}},source:`patched`};return{redos:[{id:n.UpdateDataValidationMutation.id,params:l}],undos:[{id:n.UpdateDataValidationMutation.id,params:u}]}},c=this._dataValidationFormulaService.getRuleFormulaInfo(e,t,i);if(c){let[n,r]=c;if(n){let r=this._formulaRefRangeService.registerFormula(e,t,n.text,e=>s(`formula1`,e));o.add(()=>r.dispose())}if(r){let n=this._formulaRefRangeService.registerFormula(e,t,r.text,e=>s(`formula2`,e));o.add(()=>n.dispose())}}}register(e,r,a){let o=o=>{let s=[...a.ranges],c=s.map(e=>(0,i.handleCommonDefaultRangeChangeWithEffectRefCommands)(e,o)).filter(e=>!!e).flat();if((0,t.isRangesEqual)(c,s))return{redos:[],undos:[]};if(c.length){let t={unitId:e,subUnitId:r,ruleId:a.uid,payload:{type:n.UpdateRuleType.RANGE,payload:c},source:`patched`};return{redos:[{id:n.UpdateDataValidationMutation.id,params:t}],undos:[{id:n.UpdateDataValidationMutation.id,params:{unitId:e,subUnitId:r,ruleId:a.uid,payload:{type:n.UpdateRuleType.RANGE,payload:s},source:`patched`}}]}}{let t={unitId:e,subUnitId:r,ruleId:a.uid};return{redos:[{id:n.RemoveDataValidationMutation.id,params:t}],undos:ye(this._injector,t)}}},s=[];a.ranges.forEach(t=>{let n=this._refRangeService.registerRefRange(t,o,e,r);s.push(()=>n.dispose())});let c=this._getIdWithUnitId(e,r,a.uid),l=this._disposableMap.get(c)??/* @__PURE__ */ new Set;l.add(()=>s.forEach(e=>e())),this._disposableMap.set(c,l)}_initRefRange(){let e=this._dataValidationModel.getAll();for(let[t,n]of e)for(let[e,r]of n)for(let n of r)this.registerRule(t,e,n);this.disposeWithMe(this._dataValidationModel.ruleChange$.subscribe(e=>{let{unitId:t,subUnitId:n,rule:r}=e;switch(e.type){case`add`:{let t=e.rule;this.registerRule(e.unitId,e.subUnitId,t);break}case`remove`:{let e=this._disposableMap.get(this._getIdWithUnitId(t,n,r.uid));e&&e.forEach(e=>e());break}case`update`:{let r=e.rule,i=this._disposableMap.get(this._getIdWithUnitId(t,n,r.uid));i&&i.forEach(e=>e()),this.registerRule(e.unitId,e.subUnitId,r);break}}})),this.disposeWithMe((0,t.toDisposable)(()=>{this._disposableMap.forEach(e=>{e.forEach(e=>e())}),this._disposableMap.clear()}))}};Y=h([m(0,(0,t.Inject)(E)),m(1,(0,t.Inject)(t.Injector)),m(2,(0,t.Inject)(i.RefRangeService)),m(3,(0,t.Inject)(T)),m(4,(0,t.Inject)(s.FormulaRefRangeService)),m(5,(0,t.Inject)(n.DataValidatorRegistryService))],Y);let X=class extends t.Disposable{constructor(e,t,n){super(),this._sheetInterceptorService=e,this._univerInstanceService=t,this._sheetDataValidationModel=n,this._initSheetChange()}_initSheetChange(){this.disposeWithMe(this._sheetInterceptorService.interceptCommand({getMutations:e=>{if(e.id===i.RemoveSheetCommand.id){var r;let i=e.params,a=i.unitId||this._univerInstanceService.getCurrentUnitOfType(t.UniverInstanceType.UNIVER_SHEET).getUnitId(),o=this._univerInstanceService.getUnit(a,t.UniverInstanceType.UNIVER_SHEET);if(!o)return{redos:[],undos:[]};let s=i.subUnitId||((r=o.getActiveSheet())==null?void 0:r.getSheetId());if(!s)return{redos:[],undos:[]};let c=this._sheetDataValidationModel.getRules(a,s);if(c.length===0)return{redos:[],undos:[]};let l={unitId:a,subUnitId:s,ruleId:c.map(e=>e.uid),source:`patched`},u={unitId:a,subUnitId:s,rule:[...c],source:`patched`};return{redos:[{id:n.RemoveDataValidationMutation.id,params:l}],undos:[{id:n.AddDataValidationMutation.id,params:u}]}}if(e.id===i.CopySheetCommand.id){let{unitId:r,subUnitId:i,targetSubUnitId:a}=e.params;if(!r||!i||!a)return{redos:[],undos:[]};let o=this._sheetDataValidationModel.getRules(r,i);if(o.length===0)return{redos:[],undos:[]};let s=o.map(e=>({...e,uid:(0,t.generateRandomId)(6)}));return{redos:[{id:n.AddDataValidationMutation.id,params:{unitId:r,subUnitId:a,rule:s,source:`patched`}}],undos:[{id:n.RemoveDataValidationMutation.id,params:{unitId:r,subUnitId:a,ruleId:s.map(e=>e.uid),source:`patched`}}]}}return{redos:[],undos:[]}}}))}};X=h([m(0,(0,t.Inject)(i.SheetInterceptorService)),m(1,(0,t.Inject)(t.IUniverInstanceService)),m(2,(0,t.Inject)(E))],X);var Te=class extends M{constructor(...e){super(...e),p(this,`id`,t.DataValidationType.ANY),p(this,`title`,`sheets-data-validation.any.title`),p(this,`operators`,[]),p(this,`scopes`,[`sheet`]),p(this,`order`,0),p(this,`offsetFormulaByRange`,!1)}async parseFormula(e,t,n){return{formula1:e.formula1,formula2:e.formula2,isFormulaValid:!0}}validatorFormula(e,t,n){return{success:!0}}async isValidType(e,t,n){return!0}generateRuleErrorMessage(e){return this.localeService.t(`sheets-data-validation.any.error`)}},Ee=class extends M{constructor(...e){super(...e),p(this,`id`,t.DataValidationType.CUSTOM),p(this,`title`,`sheets-data-validation.custom.title`),p(this,`operators`,[]),p(this,`scopes`,[`sheet`]),p(this,`order`,60),p(this,`_customFormulaService`,this.injector.get(x)),p(this,`_lexerTreeBuilder`,this.injector.get(r.LexerTreeBuilder))}validatorFormula(e,n,i){let a=(0,t.isFormulaString)(e.formula1),o=e.formula1??``,s=this._lexerTreeBuilder.checkIfAddBracket(o)===0&&o.startsWith(r.operatorToken.EQUALS);return{success:a&&s,formula1:a&&s?``:this.localeService.t(`sheets-data-validation.validFail.formula`)}}async parseFormula(e,t,n){return{formula1:void 0,formula2:void 0,isFormulaValid:!0}}async isValidType(e,n,r){let{column:i,row:a,unitId:o,subUnitId:s}=e,c=await this._customFormulaService.getCellFormulaValue(o,s,r.uid,a,i),l=c==null?void 0:c.v;return y(String(l))&&t.Tools.isDefine(l)&&l!==``?c.t===t.CellValueType.BOOLEAN?!!l:typeof l==`boolean`?l:typeof l==`number`?!!l:typeof l==`string`?y(l):!!l:!1}generateRuleErrorMessage(e){return this.localeService.t(`sheets-data-validation.custom.error`)}generateRuleName(e){return this.localeService.t(`sheets-data-validation.custom.ruleName`).replace(`{FORMULA1}`,e.formula1??``)}},De=class extends U{constructor(...e){super(...e),p(this,`id`,t.DataValidationType.LIST_MULTIPLE),p(this,`title`,`sheets-data-validation.listMultiple.title`),p(this,`offsetFormulaByRange`,!1),p(this,`skipDefaultFontRender`,()=>!0)}},Oe=class extends M{constructor(...e){super(...e),p(this,`_customFormulaService`,this.injector.get(x)),p(this,`_lexerTreeBuilder`,this.injector.get(r.LexerTreeBuilder)),p(this,`id`,t.DataValidationType.WHOLE),p(this,`title`,`sheets-data-validation.whole.title`),p(this,`order`,10),p(this,`operators`,[t.DataValidationOperator.BETWEEN,t.DataValidationOperator.EQUAL,t.DataValidationOperator.GREATER_THAN,t.DataValidationOperator.GREATER_THAN_OR_EQUAL,t.DataValidationOperator.LESS_THAN,t.DataValidationOperator.LESS_THAN_OR_EQUAL,t.DataValidationOperator.NOT_BETWEEN,t.DataValidationOperator.NOT_EQUAL]),p(this,`scopes`,[`sheet`])}_isFormulaOrInt(e){return!t.Tools.isBlank(e)&&((0,t.isFormulaString)(e)||!Number.isNaN(+e)&&Number.isInteger(+e))}async isValidType(e,t,n){let{value:r}=e,i=H(r);return!Number.isNaN(i)&&Number.isInteger(i)}transform(e,t,n){let{value:r}=e;return{...e,value:H(r)}}_parseNumber(e){return e==null?NaN:+e}async parseFormula(e,n,r,i,a){let o=await this._customFormulaService.getCellFormulaValue(n,r,e.uid,i,a),s=await this._customFormulaService.getCellFormula2Value(n,r,e.uid,i,a),{formula1:c,formula2:l}=e,u=(0,t.isFormulaString)(c)?o==null?void 0:o.v:c,d=(0,t.isFormulaString)(l)?s==null?void 0:s.v:l,f=y(`${u}`)&&y(`${d}`);return{formula1:this._parseNumber(u),formula2:this._parseNumber(d),isFormulaValid:f}}validatorFormula(e,n,r){let i=e.operator;if(!i)return{success:!0};let a=t.Tools.isDefine(e.formula1)&&this._isFormulaOrInt(e.formula1),o=t.Tools.isDefine(e.formula2)&&this._isFormulaOrInt(e.formula2),s=F.includes(i),c=this.localeService.t(`sheets-data-validation.validFail.number`);return s?{success:a&&o,formula1:a?void 0:c,formula2:o?void 0:c}:{success:a,formula1:c}}generateRuleErrorMessage(e,t){if(!e.operator)return this.localeService.t(O.NONE).replace(`{TYPE}`,this.titleStr);let{transformedFormula1:n,transformedFormula2:r}=R(this._lexerTreeBuilder,e,t);return`${this.localeService.t(O[e.operator]).replace(I,n??``).replace(L,r??``)}`}};let Z=class extends t.RxDisposable{constructor(e,t,n,r,i,a){super(),this._univerInstanceService=e,this._dataValidatorRegistryService=t,this._injector=n,this._selectionManagerService=r,this._sheetInterceptorService=i,this._sheetDataValidationModel=a,this._init()}_init(){this._registerValidators(),this._initCommandInterceptor()}_registerValidators(){[Te,ce,Oe,pe,V,re,U,De,Ee].forEach(e=>{let n=this._injector.createInstance(e);this.disposeWithMe(this._dataValidatorRegistryService.register(n)),this.disposeWithMe((0,t.toDisposable)(()=>this._injector.delete(e)))})}_initCommandInterceptor(){this._sheetInterceptorService.interceptCommand({getMutations:e=>{if(e.id===i.ClearSelectionAllCommand.id){var n;let e=this._univerInstanceService.getCurrentUnitOfType(t.UniverInstanceType.UNIVER_SHEET),r=e.getUnitId(),i=e.getActiveSheet();if(!i)throw Error(`No active sheet found`);let a=i.getSheetId(),o=(n=this._selectionManagerService.getCurrentSelections())==null?void 0:n.map(e=>e.range),s=this._sheetDataValidationModel.getRuleObjectMatrix(r,a).clone();o&&s.removeRange(o);let{redoMutations:c,undoMutations:l}=G(r,a,s.diff(this._sheetDataValidationModel.getRules(r,a)),this._injector,`patched`);return{undos:l,redos:c}}return{undos:[],redos:[]}}})}};Z=h([m(0,t.IUniverInstanceService),m(1,(0,t.Inject)(n.DataValidatorRegistryService)),m(2,(0,t.Inject)(t.Injector)),m(3,(0,t.Inject)(i.SheetsSelectionsService)),m(4,(0,t.Inject)(i.SheetInterceptorService)),m(5,(0,t.Inject)(E))],Z);let Q=class extends t.Disposable{constructor(e,t,n,r){super(),this._univerInstanceService=e,this._sheetDataValidationModel=t,this._dataValidationCacheService=n,this._lifecycleService=r,this._initRecalculate()}_initRecalculate(){let e=e=>{if(e.length===0)return;let n=this._univerInstanceService.getCurrentUnitOfType(t.UniverInstanceType.UNIVER_SHEET),r=n==null?void 0:n.getActiveSheet(),i=Object.create(null);e.flat().forEach(e=>{i[e.unitId]||(i[e.unitId]=Object.create(null)),i[e.unitId][e.subUnitId]||(i[e.unitId][e.subUnitId]=[]);let n=this._univerInstanceService.getUnit(e.unitId,t.UniverInstanceType.UNIVER_SHEET),r=n==null?void 0:n.getSheetBySheetId(e.subUnitId);r&&i[e.unitId][e.subUnitId].push(...l(e.ranges,r))}),Object.entries(i).forEach(([e,t])=>{Object.entries(t).forEach(([t,i])=>{if((n==null?void 0:n.getUnitId())===e&&(r==null?void 0:r.getSheetId())===t)this.validatorRanges(e,t,i).catch(console.error);else{let n,r=this.disposeWithMe(()=>cancelIdleCallback(n));n=requestIdleCallback(()=>{r.dispose(),this.validatorRanges(e,t,i).catch(console.error)})}})})};this.disposeWithMe(this._dataValidationCacheService.dirtyRanges$.pipe((0,a.bufferWhen)(()=>this._lifecycleService.lifecycle$.pipe((0,a.skip)(1),(0,a.filter)(e=>e===t.LifecycleStages.Rendered)))).subscribe(e)),this.disposeWithMe(this._dataValidationCacheService.dirtyRanges$.pipe((0,a.filter)(()=>this._lifecycleService.stage>=t.LifecycleStages.Rendered),(0,t.bufferDebounceTime)(20)).subscribe(e))}async _validatorByCell(e,n,r,i){let a=e.getUnitId(),o=n.getSheetId();if(!t.Tools.isDefine(r)||!t.Tools.isDefine(i))throw Error(`row or col is not defined, row: ${r}, col: ${i}`);let s=r,c=i,l=n.getMergedCell(r,i);l&&(s=l.startRow,c=l.startColumn);let u=this._sheetDataValidationModel.getRuleByLocation(a,o,s,c);return u?new Promise(t=>{this._sheetDataValidationModel.validator(u,{unitId:a,subUnitId:o,row:s,col:c,worksheet:n,workbook:e},e=>{t(e)})}):t.DataValidationStatus.VALID}async validatorCell(e,n,r,i){let a=this._univerInstanceService.getUnit(e,t.UniverInstanceType.UNIVER_SHEET);if(!a)throw Error(`cannot find current workbook, unitId: ${e}`);let o=a.getSheetBySheetId(n);if(!o)throw Error(`cannot find current worksheet, sheetId: ${n}`);return this._validatorByCell(a,o,r,i)}async validatorRanges(e,n,r){if(!r.length)return Promise.resolve([]);let i=this._univerInstanceService.getUnit(e,t.UniverInstanceType.UNIVER_SHEET);if(!i)throw Error(`cannot find current workbook, unitId: ${e}`);let a=i.getSheetBySheetId(n);if(!a)throw Error(`cannot find current worksheet, sheetId: ${n}`);let o=l(r,a),s=l(this._sheetDataValidationModel.getRules(e,n).flatMap(e=>e.ranges),a),c=[];for(let e of o)for(let n of s){let r=(0,t.getIntersectRange)(e,n);r&&c.push(r)}let u=[],d=await Promise.all(c.map((e,t)=>{let n=[];for(let r=e.startRow;r<=e.endRow;r++)for(let o=e.startColumn;o<=e.endColumn;o++){n.push(this._validatorByCell(i,a,r,o));let e=a.getMergedCell(r,o);e&&u.push({resultRowIndex:t,resultColIndex:n.length-1,row:e.startRow,col:e.startColumn})}return Promise.all(n)}));return u.length&&u.forEach(({resultRowIndex:r,resultColIndex:i,row:a,col:o})=>{d[r][i]===t.DataValidationStatus.VALIDATING&&(d[r][i]=this._dataValidationCacheService.getValue(e,n,a,o)??t.DataValidationStatus.VALID)}),d}async validatorWorksheet(e,n){let r=this._univerInstanceService.getUnit(e,t.UniverInstanceType.UNIVER_SHEET);if(!r)throw Error(`cannot find current workbook, unitId: ${e}`);let i=r.getSheetBySheetId(n);if(!i)throw Error(`cannot find current worksheet, sheetId: ${n}`);let a=l(this._sheetDataValidationModel.getRules(e,n).flatMap(e=>e.ranges),i);return await Promise.all(a.map(e=>{let n=[];return t.Range.foreach(e,(e,t)=>{n.push(this._validatorByCell(r,i,e,t))}),Promise.all(n)})),this._dataValidationCacheService.ensureCache(e,n)}async validatorWorkbook(e){let t=this._sheetDataValidationModel.getSubUnitIds(e),n=await Promise.all(t.map(t=>this.validatorWorksheet(e,t))),r=Object.create(null);return n.forEach((e,n)=>{r[t[n]]=e}),r}getDataValidations(e,n,r){var i;let a=(i=this._univerInstanceService.getUnit(e,t.UniverInstanceType.UNIVER_SHEET))==null?void 0:i.getSheetBySheetId(n);if(!a)return[];let o=this._sheetDataValidationModel.getRuleObjectMatrix(e,n),s=/* @__PURE__ */ new Set;l(r,a).forEach(e=>{t.Range.foreach(e,(e,t)=>{let n=o.getValue(e,t);n&&s.add(n)})});let c=[];for(let t of s){let r=this._sheetDataValidationModel.getRuleById(e,n,t);r&&c.push(r)}return c}getDataValidation(e,t,n){return this.getDataValidations(e,t,n)[0]}};Q=h([m(0,t.IUniverInstanceService),m(1,(0,t.Inject)(E)),m(2,(0,t.Inject)(g)),m(3,(0,t.Inject)(t.LifecycleService))],Q);let $=class extends t.Plugin{constructor(e=we,n,r,i){super(),this._config=e,this._injector=n,this._commandService=r,this._configService=i;let{...a}=(0,t.merge)({},we,this._config);this._configService.setConfig(`sheets-data-validation.config`,a)}onStarting(){[[g],[w],[T],[x],[Q],[E],[Z],[q],[X],[Y],[J]].forEach(e=>{this._injector.add(e)}),[me,K,he,ge,be,ve,_e].forEach(e=>{this._commandService.registerCommand(e)}),this._injector.get(g),this._injector.get(Q),this._injector.get(Z),this._injector.get(J),this._injector.get(Y)}onReady(){this._injector.get(X)}onRendered(){this._injector.get(q)}};p($,`pluginName`,xe),p($,`packageName`,Se),p($,`version`,Ce),p($,`type`,t.UniverInstanceType.UNIVER_SHEET),$=h([(0,t.DependentOn)(n.UniverDataValidationPlugin,r.UniverFormulaEnginePlugin,i.UniverSheetsPlugin,s.UniverSheetsFormulaPlugin),m(1,(0,t.Inject)(t.Injector)),m(2,t.ICommandService),m(3,t.IConfigService)],$);function ke(e){let n=e.get(i.SheetsSelectionsService).getCurrentSelections().map(e=>e.range);return{uid:(0,t.generateRandomId)(6),type:t.DataValidationType.DECIMAL,operator:t.DataValidationOperator.EQUAL,formula1:`100`,ranges:n??[{startColumn:0,endColumn:0,startRow:0,endRow:0}]}}e.AddSheetDataValidationCommand=me,e.BASE_FORMULA_INPUT_NAME=`sheets-data-validation.formula-input`,e.CHECKBOX_FORMULA_1=1,e.CHECKBOX_FORMULA_2=0,e.CHECKBOX_FORMULA_INPUT_NAME=`sheets-data-validation.checkbox-formula-input`,e.CUSTOM_FORMULA_INPUT_NAME=`sheets-data-validation.custom-formula-input`,e.CheckboxValidator=re,e.ClearRangeDataValidationCommand=_e,e.DATA_VALIDATION_PLUGIN_NAME=xe,Object.defineProperty(e,"DataValidationCacheService",{enumerable:!0,get:function(){return g}}),Object.defineProperty(e,"DataValidationCustomFormulaService",{enumerable:!0,get:function(){return x}}),Object.defineProperty(e,"DataValidationFormulaController",{enumerable:!0,get:function(){return q}}),Object.defineProperty(e,"DataValidationFormulaService",{enumerable:!0,get:function(){return T}}),Object.defineProperty(e,"DataValidationListCacheService",{enumerable:!0,get:function(){return w}}),e.DateValidator=V,e.LIST_FORMULA_INPUT_NAME=`sheets-data-validation.list-formula-input`,e.ListMultipleValidator=De,e.ListValidator=U,e.RemoveSheetAllDataValidationCommand=ve,e.RemoveSheetDataValidationCommand=be,Object.defineProperty(e,"SheetDataValidationModel",{enumerable:!0,get:function(){return E}}),Object.defineProperty(e,"SheetsDataValidationValidatorService",{enumerable:!0,get:function(){return Q}}),Object.defineProperty(e,"UniverSheetsDataValidationPlugin",{enumerable:!0,get:function(){return $}}),e.UpdateSheetDataValidationOptionsCommand=ge,e.UpdateSheetDataValidationRangeCommand=K,e.UpdateSheetDataValidationSettingCommand=he,e.createDefaultNewRule=ke,e.getCellValueNumber=H,e.getCellValueOrigin=S,e.getDataValidationCellValue=se,e.getDataValidationDiffMutations=G,e.getFormulaCellData=v,e.getFormulaResult=_,e.getRangesInWorksheet=l,e.getTransformedFormula=R,e.isLegalFormulaResult=y,e.transformCheckboxValue=P});
1
+ (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("@univerjs/data-validation"),require("@univerjs/engine-formula"),require("@univerjs/sheets"),require("rxjs"),require("@univerjs/protocol"),require("@univerjs/sheets-formula")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`@univerjs/data-validation`,`@univerjs/engine-formula`,`@univerjs/sheets`,`rxjs`,`@univerjs/protocol`,`@univerjs/sheets-formula`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverSheetsDataValidation={},e.UniverCore,e.UniverDataValidation,e.UniverEngineFormula,e.UniverSheets,e.rxjs,e.UniverProtocol,e.UniverSheetsFormula))})(this,function(e,t,n,r,i,a,o,s){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});function c(e,n){let r=n.getMaxRows(),i=n.getMaxColumns();if(r<=0||i<=0)return null;let a=(0,t.getIntersectRange)(t.Range.transformRange(e,n),{startRow:0,endRow:r-1,startColumn:0,endColumn:i-1});return a?{startRow:a.startRow,endRow:a.endRow,startColumn:a.startColumn,endColumn:a.endColumn}:null}function l(e,t){let n=[];return e.forEach(e=>{let r=c(e,t);r&&n.push(r)}),n}function u(e){"@babel/helpers - typeof";return u=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},u(e)}function d(e,t){if(u(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(u(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function f(e){var t=d(e,`string`);return u(t)==`symbol`?t:t+``}function p(e,t,n){return(t=f(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function m(e,t){return function(n,r){t(n,r,e)}}function h(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let g=class extends t.Disposable{constructor(e,t,n){super(),this._commandService=e,this._univerInstanceService=t,this._sheetDataValidationModel=n,p(this,`_cacheMatrix`,/* @__PURE__ */ new Map),p(this,`_dirtyRanges$`,new a.Subject),p(this,`dirtyRanges$`,this._dirtyRanges$.asObservable()),this._initDirtyRanges(),this._initSheetRemove()}_initDirtyRanges(){this.disposeWithMe(this._commandService.onCommandExecuted((e,n)=>{if(e.id===i.SetRangeValuesMutation.id&&!(n!=null&&n.onlyLocal)){let{cellValue:n,unitId:r,subUnitId:i}=e.params;if(n){let e=new t.ObjectMatrix(n).getDataRange();if(e.endRow===-1)return;let a=this._sheetDataValidationModel.getRules(r,i),o=[];for(let e of a)o.push(...e.ranges);let s=[];for(let n of o){let r=(0,t.getIntersectRange)(n,e);r&&s.push(r)}s.length&&this.markRangeDirty(r,i,s,!0)}}}))}_initSheetRemove(){this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(e.id===i.RemoveSheetMutation.id){var t;let{unitId:n,subUnitId:r}=e.params;(t=this._cacheMatrix.get(n))==null||t.delete(r)}})),this.disposeWithMe(this._univerInstanceService.unitDisposed$.subscribe(e=>{e.type===t.UniverInstanceType.UNIVER_SHEET&&this._cacheMatrix.delete(e.getUnitId())}))}_ensureCache(e,n){let r=this._cacheMatrix.get(e);r||(r=/* @__PURE__ */ new Map,this._cacheMatrix.set(e,r));let i=r.get(n);return i||(i=new t.ObjectMatrix,r.set(n,i)),i}ensureCache(e,t){return this._ensureCache(e,t)}addRule(e,t,n){this.markRangeDirty(e,t,n.ranges)}removeRule(e,t,n){this._deleteRange(e,t,n.ranges)}_getEffectiveRanges(e,n,r){var i;let a=(i=this._univerInstanceService.getUnit(e,t.UniverInstanceType.UNIVER_SHEET))==null?void 0:i.getSheetBySheetId(n);return a?l(r,a):[]}markRangeDirty(e,n,r,i){let a=this._ensureCache(e,n);this._getEffectiveRanges(e,n,r).forEach(e=>{t.Range.foreach(e,(e,t)=>{a.getValue(e,t)!==void 0&&a.setValue(e,t,void 0)})}),this._dirtyRanges$.next({unitId:e,subUnitId:n,ranges:r,isSetRange:i})}_deleteRange(e,n,r){let i=this._ensureCache(e,n);this._getEffectiveRanges(e,n,r).forEach(e=>{t.Range.foreach(e,(e,t)=>{i.realDeleteValue(e,t)})}),this._dirtyRanges$.next({unitId:e,subUnitId:n,ranges:r})}getValue(e,t,n,r){return this._ensureCache(e,t).getValue(n,r)}};g=h([m(0,(0,t.Inject)(t.ICommandService)),m(1,(0,t.Inject)(t.IUniverInstanceService)),m(2,(0,t.Inject)(n.DataValidationModel))],g);function _(e){var t;return e==null||(t=e[0])==null||(t=t[0])==null?void 0:t.v}function v(e){var t;return e==null||(t=e[0])==null?void 0:t[0]}function y(e){return!r.ERROR_TYPE_SET.has(e)}function b(e,t){let n=t.getValidatorItem(e);return(n==null?void 0:n.offsetFormulaByRange)??!1}let x=class extends t.Disposable{constructor(e,t,n,r,i){super(),this._instanceSrv=e,this._registerOtherFormulaService=t,this._dataValidationModel=n,this._dataValidationCacheService=r,this._validatorRegistryService=i,p(this,`_ruleFormulaMap`,/* @__PURE__ */ new Map),p(this,`_ruleFormulaMap2`,/* @__PURE__ */ new Map),this._initFormulaResultHandler(),this._initDirtyRanges()}dispose(){super.dispose(),this._ruleFormulaMap.clear(),this._ruleFormulaMap2.clear()}_initFormulaResultHandler(){this.disposeWithMe(this._registerOtherFormulaService.formulaResult$.subscribe(e=>{for(let n in e){let r=e[n];if(this._instanceSrv.getUnitType(n)===t.UniverInstanceType.UNIVER_SHEET)for(let e in r){let t=r[e],{ruleFormulaMap:i}=this._ensureMaps(n,e);t.forEach(t=>{var r,a;let o=i.get((r=t.extra)==null?void 0:r.ruleId),s=this._dataValidationModel.getRuleById(n,e,(a=t.extra)==null?void 0:a.ruleId);s&&o&&this._dataValidationCacheService.markRangeDirty(n,e,s.ranges)})}}}))}_ensureMaps(e,t){let n=this._ruleFormulaMap.get(e),r=this._ruleFormulaMap2.get(e);n||(n=/* @__PURE__ */ new Map,this._ruleFormulaMap.set(e,n)),r||(r=/* @__PURE__ */ new Map,this._ruleFormulaMap2.set(e,r));let i=n.get(t);i||(i=/* @__PURE__ */ new Map,n.set(t,i));let a=r.get(t);return a||(a=/* @__PURE__ */ new Map,r.set(t,a)),{ruleFormulaMap:i,ruleFormulaMap2:a}}_registerFormula(e,t,n,i,a){return this._registerOtherFormulaService.registerFormulaWithRange(e,t,i,a,{ruleId:n},r.OtherFormulaBizType.DATA_VALIDATION_CUSTOM,n)}_handleDirtyRanges(e,n,r){this._dataValidationModel.getRules(e,n).forEach(i=>{let a=i.ranges;t.Rectangle.doAnyRangesIntersect(a,r)&&this.makeRuleDirty(e,n,i.uid)})}_initDirtyRanges(){this.disposeWithMe(this._dataValidationCacheService.dirtyRanges$.subscribe(e=>{e.isSetRange&&this._handleDirtyRanges(e.unitId,e.subUnitId,e.ranges)}))}deleteByRuleId(e,t,n){let{ruleFormulaMap:r,ruleFormulaMap2:i}=this._ensureMaps(e,t),a=this._dataValidationModel.getRuleById(e,t,n),o=r.get(n);if(!a||!o)return;let s=r.get(n);s&&(r.delete(n),this._registerOtherFormulaService.deleteFormula(e,t,[s.formulaId]));let c=i.get(n);c&&(i.delete(n),this._registerOtherFormulaService.deleteFormula(e,t,[c.formulaId]))}_addFormulaByRange(e,n,r,i,a,o){var s;let{ruleFormulaMap:c,ruleFormulaMap2:u}=this._ensureMaps(e,n),d=(s=this._instanceSrv.getUnit(e,t.UniverInstanceType.UNIVER_SHEET))==null?void 0:s.getSheetBySheetId(n);if(!d)return;let f=l(o,d);if(!f.length)return;let p=f[0].startRow,m=f[0].startColumn;if(i&&(0,t.isFormulaString)(i)){let t=this._registerFormula(e,n,r,i,f);c.set(r,{formula:i,originCol:m,originRow:p,formulaId:t})}if(a&&(0,t.isFormulaString)(a)){let t=this._registerFormula(e,n,r,a,f);u.set(r,{formula:a,originCol:m,originRow:p,formulaId:t})}}addRule(e,t,n){if(b(n.type,this._validatorRegistryService)){let{ranges:r,formula1:i,formula2:a,uid:o}=n;this._addFormulaByRange(e,t,o,i,a,r)}}async getCellFormulaValue(e,t,n,r,i){var a;let{ruleFormulaMap:o}=this._ensureMaps(e,t),s=o.get(n);if(!s)return Promise.resolve(void 0);let c=await this._registerOtherFormulaService.getFormulaValue(e,t,s.formulaId),{originRow:l,originCol:u}=s,d=r-l,f=i-u;return v(c==null||(a=c.result)==null||(a=a[d])==null?void 0:a[f])}async getCellFormula2Value(e,t,n,r,i){var a;let{ruleFormulaMap2:o}=this._ensureMaps(e,t),s=o.get(n);if(!s)return Promise.resolve(void 0);let c=await this._registerOtherFormulaService.getFormulaValue(e,t,s.formulaId),{originRow:l,originCol:u}=s,d=r-l,f=i-u;return v(c==null||(a=c.result)==null||(a=a[d])==null?void 0:a[f])}getCellFormulaValueSync(e,t,n,r,i){var a;let{ruleFormulaMap:o}=this._ensureMaps(e,t),s=o.get(n);if(!s)return;let c=this._registerOtherFormulaService.getFormulaValueSync(e,t,s.formulaId),{originRow:l,originCol:u}=s,d=r-l,f=i-u;return v(c==null||(a=c.result)==null||(a=a[d])==null?void 0:a[f])}getCellFormula2ValueSync(e,t,n,r,i){var a;let{ruleFormulaMap2:o}=this._ensureMaps(e,t),s=o.get(n);if(!s)return;let c=this._registerOtherFormulaService.getFormulaValueSync(e,t,s.formulaId),{originRow:l,originCol:u}=s,d=r-l,f=i-u;return v(c==null||(a=c.result)==null||(a=a[d])==null?void 0:a[f])}getRuleFormulaInfo(e,t,n){let{ruleFormulaMap:r}=this._ensureMaps(e,t);return r.get(n)}makeRuleDirty(e,t,n){var r,i;let a=(r=this._ruleFormulaMap.get(e))==null||(r=r.get(t))==null?void 0:r.get(n),o=(i=this._ruleFormulaMap2.get(e))==null||(i=i.get(t))==null?void 0:i.get(n);a&&this._registerOtherFormulaService.markFormulaDirty(e,t,a.formulaId),o&&this._registerOtherFormulaService.markFormulaDirty(e,t,o.formulaId)}};x=h([m(0,t.IUniverInstanceService),m(1,(0,t.Inject)(r.RegisterOtherFormulaService)),m(2,(0,t.Inject)(n.DataValidationModel)),m(3,(0,t.Inject)(g)),m(4,(0,t.Inject)(n.DataValidatorRegistryService))],x);function S(e){return(0,t.getOriginCellValue)(e)}function C(e){return String(S(e)??``)}let w=class extends t.Disposable{constructor(e,t){super(),this._injector=e,this._dataValidationModel=t,p(this,`_cache`,/* @__PURE__ */ new Map),this._initRuleChangeListener()}_initRuleChangeListener(){this.disposeWithMe(this._dataValidationModel.ruleChange$.subscribe(e=>{(e.type===`remove`||e.type===`update`)&&this.markRuleDirty(e.unitId,e.subUnitId,e.rule.uid)}))}getOrCompute(e,t,n){let r=this.getCache(e,t,n.uid);if(r)return r;let i=this._injector.get(T).getRuleFormulaResultSync(e,t,n.uid);return this.computeAndCache(e,t,n,i)}_ensureCache(e,t){let n=this._cache.get(e);n||(n=/* @__PURE__ */ new Map,this._cache.set(e,n));let r=n.get(t);return r||(r=/* @__PURE__ */ new Map,n.set(t,r)),r}getCache(e,t,n){var r;return(r=this._cache.get(e))==null||(r=r.get(t))==null?void 0:r.get(n)}setCache(e,t,n,r){this._ensureCache(e,t).set(n,r)}markRuleDirty(e,t,n){var r;(r=this._cache.get(e))==null||(r=r.get(t))==null||r.delete(n)}clear(){this._cache.clear()}computeAndCache(e,n,r,a){var o;let{formula1:s=``,formula2:c=``}=r,l=(0,t.isFormulaString)(s)?this._getRuleFormulaResultSet(a==null||(o=a[0])==null||(o=o.result)==null||(o=o[0])==null?void 0:o[0]):(0,i.deserializeListOptions)(s),u=c.split(`,`),d=l.map((e,t)=>({label:e,color:u[t]||``})),f={};for(let e of d)e.color&&(f[e.label]=e.color);let p={list:l,listWithColor:d,colorMap:f,set:new Set(l)};return this.setCache(e,n,r.uid,p),p}_getRuleFormulaResultSet(e){if(!e)return[];let n=/* @__PURE__ */ new Set;for(let i=0,a=e.length;i<a;i++){let a=e[i];if(a)for(let e=0,i=a.length;e<i;e++){let i=a[e],o=S(i);if(o!=null){var r;if(typeof o!=`string`&&typeof(i==null?void 0:i.s)==`object`&&(r=i.s)!=null&&(r=r.n)!=null&&r.pattern){n.add(t.numfmt.format(i.s.n.pattern,o,{throws:!1}));continue}let e=typeof o==`string`?o:String(o);y(e)&&n.add(e)}}}return[...n]}};w=h([m(0,(0,t.Inject)(t.Injector)),m(1,(0,t.Inject)(n.DataValidationModel))],w);let T=class extends t.Disposable{constructor(e,t,n,r,i,a){super(),this._instanceService=e,this._registerOtherFormulaService=t,this._dataValidationCacheService=n,this._dataValidationModel=r,this._validatorRegistryService=i,this._listCacheService=a,p(this,`_formulaRuleMap`,/* @__PURE__ */ new Map),this._initFormulaResultHandler()}_initFormulaResultHandler(){this.disposeWithMe(this._registerOtherFormulaService.formulaResult$.subscribe(e=>{for(let n in e){let r=e[n];if(this._instanceService.getUnitType(n)===t.UniverInstanceType.UNIVER_SHEET)for(let e in r){let t=r[e],i=this._ensureRuleFormulaMap(n,e);t.forEach(t=>{var r;let a=(r=t.extra)==null?void 0:r.ruleId;if(a&&i.get(a)){let t=this._dataValidationModel.getRuleById(n,e,a);t&&(this._listCacheService.markRuleDirty(n,e,a),this._dataValidationCacheService.markRangeDirty(n,e,t.ranges))}})}}}))}_ensureRuleFormulaMap(e,t){let n=this._formulaRuleMap.get(e);n||(n=/* @__PURE__ */ new Map,this._formulaRuleMap.set(e,n));let r=n.get(t);return r||(r=/* @__PURE__ */ new Map,n.set(t,r)),r}_registerSingleFormula(e,t,n,i){return this._registerOtherFormulaService.registerFormulaWithRange(e,t,n,[{startColumn:0,endColumn:0,startRow:0,endRow:0}],{ruleId:i},r.OtherFormulaBizType.DATA_VALIDATION,i)}addRule(e,n,r){if(!b(r.type,this._validatorRegistryService)&&r.type!==t.DataValidationType.CHECKBOX){let{formula1:i,formula2:a,uid:o}=r,s=(0,t.isFormulaString)(i),c=(0,t.isFormulaString)(a);if(!s&&!c)return;let l=this._ensureRuleFormulaMap(e,n),u=[void 0,void 0];s&&(u[0]={id:this._registerSingleFormula(e,n,i,o),text:i}),c&&(u[1]={id:this._registerSingleFormula(e,n,a,o),text:a}),l.set(o,u)}}removeRule(e,t,n){let r=this._ensureRuleFormulaMap(e,t).get(n);if(!r)return;let[i,a]=r,o=[i==null?void 0:i.id,a==null?void 0:a.id].filter(Boolean);o.length&&this._registerOtherFormulaService.deleteFormula(e,t,o)}getRuleFormulaResult(e,t,n){let r=this._ensureRuleFormulaMap(e,t).get(n);if(!r)return Promise.resolve(null);let i=async n=>n&&this._registerOtherFormulaService.getFormulaValue(e,t,n.id);return Promise.all([i(r[0]),i(r[1])])}getRuleFormulaResultSync(e,t,n){let r=this._ensureRuleFormulaMap(e,t).get(n);if(r)return r.map(n=>{if(n)return this._registerOtherFormulaService.getFormulaValueSync(e,t,n.id)})}getRuleFormulaInfo(e,t,n){return this._ensureRuleFormulaMap(e,t).get(n)}};T=h([m(0,t.IUniverInstanceService),m(1,(0,t.Inject)(r.RegisterOtherFormulaService)),m(2,(0,t.Inject)(g)),m(3,(0,t.Inject)(n.DataValidationModel)),m(4,(0,t.Inject)(n.DataValidatorRegistryService)),m(5,(0,t.Inject)(w))],T);var ee=class e{constructor(e,n,r,i,a=!1){this._unitId=n,this._subUnitId=r,this._univerInstanceService=i,this._disableTree=a,p(this,`_map`,void 0),p(this,`_tree`,new t.RBush),p(this,`_dirty`,!0),p(this,`_buildTree`,()=>{if(!this._dirty||this._disableTree)return;this._tree.clear();let e=[];this._map.forEach((t,n)=>{t.forEach(t=>{e.push({minX:t.startRow,maxX:t.endRow,minY:t.startColumn,maxY:t.endColumn,ruleId:n})})}),this._tree.load(e),this._dirty=!1}),p(this,`_debonceBuildTree`,(0,t.debounce)(this._buildTree,0)),this._map=e,this._buildTree()}get _worksheet(){var e;return(e=this._univerInstanceService.getUnit(this._unitId,t.UniverInstanceType.UNIVER_SHEET))==null?void 0:e.getSheetBySheetId(this._subUnitId)}_addRule(e,n){if(!this._worksheet)return;let r=t.Rectangle.mergeRanges(n.map(e=>t.Range.transformRange(e,this._worksheet)));this._map.forEach((e,n)=>{let i=t.Rectangle.subtractMulti(e,r);i.length===0?this._map.delete(n):this._map.set(n,i)}),this._dirty=!0,this._map.set(e,r),this._debonceBuildTree()}addRule(e){this._addRule(e.uid,e.ranges)}removeRange(e,n){if(!this._worksheet)return;let r=e.map(e=>t.Range.transformRange(e,this._worksheet));this._map.forEach((e,i)=>{if(n&&!n.has(i))return;let a=t.Rectangle.subtractMulti(e,r);a.length===0?this._map.delete(i):this._map.set(i,a)}),this._dirty=!0,this._debonceBuildTree()}_removeRule(e){this._map.delete(e),this._dirty=!0,this._debonceBuildTree()}removeRule(e){this._removeRule(e.uid)}updateRange(e,t){this._removeRule(e),this._addRule(e,t)}addRangeRules(e){e.forEach(({id:e,ranges:n})=>{if(!n.length)return;let r=this._map.get(e);r?(this._map.set(e,t.Rectangle.mergeRanges([...r,...n])),r=this._map.get(e)):(r=n,this._map.set(e,r)),this._map.forEach((r,i)=>{if(i===e)return;let a=t.Rectangle.subtractMulti(r,n);a.length===0?this._map.delete(i):this._map.set(i,a)})}),this._dirty=!0,this._debonceBuildTree()}diff(e){let n=[],r=0;return e.forEach((e,i)=>{let a=this._map.get(e.uid)??[],o=e.ranges;a.length!==0&&(a.length!==o.length||a.some((e,n)=>!t.Rectangle.equals(e,o[n])))&&n.push({type:`update`,ruleId:e.uid,oldRanges:o,newRanges:t.Rectangle.sort(a),rule:e}),a.length===0&&(n.push({type:`delete`,rule:e,index:i-r}),r++)}),n}diffWithAddition(e,n){let r=[],i=0;return e.forEach((e,n)=>{let a=this._map.get(e.uid)??[],o=e.ranges;a.length!==0&&(a.length!==o.length||a.some((e,n)=>!t.Rectangle.equals(e,o[n])))&&r.push({type:`update`,ruleId:e.uid,oldRanges:o,newRanges:t.Rectangle.sort(a),rule:e}),a.length===0&&(r.push({type:`delete`,rule:e,index:n-i}),i++)}),Array.from(n).forEach(e=>{let n=this._map.get(e.uid)??[];r.push({type:`add`,rule:{...e,ranges:t.Rectangle.sort(n)}})}),r}clone(){return new e(new Map(t.Tools.deepClone(Array.from(this._map.entries()))),this._unitId,this._subUnitId,this._univerInstanceService,!0)}getValue(e,t){this._dirty&&this._buildTree();let n=this._tree.search({minX:e,maxX:e,minY:t,maxY:t});return n.length>0?n[0].ruleId:void 0}};let E=class extends t.Disposable{constructor(e,t,n,r,i,o,s){super(),this._dataValidationModel=e,this._univerInstanceService=t,this._dataValidatorRegistryService=n,this._dataValidationCacheService=r,this._dataValidationFormulaService=i,this._dataValidationCustomFormulaService=o,this._commandService=s,p(this,`_ruleMatrixMap`,/* @__PURE__ */ new Map),p(this,`_validStatusChange$`,new a.Subject),p(this,`_ruleChange$`,new a.Subject),p(this,`ruleChange$`,this._ruleChange$.asObservable()),p(this,`validStatusChange$`,this._validStatusChange$.asObservable()),this._initRuleUpdateListener(),this.disposeWithMe(()=>{this._ruleChange$.complete(),this._validStatusChange$.complete()}),this._initUniverInstanceListener()}_initUniverInstanceListener(){this.disposeWithMe(this._univerInstanceService.unitDisposed$.subscribe(e=>{this._ruleMatrixMap.delete(e.getUnitId())})),this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(e.id===i.RemoveSheetMutation.id){let{unitId:t,subUnitId:n}=e.params,r=this._ruleMatrixMap.get(t);r&&r.delete(n)}}))}_initRuleUpdateListener(){let e=this._dataValidationModel.getAll();for(let[t,n]of e)for(let[e,r]of n)for(let n of r)this._addRule(t,e,n),this._ruleChange$.next({type:`add`,unitId:t,subUnitId:e,rule:n,source:`patched`});this.disposeWithMe(this._dataValidationModel.ruleChange$.subscribe(e=>{switch(e.type){case`add`:this._addRule(e.unitId,e.subUnitId,e.rule);break;case`update`:this._updateRule(e.unitId,e.subUnitId,e.rule.uid,e.oldRule,e.updatePayload);break;case`remove`:this._removeRule(e.unitId,e.subUnitId,e.rule)}this._ruleChange$.next(e)}))}_ensureRuleMatrix(e,t){let n=this._ruleMatrixMap.get(e);n||(n=/* @__PURE__ */ new Map,this._ruleMatrixMap.set(e,n));let r=n.get(t);return r||(r=new ee(/* @__PURE__ */ new Map,e,t,this._univerInstanceService),n.set(t,r)),r}_addRuleSideEffect(e,t,n){this._ensureRuleMatrix(e,t).addRule(n),this._dataValidationCacheService.addRule(e,t,n),this._dataValidationFormulaService.addRule(e,t,n),this._dataValidationCustomFormulaService.addRule(e,t,n)}_addRule(e,t,n){(Array.isArray(n)?n:[n]).forEach(n=>{this._addRuleSideEffect(e,t,n)})}_updateRule(e,t,r,i,a){let o=this._ensureRuleMatrix(e,t),s={...i,...a.payload};a.type===n.UpdateRuleType.RANGE?o.updateRange(r,a.payload):a.type===n.UpdateRuleType.ALL&&o.updateRange(r,a.payload.ranges),this._dataValidationCacheService.removeRule(e,t,i),this._dataValidationCacheService.addRule(e,t,s),this._dataValidationFormulaService.removeRule(e,t,i.uid),this._dataValidationFormulaService.addRule(e,t,s),this._dataValidationCustomFormulaService.deleteByRuleId(e,t,r),this._dataValidationCustomFormulaService.addRule(e,t,s)}_removeRule(e,t,n){this._ensureRuleMatrix(e,t).removeRule(n),this._dataValidationCacheService.removeRule(e,t,n),this._dataValidationCustomFormulaService.deleteByRuleId(e,t,n.uid)}getValidator(e){return this._dataValidatorRegistryService.getValidatorItem(e)}getRuleIdByLocation(e,t,n,r){return this._ensureRuleMatrix(e,t).getValue(n,r)}getRuleByLocation(e,t,n,r){let i=this.getRuleIdByLocation(e,t,n,r);if(i)return this._dataValidationModel.getRuleById(e,t,i)}validator(e,n,r){let{col:i,row:a,unitId:o,subUnitId:s,worksheet:c}=n,l=(t,n)=>{r&&r(t,n),n&&this._validStatusChange$.next({unitId:o,subUnitId:s,ruleId:e.uid,status:t,row:a,col:i})},u=c.getCellValueOnly(a,i),d=this.getValidator(e.type),f=c.getCellRaw(a,i),p=S(f);if(d){let r=this._dataValidationCacheService.ensureCache(o,s),c=r.getValue(a,i);return c==null?(r.setValue(a,i,t.DataValidationStatus.VALIDATING),d.validator({value:p,unitId:o,subUnitId:s,row:a,column:i,worksheet:n.worksheet,workbook:n.workbook,interceptValue:S(u),t:f==null?void 0:f.t},e).then(e=>{let n=e?t.DataValidationStatus.VALID:t.DataValidationStatus.INVALID,o=r.getValue(a,i);n===t.DataValidationStatus.VALID?r.realDeleteValue(a,i):r.setValue(a,i,n),l(n,c!==o)}),t.DataValidationStatus.VALIDATING):(l(c??t.DataValidationStatus.VALID,!1),c??t.DataValidationStatus.VALID)}return l(t.DataValidationStatus.VALID,!1),t.DataValidationStatus.VALID}getRuleObjectMatrix(e,t){return this._ensureRuleMatrix(e,t)}getRuleById(e,t,n){return this._dataValidationModel.getRuleById(e,t,n)}getRuleIndex(e,t,n){return this._dataValidationModel.getRuleIndex(e,t,n)}getRules(e,t){return[...this._dataValidationModel.getRules(e,t)]}getUnitRules(e){return this._dataValidationModel.getUnitRules(e)}deleteUnitRules(e){return this._dataValidationModel.deleteUnitRules(e)}getSubUnitIds(e){return this._dataValidationModel.getSubUnitIds(e)}getAll(){return this._dataValidationModel.getAll()}};E=h([m(0,(0,t.Inject)(n.DataValidationModel)),m(1,t.IUniverInstanceService),m(2,(0,t.Inject)(n.DataValidatorRegistryService)),m(3,(0,t.Inject)(g)),m(4,(0,t.Inject)(T)),m(5,(0,t.Inject)(x)),m(6,t.ICommandService)],E);let te={[t.DataValidationOperator.BETWEEN]:`sheets-data-validation.operators.between`,[t.DataValidationOperator.EQUAL]:`sheets-data-validation.operators.equal`,[t.DataValidationOperator.GREATER_THAN]:`sheets-data-validation.operators.greaterThan`,[t.DataValidationOperator.GREATER_THAN_OR_EQUAL]:`sheets-data-validation.operators.greaterThanOrEqual`,[t.DataValidationOperator.LESS_THAN]:`sheets-data-validation.operators.lessThan`,[t.DataValidationOperator.LESS_THAN_OR_EQUAL]:`sheets-data-validation.operators.lessThanOrEqual`,[t.DataValidationOperator.NOT_BETWEEN]:`sheets-data-validation.operators.notBetween`,[t.DataValidationOperator.NOT_EQUAL]:`sheets-data-validation.operators.notEqual`},D={[t.DataValidationOperator.BETWEEN]:`sheets-data-validation.ruleName.between`,[t.DataValidationOperator.EQUAL]:`sheets-data-validation.ruleName.equal`,[t.DataValidationOperator.GREATER_THAN]:`sheets-data-validation.ruleName.greaterThan`,[t.DataValidationOperator.GREATER_THAN_OR_EQUAL]:`sheets-data-validation.ruleName.greaterThanOrEqual`,[t.DataValidationOperator.LESS_THAN]:`sheets-data-validation.ruleName.lessThan`,[t.DataValidationOperator.LESS_THAN_OR_EQUAL]:`sheets-data-validation.ruleName.lessThanOrEqual`,[t.DataValidationOperator.NOT_BETWEEN]:`sheets-data-validation.ruleName.notBetween`,[t.DataValidationOperator.NOT_EQUAL]:`sheets-data-validation.ruleName.notEqual`,NONE:`sheets-data-validation.ruleName.legal`},O={[t.DataValidationOperator.BETWEEN]:`sheets-data-validation.errorMsg.between`,[t.DataValidationOperator.EQUAL]:`sheets-data-validation.errorMsg.equal`,[t.DataValidationOperator.GREATER_THAN]:`sheets-data-validation.errorMsg.greaterThan`,[t.DataValidationOperator.GREATER_THAN_OR_EQUAL]:`sheets-data-validation.errorMsg.greaterThanOrEqual`,[t.DataValidationOperator.LESS_THAN]:`sheets-data-validation.errorMsg.lessThan`,[t.DataValidationOperator.LESS_THAN_OR_EQUAL]:`sheets-data-validation.errorMsg.lessThanOrEqual`,[t.DataValidationOperator.NOT_BETWEEN]:`sheets-data-validation.errorMsg.notBetween`,[t.DataValidationOperator.NOT_EQUAL]:`sheets-data-validation.errorMsg.notEqual`,NONE:`sheets-data-validation.errorMsg.legal`},ne={[t.DataValidationOperator.BETWEEN]:`sheets-data-validation.textLength.errorMsg.between`,[t.DataValidationOperator.EQUAL]:`sheets-data-validation.textLength.errorMsg.equal`,[t.DataValidationOperator.GREATER_THAN]:`sheets-data-validation.textLength.errorMsg.greaterThan`,[t.DataValidationOperator.GREATER_THAN_OR_EQUAL]:`sheets-data-validation.textLength.errorMsg.greaterThanOrEqual`,[t.DataValidationOperator.LESS_THAN]:`sheets-data-validation.textLength.errorMsg.lessThan`,[t.DataValidationOperator.LESS_THAN_OR_EQUAL]:`sheets-data-validation.textLength.errorMsg.lessThanOrEqual`,[t.DataValidationOperator.NOT_BETWEEN]:`sheets-data-validation.textLength.errorMsg.notBetween`,[t.DataValidationOperator.NOT_EQUAL]:`sheets-data-validation.textLength.errorMsg.notEqual`},k=`{FORMULA1}`,A=`{FORMULA2}`,j=`{TYPE}`;var M=class extends n.BaseDataValidator{get operatorNames(){return this.operators.map(e=>this.localeService.t(te[e]))}generateRuleName(e){if(!e.operator)return this.localeService.t(D.NONE).replace(j,this.titleStr);let t=this.localeService.t(D[e.operator]).replace(k,e.formula1??``).replace(A,e.formula2??``);return`${this.titleStr} ${t}`}generateRuleErrorMessage(e,t){return e.operator?`${this.localeService.t(O[e.operator]).replace(k,e.formula1??``).replace(A,e.formula2??``)}`:this.localeService.t(O.NONE).replace(j,this.titleStr)}};function N(e,n){return t.Tools.isBlank(e)?n.t(`sheets-data-validation.validFail.value`):(0,t.isFormulaString)(e)?n.t(`sheets-data-validation.validFail.primitive`):``}let P=e=>t.Tools.isDefine(e)&&String(e).toLowerCase()===`true`?`1`:String(e).toLowerCase()===`false`?`0`:e;var re=class extends M{constructor(...e){super(...e),p(this,`id`,t.DataValidationType.CHECKBOX),p(this,`title`,`sheets-data-validation.checkbox.title`),p(this,`operators`,[]),p(this,`scopes`,[`sheet`]),p(this,`order`,41),p(this,`offsetFormulaByRange`,!1),p(this,`_formulaService`,this.injector.get(T)),p(this,`skipDefaultFontRender`,(e,t,n)=>{let{unitId:r,subUnitId:i}=n,{formula1:a,formula2:o}=this.parseFormulaSync(e,r,i),s=`${t??``}`;return!s||s===`${a}`||s===`${o}`})}validatorFormula(e,n,r){let{formula1:i,formula2:a}=e,o=i===a;if(t.Tools.isBlank(i)&&t.Tools.isBlank(a))return{success:!0};if(o)return{success:!1,formula1:this.localeService.t(`sheets-data-validation.validFail.checkboxEqual`),formula2:this.localeService.t(`sheets-data-validation.validFail.checkboxEqual`)};let s=N(i,this.localeService),c=N(a,this.localeService);return{success:!s&&!c,formula1:s,formula2:c}}async parseFormula(e,n,r){var i,a;let{formula1:o=1,formula2:s=0}=e,c=await this._formulaService.getRuleFormulaResult(n,r,e.uid),l=(0,t.isFormulaString)(o)?_(c==null||(i=c[0])==null||(i=i.result)==null?void 0:i[0][0]):o,u=(0,t.isFormulaString)(s)?_(c==null||(a=c[1])==null||(a=a.result)==null?void 0:a[0][0]):s,d=y(String(l))&&y(String(u));return{formula1:P(l),formula2:P(u),originFormula1:l,originFormula2:u,isFormulaValid:d}}getExtraStyle(e,n){return{tb:t.WrapStrategy.CLIP}}parseFormulaSync(e,n,r){var i,a;let{formula1:o=1,formula2:s=0}=e,c=this._formulaService.getRuleFormulaResultSync(n,r,e.uid),l=(0,t.isFormulaString)(o)?_(c==null||(i=c[0])==null||(i=i.result)==null?void 0:i[0][0]):o,u=(0,t.isFormulaString)(s)?_(c==null||(a=c[1])==null||(a=a.result)==null?void 0:a[0][0]):s,d=y(String(l))&&y(String(u));return{formula1:P(l),formula2:P(u),originFormula1:l,originFormula2:u,isFormulaValid:d}}async isValidType(e,n,r){let{value:i,unitId:a,subUnitId:o}=e,{formula1:s,formula2:c,originFormula1:l,originFormula2:u}=await this.parseFormula(r,a,o);return!t.Tools.isDefine(s)||!t.Tools.isDefine(c)||t.Tools.isDefine(i)&&(String(i)===String(s)||String(i)===String(c)||String(i)===String(l??``)||String(i)===String(u??``))}generateRuleErrorMessage(e){return this.localeService.t(`sheets-data-validation.checkbox.error`)}generateRuleName(e){return this.titleStr}};let ie={[t.DataValidationOperator.BETWEEN]:`sheets-data-validation.date.operators.between`,[t.DataValidationOperator.EQUAL]:`sheets-data-validation.date.operators.equal`,[t.DataValidationOperator.GREATER_THAN]:`sheets-data-validation.date.operators.greaterThan`,[t.DataValidationOperator.GREATER_THAN_OR_EQUAL]:`sheets-data-validation.date.operators.greaterThanOrEqual`,[t.DataValidationOperator.LESS_THAN]:`sheets-data-validation.date.operators.lessThan`,[t.DataValidationOperator.LESS_THAN_OR_EQUAL]:`sheets-data-validation.date.operators.lessThanOrEqual`,[t.DataValidationOperator.NOT_BETWEEN]:`sheets-data-validation.date.operators.notBetween`,[t.DataValidationOperator.NOT_EQUAL]:`sheets-data-validation.date.operators.notEqual`};t.DataValidationOperator.BETWEEN,t.DataValidationOperator.EQUAL,t.DataValidationOperator.GREATER_THAN,t.DataValidationOperator.GREATER_THAN_OR_EQUAL,t.DataValidationOperator.LESS_THAN,t.DataValidationOperator.LESS_THAN_OR_EQUAL,t.DataValidationOperator.NOT_BETWEEN,t.DataValidationOperator.NOT_EQUAL;let ae={[t.DataValidationOperator.BETWEEN]:`sheets-data-validation.date.ruleName.between`,[t.DataValidationOperator.EQUAL]:`sheets-data-validation.date.ruleName.equal`,[t.DataValidationOperator.GREATER_THAN]:`sheets-data-validation.date.ruleName.greaterThan`,[t.DataValidationOperator.GREATER_THAN_OR_EQUAL]:`sheets-data-validation.date.ruleName.greaterThanOrEqual`,[t.DataValidationOperator.LESS_THAN]:`sheets-data-validation.date.ruleName.lessThan`,[t.DataValidationOperator.LESS_THAN_OR_EQUAL]:`sheets-data-validation.date.ruleName.lessThanOrEqual`,[t.DataValidationOperator.NOT_BETWEEN]:`sheets-data-validation.date.ruleName.notBetween`,[t.DataValidationOperator.NOT_EQUAL]:`sheets-data-validation.date.ruleName.notEqual`,NONE:`sheets-data-validation.date.ruleName.legal`},oe={[t.DataValidationOperator.BETWEEN]:`sheets-data-validation.date.errorMsg.between`,[t.DataValidationOperator.EQUAL]:`sheets-data-validation.date.errorMsg.equal`,[t.DataValidationOperator.GREATER_THAN]:`sheets-data-validation.date.errorMsg.greaterThan`,[t.DataValidationOperator.GREATER_THAN_OR_EQUAL]:`sheets-data-validation.date.errorMsg.greaterThanOrEqual`,[t.DataValidationOperator.LESS_THAN]:`sheets-data-validation.date.errorMsg.lessThan`,[t.DataValidationOperator.LESS_THAN_OR_EQUAL]:`sheets-data-validation.date.errorMsg.lessThanOrEqual`,[t.DataValidationOperator.NOT_BETWEEN]:`sheets-data-validation.date.errorMsg.notBetween`,[t.DataValidationOperator.NOT_EQUAL]:`sheets-data-validation.date.errorMsg.notEqual`,NONE:`sheets-data-validation.date.errorMsg.legal`},F=[t.DataValidationOperator.BETWEEN,t.DataValidationOperator.NOT_BETWEEN],I=`{FORMULA1}`,L=`{FORMULA2}`;function se(e){let t=S(e);return t==null?``:t.toString()}function R(e,n,r){let{formula1:i,formula2:a}=n,o=n.ranges[0].startRow,s=n.ranges[0].startColumn,c=r.row-o,l=r.col-s;return{transformedFormula1:(0,t.isFormulaString)(i)?e.moveFormulaRefOffset(i,l,c,!0):i,transformedFormula2:(0,t.isFormulaString)(a)?e.moveFormulaRefOffset(a,l,c,!0):a}}let z=(e,n={})=>{var r;if(e!=null&&typeof e!=`boolean`)return typeof e==`number`||!Number.isNaN(+e)?+e:(r=t.numfmt.parseDate(e,n))==null?void 0:r.v};function B(e){return String(e).padStart(2,`0`)}var V=class extends M{constructor(...e){super(...e),p(this,`id`,t.DataValidationType.DATE),p(this,`title`,`sheets-data-validation.date.title`),p(this,`order`,40),p(this,`operators`,[t.DataValidationOperator.BETWEEN,t.DataValidationOperator.EQUAL,t.DataValidationOperator.GREATER_THAN,t.DataValidationOperator.GREATER_THAN_OR_EQUAL,t.DataValidationOperator.LESS_THAN,t.DataValidationOperator.LESS_THAN_OR_EQUAL,t.DataValidationOperator.NOT_BETWEEN,t.DataValidationOperator.NOT_EQUAL]),p(this,`scopes`,[`sheet`]),p(this,`_customFormulaService`,this.injector.get(x)),p(this,`_lexerTreeBuilder`,this.injector.get(r.LexerTreeBuilder)),p(this,`_univerInstanceService`,this.injector.get(t.IUniverInstanceService))}_getParseOptions(e,n){let r=n??(e?this._univerInstanceService.getUnit(e,t.UniverInstanceType.UNIVER_SHEET):void 0);return r?{locale:(0,t.getNumfmtLocaleTag)(r.getSnapshot().locale),dateSystem:r.getDateSystem()}:{dateSystem:t.DateSystem.Date1900}}async parseFormula(e,n,r,i,a){let o=await this._customFormulaService.getCellFormulaValue(n,r,e.uid,i,a),s=await this._customFormulaService.getCellFormula2Value(n,r,e.uid,i,a),{formula1:c,formula2:l}=e,u=y(String(o==null?void 0:o.v))&&y(String(s==null?void 0:s.v));return{formula1:z((0,t.isFormulaString)(c)?o==null?void 0:o.v:c,this._getParseOptions(n)),formula2:z((0,t.isFormulaString)(l)?s==null?void 0:s.v:l,this._getParseOptions(n)),isFormulaValid:u}}async isValidType(e){let{interceptValue:n,value:r}=e,i=this._getParseOptions(e.unitId,e.workbook);return typeof r==`number`&&typeof n==`string`||typeof n==`string`?!!t.numfmt.parseDate(n,i):!1}_validatorSingleFormula(e,n){return!t.Tools.isBlank(e)&&((0,t.isFormulaString)(e)||!Number.isNaN(+e)||!!(e&&t.numfmt.parseDate(e,n)))}validatorFormula(e,t,n){let r=e.operator;if(!r)return{success:!0};let i=this._getParseOptions(t),a=this._validatorSingleFormula(e.formula1,i),o=this.localeService.t(`sheets-data-validation.validFail.date`);if(F.includes(r)){let t=this._validatorSingleFormula(e.formula2,i);return{success:a&&t,formula1:a?void 0:o,formula2:t?void 0:o}}return{success:a,formula1:a?void 0:o}}normalizeFormula(e,n,r){let{formula1:i,formula2:a,bizInfo:o}=e,s=this._getParseOptions(n),c=e=>{var n;if(!e)return e;let r=Number.isNaN(+e)?(n=t.numfmt.parseDate(e,s))==null?void 0:n.v:+e;if(typeof r!=`number`)return``;let i=(0,t.excelSerialToDateTimeParts)(r,{dateSystem:s.dateSystem??t.DateSystem.Date1900});if(!i)return``;let a=`${String(i.year).padStart(4,`0`)}-${B(i.month)}-${B(i.day)}`;return o!=null&&o.showTime?`${a} ${B(i.hours)}:${B(i.minutes)}:${B(i.seconds)}`:a};return{formula1:(0,t.isFormulaString)(i)?i:c(`${i}`),formula2:(0,t.isFormulaString)(a)?a:c(`${a}`)}}transform(e,t,n){let{value:r}=e;return{...e,value:z(r,this._getParseOptions(e.unitId,e.workbook))}}get operatorNames(){return this.operators.map(e=>this.localeService.t(ie[e]))}generateRuleName(e){if(!e.operator)return this.localeService.t(ae.NONE);let t=this.localeService.t(ae[e.operator]).replace(I,e.formula1??``).replace(L,e.formula2??``);return`${this.titleStr} ${t}`}generateRuleErrorMessage(e,t){if(!e.operator)return this.titleStr;let{transformedFormula1:n,transformedFormula2:r}=R(this._lexerTreeBuilder,e,t);return`${this.localeService.t(oe[e.operator]).replace(I,n??``).replace(L,r??``)}`}};function H(e){return+e}var ce=class extends M{constructor(...e){super(...e),p(this,`_customFormulaService`,this.injector.get(x)),p(this,`id`,t.DataValidationType.DECIMAL),p(this,`_lexerTreeBuilder`,this.injector.get(r.LexerTreeBuilder)),p(this,`title`,`sheets-data-validation.decimal.title`),p(this,`order`,20),p(this,`operators`,[t.DataValidationOperator.BETWEEN,t.DataValidationOperator.EQUAL,t.DataValidationOperator.GREATER_THAN,t.DataValidationOperator.GREATER_THAN_OR_EQUAL,t.DataValidationOperator.LESS_THAN,t.DataValidationOperator.LESS_THAN_OR_EQUAL,t.DataValidationOperator.NOT_BETWEEN,t.DataValidationOperator.NOT_EQUAL]),p(this,`scopes`,[`sheet`])}_isFormulaOrNumber(e){return!t.Tools.isBlank(e)&&((0,t.isFormulaString)(e)||!Number.isNaN(+e))}async isValidType(e,t,n){let{value:r}=e;return!Number.isNaN(H(r))}transform(e,t,n){let{value:r}=e;return{...e,value:H(r)}}_parseNumber(e){return e==null?NaN:+e}async parseFormula(e,n,r,i,a){let o=await this._customFormulaService.getCellFormulaValue(n,r,e.uid,i,a),s=await this._customFormulaService.getCellFormula2Value(n,r,e.uid,i,a),{formula1:c,formula2:l}=e,u=y(String(o==null?void 0:o.v))&&y(String(s==null?void 0:s.v));return{formula1:this._parseNumber((0,t.isFormulaString)(c)?o==null?void 0:o.v:c),formula2:this._parseNumber((0,t.isFormulaString)(l)?s==null?void 0:s.v:l),isFormulaValid:u}}validatorFormula(e,n,r){let i=e.operator;if(!i)return{success:!0};let a=t.Tools.isDefine(e.formula1)&&this._isFormulaOrNumber(e.formula1),o=t.Tools.isDefine(e.formula2)&&this._isFormulaOrNumber(e.formula2),s=F.includes(i),c=this.localeService.t(`sheets-data-validation.validFail.number`);return s?{success:a&&o,formula1:a?void 0:c,formula2:o?void 0:c}:{success:a,formula1:a?``:c}}generateRuleErrorMessage(e,t){if(!e.operator)return this.localeService.t(O.NONE).replace(`{TYPE}`,this.titleStr);let{transformedFormula1:n,transformedFormula2:r}=R(this._lexerTreeBuilder,e,t);return`${this.localeService.t(O[e.operator]).replace(I,n??``).replace(L,r??``)}`}};function le(e){if(!e)return[];let n=/* @__PURE__ */ new Set;for(let i=0,a=e.length;i<a;i++){let a=e[i];if(a)for(let e=0,i=a.length;e<i;e++){let i=a[e],o=S(i);if(o!=null){var r;if(typeof o!=`string`&&typeof(i==null?void 0:i.s)==`object`&&(r=i.s)!=null&&(r=r.n)!=null&&r.pattern){n.add(t.numfmt.format(i.s.n.pattern,o,{throws:!1}));continue}let e=typeof o==`string`?o:String(o);y(e)&&n.add(e)}}}return[...n]}let ue=[`if`,`indirect`,`choose`,`offset`];function de(e,n){if(!(0,t.isFormulaString)(e)||(0,r.isReferenceString)(e.slice(1)))return!0;let i=n.sequenceNodesBuilder(e);return i&&i.some(e=>typeof e==`object`&&e.nodeType===r.sequenceNodeType.FUNCTION&&ue.indexOf(e.token.toLowerCase())>-1)}function fe(e,n){let{formula1:i=``,ranges:a}=e;if((0,r.isReferenceString)(i.slice(1))){let e=(0,r.deserializeRangeWithSheet)(i.slice(1));if((!e.sheetName||e.sheetName===n)&&a.some(n=>t.Rectangle.intersects(n,e.range)))return!0}return!1}var U=class extends M{constructor(...e){super(...e),p(this,`formulaService`,this.injector.get(T)),p(this,`_lexer`,this.injector.get(r.LexerTreeBuilder)),p(this,`_univerInstanceService`,this.injector.get(t.IUniverInstanceService)),p(this,`_listCacheService`,this.injector.get(w)),p(this,`order`,50),p(this,`offsetFormulaByRange`,!1),p(this,`id`,t.DataValidationType.LIST),p(this,`title`,`sheets-data-validation.list.title`),p(this,`operators`,[]),p(this,`scopes`,[`sheet`]),p(this,`skipDefaultFontRender`,e=>e.renderMode!==t.DataValidationRenderMode.TEXT)}validatorFormula(e,n,r){var i;let a=!t.Tools.isBlank(e.formula1),o=de(e.formula1??``,this._lexer),s=fe(e,((i=this._univerInstanceService.getUnit(n,t.UniverInstanceType.UNIVER_SHEET))==null||(i=i.getSheetBySheetId(r))==null?void 0:i.getName())??``);return{success:!!(a&&o&&!s),formula1:a?o?s?this.localeService.t(`sheets-data-validation.validFail.listIntersects`):void 0:this.localeService.t(`sheets-data-validation.validFail.listInvalid`):this.localeService.t(`sheets-data-validation.validFail.list`)}}getExtraStyle(e,n,{style:r}){let i=(r.tb===t.WrapStrategy.OVERFLOW?t.WrapStrategy.CLIP:r.tb)??t.WrapStrategy.WRAP;if(e.type===t.DataValidationType.LIST&&(e.renderMode===t.DataValidationRenderMode.ARROW||e.renderMode===t.DataValidationRenderMode.TEXT)){let t=this.getListWithColorMap(e)[`${n??``}`];if(t)return{bg:{rgb:t},tb:i}}return{tb:i}}parseCellValue(e){let t=e.toString();return(0,i.deserializeListOptions)(t)}async parseFormula(e,t,n){var r;let i=await this.formulaService.getRuleFormulaResult(t,n,e.uid),a=_(i==null||(r=i[0])==null||(r=r.result)==null?void 0:r[0][0]);return{formula1:void 0,formula2:void 0,isFormulaValid:y(String(a))}}async isValidType(e,n,r){let{value:a,unitId:o,subUnitId:s}=e,{formula1:c=``}=r,l=(0,t.isFormulaString)(c)?this._listCacheService.getOrCompute(o,s,r).list:(0,i.deserializeListOptions)(c);return(r.type===t.DataValidationType.LIST?a==null||a===``?[]:[`${a}`]:this.parseCellValue(a)).every(e=>l.includes(e))}generateRuleName(){return this.localeService.t(`sheets-data-validation.list.name`)}generateRuleErrorMessage(){return this.localeService.t(`sheets-data-validation.list.error`)}_getUnitAndSubUnit(e,n){let r=(e?this._univerInstanceService.getUnit(e,t.UniverInstanceType.UNIVER_SHEET):void 0)??this._univerInstanceService.getCurrentUnitOfType(t.UniverInstanceType.UNIVER_SHEET);if(!r)return null;let i=(n?r.getSheetBySheetId(n):void 0)??r.getActiveSheet();return i?{unitId:r.getUnitId(),subUnitId:i.getSheetId()}:null}getList(e,t,n){let r=this._getUnitAndSubUnit(t,n);if(!r)return[];let{unitId:i,subUnitId:a}=r;return this._listCacheService.getOrCompute(i,a,e).list}async getListAsync(e,n,r){var a;let{formula1:o=``}=e,s=this._getUnitAndSubUnit(n,r);if(!s)return[];let{unitId:c,subUnitId:l}=s,u=await this.formulaService.getRuleFormulaResult(c,l,e.uid);return(0,t.isFormulaString)(o)?le(u==null||(a=u[0])==null||(a=a.result)==null?void 0:a[0][0]):(0,i.deserializeListOptions)(o)}getListWithColor(e,t,n){let r=this._getUnitAndSubUnit(t,n);if(!r)return[];let{unitId:i,subUnitId:a}=r;return this._listCacheService.getOrCompute(i,a,e).listWithColor}getListWithColorMap(e,t,n){let r=this._getUnitAndSubUnit(t,n);if(!r)return{};let{unitId:i,subUnitId:a}=r;return this._listCacheService.getOrCompute(i,a,e).colorMap}},pe=class extends M{constructor(...e){super(...e),p(this,`id`,t.DataValidationType.TEXT_LENGTH),p(this,`title`,`sheets-data-validation.textLength.title`),p(this,`_lexerTreeBuilder`,this.injector.get(r.LexerTreeBuilder)),p(this,`order`,30),p(this,`operators`,[t.DataValidationOperator.BETWEEN,t.DataValidationOperator.EQUAL,t.DataValidationOperator.GREATER_THAN,t.DataValidationOperator.GREATER_THAN_OR_EQUAL,t.DataValidationOperator.LESS_THAN,t.DataValidationOperator.LESS_THAN_OR_EQUAL,t.DataValidationOperator.NOT_BETWEEN,t.DataValidationOperator.NOT_EQUAL]),p(this,`scopes`,[`sheet`]),p(this,`_customFormulaService`,this.injector.get(x))}_isFormulaOrInt(e){return!t.Tools.isBlank(e)&&((0,t.isFormulaString)(e)||!Number.isNaN(+e)&&Number.isInteger(+e))}validatorFormula(e,n,r){let i=e.operator;if(!i)return{success:!1};let a=t.Tools.isDefine(e.formula1)&&this._isFormulaOrInt(e.formula1),o=t.Tools.isDefine(e.formula2)&&this._isFormulaOrInt(e.formula2),s=F.includes(i),c=this.localeService.t(`sheets-data-validation.validFail.number`);return s?{success:a&&o,formula1:a?void 0:c,formula2:o?void 0:c}:{success:a,formula1:c}}_parseNumber(e){return e==null?NaN:+e}async parseFormula(e,n,r,i,a){let o=await this._customFormulaService.getCellFormulaValue(n,r,e.uid,i,a),s=await this._customFormulaService.getCellFormula2Value(n,r,e.uid,i,a),{formula1:c,formula2:l}=e,u=y(String(o==null?void 0:o.v))&&y(String(s==null?void 0:s.v));return{formula1:this._parseNumber((0,t.isFormulaString)(c)?o==null?void 0:o.v:c),formula2:this._parseNumber((0,t.isFormulaString)(l)?s==null?void 0:s.v:l),isFormulaValid:u}}transform(e,t,n){return{...e,value:e.value.toString().length}}async isValidType(e,t,n){let{value:r}=e;return typeof r==`string`||typeof r==`number`}generateRuleErrorMessage(e,t){if(!e.operator)return this.titleStr;let{transformedFormula1:n,transformedFormula2:r}=R(this._lexerTreeBuilder,e,t);return`${this.localeService.t(ne[e.operator]).replace(I,n??``).replace(L,r??``)}`}};function W(e){var n;return e?e.p?!(((n=e.p.body)==null?void 0:n.dataStream)??``).slice(0,-2).trim():t.Tools.isBlank(e.v):!0}function G(e,a,o,s,c=`command`,u=!0){let d=s.get(r.LexerTreeBuilder),f=s.get(n.DataValidatorRegistryService),p=[],m=[],h=s.get(E),g=s.get(t.IUniverInstanceService),_=(0,i.getSheetCommandTarget)(g,{unitId:e,subUnitId:a});if(!_)return{redoMutations:p,undoMutations:m};let{worksheet:v}=_,y=new t.ObjectMatrix,x=!1;function S(e,n){u&&l(e,v).forEach(e=>{t.Range.foreach(e,(e,t)=>{let r=v.getCellRaw(e,t),i=C(r);(W(r)||i===n)&&!(r!=null&&r.p)&&(x=!0,y.setValue(e,t,{v:n,p:null}))})})}if(o.forEach(r=>{switch(r.type){case`delete`:p.push({id:n.RemoveDataValidationMutation.id,params:{unitId:e,subUnitId:a,ruleId:r.rule.uid,source:c}}),m.unshift({id:n.AddDataValidationMutation.id,params:{unitId:e,subUnitId:a,rule:r.rule,index:r.index,source:c}});break;case`update`:{if(b(r.rule.type,f)){let i=r.oldRanges[0].startRow,o=r.oldRanges[0].startColumn,s=r.newRanges[0].startRow,l=r.newRanges[0].startColumn,u=s-i,f=l-o,h=(0,t.isFormulaString)(r.rule.formula1)?d.moveFormulaRefOffset(r.rule.formula1,f,u):r.rule.formula1,g=(0,t.isFormulaString)(r.rule.formula2)?d.moveFormulaRefOffset(r.rule.formula2,f,u):r.rule.formula2;h!==r.rule.formula1||g!==r.rule.formula2||!(0,t.isRangesEqual)(r.newRanges,r.oldRanges)?(p.push({id:n.UpdateDataValidationMutation.id,params:{unitId:e,subUnitId:a,ruleId:r.ruleId,payload:{type:n.UpdateRuleType.ALL,payload:{formula1:h,formula2:g,ranges:r.newRanges}}}}),m.unshift({id:n.UpdateDataValidationMutation.id,params:{unitId:e,subUnitId:a,ruleId:r.ruleId,payload:{type:n.UpdateRuleType.ALL,payload:{formula1:r.rule.formula1,formula2:r.rule.formula2,ranges:r.oldRanges}}}})):(p.push({id:n.UpdateDataValidationMutation.id,params:{unitId:e,subUnitId:a,ruleId:r.ruleId,payload:{type:n.UpdateRuleType.RANGE,payload:r.newRanges},source:c}}),m.unshift({id:n.UpdateDataValidationMutation.id,params:{unitId:e,subUnitId:a,ruleId:r.ruleId,payload:{type:n.UpdateRuleType.RANGE,payload:r.oldRanges},source:c}}))}else p.push({id:n.UpdateDataValidationMutation.id,params:{unitId:e,subUnitId:a,ruleId:r.ruleId,payload:{type:n.UpdateRuleType.RANGE,payload:r.newRanges},source:c}}),m.unshift({id:n.UpdateDataValidationMutation.id,params:{unitId:e,subUnitId:a,ruleId:r.ruleId,payload:{type:n.UpdateRuleType.RANGE,payload:r.oldRanges},source:c}});let i=h.getRuleById(e,a,r.ruleId);if(i&&i.type===t.DataValidationType.CHECKBOX){let n=h.getValidator(t.DataValidationType.CHECKBOX).parseFormulaSync(i,e,a);S(r.newRanges,n.formula2)}break}case`add`:if(p.push({id:n.AddDataValidationMutation.id,params:{unitId:e,subUnitId:a,rule:r.rule,source:c}}),m.unshift({id:n.RemoveDataValidationMutation.id,params:{unitId:e,subUnitId:a,ruleId:r.rule.uid,source:c}}),r.rule.type===t.DataValidationType.CHECKBOX){let n=h.getValidator(t.DataValidationType.CHECKBOX).parseFormulaSync(r.rule,e,a);S(r.rule.ranges,n.originFormula2)}}}),x){let t={id:i.SetRangeValuesMutation.id,params:{unitId:e,subUnitId:a,cellValue:y.clone()}},n={id:i.SetRangeValuesMutation.id,params:(0,i.SetRangeValuesUndoMutationFactory)(s,t.params)};p.push(t),m.push(n)}return{redoMutations:p,undoMutations:m}}let K={type:t.CommandType.COMMAND,id:`sheet.command.updateDataValidationRuleRange`,handler(e,n){if(!n)return!1;let{unitId:r,subUnitId:i,ranges:a,ruleId:o}=n,s=e.get(E),c=e.get(t.ICommandService),l=e.get(t.IUndoRedoService);if(!s.getRuleById(r,i,o))return!1;let u=s.getRuleObjectMatrix(r,i).clone();u.updateRange(o,a);let{redoMutations:d,undoMutations:f}=G(r,i,u.diff(s.getRules(r,i)),e);return l.pushUndoRedo({undoMutations:f,redoMutations:d,unitID:r}),(0,t.sequenceExecute)(d,c),!0}},me={type:t.CommandType.COMMAND,id:`sheet.command.addDataValidation`,handler(e,r){if(!r)return!1;let{unitId:i,subUnitId:a,rule:o}=r,s=e.get(E),c=e.get(t.ICommandService),l=e.get(t.IUndoRedoService),u=s.getRuleObjectMatrix(i,a).clone();u.addRule(o);let d=u.diff(s.getRules(i,a)),f=s.getValidator(o.type),p={unitId:i,subUnitId:a,rule:{...o,...f==null?void 0:f.normalizeFormula(o,i,a)}},{redoMutations:m,undoMutations:h}=G(i,a,d,e);return m.push({id:n.AddDataValidationMutation.id,params:p}),h.unshift({id:n.RemoveDataValidationMutation.id,params:{unitId:i,subUnitId:a,ruleId:o.uid}}),l.pushUndoRedo({unitID:i,redoMutations:m,undoMutations:h}),(0,t.sequenceExecute)(m,c),!0}},he={type:t.CommandType.COMMAND,id:`sheets.command.update-data-validation-setting`,handler(e,r){if(!r)return!1;let a=e.get(t.ICommandService),o=e.get(t.IUndoRedoService),s=e.get(E),c=e.get(n.DataValidatorRegistryService),{unitId:u,subUnitId:d,ruleId:f,setting:p}=r,m=c.getValidatorItem(p.type);if(!m)return!1;let h=s.getRuleById(u,d,f);if(!h)return!1;let g={...h,...p};if(!m.validatorFormula(g,u,d).success)return!1;let _={unitId:u,subUnitId:d,ruleId:f,payload:{type:n.UpdateRuleType.SETTING,payload:{...p,...m.normalizeFormula(g,u,d)}}},v=[{id:n.UpdateDataValidationMutation.id,params:_}],y={unitId:u,subUnitId:d,ruleId:f,payload:{type:n.UpdateRuleType.SETTING,payload:(0,n.getRuleSetting)(h)}},b=[{id:n.UpdateDataValidationMutation.id,params:y}];if(p.type===t.DataValidationType.CHECKBOX){let n=h.ranges,r=e.get(t.IUniverInstanceService),a=(0,i.getSheetCommandTarget)(r,{unitId:u,subUnitId:d});if(a){let r=new t.ObjectMatrix,{worksheet:o}=a,{formula2:s=0,formula1:c=1}=h,{formula2:f=0,formula1:m=1}=p,g=!1;if(l(n,o).forEach(e=>{t.Range.foreach(e,(e,t)=>{let n=o.getCellRaw(e,t),i=C(n);(W(n)||i===String(s))&&!(n!=null&&n.p)?(r.setValue(e,t,{v:f,p:null}),g=!0):i===String(c)&&!(n!=null&&n.p)&&(r.setValue(e,t,{v:m,p:null}),g=!0)})}),g){let t={id:i.SetRangeValuesMutation.id,params:{unitId:u,subUnitId:d,cellValue:r.clone()}},n={id:i.SetRangeValuesMutation.id,params:(0,i.SetRangeValuesUndoMutationFactory)(e,t.params)};v.push(t),b.push(n)}}}return(0,t.sequenceExecute)(v,a).result?(o.pushUndoRedo({unitID:u,redoMutations:v,undoMutations:b}),!0):!1}},ge={type:t.CommandType.COMMAND,id:`sheets.command.update-data-validation-options`,handler(e,r){if(!r)return!1;let i=e.get(t.ICommandService),a=e.get(t.IUndoRedoService),o=e.get(E),{unitId:s,subUnitId:c,ruleId:l,options:u}=r,d=o.getRuleById(s,c,l);if(!d)return!1;let f={unitId:s,subUnitId:c,ruleId:l,payload:{type:n.UpdateRuleType.OPTIONS,payload:u}},p=[{id:n.UpdateDataValidationMutation.id,params:f}],m={unitId:s,subUnitId:c,ruleId:l,payload:{type:n.UpdateRuleType.OPTIONS,payload:(0,n.getRuleOptions)(d)}},h=[{id:n.UpdateDataValidationMutation.id,params:m}];return a.pushUndoRedo({unitID:s,redoMutations:p,undoMutations:h}),i.executeCommand(n.UpdateDataValidationMutation.id,f),!0}},_e={type:t.CommandType.COMMAND,id:`sheets.command.clear-range-data-validation`,handler(e,n){if(!n)return!1;let{unitId:r,subUnitId:a,ranges:o,types:s}=n,c=e.get(t.ICommandService),l=e.get(t.IUniverInstanceService),u=(0,i.getSheetCommandTarget)(l,{unitId:r,subUnitId:a}),d=e.get(E);if(!u)return!1;let f=e.get(t.IUndoRedoService),p=d.getRuleObjectMatrix(r,a).clone(),m=s?new Set(d.getRules(r,a).filter(e=>s.includes(e.type)).map(e=>e.uid)):void 0;p.removeRange(o,m);let{redoMutations:h,undoMutations:g}=G(r,a,p.diff(d.getRules(r,a)),e);return f.pushUndoRedo({unitID:r,redoMutations:h,undoMutations:g}),(0,t.sequenceExecute)(h,c).result}},ve={type:t.CommandType.COMMAND,id:`sheet.command.remove-all-data-validation`,handler(e,r){if(!r)return!1;let{unitId:i,subUnitId:a}=r,o=e.get(t.ICommandService),s=e.get(E),c=e.get(t.IUndoRedoService),l=[...s.getRules(i,a)],u={unitId:i,subUnitId:a,ruleId:l.map(e=>e.uid)},d=[{id:n.RemoveDataValidationMutation.id,params:u}],f=[{id:n.AddDataValidationMutation.id,params:{unitId:i,subUnitId:a,rule:l}}];return c.pushUndoRedo({redoMutations:d,undoMutations:f,unitID:i}),o.executeCommand(n.RemoveDataValidationMutation.id,u),!0}},ye=(e,t)=>{let r=e.get(E),{unitId:i,subUnitId:a,ruleId:o,source:s}=t;if(Array.isArray(o)){let e=o.map(e=>r.getRuleById(i,a,e)).filter(Boolean);return[{id:n.AddDataValidationMutation.id,params:{unitId:i,subUnitId:a,rule:e,source:s}}]}return[{id:n.AddDataValidationMutation.id,params:{unitId:i,subUnitId:a,rule:{...r.getRuleById(i,a,o)},index:r.getRuleIndex(i,a,o)}}]},be={type:t.CommandType.COMMAND,id:`sheet.command.remove-data-validation-rule`,handler(e,r){if(!r)return!1;let{unitId:i,subUnitId:a,ruleId:o}=r,s=e.get(t.ICommandService),c=e.get(t.IUndoRedoService),l=e.get(E),u=[{id:n.RemoveDataValidationMutation.id,params:r}],d=[{id:n.AddDataValidationMutation.id,params:{unitId:i,subUnitId:a,rule:{...l.getRuleById(i,a,o)},index:l.getRuleIndex(i,a,o)}}];return c.pushUndoRedo({undoMutations:d,redoMutations:u,unitID:r.unitId}),s.executeCommand(n.RemoveDataValidationMutation.id,r),!0}},xe=`SHEET_DATA_VALIDATION_PLUGIN`,q=class extends t.Disposable{constructor(e,t,n){super(),this._univerInstanceService=e,this._permissionService=t,this._lexerTreeBuilder=n}getFormulaRefCheck(e){let n=this._lexerTreeBuilder.sequenceNodesBuilder(e);if(!n)return!0;for(let e=0;e<n.length;e++){let s=n[e];if(typeof s==`string`)continue;let{token:l}=s,u=(0,r.deserializeRangeWithSheetWithCache)(l),d=this._univerInstanceService.getCurrentUnitOfType(t.UniverInstanceType.UNIVER_SHEET),f=d.getActiveSheet(),p=d.getUnitId();if(u.sheetName){if(f=d.getSheetBySheetName(u.sheetName),!f)return!1;let e=f==null?void 0:f.getSheetId();if(!this._permissionService.getPermissionPoint(new i.WorksheetViewPermission(p,e).id))return!1}if(!f)return!1;let m=c(u.range,f);if(!m)continue;let{startRow:h,endRow:g,startColumn:_,endColumn:v}=m;for(let e=h;e<=g;e++)for(let t=_;t<=v;t++){var a;let n=(a=f.getCell(e,t))==null||(a=a.selectionProtection)==null?void 0:a[0];if((n==null?void 0:n[o.UnitAction.View])===!1)return!1}}return!0}};q=h([m(0,t.IUniverInstanceService),m(1,t.IPermissionService),m(2,(0,t.Inject)(r.LexerTreeBuilder))],q);var Se=`@univerjs/sheets-data-validation`,Ce=`1.3.0`;let we={},J=class extends t.Disposable{constructor(e,t,n){super(),this._dataValidationModel=e,this._formulaRefRangeService=t,this._validatorRegistryService=n,p(this,`_disposableMap`,/* @__PURE__ */ new Map),p(this,`registerRule`,(e,t,n)=>{b(n.type,this._validatorRegistryService)&&this.register(e,t,n)}),this._initRefRange()}_getIdWithUnitId(e,t,n){return`${e}_${t}_${n}`}register(e,r,i){let a=i.ranges,o=i.formula1,s=i.formula2,c=this._formulaRefRangeService.registerRangeFormula(e,r,a,[o??``,s??``],c=>{if(c.length===0)return{undos:[{id:n.AddDataValidationMutation.id,params:{unitId:e,subUnitId:r,rule:i,source:`patched`}}],redos:[{id:n.RemoveDataValidationMutation.id,params:{unitId:e,subUnitId:r,ruleId:i.uid,source:`patched`}}]};let l=[],u=[],d=c[0];l.push({id:n.UpdateDataValidationMutation.id,params:{unitId:e,subUnitId:r,ruleId:i.uid,payload:{type:n.UpdateRuleType.ALL,payload:{ranges:d.ranges,formula1:d.formulas[0],formula2:d.formulas[1]}},source:`patched`}}),u.push({id:n.UpdateDataValidationMutation.id,params:{unitId:e,subUnitId:r,ruleId:i.uid,payload:{type:n.UpdateRuleType.ALL,payload:{ranges:a,formula1:o,formula2:s}},source:`patched`}});for(let a=1;a<c.length;a++){let o=c[a],s=(0,t.generateRandomId)();l.push({id:n.AddDataValidationMutation.id,params:{unitId:e,subUnitId:r,rule:{...i,uid:s,formula1:o.formulas[0],formula2:o.formulas[1],ranges:o.ranges},source:`patched`}}),u.push({id:n.RemoveDataValidationMutation.id,params:{unitId:e,subUnitId:r,ruleId:s,source:`patched`}})}return{undos:u,redos:l}}),l=this._getIdWithUnitId(e,r,i.uid);this._disposableMap.set(l,c)}_initRefRange(){let e=this._dataValidationModel.getAll();for(let[t,n]of e)for(let[e,r]of n)for(let n of r)this.registerRule(t,e,n);this.disposeWithMe(this._dataValidationModel.ruleChange$.subscribe(e=>{let{unitId:t,subUnitId:n,rule:r}=e;switch(e.type){case`add`:{let t=e.rule;this.registerRule(e.unitId,e.subUnitId,t);break}case`remove`:{let e=this._disposableMap.get(this._getIdWithUnitId(t,n,r.uid));e&&e.dispose();break}case`update`:{let r=e.rule,i=this._disposableMap.get(this._getIdWithUnitId(t,n,r.uid));i&&i.dispose(),this.registerRule(e.unitId,e.subUnitId,r);break}}})),this.disposeWithMe((0,t.toDisposable)(()=>{this._disposableMap.forEach(e=>{e.dispose()}),this._disposableMap.clear()}))}};J=h([m(0,(0,t.Inject)(E)),m(1,(0,t.Inject)(s.FormulaRefRangeService)),m(2,(0,t.Inject)(n.DataValidatorRegistryService))],J);let Y=class extends t.Disposable{constructor(e,t,n,r,i,a){super(),this._dataValidationModel=e,this._injector=t,this._refRangeService=n,this._dataValidationFormulaService=r,this._formulaRefRangeService=i,this._validatorRegistryService=a,p(this,`_disposableMap`,/* @__PURE__ */ new Map),p(this,`registerRule`,(e,t,n)=>{b(n.type,this._validatorRegistryService)||(this.register(e,t,n),this.registerFormula(e,t,n))}),this._initRefRange()}_getIdWithUnitId(e,t,n){return`${e}_${t}_${n}`}registerFormula(e,t,r){let i=r.uid,a=this._getIdWithUnitId(e,t,i),o=this._disposableMap.get(a)??/* @__PURE__ */ new Set,s=(a,o)=>{let s=this._dataValidationModel.getRuleById(e,t,i);if(!s)return{redos:[],undos:[]};let c=s[a];if(!c||c===o)return{redos:[],undos:[]};let l={unitId:e,subUnitId:t,ruleId:r.uid,payload:{type:n.UpdateRuleType.SETTING,payload:{type:s.type,formula1:s.formula1,formula2:s.formula2,[a]:o}},source:`patched`},u={unitId:e,subUnitId:t,ruleId:r.uid,payload:{type:n.UpdateRuleType.SETTING,payload:{type:s.type,formula1:s.formula1,formula2:s.formula2}},source:`patched`};return{redos:[{id:n.UpdateDataValidationMutation.id,params:l}],undos:[{id:n.UpdateDataValidationMutation.id,params:u}]}},c=this._dataValidationFormulaService.getRuleFormulaInfo(e,t,i);if(c){let[n,r]=c;if(n){let r=this._formulaRefRangeService.registerFormula(e,t,n.text,e=>s(`formula1`,e));o.add(()=>r.dispose())}if(r){let n=this._formulaRefRangeService.registerFormula(e,t,r.text,e=>s(`formula2`,e));o.add(()=>n.dispose())}}}register(e,r,a){let o=o=>{let s=[...a.ranges],c=s.map(e=>(0,i.handleCommonDefaultRangeChangeWithEffectRefCommands)(e,o)).filter(e=>!!e).flat();if((0,t.isRangesEqual)(c,s))return{redos:[],undos:[]};if(c.length){let t={unitId:e,subUnitId:r,ruleId:a.uid,payload:{type:n.UpdateRuleType.RANGE,payload:c},source:`patched`};return{redos:[{id:n.UpdateDataValidationMutation.id,params:t}],undos:[{id:n.UpdateDataValidationMutation.id,params:{unitId:e,subUnitId:r,ruleId:a.uid,payload:{type:n.UpdateRuleType.RANGE,payload:s},source:`patched`}}]}}{let t={unitId:e,subUnitId:r,ruleId:a.uid};return{redos:[{id:n.RemoveDataValidationMutation.id,params:t}],undos:ye(this._injector,t)}}},s=[];a.ranges.forEach(t=>{let n=this._refRangeService.registerRefRange(t,o,e,r);s.push(()=>n.dispose())});let c=this._getIdWithUnitId(e,r,a.uid),l=this._disposableMap.get(c)??/* @__PURE__ */ new Set;l.add(()=>s.forEach(e=>e())),this._disposableMap.set(c,l)}_initRefRange(){let e=this._dataValidationModel.getAll();for(let[t,n]of e)for(let[e,r]of n)for(let n of r)this.registerRule(t,e,n);this.disposeWithMe(this._dataValidationModel.ruleChange$.subscribe(e=>{let{unitId:t,subUnitId:n,rule:r}=e;switch(e.type){case`add`:{let t=e.rule;this.registerRule(e.unitId,e.subUnitId,t);break}case`remove`:{let e=this._disposableMap.get(this._getIdWithUnitId(t,n,r.uid));e&&e.forEach(e=>e());break}case`update`:{let r=e.rule,i=this._disposableMap.get(this._getIdWithUnitId(t,n,r.uid));i&&i.forEach(e=>e()),this.registerRule(e.unitId,e.subUnitId,r);break}}})),this.disposeWithMe((0,t.toDisposable)(()=>{this._disposableMap.forEach(e=>{e.forEach(e=>e())}),this._disposableMap.clear()}))}};Y=h([m(0,(0,t.Inject)(E)),m(1,(0,t.Inject)(t.Injector)),m(2,(0,t.Inject)(i.RefRangeService)),m(3,(0,t.Inject)(T)),m(4,(0,t.Inject)(s.FormulaRefRangeService)),m(5,(0,t.Inject)(n.DataValidatorRegistryService))],Y);let X=class extends t.Disposable{constructor(e,t,n){super(),this._sheetInterceptorService=e,this._univerInstanceService=t,this._sheetDataValidationModel=n,this._initSheetChange()}_initSheetChange(){this.disposeWithMe(this._sheetInterceptorService.interceptCommand({getMutations:e=>{if(e.id===i.RemoveSheetCommand.id){var r;let i=e.params,a=i.unitId||this._univerInstanceService.getCurrentUnitOfType(t.UniverInstanceType.UNIVER_SHEET).getUnitId(),o=this._univerInstanceService.getUnit(a,t.UniverInstanceType.UNIVER_SHEET);if(!o)return{redos:[],undos:[]};let s=i.subUnitId||((r=o.getActiveSheet())==null?void 0:r.getSheetId());if(!s)return{redos:[],undos:[]};let c=this._sheetDataValidationModel.getRules(a,s);if(c.length===0)return{redos:[],undos:[]};let l={unitId:a,subUnitId:s,ruleId:c.map(e=>e.uid),source:`patched`},u={unitId:a,subUnitId:s,rule:[...c],source:`patched`};return{redos:[{id:n.RemoveDataValidationMutation.id,params:l}],undos:[{id:n.AddDataValidationMutation.id,params:u}]}}if(e.id===i.CopySheetCommand.id){let{unitId:r,subUnitId:i,targetSubUnitId:a}=e.params;if(!r||!i||!a)return{redos:[],undos:[]};let o=this._sheetDataValidationModel.getRules(r,i);if(o.length===0)return{redos:[],undos:[]};let s=o.map(e=>({...e,uid:(0,t.generateRandomId)(6)}));return{redos:[{id:n.AddDataValidationMutation.id,params:{unitId:r,subUnitId:a,rule:s,source:`patched`}}],undos:[{id:n.RemoveDataValidationMutation.id,params:{unitId:r,subUnitId:a,ruleId:s.map(e=>e.uid),source:`patched`}}]}}return{redos:[],undos:[]}}}))}};X=h([m(0,(0,t.Inject)(i.SheetInterceptorService)),m(1,(0,t.Inject)(t.IUniverInstanceService)),m(2,(0,t.Inject)(E))],X);var Te=class extends M{constructor(...e){super(...e),p(this,`id`,t.DataValidationType.ANY),p(this,`title`,`sheets-data-validation.any.title`),p(this,`operators`,[]),p(this,`scopes`,[`sheet`]),p(this,`order`,0),p(this,`offsetFormulaByRange`,!1)}async parseFormula(e,t,n){return{formula1:e.formula1,formula2:e.formula2,isFormulaValid:!0}}validatorFormula(e,t,n){return{success:!0}}async isValidType(e,t,n){return!0}generateRuleErrorMessage(e){return this.localeService.t(`sheets-data-validation.any.error`)}},Ee=class extends M{constructor(...e){super(...e),p(this,`id`,t.DataValidationType.CUSTOM),p(this,`title`,`sheets-data-validation.custom.title`),p(this,`operators`,[]),p(this,`scopes`,[`sheet`]),p(this,`order`,60),p(this,`_customFormulaService`,this.injector.get(x)),p(this,`_lexerTreeBuilder`,this.injector.get(r.LexerTreeBuilder))}validatorFormula(e,n,i){let a=(0,t.isFormulaString)(e.formula1),o=e.formula1??``,s=this._lexerTreeBuilder.checkIfAddBracket(o)===0&&o.startsWith(r.operatorToken.EQUALS);return{success:a&&s,formula1:a&&s?``:this.localeService.t(`sheets-data-validation.validFail.formula`)}}async parseFormula(e,t,n){return{formula1:void 0,formula2:void 0,isFormulaValid:!0}}async isValidType(e,n,r){let{column:i,row:a,unitId:o,subUnitId:s}=e,c=await this._customFormulaService.getCellFormulaValue(o,s,r.uid,a,i),l=c==null?void 0:c.v;return y(String(l))&&t.Tools.isDefine(l)&&l!==``?c.t===t.CellValueType.BOOLEAN?!!l:typeof l==`boolean`?l:typeof l==`number`?!!l:typeof l==`string`?y(l):!!l:!1}generateRuleErrorMessage(e){return this.localeService.t(`sheets-data-validation.custom.error`)}generateRuleName(e){return this.localeService.t(`sheets-data-validation.custom.ruleName`).replace(`{FORMULA1}`,e.formula1??``)}},De=class extends U{constructor(...e){super(...e),p(this,`id`,t.DataValidationType.LIST_MULTIPLE),p(this,`title`,`sheets-data-validation.listMultiple.title`),p(this,`offsetFormulaByRange`,!1),p(this,`skipDefaultFontRender`,()=>!0)}},Oe=class extends M{constructor(...e){super(...e),p(this,`_customFormulaService`,this.injector.get(x)),p(this,`_lexerTreeBuilder`,this.injector.get(r.LexerTreeBuilder)),p(this,`id`,t.DataValidationType.WHOLE),p(this,`title`,`sheets-data-validation.whole.title`),p(this,`order`,10),p(this,`operators`,[t.DataValidationOperator.BETWEEN,t.DataValidationOperator.EQUAL,t.DataValidationOperator.GREATER_THAN,t.DataValidationOperator.GREATER_THAN_OR_EQUAL,t.DataValidationOperator.LESS_THAN,t.DataValidationOperator.LESS_THAN_OR_EQUAL,t.DataValidationOperator.NOT_BETWEEN,t.DataValidationOperator.NOT_EQUAL]),p(this,`scopes`,[`sheet`])}_isFormulaOrInt(e){return!t.Tools.isBlank(e)&&((0,t.isFormulaString)(e)||!Number.isNaN(+e)&&Number.isInteger(+e))}async isValidType(e,t,n){let{value:r}=e,i=H(r);return!Number.isNaN(i)&&Number.isInteger(i)}transform(e,t,n){let{value:r}=e;return{...e,value:H(r)}}_parseNumber(e){return e==null?NaN:+e}async parseFormula(e,n,r,i,a){let o=await this._customFormulaService.getCellFormulaValue(n,r,e.uid,i,a),s=await this._customFormulaService.getCellFormula2Value(n,r,e.uid,i,a),{formula1:c,formula2:l}=e,u=(0,t.isFormulaString)(c)?o==null?void 0:o.v:c,d=(0,t.isFormulaString)(l)?s==null?void 0:s.v:l,f=y(`${u}`)&&y(`${d}`);return{formula1:this._parseNumber(u),formula2:this._parseNumber(d),isFormulaValid:f}}validatorFormula(e,n,r){let i=e.operator;if(!i)return{success:!0};let a=t.Tools.isDefine(e.formula1)&&this._isFormulaOrInt(e.formula1),o=t.Tools.isDefine(e.formula2)&&this._isFormulaOrInt(e.formula2),s=F.includes(i),c=this.localeService.t(`sheets-data-validation.validFail.number`);return s?{success:a&&o,formula1:a?void 0:c,formula2:o?void 0:c}:{success:a,formula1:c}}generateRuleErrorMessage(e,t){if(!e.operator)return this.localeService.t(O.NONE).replace(`{TYPE}`,this.titleStr);let{transformedFormula1:n,transformedFormula2:r}=R(this._lexerTreeBuilder,e,t);return`${this.localeService.t(O[e.operator]).replace(I,n??``).replace(L,r??``)}`}};let Z=class extends t.RxDisposable{constructor(e,t,n,r,i,a){super(),this._univerInstanceService=e,this._dataValidatorRegistryService=t,this._injector=n,this._selectionManagerService=r,this._sheetInterceptorService=i,this._sheetDataValidationModel=a,this._init()}_init(){this._registerValidators(),this._initCommandInterceptor()}_registerValidators(){[Te,ce,Oe,pe,V,re,U,De,Ee].forEach(e=>{let n=this._injector.createInstance(e);this.disposeWithMe(this._dataValidatorRegistryService.register(n)),this.disposeWithMe((0,t.toDisposable)(()=>this._injector.delete(e)))})}_initCommandInterceptor(){this._sheetInterceptorService.interceptCommand({getMutations:e=>{if(e.id===i.ClearSelectionAllCommand.id){var n;let e=this._univerInstanceService.getCurrentUnitOfType(t.UniverInstanceType.UNIVER_SHEET),r=e.getUnitId(),i=e.getActiveSheet();if(!i)throw Error(`No active sheet found`);let a=i.getSheetId(),o=(n=this._selectionManagerService.getCurrentSelections())==null?void 0:n.map(e=>e.range),s=this._sheetDataValidationModel.getRuleObjectMatrix(r,a).clone();o&&s.removeRange(o);let{redoMutations:c,undoMutations:l}=G(r,a,s.diff(this._sheetDataValidationModel.getRules(r,a)),this._injector,`patched`);return{undos:l,redos:c}}return{undos:[],redos:[]}}})}};Z=h([m(0,t.IUniverInstanceService),m(1,(0,t.Inject)(n.DataValidatorRegistryService)),m(2,(0,t.Inject)(t.Injector)),m(3,(0,t.Inject)(i.SheetsSelectionsService)),m(4,(0,t.Inject)(i.SheetInterceptorService)),m(5,(0,t.Inject)(E))],Z);let Q=class extends t.Disposable{constructor(e,t,n,r){super(),this._univerInstanceService=e,this._sheetDataValidationModel=t,this._dataValidationCacheService=n,this._lifecycleService=r,this._initRecalculate()}_initRecalculate(){let e=e=>{if(e.length===0)return;let n=this._univerInstanceService.getCurrentUnitOfType(t.UniverInstanceType.UNIVER_SHEET),r=n==null?void 0:n.getActiveSheet(),i=Object.create(null);e.flat().forEach(e=>{i[e.unitId]||(i[e.unitId]=Object.create(null)),i[e.unitId][e.subUnitId]||(i[e.unitId][e.subUnitId]=[]);let n=this._univerInstanceService.getUnit(e.unitId,t.UniverInstanceType.UNIVER_SHEET),r=n==null?void 0:n.getSheetBySheetId(e.subUnitId);r&&i[e.unitId][e.subUnitId].push(...l(e.ranges,r))}),Object.entries(i).forEach(([e,t])=>{Object.entries(t).forEach(([t,i])=>{if((n==null?void 0:n.getUnitId())===e&&(r==null?void 0:r.getSheetId())===t)this.validatorRanges(e,t,i).catch(console.error);else{let n,r=this.disposeWithMe(()=>cancelIdleCallback(n));n=requestIdleCallback(()=>{r.dispose(),this.validatorRanges(e,t,i).catch(console.error)})}})})};this.disposeWithMe(this._dataValidationCacheService.dirtyRanges$.pipe((0,a.bufferWhen)(()=>this._lifecycleService.lifecycle$.pipe((0,a.skip)(1),(0,a.filter)(e=>e===t.LifecycleStages.Rendered)))).subscribe(e)),this.disposeWithMe(this._dataValidationCacheService.dirtyRanges$.pipe((0,a.filter)(()=>this._lifecycleService.stage>=t.LifecycleStages.Rendered),(0,t.bufferDebounceTime)(20)).subscribe(e))}async _validatorByCell(e,n,r,i){let a=e.getUnitId(),o=n.getSheetId();if(!t.Tools.isDefine(r)||!t.Tools.isDefine(i))throw Error(`row or col is not defined, row: ${r}, col: ${i}`);let s=r,c=i,l=n.getMergedCell(r,i);l&&(s=l.startRow,c=l.startColumn);let u=this._sheetDataValidationModel.getRuleByLocation(a,o,s,c);return u?new Promise(t=>{this._sheetDataValidationModel.validator(u,{unitId:a,subUnitId:o,row:s,col:c,worksheet:n,workbook:e},e=>{t(e)})}):t.DataValidationStatus.VALID}async validatorCell(e,n,r,i){let a=this._univerInstanceService.getUnit(e,t.UniverInstanceType.UNIVER_SHEET);if(!a)throw Error(`cannot find current workbook, unitId: ${e}`);let o=a.getSheetBySheetId(n);if(!o)throw Error(`cannot find current worksheet, sheetId: ${n}`);return this._validatorByCell(a,o,r,i)}async validatorRanges(e,n,r){if(!r.length)return Promise.resolve([]);let i=this._univerInstanceService.getUnit(e,t.UniverInstanceType.UNIVER_SHEET);if(!i)throw Error(`cannot find current workbook, unitId: ${e}`);let a=i.getSheetBySheetId(n);if(!a)throw Error(`cannot find current worksheet, sheetId: ${n}`);let o=l(r,a),s=l(this._sheetDataValidationModel.getRules(e,n).flatMap(e=>e.ranges),a),c=[];for(let e of o)for(let n of s){let r=(0,t.getIntersectRange)(e,n);r&&c.push(r)}let u=[],d=await Promise.all(c.map((e,t)=>{let n=[];for(let r=e.startRow;r<=e.endRow;r++)for(let o=e.startColumn;o<=e.endColumn;o++){n.push(this._validatorByCell(i,a,r,o));let e=a.getMergedCell(r,o);e&&u.push({resultRowIndex:t,resultColIndex:n.length-1,row:e.startRow,col:e.startColumn})}return Promise.all(n)}));return u.length&&u.forEach(({resultRowIndex:r,resultColIndex:i,row:a,col:o})=>{d[r][i]===t.DataValidationStatus.VALIDATING&&(d[r][i]=this._dataValidationCacheService.getValue(e,n,a,o)??t.DataValidationStatus.VALID)}),d}async validatorWorksheet(e,n){let r=this._univerInstanceService.getUnit(e,t.UniverInstanceType.UNIVER_SHEET);if(!r)throw Error(`cannot find current workbook, unitId: ${e}`);let i=r.getSheetBySheetId(n);if(!i)throw Error(`cannot find current worksheet, sheetId: ${n}`);let a=l(this._sheetDataValidationModel.getRules(e,n).flatMap(e=>e.ranges),i);return await Promise.all(a.map(e=>{let n=[];return t.Range.foreach(e,(e,t)=>{n.push(this._validatorByCell(r,i,e,t))}),Promise.all(n)})),this._dataValidationCacheService.ensureCache(e,n)}async validatorWorkbook(e){let t=this._sheetDataValidationModel.getSubUnitIds(e),n=await Promise.all(t.map(t=>this.validatorWorksheet(e,t))),r=Object.create(null);return n.forEach((e,n)=>{r[t[n]]=e}),r}getDataValidations(e,n,r){var i;let a=(i=this._univerInstanceService.getUnit(e,t.UniverInstanceType.UNIVER_SHEET))==null?void 0:i.getSheetBySheetId(n);if(!a)return[];let o=this._sheetDataValidationModel.getRuleObjectMatrix(e,n),s=/* @__PURE__ */ new Set;l(r,a).forEach(e=>{t.Range.foreach(e,(e,t)=>{let n=o.getValue(e,t);n&&s.add(n)})});let c=[];for(let t of s){let r=this._sheetDataValidationModel.getRuleById(e,n,t);r&&c.push(r)}return c}getDataValidation(e,t,n){return this.getDataValidations(e,t,n)[0]}};Q=h([m(0,t.IUniverInstanceService),m(1,(0,t.Inject)(E)),m(2,(0,t.Inject)(g)),m(3,(0,t.Inject)(t.LifecycleService))],Q);let $=class extends t.Plugin{constructor(e=we,n,r,i){super(),this._config=e,this._injector=n,this._commandService=r,this._configService=i;let{...a}=(0,t.merge)({},we,this._config);this._configService.setConfig(`sheets-data-validation.config`,a)}onStarting(){[[g],[w],[T],[x],[Q],[E],[Z],[q],[X],[Y],[J]].forEach(e=>{this._injector.add(e)}),[me,K,he,ge,be,ve,_e].forEach(e=>{this._commandService.registerCommand(e)}),this._injector.get(g),this._injector.get(Q),this._injector.get(Z),this._injector.get(J),this._injector.get(Y)}onReady(){this._injector.get(X)}onRendered(){this._injector.get(q)}};p($,`pluginName`,xe),p($,`packageName`,Se),p($,`version`,Ce),p($,`type`,t.UniverInstanceType.UNIVER_SHEET),$=h([(0,t.DependentOn)(n.UniverDataValidationPlugin,r.UniverFormulaEnginePlugin,i.UniverSheetsPlugin,s.UniverSheetsFormulaPlugin),m(1,(0,t.Inject)(t.Injector)),m(2,t.ICommandService),m(3,t.IConfigService)],$);function ke(e){let n=e.get(i.SheetsSelectionsService).getCurrentSelections().map(e=>e.range);return{uid:(0,t.generateRandomId)(6),type:t.DataValidationType.DECIMAL,operator:t.DataValidationOperator.EQUAL,formula1:`100`,ranges:n??[{startColumn:0,endColumn:0,startRow:0,endRow:0}]}}e.AddSheetDataValidationCommand=me,e.BASE_FORMULA_INPUT_NAME=`sheets-data-validation.formula-input`,e.CHECKBOX_FORMULA_1=1,e.CHECKBOX_FORMULA_2=0,e.CHECKBOX_FORMULA_INPUT_NAME=`sheets-data-validation.checkbox-formula-input`,e.CUSTOM_FORMULA_INPUT_NAME=`sheets-data-validation.custom-formula-input`,e.CheckboxValidator=re,e.ClearRangeDataValidationCommand=_e,e.DATA_VALIDATION_PLUGIN_NAME=xe,Object.defineProperty(e,"DataValidationCacheService",{enumerable:!0,get:function(){return g}}),Object.defineProperty(e,"DataValidationCustomFormulaService",{enumerable:!0,get:function(){return x}}),Object.defineProperty(e,"DataValidationFormulaController",{enumerable:!0,get:function(){return q}}),Object.defineProperty(e,"DataValidationFormulaService",{enumerable:!0,get:function(){return T}}),Object.defineProperty(e,"DataValidationListCacheService",{enumerable:!0,get:function(){return w}}),e.DateValidator=V,e.LIST_FORMULA_INPUT_NAME=`sheets-data-validation.list-formula-input`,e.ListMultipleValidator=De,e.ListValidator=U,e.RemoveSheetAllDataValidationCommand=ve,e.RemoveSheetDataValidationCommand=be,Object.defineProperty(e,"SheetDataValidationModel",{enumerable:!0,get:function(){return E}}),Object.defineProperty(e,"SheetsDataValidationValidatorService",{enumerable:!0,get:function(){return Q}}),Object.defineProperty(e,"UniverSheetsDataValidationPlugin",{enumerable:!0,get:function(){return $}}),e.UpdateSheetDataValidationOptionsCommand=ge,e.UpdateSheetDataValidationRangeCommand=K,e.UpdateSheetDataValidationSettingCommand=he,e.createDefaultNewRule=ke,e.getCellValueNumber=H,e.getCellValueOrigin=S,e.getDataValidationCellValue=se,e.getDataValidationDiffMutations=G,e.getFormulaCellData=v,e.getFormulaResult=_,e.getRangesInWorksheet=l,e.getTransformedFormula=R,e.isLegalFormulaResult=y,e.transformCheckboxValue=P});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@univerjs/sheets-data-validation",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "private": false,
5
5
  "description": "Sheet-specific data validation commands, services, and Facade APIs for Univer Sheets.",
6
6
  "author": "DreamNum Co., Ltd. <developer@univer.ai>",
@@ -67,16 +67,16 @@
67
67
  "rxjs": ">=7.0.0"
68
68
  },
69
69
  "dependencies": {
70
- "@univerjs/core": "1.1.0",
71
- "@univerjs/data-validation": "1.1.0",
72
- "@univerjs/engine-formula": "1.1.0",
73
- "@univerjs/protocol": "1.1.0",
74
- "@univerjs/sheets": "1.1.0",
75
- "@univerjs/sheets-formula": "1.1.0"
70
+ "@univerjs/core": "1.3.0",
71
+ "@univerjs/data-validation": "1.3.0",
72
+ "@univerjs/engine-formula": "1.3.0",
73
+ "@univerjs/protocol": "1.3.0",
74
+ "@univerjs/sheets": "1.3.0",
75
+ "@univerjs/sheets-formula": "1.3.0"
76
76
  },
77
77
  "devDependencies": {
78
- "@univerjs-infra/shared": "1.1.0",
79
- "rxjs": "^7.8.2",
78
+ "@univerjs-infra/shared": "1.3.0",
79
+ "rxjs": "7.8.2",
80
80
  "typescript": "^6.0.3",
81
81
  "vitest": "^5.0.4"
82
82
  },