@univerjs/sheets-ui 0.6.0-nightly.202502171606 → 0.6.0-nightly.202502191606

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
@@ -1,4 +1,4 @@
1
- "use strict";var _d=Object.defineProperty;var Id=(r,i,e)=>i in r?_d(r,i,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[i]=e;var E=(r,i,e)=>Id(r,typeof i!="symbol"?i+"":i,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const $=require("@univerjs/docs-ui"),a=require("@univerjs/core"),w=require("@univerjs/engine-render"),_=require("@univerjs/ui"),V=require("react"),O=require("rxjs"),m=require("@univerjs/sheets"),Re=require("@univerjs/docs"),K=require("@univerjs/engine-formula"),bd=require("@univerjs/engine-numfmt"),Z=require("@univerjs/design"),I=require("react/jsx-runtime"),Rd=require("@univerjs/telemetry"),Ka=require("rxjs/operators");function wi(r,i){if(r==null)return;const e=i.getRenderById(r);if(e==null)return;const{mainComponent:t,scene:n,engine:o,components:s}=e,l=t,c=s.get($.DOCS_VIEW_KEY.BACKGROUND);return{document:l,docBackground:c,scene:n,engine:o}}const ko=[10,400],hs="RANGE_SELECTOR_COMPONENT_KEY",yi="EMBEDDING_FORMULA_EDITOR_COMPONENT_KEY";var me=(r=>(r.MAIN="__SpreadsheetRender__",r.ROW="__SpreadsheetRowHeader__",r.COLUMN="__SpreadsheetColumnHeader__",r.LEFT_TOP="__SpreadsheetLeftTopPlaceholder__",r))(me||{});const zs=0,Jr=1,It=10,Ed=11,Xs=12,Mi={id:"sheet.command.set-row-header-width",type:a.CommandType.COMMAND,handler:async(r,i)=>{if(!i)return!1;const e=r.get(w.IRenderManagerService);if(!e)return!1;const{unitId:t,subUnitId:n,size:o}=i,s=e.getRenderById(t);if(s){e.getRenderById(t).with(exports.SheetSkeletonManagerService).setRowHeaderSize(s,n,o);const{components:c}=s,d=c.get(me.ROW);d&&d.setCustomHeader({headerStyle:{size:o}})}return!0}},Pi={id:"sheet.command.set-col-header-height",type:a.CommandType.COMMAND,handler:async(r,i)=>{if(!i)return!1;const e=r.get(w.IRenderManagerService);if(!e)return!1;const{unitId:t,subUnitId:n,size:o}=i,s=e.getRenderById(t);if(s){e.getRenderById(t).with(exports.SheetSkeletonManagerService).setColumnHeaderSize(s,n,o);const{components:c}=s,d=c.get(me.COLUMN);d&&d.setCustomHeader({headerStyle:{size:o}})}return!0}},Vo=a.createInterceptorKey("rangeMovePermissionCheck"),Wo=a.createInterceptorKey("rangeFillPermissionCheck");var ms=(r=>(r[r.FORMULA_EDITOR_SHOW=100]="FORMULA_EDITOR_SHOW",r[r.MARK_SELECTION=1e4]="MARK_SELECTION",r))(ms||{});function bt(r){const i=r.getCurrentTheme(),e=new a.ColorKit(i.primaryColor).setAlpha(.07).toRgbString();return{strokeWidth:1,stroke:i.primaryColor,fill:e,widgets:{},widgetSize:6,widgetStrokeWidth:1,widgetStroke:i.colorWhite,autofillSize:6,autofillStrokeWidth:1,autofillStroke:i.colorWhite,rowHeaderFill:e,rowHeaderStroke:i.primaryColor,rowHeaderStrokeWidth:1,columnHeaderFill:e,columnHeaderStroke:i.primaryColor,columnHeaderStrokeWidth:1,expandCornerSize:40}}class Td{constructor(){E(this,"_startColumn",-1);E(this,"_startRow",-1);E(this,"_endColumn",-1);E(this,"_endRow",-1);E(this,"_startX",0);E(this,"_startY",0);E(this,"_endX",0);E(this,"_endY",0);E(this,"_primary");E(this,"_rangeType",a.RANGE_TYPE.NORMAL)}get startColumn(){return this._startColumn}get startRow(){return this._startRow}get endColumn(){return this._endColumn}get endRow(){return this._endRow}get startX(){return this._startX}get startY(){return this._startY}get endX(){return this._endX}get endY(){return this._endY}get currentCell(){return this._primary}get rangeType(){return this._rangeType}isEqual(i){const{startColumn:e,startRow:t,endColumn:n,endRow:o}=this,{startColumn:s,startRow:l,endColumn:c,endRow:d}=i;return e===s&&t===l&&n===c&&o===d}isInclude(i){const{startColumn:e,startRow:t,endColumn:n,endRow:o}=this,{startColumn:s,startRow:l,endColumn:c,endRow:d}=i;return!(c<e||s>n||l>o||d<t)}highlightToSelection(){if(this._primary)return a.convertCellToRange(this._primary)}getRange(){return{startColumn:this._startColumn,startRow:this._startRow,endColumn:this._endColumn,endRow:this._endRow,startX:this._startX,startY:this._startY,endX:this._endX,endY:this._endY,rangeType:this.rangeType}}getCell(){return this._primary}getRangeType(){return this._rangeType}setRangeType(i){this._rangeType=i}getValue(){return{rangeWithCoord:this.getRange(),primaryWithCoord:this._primary}}setValue(i,e){const{startColumn:t,startRow:n,endColumn:o,endRow:s,startX:l,startY:c,endX:d,endY:u,rangeType:h}=i;this._startColumn=t,this._startRow=n,this._endColumn=o,this._endRow=s,this._startX=l,this._startY=c,this._endX=d,this._endY=u,h!==void 0&&(this._rangeType=h),e!==void 0&&this.setCurrentCell(e)}setCurrentCell(i){this._primary=i}clearCurrentCell(){this._primary=null}}function bn(r,i){const{startRow:e,startColumn:t,endRow:n,endColumn:o,rangeType:s}=i,l=n<e?n:e,c=n<e?e:n,d=o<t?o:t,u=o<t?t:o,h=r.getNoMergeCellWithCoordByIndex(l,d),g=r.getNoMergeCellWithCoordByIndex(c,u);return{startRow:e,startColumn:t,endRow:n,endColumn:o,rangeType:s,startY:(h==null?void 0:h.startY)||0,endY:(g==null?void 0:g.endY)||0,startX:(h==null?void 0:h.startX)||0,endX:(g==null?void 0:g.endX)||0}}function Me(r,i){const{range:e,primary:t,style:n}=r,o=bn(i,e),s=t&&Jn(i,t);return{rangeWithCoord:o,primaryWithCoord:s,style:n}}function Jn(r,i){const{actualRow:e,actualColumn:t,isMerged:n,isMergedMainCell:o,startRow:s,startColumn:l,endRow:c,endColumn:d}=i,u=r.getNoMergeCellWithCoordByIndex(e,t),h=r.getNoMergeCellWithCoordByIndex(s,l),g=r.getNoMergeCellWithCoordByIndex(c,d);return{actualRow:e,actualColumn:t,isMerged:n,isMergedMainCell:o,startX:u.startX,startY:u.startY,endX:u.endX,endY:u.endY,mergeInfo:{startRow:s,startColumn:l,endRow:c,endColumn:d,startY:(h==null?void 0:h.startY)||0,endY:(g==null?void 0:g.endY)||0,startX:(h==null?void 0:h.startX)||0,endX:(g==null?void 0:g.endX)||0}}}const Ys="__SpreadsheetHelperSelectionTempRect",wd=35;class qa{constructor(i,e){E(this,"_startOffsetX",0);E(this,"_startOffsetY",0);E(this,"_relativeSelectionPositionRow",0);E(this,"_relativeSelectionPositionColumn",0);E(this,"_relativeSelectionRowLength",0);E(this,"_relativeSelectionColumnLength",0);E(this,"_scenePointerMoveSub");E(this,"_scenePointerUpSub");E(this,"_helperSelection");E(this,"_scrollTimer");E(this,"_activeViewport");E(this,"_targetSelection",{startY:0,endY:0,startX:0,endX:0,startRow:-1,endRow:-1,startColumn:-1,endColumn:-1});E(this,"_isInMergeState",!1);E(this,"_fillControlColors",[]);E(this,"_skeleton");E(this,"_scene");E(this,"_themeService");E(this,"_injector");E(this,"_selectionHooks");this._control=i,this._skeleton=e.skeleton,this._scene=e.scene,this._themeService=e.themeService,this._injector=e.injector,this._selectionHooks=e.selectionHooks,this._initialControl(),this._initialWidget(),this._initialAutoFill(),this._control.dispose$.subscribe(()=>{this.dispose()})}get isHelperSelection(){return this._control.isHelperSelection}dispose(){var i,e;(i=this._scrollTimer)==null||i.dispose(),this._fillControlColors=[],this._clearObserverEvent(),(e=this._helperSelection)==null||e.dispose()}_getFreeze(){var t,n;return(n=(t=this._injector.get(w.IRenderManagerService).withCurrentTypeOfUnit(a.UniverInstanceType.UNIVER_SHEET,exports.SheetSkeletonManagerService))==null?void 0:t.getCurrentParam())==null?void 0:n.skeleton.getWorksheetConfig().freeze}_isSelectionInViewport(i,e){const t=this._getFreeze()||{startRow:-1,startColumn:-1,xSplit:0,ySplit:0};switch(e.viewportKey){case w.SHEET_VIEWPORT_KEY.VIEW_MAIN:return i.endRow>=t.startRow&&i.endColumn>=t.startColumn;case w.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP:case w.SHEET_VIEWPORT_KEY.VIEW_COLUMN_RIGHT:return i.endColumn>=t.startColumn&&i.startRow<t.startRow;case w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT:case w.SHEET_VIEWPORT_KEY.VIEW_ROW_BOTTOM:return i.endRow>=t.startRow&&i.startColumn<t.startColumn;case w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP:case w.SHEET_VIEWPORT_KEY.VIEW_COLUMN_LEFT:case w.SHEET_VIEWPORT_KEY.VIEW_ROW_TOP:case w.SHEET_VIEWPORT_KEY.VIEW_LEFT_TOP:return i.startRow<t.startRow&&i.startColumn<t.startColumn}}_clearObserverEvent(){var i,e;(i=this._scenePointerMoveSub)==null||i.unsubscribe(),(e=this._scenePointerUpSub)==null||e.unsubscribe(),this._scenePointerMoveSub=null,this._scenePointerUpSub=null}_initialControl(){const{leftControl:i,rightControl:e,topControl:t,bottomControl:n}=this._control;[i,e,t,n].forEach(o=>{o.onPointerEnter$.subscribeEvent(()=>{var l;((l=this._injector.get(Ce,a.Quantity.OPTIONAL))==null?void 0:l.interceptor.fetchThroughInterceptors(Vo)(!1,null))!==!1&&o.setCursor(w.CURSOR_TYPE.MOVE)}),o.onPointerLeave$.subscribeEvent(()=>{o.resetCursor()}),o.onPointerDown$.subscribeEvent(this._controlPointerDownHandler.bind(this))})}_controlMoving(i,e){var A;const t=this._scene,n=t.getScrollXYInfoByViewport(w.Vector2.FromArray([i,e])),{scaleX:o,scaleY:s}=t.getAncestorScale(),l=this._skeleton.getCellIndexByOffset(i,e,o,s,n),{row:c,column:d}=l,u=this._skeleton.getRowCount()-1,h=this._skeleton.getColumnCount()-1;let g=Math.max(0,c+this._relativeSelectionPositionRow),S=g+this._relativeSelectionRowLength;S>u&&(S=u,S-g<this._relativeSelectionRowLength&&(g=S-this._relativeSelectionRowLength));let f=Math.max(0,d+this._relativeSelectionPositionColumn),p=f+this._relativeSelectionColumnLength;p>h&&(p=h,p-f<this._relativeSelectionColumnLength&&(f=p-this._relativeSelectionColumnLength));const C=this._skeleton.worksheet.getCellInfoInMergeData(g,f),b=Me({range:{startRow:g,endRow:S,startColumn:f,endColumn:p},primary:C,style:null},this._skeleton),R=this._skeleton.getNoMergeCellWithCoordByIndex(g,f),T=this._skeleton.getNoMergeCellWithCoordByIndex(S,p),M=(R==null?void 0:R.startY)||0,y=(T==null?void 0:T.endY)||0,P=(R==null?void 0:R.startX)||0,x=(T==null?void 0:T.endX)||0;(A=this._helperSelection)==null||A.transformByState({left:P,top:M,width:x-P,height:y-M}),this._targetSelection={...b.rangeWithCoord},this._control.selectionMoving$.next(b.rangeWithCoord)}_controlPointerDownHandler(i){const{offsetX:e,offsetY:t}=i,n=this._scene,o=n.getCoordRelativeToViewport(w.Vector2.FromArray([e,t])),{x:s,y:l}=o,c=n.getScrollXYInfoByViewport(o),{scaleX:d,scaleY:u}=n.getAncestorScale(),h=this._skeleton.getCellIndexByOffset(s,l,d,u,c);this._startOffsetX=s,this._startOffsetY=l;const{row:g,column:S}=h,{startRow:f,startColumn:p,endRow:C,endColumn:v}=this._control.model;let b=0,R=0;g<f?b-=1:g>C&&(b+=1),S<p?R-=1:S>v&&(R+=1),this._relativeSelectionPositionRow=f-g+b,this._relativeSelectionPositionColumn=p-S+R,this._relativeSelectionRowLength=C-f,this._relativeSelectionColumnLength=v-p;const T=this._control.currentStyle,M=this._getScale();this.isHelperSelection&&(this._helperSelection=new w.Rect(Ys,{stroke:T.stroke,strokeWidth:T.strokeWidth/M}),n.addObject(this._helperSelection));const y=n.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN),P=w.ScrollTimer.create(n);this._scrollTimer=P,P.startScroll(s,l,y),n.disableObjectsEvent(),this._scenePointerMoveSub=n.onPointerMove$.subscribeEvent(x=>{var k;const{offsetX:A,offsetY:N}=x;if(((k=this._injector.get(Ce,a.Quantity.OPTIONAL))==null?void 0:k.interceptor.fetchThroughInterceptors(Vo)(!1,null))===!1)return;const{x:H,y:W}=n.getCoordRelativeToViewport(w.Vector2.FromArray([A,N]));this._controlMoving(H,W),n.setCursor(w.CURSOR_TYPE.MOVE),P.scrolling(H,W,()=>{this._controlMoving(H,W)})}),this._scenePointerUpSub=n.onPointerUp$.subscribeEvent(()=>{var A,N,D,H;(A=this._helperSelection)==null||A.dispose();const x=this._scene;x.resetCursor(),this._clearObserverEvent(),x.enableObjectsEvent(),(N=this._scrollTimer)==null||N.dispose(),this._control.selectionMoveEnd$.next(this._targetSelection),(H=(D=this._selectionHooks).selectionMoveEnd)==null||H.call(D)})}_initialWidget(){const{topLeftWidget:i,topCenterWidget:e,topRightWidget:t,middleLeftWidget:n,middleRightWidget:o,bottomLeftWidget:s,bottomCenterWidget:l,bottomRightWidget:c}=this._control,d=[w.CURSOR_TYPE.NORTH_WEST_RESIZE,w.CURSOR_TYPE.NORTH_RESIZE,w.CURSOR_TYPE.NORTH_EAST_RESIZE,w.CURSOR_TYPE.WEST_RESIZE,w.CURSOR_TYPE.EAST_RESIZE,w.CURSOR_TYPE.SOUTH_WEST_RESIZE,w.CURSOR_TYPE.SOUTH_RESIZE,w.CURSOR_TYPE.SOUTH_EAST_RESIZE];[i,e,t,n,o,s,l,c].forEach((u,h)=>{u.onPointerEnter$.subscribeEvent(()=>{u.setCursor(d[h])}),u.onPointerLeave$.subscribeEvent(()=>{u.resetCursor()}),u.onPointerDown$.subscribeEvent(g=>{this._widgetPointerDownEvent(g,d[h])})})}_widgetPointerDownEvent(i,e){const t=this._scene,{offsetX:n,offsetY:o}=i,s=t.getCoordRelativeToViewport(w.Vector2.FromArray([n,o])),{x:l,y:c}=s;this._startOffsetX=n,this._startOffsetY=o;const{startRow:d,startColumn:u,endRow:h,endColumn:g}=this._control.model,S=Math.min(d,h),f=Math.min(u,g),p=Math.max(d,h),C=Math.max(u,g);this._relativeSelectionPositionRow=S,this._relativeSelectionPositionColumn=f,this._relativeSelectionRowLength=p-S,this._relativeSelectionColumnLength=C-f,e===w.CURSOR_TYPE.NORTH_WEST_RESIZE?(this._relativeSelectionPositionRow=p,this._relativeSelectionPositionColumn=C):e===w.CURSOR_TYPE.NORTH_RESIZE?this._relativeSelectionPositionRow=p:e===w.CURSOR_TYPE.NORTH_EAST_RESIZE?this._relativeSelectionPositionRow=p:e===w.CURSOR_TYPE.WEST_RESIZE?this._relativeSelectionPositionColumn=C:e===w.CURSOR_TYPE.SOUTH_WEST_RESIZE?this._relativeSelectionPositionColumn=C:e===w.CURSOR_TYPE.SOUTH_RESIZE&&(this._relativeSelectionPositionRow=S);const v=w.ScrollTimer.create(t),b=t.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN);v.startScroll(l,c,b),this._scrollTimer=v,t.disableObjectsEvent(),this._scenePointerMoveSub=t.onPointerMove$.subscribeEvent(R=>{const{offsetX:T,offsetY:M}=R,{x:y,y:P}=t.getCoordRelativeToViewport(w.Vector2.FromArray([T,M]));this._widgetMoving(y,P,e),t.setCursor(e),v.scrolling(y,P,()=>{this._widgetMoving(y,P,e)})}),this._scenePointerUpSub=t.onPointerUp$.subscribeEvent(()=>{var T,M,y;const R=this._scene;R.resetCursor(),this._clearObserverEvent(),R.enableObjectsEvent(),(T=this._scrollTimer)==null||T.dispose(),this._control.selectionScaled$.next(this._targetSelection),(y=(M=this._selectionHooks).selectionMoveEnd)==null||y.call(M)})}_widgetMoving(i,e,t){const n=this._scene,o=n.getScrollXYInfoByViewport(w.Vector2.FromArray([this._startOffsetX,this._startOffsetY])),{scaleX:s,scaleY:l}=n.getAncestorScale(),c=this._skeleton.getCellIndexByOffset(i,e,s,l,o),{row:d,column:u}=c;let h=this._relativeSelectionPositionRow,g=this._relativeSelectionPositionColumn,S=d,f=u;t===w.CURSOR_TYPE.NORTH_WEST_RESIZE?(h=d,g=u,S=this._relativeSelectionPositionRow,f=this._relativeSelectionPositionColumn):t===w.CURSOR_TYPE.NORTH_RESIZE?(h=d,g=this._relativeSelectionPositionColumn,S=this._relativeSelectionPositionRow,f=this._relativeSelectionPositionColumn+this._relativeSelectionColumnLength):t===w.CURSOR_TYPE.NORTH_EAST_RESIZE?(h=d,g=this._relativeSelectionPositionColumn,S=this._relativeSelectionPositionRow,f=u):t===w.CURSOR_TYPE.WEST_RESIZE?(h=this._relativeSelectionPositionRow,g=u,S=this._relativeSelectionPositionRow+this._relativeSelectionRowLength,f=this._relativeSelectionPositionColumn):t===w.CURSOR_TYPE.EAST_RESIZE?S=this._relativeSelectionPositionRow+this._relativeSelectionRowLength:t===w.CURSOR_TYPE.SOUTH_WEST_RESIZE?(h=this._relativeSelectionPositionRow,g=u,S=d,f=this._relativeSelectionPositionColumn):t===w.CURSOR_TYPE.SOUTH_RESIZE&&(h=this._relativeSelectionPositionRow,g=this._relativeSelectionPositionColumn,S=d,f=this._relativeSelectionPositionColumn+this._relativeSelectionColumnLength);const p=this._swapPositions(h,g,S,f),C=this._skeleton.getCellWithMergeInfoByIndex(h,g),b=Me({range:p,primary:C,style:null},this._skeleton);this._targetSelection={...b.rangeWithCoord},this._control.updateRangeBySelectionWithCoord(b),this._control.selectionScaling$.next(this._targetSelection)}_initialAutoFill(){const{fillControl:i}=this._control;i.onPointerEnter$.subscribeEvent(e=>{this._injector.get(Ce).interceptor.fetchThroughInterceptors(Wo)(!1,{x:e.offsetX,y:e.offsetY,skeleton:this._skeleton,scene:this._scene})&&i.setCursor(w.CURSOR_TYPE.CROSSHAIR)}),i.onPointerLeave$.subscribeEvent(()=>{i.resetCursor()}),i.onPointerDown$.subscribeEvent(this._autoFillForPointerdown.bind(this))}_autoFillMoving(i,e){var N,D,H;const t=this._scene,n=t.getViewportScrollXY(this._activeViewport),{scaleX:o,scaleY:s}=t.getAncestorScale(),l=this._skeleton.getCellIndexByOffset(i,e,o,s,n),{row:c,column:d}=l,u=t.getCoordRelativeToViewport(w.Vector2.FromArray([i,e])),h=this._skeleton.getRowCount()-1,g=this._skeleton.getColumnCount()-1;let S=this._relativeSelectionPositionRow,f=this._relativeSelectionPositionColumn,p=this._relativeSelectionPositionRow+this._relativeSelectionRowLength,C=this._relativeSelectionPositionColumn+this._relativeSelectionColumnLength,v=!1,b=!0;if((d<f||d>C)&&c>=S&&c<=p){const W=this._fillRuler(d,f,C,this._relativeSelectionColumnLength,g);f=W.startRowOrColumn,C=W.endRowOrColumn,v=W.isLighten,b=!1}else if((c<S||c>p)&&d>=f&&d<=C){const W=this._fillRuler(c,S,p,this._relativeSelectionRowLength,h);S=W.startRowOrColumn,p=W.endRowOrColumn,v=W.isLighten}else if(Math.abs(this._startOffsetX-u.x-n.x)/2>Math.abs(this._startOffsetY-u.y-n.y)){const W=this._fillRuler(d,f,C,this._relativeSelectionColumnLength,g);f=W.startRowOrColumn,C=W.endRowOrColumn,v=W.isLighten,b=!1}else{const W=this._fillRuler(c,S,p,this._relativeSelectionRowLength,h);S=W.startRowOrColumn,p=W.endRowOrColumn,v=W.isLighten}const R=this._skeleton.getNoMergeCellWithCoordByIndex(S,f),T=this._skeleton.getNoMergeCellWithCoordByIndex(p,C),M=(R==null?void 0:R.startY)||0,y=(T==null?void 0:T.endY)||0,P=(R==null?void 0:R.startX)||0,x=(T==null?void 0:T.endX)||0;v?this._controlHandler((W,k)=>{const U=new a.ColorKit(this._fillControlColors[k]).lighten(wd).toRgbString();W.setProps({fill:U})}):this._controlHandler((W,k)=>{W.setProps({fill:this._fillControlColors[k]})});const A=m.SELECTION_CONTROL_BORDER_BUFFER_WIDTH/this._getScale();S===p&&b===!0||f===C&&b===!1?(N=this._helperSelection)==null||N.hide():((D=this._helperSelection)==null||D.transformByState({left:P-A/2,top:M-A/2,width:x-P,height:y-M}),(H=this._helperSelection)==null||H.show()),this._targetSelection={startY:M,endY:y,startX:P,endX:x,startRow:S,endRow:p,startColumn:f,endColumn:C},this._control.selectionFilling$.next(this._targetSelection)}_autoFillForPointerdown(i){const{offsetX:e,offsetY:t}=i,n=this._scene,o=n.getCoordRelativeToViewport(w.Vector2.FromArray([e,t])),{x:s,y:l}=o;this._startOffsetX=s,this._startOffsetY=l;const{startRow:c,startColumn:d,endRow:u,endColumn:h}=this._control.model;this._isInMergeState=this._hasMergeInRange(c,d,u,h),this._relativeSelectionPositionRow=c,this._relativeSelectionPositionColumn=d,this._relativeSelectionRowLength=u-c,this._relativeSelectionColumnLength=h-d;const g=this._control.currentStyle;let S=g==null?void 0:g.stroke,f=g==null?void 0:g.strokeWidth;const p=bt(this._themeService);S==null&&(S=p.stroke),f==null&&(f=p.strokeWidth);const C=this._getScale();f/=C;const v=m.SELECTION_CONTROL_BORDER_BUFFER_WIDTH/C,b=new a.ColorKit(S).darken(2).toRgbString();this.isHelperSelection&&(this._helperSelection=new w.Rect(Ys,{stroke:b,strokeWidth:f+v/2}),n.addObject(this._helperSelection)),this._activeViewport=n.getActiveViewportByCoord(w.Vector2.FromArray([e,t]));const R=n.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN),T=w.ScrollTimer.create(n,this._activeViewport.viewportKey===w.SHEET_VIEWPORT_KEY.VIEW_MAIN?w.ScrollTimerType.ALL:w.ScrollTimerType.NONE);T.startScroll(s,l,R),this._scrollTimer=T,n.disableObjectsEvent(),this._controlHandler(M=>{this._fillControlColors.push(M.fill)}),this._scenePointerMoveSub=n.onPointerMove$.subscribeEvent(M=>{var W;const{offsetX:y,offsetY:P}=M,x=n.getActiveViewportByCoord(w.Vector2.FromArray([y,P]));if(!this._injector.get(Ce).interceptor.fetchThroughInterceptors(Wo)(!1,{x:i.offsetX,y:i.offsetY,skeleton:this._skeleton,scene:this._scene}))return;const{x:N,y:D}=n.getCoordRelativeToViewport(w.Vector2.FromArray([y,P]));this._autoFillMoving(N,D),n.setCursor(w.CURSOR_TYPE.CROSSHAIR);const H=this._targetSelection;if(R&&x&&((W=this._activeViewport)==null?void 0:W.viewportKey)!==(x==null?void 0:x.viewportKey)){let k;H.startRow!==c?(T.scrollTimerType=w.ScrollTimerType.Y,k={...H,endRow:H.startRow}):H.endRow!==u?(T.scrollTimerType=w.ScrollTimerType.Y,k={...H,startRow:H.endRow}):H.startColumn!==d?(T.scrollTimerType=w.ScrollTimerType.X,k={...H,endColumn:H.startColumn}):(T.scrollTimerType=w.ScrollTimerType.X,k={...H,startColumn:H.endColumn}),this._isSelectionInViewport(k,x)&&(R.scrollToBarPos({x:T.scrollTimerType===w.ScrollTimerType.X?0:void 0,y:T.scrollTimerType===w.ScrollTimerType.Y?0:void 0}),this._activeViewport=x)}T.scrolling(N,D,()=>{this._autoFillMoving(N,D)})}),this._scenePointerUpSub=n.onPointerUp$.subscribeEvent(()=>{var y,P;(y=this._helperSelection)==null||y.dispose();const M=this._scene;M.resetCursor(),this._clearObserverEvent(),M.enableObjectsEvent(),(P=this._scrollTimer)==null||P.dispose(),this._control.refreshSelectionFilled(this._targetSelection),this._isInMergeState=!1,this._controlHandler((x,A)=>{x.setProps({fill:this._fillControlColors[A]})}),this._fillControlColors=[]})}_hasMergeInRange(i,e,t,n){return this._skeleton.worksheet.getMergedCellRange(i,e,t,n).length>0}_swapPositions(i,e,t,n){const o=Math.min(i,t),s=Math.min(e,n),l=Math.max(i,t),c=Math.max(e,n);return{startRow:o,startColumn:s,endRow:l,endColumn:c}}_controlHandler(i){const{leftControl:e,rightControl:t,topControl:n,bottomControl:o,backgroundControlTop:s,backgroundControlMiddleLeft:l,backgroundControlMiddleRight:c,backgroundControlBottom:d,fillControl:u}=this._control,h=[e,t,n,o,s,l,c,d,u];for(let g=0,S=h.length;g<S;g++){const f=h[g];i(f,g)}}_fillRuler(i,e,t,n,o){let s=!1;if(i<e)if(this._isInMergeState&&i<e){const l=e-i,c=n+1,d=Math.ceil(l/c);let u=e-d*c;u<0&&(u=e-(d-1)*c),e=u}else e=i;else if(i>=e&&i<=t)s=!0,this._isInMergeState||(t=i);else if(this._isInMergeState&&i>t){const l=i-t,c=n+1,d=Math.ceil(l/c);let u=t+d*c;u>o&&(u=t+(d-1)*c),t=u}else t=i;return{rowOrColumn:i,startRowOrColumn:e,endRowOrColumn:t,isLighten:s}}_getScale(){const{scaleX:i,scaleY:e}=this._scene.getAncestorScale();return Math.max(i,e)}}var Uo=(r=>(r.Selection="__SpreadsheetSelectionShape__",r.top="__SpreadsheetSelectionTopControl__",r.bottom="__SpreadsheetSelectionBottomControl__",r.left="__SpreadsheetSelectionShapeLeftControl__",r.right="__SpreadsheetSelectionShapeRightControl__",r.backgroundTop="__SpreadsheetSelectionBackgroundControlTop__",r.backgroundMiddleLeft="__SpreadsheetSelectionBackgroundControlMiddleLeft__",r.backgroundMiddleRight="__SpreadsheetSelectionBackgroundControlMiddleRight__",r.backgroundBottom="__SpreadsheetSelectionBackgroundControlBottom__",r.fill="__SpreadsheetSelectionFillControl__",r.fillTopLeft="__SpreadsheetSelectionFillControlTopLeft__",r.fillBottomRight="__SpreadsheetSelectionFillControlBottomRight__",r.fillTopLeftInner="__SpreadsheetSelectionFillControlTopLeftInner__",r.fillBottomRightInner="__SpreadsheetSelectionFillControlBottomRightInner__",r.lineMain="__SpreadsheetDragLineMainControl__",r.lineContent="__SpreadsheetDragLineContentControl__",r.line="__SpreadsheetDragLineControl__",r.dash="__SpreadsheetDragDashControl__",r.rowHeaderBackground="__SpreadSheetSelectionRowHeaderBackground__",r.rowHeaderBorder="__SpreadSheetSelectionRowHeaderBorder__",r.rowHeaderGroup="__SpreadSheetSelectionRowHeaderGroup__",r.columnHeaderBackground="__SpreadSheetSelectionColumnHeaderBackground__",r.columnHeaderBorder="__SpreadSheetSelectionColumnHeaderBorder__",r.columnHeaderGroup="__SpreadSheetSelectionColumnHeaderGroup__",r.topLeftWidget="__SpreadSheetSelectionTopLeftWidget__",r.topCenterWidget="__SpreadSheetSelectionTopCenterWidget__",r.topRightWidget="__SpreadSheetSelectionTopRightWidget__",r.middleLeftWidget="__SpreadSheetSelectionMiddleLeftWidget__",r.middleRightWidget="__SpreadSheetSelectionMiddleRightWidget__",r.bottomLeftWidget="__SpreadSheetSelectionBottomLeftWidget__",r.bottomCenterWidget="__SpreadSheetSelectionBottomCenterWidget__",r.bottomRightWidget="__SpreadSheetSelectionBottomRightWidget__",r))(Uo||{});const Gs=.3;class lr extends a.Disposable{constructor(e,t,n,o){var s,l,c,d;super();E(this,"_isHelperSelection",!0);E(this,"_enableAutoFill",!0);E(this,"_showAutoFill",!1);E(this,"_highlightHeader",!0);E(this,"_selectionRenderModel");E(this,"_leftBorder");E(this,"_rightBorder");E(this,"_topBorder");E(this,"_bottomBorder");E(this,"_backgroundControlTop");E(this,"_backgroundControlBottom");E(this,"_backgroundControlMiddleLeft");E(this,"_backgroundControlMiddleRight");E(this,"_autoFillControl");E(this,"_selectionShapeGroup");E(this,"_rowHeaderBackground");E(this,"_rowHeaderBorder");E(this,"_rowHeaderGroup");E(this,"_columnHeaderBackground");E(this,"_columnHeaderBorder");E(this,"_columnHeaderGroup");E(this,"_dashedRect");E(this,"_topLeftWidget");E(this,"_topCenterWidget");E(this,"_topRightWidget");E(this,"_middleLeftWidget");E(this,"_middleRightWidget");E(this,"_bottomLeftWidget");E(this,"_bottomCenterWidget");E(this,"_bottomRightWidget");E(this,"_defaultStyle");E(this,"_currentStyle");E(this,"_rowHeaderWidth",0);E(this,"_columnHeaderHeight",0);E(this,"_widgetRects",[]);E(this,"_controlExtension");E(this,"_dispose$",new O.BehaviorSubject(this));E(this,"dispose$",this._dispose$.asObservable());E(this,"selectionMoving$",new O.Subject);E(this,"selectionMoveEnd$",new O.Subject);E(this,"selectionScaling$",new O.Subject);E(this,"selectionScaled$",new O.Subject);E(this,"selectionFilling$",new O.Subject);E(this,"_selectionFilled$",new O.Subject);E(this,"selectionFilled$",this._selectionFilled$.asObservable());E(this,"_antLineOffset",0);E(this,"_antRequestNewFrame",-1);this._scene=e,this._zIndex=t,this._themeService=n,this._enableAutoFill=(s=o==null?void 0:o.enableAutoFill)!=null?s:!0,this._highlightHeader=(l=o==null?void 0:o.highlightHeader)!=null?l:!0,this._rowHeaderWidth=(c=o==null?void 0:o.rowHeaderWidth)!=null?c:0,this._columnHeaderHeight=(d=o==null?void 0:o.columnHeaderHeight)!=null?d:0,this._initializeSheetBody(),this._initialHeader()}_initializeSheetBody(){this._defaultStyle=bt(this._themeService),this._currentStyle=bt(this._themeService),this._selectionRenderModel=new Td;const e=this._zIndex;this._leftBorder=new w.Rect("__SpreadsheetSelectionShapeLeftControl__"+e,{zIndex:e}),this._rightBorder=new w.Rect("__SpreadsheetSelectionShapeRightControl__"+e,{zIndex:e}),this._topBorder=new w.Rect("__SpreadsheetSelectionTopControl__"+e,{zIndex:e}),this._bottomBorder=new w.Rect("__SpreadsheetSelectionBottomControl__"+e,{zIndex:e}),this._backgroundControlTop=new w.Rect("__SpreadsheetSelectionBackgroundControlTop__"+e,{zIndex:e-1,evented:!1}),this._backgroundControlBottom=new w.Rect("__SpreadsheetSelectionBackgroundControlBottom__"+e,{zIndex:e-1,evented:!1}),this._backgroundControlMiddleLeft=new w.Rect("__SpreadsheetSelectionBackgroundControlMiddleLeft__"+e,{zIndex:e-1,evented:!1}),this._backgroundControlMiddleRight=new w.Rect("__SpreadsheetSelectionBackgroundControlMiddleRight__"+e,{zIndex:e-1,evented:!1}),this._autoFillControl=new w.Rect("__SpreadsheetSelectionFillControl__"+e,{zIndex:e+1}),this._autoFillControl.hide(),this._dashedRect=new w.DashedRect("__SpreadsheetDragDashControl__"+e,{zIndex:e+2,evented:!1,stroke:"#fff"});const t=[this._autoFillControl,this._leftBorder,this._rightBorder,this._topBorder,this._bottomBorder,this._backgroundControlTop,this._backgroundControlMiddleLeft,this._backgroundControlMiddleRight,this._backgroundControlBottom,this._dashedRect];this._widgetRects=this._initialWidget(),this._selectionShapeGroup=new w.Group("__SpreadsheetSelectionShape__"+e,...t,...this._widgetRects),this._selectionShapeGroup.hide(),this._selectionShapeGroup.evented=!1,this._selectionShapeGroup.zIndex=e;const n=this.getScene();n.addObject(this._selectionShapeGroup,Jr),this.disposeWithMe(a.toDisposable(n.onTransformChange$.subscribeEvent(o=>{o.type===w.TRANSFORM_CHANGE_OBSERVABLE_TYPE.scale&&(this._updateLayoutOfSelectionControl(this._currentStyle),this._updateControlCoord())})))}_initialHeader(){const e=this._zIndex;this._rowHeaderBackground=new w.Rect("__SpreadSheetSelectionRowHeaderBackground__"+e,{zIndex:e-1,evented:!1}),this._rowHeaderBorder=new w.Rect("__SpreadSheetSelectionRowHeaderBorder__"+e,{zIndex:e-1,evented:!1}),this._rowHeaderGroup=new w.Group("__SpreadSheetSelectionRowHeaderGroup__"+e,this._rowHeaderBackground,this._rowHeaderBorder),this._rowHeaderGroup.hide(),this._rowHeaderGroup.evented=!1,this._rowHeaderGroup.zIndex=e,this._columnHeaderBackground=new w.Rect("__SpreadSheetSelectionColumnHeaderBackground__"+e,{zIndex:e-1,evented:!1}),this._columnHeaderBorder=new w.Rect("__SpreadSheetSelectionColumnHeaderBorder__"+e,{zIndex:e-1,evented:!1}),this._columnHeaderGroup=new w.Group("__SpreadSheetSelectionColumnHeaderGroup__"+e,this._columnHeaderBackground,this._columnHeaderBorder),this._columnHeaderGroup.hide(),this._columnHeaderGroup.evented=!1,this._columnHeaderGroup.zIndex=e,this.getScene().addObjects([this._rowHeaderGroup,this._columnHeaderGroup],Ed)}_initialWidget(){const e=this._zIndex;return this._topLeftWidget=new w.Rect("__SpreadSheetSelectionTopLeftWidget__"+e,{zIndex:e+1}),this._topCenterWidget=new w.Rect("__SpreadSheetSelectionTopCenterWidget__"+e,{zIndex:e+1}),this._topRightWidget=new w.Rect("__SpreadSheetSelectionTopRightWidget__"+e,{zIndex:e+1}),this._middleLeftWidget=new w.Rect("__SpreadSheetSelectionMiddleLeftWidget__"+e,{zIndex:e+1}),this._middleRightWidget=new w.Rect("__SpreadSheetSelectionMiddleRightWidget__"+e,{zIndex:e+1}),this._bottomLeftWidget=new w.Rect("__SpreadSheetSelectionBottomLeftWidget__"+e,{zIndex:e+1}),this._bottomCenterWidget=new w.Rect("__SpreadSheetSelectionBottomCenterWidget__"+e,{zIndex:e+1}),this._bottomRightWidget=new w.Rect("__SpreadSheetSelectionBottomRightWidget__"+e,{zIndex:e+1}),[this._topLeftWidget,this._topCenterWidget,this._topRightWidget,this._middleLeftWidget,this._middleRightWidget,this._bottomLeftWidget,this._bottomCenterWidget,this._bottomRightWidget]}get zIndex(){return this._zIndex}get leftControl(){return this._leftBorder}get rightControl(){return this._rightBorder}get topControl(){return this._topBorder}get bottomControl(){return this._bottomBorder}get fillControl(){return this._autoFillControl}get backgroundControlTop(){return this._backgroundControlTop}get backgroundControlBottom(){return this._backgroundControlBottom}get backgroundControlMiddleLeft(){return this._backgroundControlMiddleLeft}get backgroundControlMiddleRight(){return this._backgroundControlMiddleRight}get selectionShape(){return this._selectionShapeGroup}get columnHeaderGroup(){return this._columnHeaderGroup}get rowHeaderGroup(){return this._rowHeaderGroup}get selectionShapeGroup(){return this._selectionShapeGroup}get model(){return this._selectionRenderModel}get topLeftWidget(){return this._topLeftWidget}get topCenterWidget(){return this._topCenterWidget}get topRightWidget(){return this._topRightWidget}get middleLeftWidget(){return this._middleLeftWidget}get middleRightWidget(){return this._middleRightWidget}get bottomLeftWidget(){return this._bottomLeftWidget}get bottomCenterWidget(){return this._bottomCenterWidget}get bottomRightWidget(){return this._bottomRightWidget}get themeService(){return this._themeService}get selectionModel(){return this._selectionRenderModel}set selectionModel(e){this._selectionRenderModel=e}get currentStyle(){return this._currentStyle}set currentStyle(e){this._currentStyle=e}get dashedRect(){return this._dashedRect}get isHelperSelection(){return this._isHelperSelection}get rowHeaderWidth(){return this._rowHeaderWidth}set rowHeaderWidth(e){this._rowHeaderWidth=e}get columnHeaderHeight(){return this._columnHeaderHeight}set columnHeaderHeight(e){this._columnHeaderHeight=e}setControlExtension(e){this._controlExtension=new qa(this,e)}setEvent(e){this.leftControl.evented=e,this.rightControl.evented=e,this.topControl.evented=e,this.bottomControl.evented=e}refreshSelectionFilled(e){this._selectionFilled$.next(e)}_updateLayoutOfSelectionControl(e){e&&(this.currentStyle=Object.assign({},this._defaultStyle,e));const t=this.currentStyle,{stroke:n,widgets:o,autofillStroke:s,strokeDash:l,isAnimationDash:c}=t;let d=t.autofillSize,u=t.strokeWidth,h=t.autofillStrokeWidth;const g=this._getScale(),S=(u+m.SELECTION_CONTROL_BORDER_BUFFER_WIDTH)/2/g;u/=g,d/=g,h/=g<1?1:g;const f=m.SELECTION_CONTROL_BORDER_BUFFER_WIDTH/g,p=w.FIX_ONE_PIXEL_BLUR_OFFSET/g,{startX:C,startY:v,endX:b,endY:R}=this._selectionRenderModel;if(this.leftControl.transformByState({height:R-v,left:-S+p,width:u,strokeWidth:f,top:-f/2+p}),this.leftControl.setProps({fill:n,stroke:m.SELECTION_CONTROL_BORDER_BUFFER_COLOR}),this.rightControl.transformByState({height:R-v,left:b-C-S+p,width:u,strokeWidth:f,top:-f/2+p}),this.rightControl.setProps({fill:n,stroke:m.SELECTION_CONTROL_BORDER_BUFFER_COLOR}),this.topControl.transformByState({width:b-C+u,top:-S+p,left:-S+p,height:u,strokeWidth:f}),this.topControl.setProps({fill:n,stroke:m.SELECTION_CONTROL_BORDER_BUFFER_COLOR}),this.bottomControl.transformByState({width:b-C+u,top:R-v-S+p,height:u,left:-S+p,strokeWidth:f}),this.bottomControl.setProps({fill:n,stroke:m.SELECTION_CONTROL_BORDER_BUFFER_COLOR}),l==null)this.dashedRect.hide(),this._stopAntLineAnimation();else{const T=t.strokeWidth*2/g;this.dashedRect.transformByState({height:R-v,width:b-C,strokeWidth:T,left:-T/2+p,top:-T/2+p}),this.dashedRect.setProps({strokeDashArray:[0,l/g]}),this._stopAntLineAnimation(),c!==!1&&this._startAntLineAnimation(),this.dashedRect.show()}if(this._showAutoFill=this._showAutoFill&&this._enableAutoFill,this._showAutoFill&&!this._hasWidgets(o)){const T={fill:n,stroke:s,strokeScaleEnabled:!1},M={width:d-h,height:d-h,left:b-C-d/2+h/2-p,top:R-v-d/2+h/2-p,strokeWidth:h};this._autoFillControl.setProps(T),this._autoFillControl.transformByState(M),this._autoFillControl.show()}else this._autoFillControl.hide();this._updateBackgroundControl(t),this._updateHeaderBackground(t),this._updateWidgets(t)}_updateControlCoord(){const{startX:e,startY:t}=this._selectionRenderModel;this.selectionShapeGroup.show(),this.selectionShapeGroup.translate(e,t),this.selectionShapeGroup.makeDirtyNoDebounce(!0)}updateStyle(e){this._updateLayoutOfSelectionControl(e),this._updateControlCoord()}updateRange(e,t){this._selectionRenderModel.setValue(e,t),this._showAutoFill=t!==null,this._updateLayoutOfSelectionControl(),this._updateControlCoord()}updateRangeBySelectionWithCoord(e,t){t&&(this._rowHeaderWidth=t.getWorksheetConfig().rowHeader.width,this._columnHeaderHeight=t.getWorksheetConfig().columnHeader.height),this._selectionRenderModel.setValue(e.rangeWithCoord,e.primaryWithCoord),this._showAutoFill=e.primaryWithCoord!==null,this._updateLayoutOfSelectionControl(e.style),this._updateControlCoord()}update(e,t=0,n=0,o,s){this._rowHeaderWidth=t,this._columnHeaderHeight=n,this.updateRangeBySelectionWithCoord({rangeWithCoord:e,primaryWithCoord:s,style:o}),o&&this.updateStyle(o)}updateCurrCell(e){this._showAutoFill=e!==null,this._selectionRenderModel.setCurrentCell(e)}clearHighlight(){this._showAutoFill=!1,this._selectionRenderModel.clearCurrentCell(),this._updateLayoutOfSelectionControl(this._currentStyle)}getScene(){return this._scene}dispose(){var e,t,n,o,s,l,c,d,u,h,g,S,f,p,C,v,b,R,T,M,y,P,x,A,N;(e=this._leftBorder)==null||e.dispose(),(t=this._rightBorder)==null||t.dispose(),(n=this._topBorder)==null||n.dispose(),(o=this._bottomBorder)==null||o.dispose(),(s=this._backgroundControlTop)==null||s.dispose(),(l=this._backgroundControlMiddleLeft)==null||l.dispose(),(c=this._backgroundControlMiddleRight)==null||c.dispose(),(d=this._backgroundControlBottom)==null||d.dispose(),this._autoFillControl.dispose(),(u=this._selectionShapeGroup)==null||u.dispose(),(h=this._rowHeaderBackground)==null||h.dispose(),(g=this._rowHeaderBorder)==null||g.dispose(),(S=this._rowHeaderGroup)==null||S.dispose(),(f=this._rowHeaderBackground)==null||f.dispose(),(p=this._columnHeaderBackground)==null||p.dispose(),(C=this._columnHeaderBorder)==null||C.dispose(),(v=this._columnHeaderGroup)==null||v.dispose(),(b=this._topLeftWidget)==null||b.dispose(),(R=this._topCenterWidget)==null||R.dispose(),(T=this._topRightWidget)==null||T.dispose(),(M=this._middleLeftWidget)==null||M.dispose(),(y=this._middleRightWidget)==null||y.dispose(),(P=this._bottomLeftWidget)==null||P.dispose(),(x=this._bottomCenterWidget)==null||x.dispose(),(A=this._bottomRightWidget)==null||A.dispose(),(N=this._controlExtension)==null||N.dispose(),super.dispose(),this._dispose$.next(this),this._dispose$.complete()}getCurrentCellInfo(){const e=this.model.currentCell;if(e){let t;if(e.isMerged){const n=e.mergeInfo;t={startRow:n.startRow,endRow:n.endRow,startColumn:n.startColumn,endColumn:n.endColumn,startX:n.startX,endX:n.endX,startY:n.startY,endY:n.endY}}else{const{actualRow:n,actualColumn:o,startX:s,endX:l,startY:c,endY:d}=e;t={startRow:n,endRow:n,startColumn:o,endColumn:o,startX:s,endX:l,startY:c,endY:d}}return t}}getValue(){return{...this._selectionRenderModel.getValue(),style:this._currentStyle}}getRange(){return this._selectionRenderModel.getValue().rangeWithCoord}enableHelperSelection(){this._isHelperSelection=!0}disableHelperSelection(){this._isHelperSelection=!1}_updateHeaderBackground(e){const{startX:t,startY:n,endX:o,endY:s,rangeType:l}=this._selectionRenderModel,c=this._currentStyle,d=this._getScale(),{stroke:u,rowHeaderFill:h=c.rowHeaderFill,rowHeaderStroke:g=c.rowHeaderStroke,columnHeaderFill:S=c.columnHeaderFill,columnHeaderStroke:f=c.columnHeaderStroke}=e;let{rowHeaderStrokeWidth:p=c.rowHeaderStrokeWidth,columnHeaderStrokeWidth:C=c.columnHeaderStrokeWidth}=e;p/=d,C/=d;const v=this._rowHeaderWidth,b=this._columnHeaderHeight;if(this._highlightHeader&&b>0){let R=S;l===a.RANGE_TYPE.COLUMN&&(R=new a.ColorKit(u).setAlpha(Gs).toString()),this._columnHeaderBackground.setProps({fill:R}),this._columnHeaderBackground.resize(o-t,b),this._columnHeaderBorder.setProps({fill:f}),this._columnHeaderBorder.transformByState({width:o-t,height:C,top:b-C+1/d}),this._columnHeaderGroup.show(),this._columnHeaderGroup.translate(t,0)}else this._columnHeaderGroup.hide();if(this._columnHeaderGroup.makeDirty(!0),this._highlightHeader&&v>0){let R=h;l===a.RANGE_TYPE.ROW&&(R=new a.ColorKit(u).setAlpha(Gs).toString()),this._rowHeaderBackground.setProps({fill:R}),this._rowHeaderBackground.resize(v,s-n),this._rowHeaderBorder.setProps({fill:g}),this._rowHeaderBorder.transformByState({width:p,height:s-n,left:v-p+1/d}),this._rowHeaderGroup.show(),this._rowHeaderGroup.translate(0,n)}else this._rowHeaderGroup.hide();this._rowHeaderGroup.makeDirty(!0)}_updateBackgroundControl(e){const{startX:t,startY:n,endX:o,endY:s}=this._selectionRenderModel,l=this._getScale(),{fill:c=e.fill}=e;let{strokeWidth:d}=e;d/=l;const u=this._selectionRenderModel.highlightToSelection();if(!u){this._backgroundControlTop.resize(o-t,s-n),this._backgroundControlTop.setProps({fill:c}),this._backgroundControlBottom.resize(0,0),this._backgroundControlMiddleLeft.resize(0,0),this._backgroundControlMiddleRight.resize(0,0);return}const{startX:h,startY:g,endX:S,endY:f}=u,p=d/2,C={left:-p,top:-p,width:o-t+p*2,height:g-n+p};C.height<0&&(C.width=0,C.height=0),this._backgroundControlTop.transformByState(C);const v={left:-p,top:g-n,width:h-t+p,height:f-g};v.width<0&&(v.width=0,v.height=0),this._backgroundControlMiddleLeft.transformByState(v);const b={left:S-t-p,top:g-n,width:o-S+p*2,height:f-g};b.width<0&&(b.width=0,b.height=0),this._backgroundControlMiddleRight.transformByState(b);const R={left:-p,top:f-n,width:o-t+p*2,height:s-f+p};R.height<0&&(R.width=0,R.height=0),this._backgroundControlBottom.transformByState(R),this._backgroundControlTop.setProps({fill:c}),this._backgroundControlMiddleLeft.setProps({fill:c}),this._backgroundControlMiddleRight.setProps({fill:c}),this._backgroundControlBottom.setProps({fill:c})}_updateWidgets(e){const{startX:t,startY:n,endX:o,endY:s}=this._selectionRenderModel,{stroke:l=e.stroke,widgets:c=e.widgets,widgetStroke:d=e.widgetStroke}=e,u=this._getScale();let{widgetSize:h,widgetStrokeWidth:g}=e;h/=u,g/=u;const S={left:-h/2+g/2,center:(o-t)/2-h/2+g/2,right:o-t-h/2+g/2,top:-h/2,middle:(s-n)/2-h/2,bottom:s-n-h/2+g/2},f=h-g;this._widgetRects.forEach(p=>{p.setProps({fill:l,stroke:d})}),c.tl===!0?(this._topLeftWidget.transformByState({height:f,width:f,left:S.left,top:S.top,strokeWidth:g}),this._topLeftWidget.show()):this._topLeftWidget.hide(),c.tc===!0?(this._topCenterWidget.transformByState({height:f,width:f,left:S.center,top:S.top,strokeWidth:g}),this._topCenterWidget.show()):this._topCenterWidget.hide(),c.tr===!0?(this._topRightWidget.transformByState({height:f,width:f,left:S.right,top:S.top,strokeWidth:g}),this._topRightWidget.show()):this._topRightWidget.hide(),c.ml===!0?(this._middleLeftWidget.transformByState({height:f,width:f,left:S.left,top:S.middle,strokeWidth:g}),this._middleLeftWidget.show()):this._middleLeftWidget.hide(),c.mr===!0?(this._middleRightWidget.transformByState({height:f,width:f,left:S.right,top:S.middle,strokeWidth:g}),this._middleRightWidget.show()):this._middleRightWidget.hide(),c.bl===!0?(this._bottomLeftWidget.transformByState({height:f,width:f,left:S.left,top:S.bottom,strokeWidth:g}),this._bottomLeftWidget.show()):this._bottomLeftWidget.hide(),c.bc===!0?(this._bottomCenterWidget.transformByState({height:f,width:f,left:S.center,top:S.bottom,strokeWidth:g}),this._bottomCenterWidget.show()):this._bottomCenterWidget.hide(),c.br===!0?(this._bottomRightWidget.transformByState({height:f,width:f,left:S.right,top:S.bottom,strokeWidth:g}),this._bottomRightWidget.show()):this._bottomRightWidget.hide()}_hasWidgets(e){if(e==null)return!1;const t=Object.keys(e);if(t.length===0)return!1;for(const n of t)if(e[n]===!0)return!0;return!1}_getScale(){const{scaleX:e,scaleY:t}=this._scene.getAncestorScale();return Math.max(e,t)}_stopAntLineAnimation(){this._antLineOffset=0,w.cancelRequestFrame(this._antRequestNewFrame)}_startAntLineAnimation(){const e=this._getScale();this._antLineOffset+=.6/e,this._antLineOffset>160/e&&(this._antLineOffset=0),this.dashedRect.setProps({strokeDashOffset:-this._antLineOffset}),this._antRequestNewFrame=w.requestNewFrame(()=>{this._startAntLineAnimation()})}}class yd extends w.Layer{render(i,e=!1){const t=a.Tools.now();return super.render(i,e),this._afterRender(t),this}_afterRender(i){this.scene.getEngine().renderFrameTimeMetric$.next(["selectionLayer",a.Tools.now()-i])}}const Ce=a.createIdentifier("univer.sheet.selection-render-service");class gs extends a.Disposable{constructor(e,t,n,o,s){super();E(this,"_downObserver");E(this,"_scenePointerMoveSub");E(this,"_scenePointerUpSub");E(this,"_controlFillConfig$",new O.BehaviorSubject(null));E(this,"controlFillConfig$",this._controlFillConfig$.asObservable());E(this,"_selectionControls",[]);E(this,"_startRangeWhenPointerDown",{startY:0,endY:0,startX:0,endX:0,startRow:-1,endRow:-1,startColumn:-1,endColumn:-1});E(this,"_activeControlIndex",-1);E(this,"_startViewportPosX",0);E(this,"_startViewportPosY",0);E(this,"_scrollTimer");E(this,"_cancelDownSubscription");E(this,"_cancelUpSubscription");E(this,"_skeleton");E(this,"_scene");E(this,"_highlightHeader",!0);E(this,"_rangeType",a.RANGE_TYPE.NORMAL);E(this,"_selectionStyle");E(this,"_remainLastEnabled",!1);E(this,"_skipLastEnabled",!1);E(this,"_singleSelectionEnabled",!1);E(this,"_selectionMoveEnd$",new O.BehaviorSubject([]));E(this,"selectionMoveEnd$",this._selectionMoveEnd$.asObservable());E(this,"_selectionMoving$",new O.Subject);E(this,"selectionMoving$",this._selectionMoving$.asObservable());E(this,"_selectionMoveStart$",new O.Subject);E(this,"selectionMoveStart$",this._selectionMoveStart$.asObservable());E(this,"_selectionMoving",!1);E(this,"_selectionTheme");E(this,"_activeViewport");E(this,"interceptor",new a.InterceptorManager({RANGE_MOVE_PERMISSION_CHECK:Vo,RANGE_FILL_PERMISSION_CHECK:Wo}));E(this,"_escapeShortcutDisposable",null);this._injector=e,this._themeService=t,this._shortcutService=n,this._sheetSkeletonManagerService=o,this.contextService=s,this._initSelectionThemeFromThemeService(),this._initMoving()}get selectionMoving(){return this._selectionMoving}get _shouldDetectMergedCells(){return this._rangeType===a.RANGE_TYPE.NORMAL}_initMoving(){this.disposeWithMe(this._selectionMoving$.subscribe(()=>{this._selectionMoving=!0})),this.disposeWithMe(this._selectionMoveEnd$.subscribe(()=>{this._selectionMoving=!1}))}_setSelectionStyle(e){this._selectionStyle=e}getViewPort(){return this._activeViewport}setSingleSelectionEnabled(e=!1){this._singleSelectionEnabled=e}newSelectionControl(e,t,n){const o=this.getSelectionControls().length,{rowHeaderWidth:s,columnHeaderHeight:l}=t,c=new lr(e,o,this._selectionTheme,{highlightHeader:this._highlightHeader,rowHeaderWidth:s,columnHeaderHeight:l});this._selectionControls.push(c);const d=Me(n,t);return c.updateRangeBySelectionWithCoord(d,t),c.setControlExtension({skeleton:t,scene:e,themeService:this._selectionTheme,injector:this._injector,selectionHooks:{selectionMoveEnd:()=>{this._selectionMoveEnd$.next(this.getSelectionDataWithStyle())}}}),c}resetSelectionsByModelData(e){const t=this.getSelectionControls(),n=this._skeleton;if(!(!t||!n)){for(let o=0,s=e.length;o<s;o++){const l=e[o],c=Me(l,this._skeleton),d=t[o];d?d.updateRangeBySelectionWithCoord(c,n):this.isSelectionEnabled()&&this.newSelectionControl(this._scene,n,l)}e.length<t.length&&t.splice(e.length).forEach(s=>s.dispose())}}refreshSelectionMoveStart(){this._selectionMoveStart$.next(this.getSelectionDataWithStyle())}refreshSelectionMoveEnd(){this._selectionMoveEnd$.next(this.getSelectionDataWithStyle())}_initSelectionThemeFromThemeService(){const e=this._themeService.getCurrentTheme();this._selectionTheme=new a.ThemeService,this._selectionTheme.setTheme(e)}setSelectionTheme(e){this._selectionTheme.setTheme(e)}_changeRuntime(e,t,n){this._skeleton=e,this._scene=t,this._activeViewport=n||(t==null?void 0:t.getViewports()[0]),t.findLayerByZIndex(Jr)||t.addLayer(new yd(t,[],Jr))}getSkeleton(){return this._skeleton}getSelectionDataWithStyle(){const e=this._selectionControls,[t,n]=this._skeleton.getLocation();return e.map(o=>{const s=o.getValue();return s.rangeWithCoord.sheetId=n,s.rangeWithCoord.unitId=t,s})}getSelectionControls(){return this._selectionControls}_addSelectionControlByModelData(e){var l;const t=this._skeleton,n=(l=e.style)!=null?l:bt(this._selectionTheme),o=this._scene;return e.style=n,this.newSelectionControl(o,t,e)}_clearAllSelectionControls(){const e=this._selectionControls;for(const t of e)t.dispose();this._selectionControls.length=0}_getFreeze(){var t;return(t=this._sheetSkeletonManagerService.getCurrentParam())==null?void 0:t.skeleton.getWorksheetConfig().freeze}_getViewportByCell(e,t){if(e===void 0||t===void 0)return null;const n=this._getFreeze();if(!n||n.startRow<=0&&n.startColumn<=0)return this._scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN);if(e>n.startRow&&t>n.startColumn)return this._scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN);if(e<=n.startRow&&t<=n.startColumn)return this._scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP);if(e<=n.startRow&&t>n.startColumn)return this._scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP);if(e>n.startRow&&t<=n.startColumn)return this._scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT)}getActiveRange(){const e=this.getSelectionControls(),t=e&&e[e.length-1].model;return t&&{startRow:t.startRow,startColumn:t.startColumn,endRow:t.endRow,endColumn:t.endColumn}}setActiveSelectionIndex(e){this._activeControlIndex=e}resetActiveSelectionIndex(){this._activeControlIndex=-1}getActiveSelectionControl(){const e=this.getSelectionControls();if(e)return this._activeControlIndex<0?e[e.length-1]:e[this._activeControlIndex]}endSelection(){var e;this._clearUpdatingListeners(),this._selectionMoveEnd$.next(this.getSelectionDataWithStyle()),(e=this._escapeShortcutDisposable)==null||e.dispose(),this._escapeShortcutDisposable=null}_reset(){var e;this._clearAllSelectionControls(),(e=this._downObserver)==null||e.unsubscribe(),this._downObserver=null}_setupPointerMoveListener(e,t,n,o=w.ScrollTimerType.ALL,s,l){var f,p;let c=0,d=0,u=s,h=l;this._scrollTimer=w.ScrollTimer.create(this._scene,o),this._scrollTimer.startScroll((f=e==null?void 0:e.left)!=null?f:0,(p=e==null?void 0:e.top)!=null?p:0,e);const g=this._scene,S=g.getActiveViewportByCoord(w.Vector2.FromArray([s,l]));this._scenePointerMoveSub=g.onPointerMove$.subscribeEvent(C=>{var H,W,k,U,B,F,L,j,z;const{offsetX:v,offsetY:b}=C,{x:R,y:T}=g.getCoordRelativeToViewport(w.Vector2.FromArray([v,b]));this._movingHandler(R,T,t,n);let M=R,y=T;const P=this.getActiveSelectionControl(),x=this._getFreeze(),A=P==null?void 0:P.model,N=(H=g.getActiveViewportByCoord(w.Vector2.FromArray([v,b])))!=null?H:this._getViewportByCell(A==null?void 0:A.endRow,A==null?void 0:A.endColumn);if((()=>{if(!S||!N||!e)return!1;const q=[w.SHEET_VIEWPORT_KEY.VIEW_MAIN,w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP,w.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP,w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT];return q.includes(S.viewportKey)&&q.includes(N.viewportKey)})()){if(!S||!N||!e)return!1;const q=u<e.left&&R>e.left||u>e.left&&R<e.left,te=h<e.top&&T>e.top||h>e.top&&T<e.top;q&&(c+=1),te&&(d+=1);const Q=S.viewportKey,ie=N.viewportKey;if(Q===w.SHEET_VIEWPORT_KEY.VIEW_ROW_TOP)b<e.top&&((W=A==null?void 0:A.endRow)!=null?W:0)<((k=x==null?void 0:x.startRow)!=null?k:0)?y=e.top:te&&d%2===1&&e.scrollToViewportPos({viewportScrollY:0});else if(Q===w.SHEET_VIEWPORT_KEY.VIEW_COLUMN_LEFT)v<e.left&&((U=A==null?void 0:A.endColumn)!=null?U:0)<((B=x==null?void 0:x.startColumn)!=null?B:0)?M=e.left:q&&c%2===1&&e.scrollToViewportPos({viewportScrollX:0});else if(Q===ie){let se=!1,X=!1;Q===w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP?(se=!0,X=!0):Q===w.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP?X=!0:Q===w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT&&(se=!0),((F=A==null?void 0:A.endRow)!=null?F:0)>((L=x==null?void 0:x.startRow)!=null?L:0)&&(X=!1),((j=A==null?void 0:A.endColumn)!=null?j:0)>((z=x==null?void 0:x.startColumn)!=null?z:0)&&(se=!1),se&&(M=e.left),X&&(y=e.top)}else{const se={x:S.scrollX,y:S.scrollY},X={x:N.scrollX,y:N.scrollY},Y=se.x!==X.x&&q&&c%2===1,G=se.y!==X.y&&te&&d%2===1;(Y||G)&&(e.scrollToBarPos({x:Y?se.x:void 0,y:G?se.y:void 0}),Y||(M=e.left),G||(y=e.top)),(Q===w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP&&ie===w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT||ie===w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP&&Q===w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT)&&(M=e.left),(Q===w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP&&ie===w.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP||ie===w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP&&Q===w.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP)&&(y=e.top)}u=R,h=T}this._scrollTimer.scrolling(M,y,()=>{this._movingHandler(R,T,t,n)})})}attachSelectionWithCoord(e){return Me(e,this._skeleton)}attachPrimaryWithCoord(e){return Jn(this._skeleton,e)}getSelectionCellByPosition(e,t){return this.getCellWithCoordByOffset(e,t)}getCellWithCoordByOffset(e,t,n){const o=this._scene,s=n!=null?n:this._skeleton,l=o.getViewportScrollXY(o.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN)),{scaleX:c,scaleY:d}=o.getAncestorScale();return s.getCellWithCoordByOffset(e,t,c,d,l)}_movingHandler(e,t,n,o){var M,y,P,x,A;const s=this._skeleton,l=this._scene,[c,d]=s.getLocation(),u={startRow:(M=n==null?void 0:n.model.startRow)!=null?M:-1,endRow:(y=n==null?void 0:n.model.endRow)!=null?y:-1,startColumn:(P=n==null?void 0:n.model.startColumn)!=null?P:-1,endColumn:(x=n==null?void 0:n.model.endColumn)!=null?x:-1,unitId:c,sheetId:d},h=l.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN),g=(A=this._getViewportByCell(u.endRow,u.endColumn))!=null?A:h,S=l.getScrollXYInfoByViewport(w.Vector2.FromArray([this._startViewportPosX,this._startViewportPosY]),g),{scaleX:f,scaleY:p}=l.getAncestorScale();o===a.RANGE_TYPE.ROW?e=Number.POSITIVE_INFINITY:o===a.RANGE_TYPE.COLUMN&&(t=Number.POSITIVE_INFINITY);const C=this._skeleton.getCellByOffset(e,t,f,p,S);if(!C)return;let v={startRow:Math.min(C.startRow,this._startRangeWhenPointerDown.startRow),startColumn:Math.min(C.startColumn,this._startRangeWhenPointerDown.startColumn),endRow:Math.max(C.endRow,this._startRangeWhenPointerDown.endRow),endColumn:Math.max(C.endColumn,this._startRangeWhenPointerDown.endColumn)};this._shouldDetectMergedCells&&(v=s.expandRangeByMerge(v));const R=Me({range:v,primary:void 0,style:null},s);R.rangeWithCoord.unitId=c,R.rangeWithCoord.sheetId=d,R.rangeWithCoord.rangeType=o;const T=u.startRow!==v.startRow||u.startColumn!==v.startColumn||u.endRow!==v.endRow||u.endColumn!==v.endColumn;n!=null&&T&&(n.updateRangeBySelectionWithCoord(R),this._selectionMoving$.next(this.getSelectionDataWithStyle()))}_clearUpdatingListeners(){var t,n,o,s,l;this._scene.enableObjectsEvent(),(t=this._scenePointerMoveSub)==null||t.unsubscribe(),this._scenePointerMoveSub=null,(n=this._scenePointerUpSub)==null||n.unsubscribe(),this._scenePointerUpSub=null,(o=this._cancelDownSubscription)==null||o.unsubscribe(),this._cancelDownSubscription=null,(s=this._cancelUpSubscription)==null||s.unsubscribe(),this._cancelUpSubscription=null,(l=this._scrollTimer)==null||l.dispose()}_addEndingListeners(){var n,o,s;const e=this._scene,t=(n=e.getEngine())==null?void 0:n.activeScene;!t||t===e||((o=this._cancelDownSubscription)==null||o.unsubscribe(),this._cancelDownSubscription=null,(s=this._cancelUpSubscription)==null||s.unsubscribe(),this._cancelUpSubscription=null,this._cancelDownSubscription=t.onPointerDown$.subscribeEvent(()=>this._clearUpdatingListeners()),this._cancelUpSubscription=t.onPointerUp$.subscribeEvent(()=>this._clearUpdatingListeners()))}_getSelectionWithCoordByOffset(e,t,n,o,s){const l=this._skeleton;if(l==null)return null;if(this._shouldDetectMergedCells){const v=l==null?void 0:l.getCellWithCoordByOffset(e,t,n,o,s,{firstMatch:!0});if(!v)return;const b=a.convertCellToRange(v);return{primaryWithCoord:v,rangeWithCoord:b}}const{row:c,column:d}=l.getCellIndexByOffset(e,t,n,o,s),u=l.getNoMergeCellWithCoordByIndex(c,d),{startX:h,startY:g,endX:S,endY:f}=u,p={startY:g,endY:f,startX:h,endX:S,startRow:c,endRow:c,startColumn:d,endColumn:d};return{primaryWithCoord:{mergeInfo:p,actualRow:c,actualColumn:d,isMerged:!1,isMergedMainCell:!1,startY:g,endY:f,startX:h,endX:S},rangeWithCoord:p}}_checkClearPreviousControls(e){this.getSelectionControls().length!==0&&(!e.ctrlKey&&!e.shiftKey&&!this._remainLastEnabled||this._singleSelectionEnabled&&!e.shiftKey)&&this._clearAllSelectionControls()}_makeSelectionByTwoCells(e,t,n,o,s){const{actualRow:l,actualColumn:c,mergeInfo:d}=e;this._startRangeWhenPointerDown={...e.mergeInfo};const u=Math.min(l,t.startRow,d.startRow),h=Math.max(l,t.endRow,d.endRow),g=Math.min(c,t.startColumn,d.startColumn),S=Math.max(c,t.endColumn,d.endColumn),p={range:n.expandRangeByMerge({startRow:u,startColumn:g,endRow:h,endColumn:S}),primary:null,style:null},C=Me(p,n);s.updateRangeBySelectionWithCoord(C)}isSelectionEnabled(){return this.contextService.getContextValue(m.SELECTIONS_ENABLED)}isSelectionDisabled(){return this.contextService.getContextValue(m.SELECTIONS_ENABLED)===!1}inRefSelectionMode(){return this.contextService.getContextValue(m.REF_SELECTIONS_ENABLED)}}function Ss(r){return{range:{startRow:0,startColumn:0,endRow:r.getRowCount()-1,endColumn:r.getColumnCount()-1,rangeType:a.RANGE_TYPE.ALL},primary:Oi(r).primary,style:null}}function Oi(r){return Qa(r,{startRow:0,startColumn:0,endRow:0,endColumn:0})}function Qa(r,i){const e=r.worksheet.getCellInfoInMergeData(i.startRow,i.startColumn),t=r.worksheet.getCellInfoInMergeData(i.endRow,i.endColumn);return{range:{startRow:e.startRow,startColumn:e.startColumn,endRow:t.endRow,endColumn:t.endColumn,rangeType:a.RANGE_TYPE.NORMAL},primary:{actualRow:e.startRow,actualColumn:e.startColumn,startRow:e.startRow,startColumn:e.startColumn,endRow:e.endRow,endColumn:e.endColumn,isMerged:e.isMerged,isMergedMainCell:e.isMergedMainCell},style:null}}var Md=Object.defineProperty,Pd=Object.getOwnPropertyDescriptor,Od=(r,i,e,t)=>{for(var n=t>1?void 0:t?Pd(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&Md(i,e,n),n},Zs=(r,i)=>(e,t)=>i(e,t,r);exports.SheetSkeletonManagerService=class extends a.Disposable{constructor(e,t,n){super();E(this,"_sheetId","");E(this,"_sheetSkeletonParamStore",new Map);E(this,"_currentSkeleton$",new O.BehaviorSubject(null));E(this,"currentSkeleton$",this._currentSkeleton$.asObservable());E(this,"_currentSkeletonBefore$",new O.BehaviorSubject(null));E(this,"currentSkeletonBefore$",this._currentSkeletonBefore$.asObservable());this._context=e,this._injector=t,this._sheetSkService=n,this.disposeWithMe(()=>{this._currentSkeletonBefore$.complete(),this._currentSkeleton$.complete(),this._sheetSkeletonParamStore.forEach(o=>this.disposeSkeleton(o.sheetId)),this._sheetSkeletonParamStore.clear()}),this._initRemoveSheet()}_initRemoveSheet(){this.disposeWithMe(this._context.unit.sheetDisposed$.subscribe(e=>{this.disposeSkeleton(e.getSheetId())}))}getCurrentSkeleton(){var e;return(e=this.getCurrentParam())==null?void 0:e.skeleton}getCurrent(){return this.getCurrentParam()}getCurrentParam(){return this._getSkeletonParam(this._sheetId)}getSkeleton(e){return this._getSkeleton(e)}getSkeletonParam(e){return this._getSkeletonParam(e)}getWorksheetSkeleton(e){return this.getSkeletonParam(e)}getUnitSkeleton(e,t){const n=this._getSkeletonParam(t);return n!=null&&(n.unitId=e),n}setCurrent(e){this._setCurrent(e.sheetId)}setSkeletonParam(e,t){this._sheetSkService.setSkeleton(t.unitId,e,t.skeleton),this._sheetSkeletonParamStore.set(e,t)}_setCurrent(e){this._sheetId=e;const t=this._getSkeletonParam(e),n=this._context.unitId;if(t!=null)this.reCalculate(t);else{const l=this._context.unit.getSheetBySheetId(e);if(l==null)return;const c=this._context.scene,d=this._buildSkeleton(l,c);this.setSkeletonParam(e,{unitId:n,sheetId:e,skeleton:d,dirty:!1})}const o=this._getSkeletonParam(e);this._currentSkeletonBefore$.next(o),this._currentSkeleton$.next(o)}reCalculate(e){e||(e=this.getCurrentParam()),e!=null&&(e.dirty&&(e.skeleton.makeDirty(!0),e.dirty=!1),e.skeleton.calculate())}makeDirty(e,t=!0){const n=this._getSkeletonParam(e.sheetId);n!=null&&(n.dirty=t)}getOrCreateSkeleton(e){return this.ensureSkeleton(e.sheetId)}ensureSkeleton(e){const t=this._getSkeletonParam(e);if(t)return t.skeleton;const o=this._context.unit.getSheetBySheetId(e);if(!o)return;const s=this._buildSkeleton(o);return this.setSkeletonParam(e,{unitId:this._context.unitId,sheetId:e,skeleton:s,dirty:!1}),s}disposeSkeleton(e){const t=this.getSkeletonParam(e);t&&(t.skeleton.dispose(),this._sheetSkeletonParamStore.delete(e),this._sheetSkService.deleteSkeleton(t.unitId,e))}attachRangeWithCoord(e){const t=this.getCurrentSkeleton();return t?bn(t,e):null}_getSkeletonParam(e){return this._sheetSkeletonParamStore.get(e)}_getSkeleton(e){const t=this._getSkeletonParam(e);return t?t.skeleton:null}_buildSkeleton(e,t){return this._injector.createInstance(w.SpreadsheetSkeleton,e,this._context.unit.getStyles())}setColumnHeaderSize(e,t,n){var u;if(!e)return;const o=(u=this.getWorksheetSkeleton(t))==null?void 0:u.skeleton;if(!o)return;o.columnHeaderHeight=n,e.scene.getViewports()[0].top=n,e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_COLUMN_RIGHT).setViewportSize({height:n}),e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_COLUMN_LEFT).setViewportSize({height:n}),e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_ROW_BOTTOM).setViewportSize({top:n}),e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_ROW_TOP).setViewportSize({top:n}),e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_LEFT_TOP).setViewportSize({height:n});const s=e==null?void 0:e.with(m.SheetsSelectionsService),l=e==null?void 0:e.with(Ce),c=s.getCurrentSelections();l.resetSelectionsByModelData(c);const d=this.getUnitSkeleton(e.unitId,t);d&&(d.commandId=Pi.id,this._currentSkeleton$.next(d))}setRowHeaderSize(e,t,n){var f;const o=(f=this.getWorksheetSkeleton(t))==null?void 0:f.skeleton;if(!e||!o)return;o.rowHeaderWidth=n;const s=e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_LEFT_TOP).width||46,l=n-s,c=e.scene.getViewports()[0].left;e.scene.getViewports()[0].left=c+l,e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_ROW_BOTTOM).setViewportSize({width:n}),e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_ROW_TOP).setViewportSize({width:n}),e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_COLUMN_LEFT).setViewportSize({left:n});const d=e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_COLUMN_RIGHT).left||0;e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_COLUMN_RIGHT).setViewportSize({left:d+l}),e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_LEFT_TOP).setViewportSize({width:n});const u=e==null?void 0:e.with(m.SheetsSelectionsService),h=e==null?void 0:e.with(Ce),g=u.getCurrentSelections();h.resetSelectionsByModelData(g);const S=this.getCurrent();S&&(S.commandId=Mi.id,this._currentSkeleton$.next(S))}};exports.SheetSkeletonManagerService=Od([Zs(1,a.Inject(a.Injector)),Zs(2,a.Inject(m.SheetSkeletonService))],exports.SheetSkeletonManagerService);function ht(){const r=_.useDependency(a.IUniverInstanceService),i=_.useObservable(()=>r.getCurrentTypeOfUnit$(a.UniverInstanceType.UNIVER_SHEET),void 0,void 0,[]);return i!=null?i:null}function xd(r){return _.useObservable(()=>{var e;return(e=r==null?void 0:r.activeSheet$)!=null?e:O.of(null)},void 0,void 0,[r])}function Ad(){const r=_.useDependency(a.IUniverInstanceService);return _.useObservable(()=>O.merge([r.getTypeOfUnitAdded$(a.UniverInstanceType.UNIVER_SHEET),r.getTypeOfUnitDisposed$(a.UniverInstanceType.UNIVER_SHEET)]).pipe(O.map(()=>r.getAllUnitsForType(a.UniverInstanceType.UNIVER_SHEET)),O.startWith(r.getAllUnitsForType(a.UniverInstanceType.UNIVER_SHEET))),[],void 0,[r])}const Nd="SheetUI";var fs=(r=>(r.FILTER_PANEL_EMBED_POINT="filter-panel-embed-point",r.SHEETS_FOOTER="sheets-footer",r.FORMULA_AUX="formula-aux",r))(fs||{}),Dd=Object.defineProperty,Hd=Object.getOwnPropertyDescriptor,kd=(r,i,e,t)=>{for(var n=t>1?void 0:t?Hd(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&Dd(i,e,n),n},kr=(r,i)=>(e,t)=>i(e,t,r);const ps=["ff","fs","tr","tb"];exports.AutoHeightController=class extends a.Disposable{constructor(i,e,t,n){super(),this._renderManagerService=i,this._sheetInterceptorService=e,this._selectionManagerService=t,this._univerInstanceService=n,this._initialize()}getUndoRedoParamsOfAutoHeight(i){const{_univerInstanceService:e}=this,t=e.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET),n=t.getActiveSheet(),o=t.getUnitId(),s=n.getSheetId(),l=this._renderManagerService.getRenderById(o).with(exports.SheetSkeletonManagerService);if(!s||!l.getCurrentParam())return{redos:[],undos:[]};const{skeleton:c}=l.getCurrentParam(),d=c.calculateAutoHeightInRange(i),u={subUnitId:s,unitId:o,rowsAutoHeightInfo:d},h=m.SetWorksheetRowAutoHeightMutationFactory(u,n);return{undos:[{id:m.SetWorksheetRowAutoHeightMutation.id,params:h}],redos:[{id:m.SetWorksheetRowAutoHeightMutation.id,params:u}]}}_initialize(){const{_sheetInterceptorService:i,_selectionManagerService:e}=this;this.disposeWithMe(i.interceptCommand({getMutations:t=>{if(t.id===m.SetRangeValuesCommand.id){const n=t.params;return this.getUndoRedoParamsOfAutoHeight(n.range)}return{redos:[],undos:[]}}})),this.disposeWithMe(i.interceptCommand({getMutations:t=>t.id!==m.SetWorksheetRowIsAutoHeightCommand.id?{redos:[],undos:[]}:this.getUndoRedoParamsOfAutoHeight(t.params.ranges)})),this.disposeWithMe(i.interceptCommand({getMutations:t=>{var o,s;if(t.id!==m.SetStyleCommand.id)return{redos:[],undos:[]};if(!ps.includes((o=t.params)==null?void 0:o.style.type))return{redos:[],undos:[]};const n=(s=e.getCurrentSelections())==null?void 0:s.map(l=>l.range);return n!=null&&n.length?this.getUndoRedoParamsOfAutoHeight(n):{redos:[],undos:[]}}})),this.disposeWithMe(i.interceptAfterCommand({getMutations:t=>{if(t.id===m.MoveRangeCommand.id){const n=t.params;return this.getUndoRedoParamsOfAutoHeight([n.fromRange,n.toRange])}if(t.id===m.ReorderRangeCommand.id){const n=t.params;return this.getUndoRedoParamsOfAutoHeight([n.range])}return{redos:[],undos:[]}}}))}};exports.AutoHeightController=kd([kr(0,w.IRenderManagerService),kr(1,a.Inject(m.SheetInterceptorService)),kr(2,a.Inject(m.SheetsSelectionsService)),kr(3,a.Inject(a.IUniverInstanceService))],exports.AutoHeightController);function Sn(r,i,e,t){const n=i.get(a.IUniverInstanceService),o=e?n.getUnit(e,a.UniverInstanceType.UNIVER_SHEET):n.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET),s=t?o==null?void 0:o.getSheetBySheetId(t):o==null?void 0:o.getActiveSheet();if(!s)return null;const{startRow:l,endRow:c,startColumn:d,endColumn:u}=r,h=[],g=[];for(let S=l;S<=c;S++)s.getRowFiltered(S)||h.push(S);for(let S=d;S<=u;S++)g.push(S);return{rows:h,cols:g}}function Rt(r){const{rows:i,cols:e}=r;return{startRow:i[0],endRow:i[i.length-1],startColumn:e[0],endColumn:e[e.length-1]}}function Kt(r){let i=[],e=[];const t=[];return r.forEach(n=>{i=i.concat(n.rows),e=e.concat(n.cols)}),i=Array.from(new Set(i)).sort((n,o)=>n-o),e=Array.from(new Set(e)).sort((n,o)=>n-o),r.forEach(n=>{t.push({startRow:i.findIndex(o=>o===n.rows[0]),endRow:i.findIndex(o=>o===n.rows[n.rows.length-1]),startColumn:e.findIndex(o=>o===n.cols[0]),endColumn:e.findIndex(o=>o===n.cols[n.cols.length-1])})}),{ranges:t,mapFunc:(n,o)=>({row:i[n],col:e[o]})}}function Vd(r){const i=new a.ObjectMatrix;return r.forEach(e=>{const{rows:t,cols:n}=e;t.forEach(o=>{n.forEach(s=>{i.setValue(o,s,{v:null,p:null,f:null,si:null,custom:null})})})}),i.getData()}const Lo={零:0,一:1,二:2,三:3,四:4,五:5,六:6,七:7,八:8,九:9},$r=["零","一","二","三","四","五","六","七","八","九"],Ks=["","万","亿","万亿","亿亿"],Wd=["","十","百","千"],Ja={十:{value:10,secUnit:!1},百:{value:100,secUnit:!1},千:{value:1e3,secUnit:!1},万:{value:1e4,secUnit:!0},亿:{value:1e8,secUnit:!0}};function ct(r){if(!r)return 0;let i=0,e=0,t=0,n=!1;const o=r.split("");for(let s=0;s<o.length;s++){const l=Lo[o[s]];if(typeof l<"u")t=l,s===o.length-1&&(e+=t);else{const c=Ja[o[s]],d=c.value;n=c.secUnit,n?(e=(e+t)*d,i+=e,e=0):e+=t*d,t=0}}return i+e}function Ud(r){let i="",e="",t=0,n=!0;for(;r>0;){const o=r%10;o===0?n||(n=!0,e=$r[o]+e):(n=!1,i=$r[o],i+=Wd[t],e=i+e),t++,r=Math.floor(r/10)}return e}function Ld(r){let i=0,e="",t="",n=!1;if(r===0)return $r[0];for(;r>0;){const o=r%1e4;n&&(t=$r[0]+t),e=Ud(o),e+=o!==0?Ks[i]:Ks[0],t=e+t,n=o<1e3&&o>0,r=Math.floor(r/1e4),i++}return t}function Bd(r){if(!r)return!1;let i=!0;if(r)if(r.length===1)r==="日"||r in Lo?i=!0:i=!1;else{const e=r.split("");for(let t=0;t<e.length;t++)if(!(e[t]in Lo||e[t]in Ja)){i=!1;break}}return i}function Vr(r){if(!r)return{isExtendNumber:!1};const i=/0|([1-9]+[0-9]*)/g,e=i.test(r);if(e){const t=r.match(i);if(t&&t.length>0){const n=t[t.length-1],o=r.lastIndexOf(n),s=r.substr(0,o),l=r.substr(o+n.length);return{isExtendNumber:!0,matchTxt:Number(n),beforeTxt:s,afterTxt:l}}return{isExtendNumber:!1}}return{isExtendNumber:e}}function Fd(r){let i;return r.length===2&&(r==="周一"||r==="周二"||r==="周三"||r==="周四"||r==="周五"||r==="周六"||r==="周日")?i=!0:i=!1,i}function jd(r){let i;return r.length===3&&(r==="星期一"||r==="星期二"||r==="星期三"||r==="星期四"||r==="星期五"||r==="星期六"||r==="星期日")?i=!0:i=!1,i}function zd(r,i){let e=0;for(let t=0;t<r.length&&r[t]<i;t++)e++;return e}function cr(r){let i=!0;const e=r[1]-r[0];for(let t=1;t<r.length;t++)if(r[t]-r[t-1]!==e){i=!1;break}return i}function Xd(r,i,e){const t=[],n=Math.floor(i/r),o=i%r;let s=0;if(n>0){for(let l=0;l<n;l++)for(let c=0;c<e.length;c++)t[e[c]+l*r]=s,s++;for(let l=0;l<e.length&&e[l]<o;l++)t[e[l]+r*n]=s,s++}else for(let l=0;l<e.length&&e[l]<o;l++)t[e[l]]=s,s++;return t}function Ft(r,i){const e=[];for(let t=1;t<=i;t++){const n=(t-1)%r.length,o=a.Tools.deepClone(r[n]);jt(o),e.push({v:null,s:null,p:null,f:null,si:null,t:null,...o})}return e}function Yd(r,i){var t;const e=[];for(let n=1;n<=i;n++){const o=(n-1)%r.length,s={s:(t=r[o])==null?void 0:t.s};e.push(s)}return e}function Gd(r){let i=!0;const e=r[1]/r[0];for(let t=1;t<r.length;t++)if(r[t]/r[t-1]!==e){i=!1;break}return i}function Zd(r){const i=[];for(let e=1;e<=r;e++)i.push(e);return i}function ei(r,i,e){var o,s,l,c;const t=[],n=[];for(let d=0;d<r.length;d++)n.push(Number((o=r[d])==null?void 0:o.v));if(r.length>2&&Gd(n))for(let d=1;d<=i;d++){const u=(d-1)%r.length,h=a.Tools.deepClone(r[u]);jt(h);const g=Number((s=r[r.length-1])==null?void 0:s.v)*(Number((l=r[1])==null?void 0:l.v)/Number((c=r[0])==null?void 0:c.v))**d;h&&($s(h)&&(h.v=g),t.push(h))}else{const d=Zd(r.length);for(let u=1;u<=i;u++){const h=(u-1)%r.length,g=a.Tools.deepClone(r[h]);jt(g);const S=e===a.Direction.DOWN||e===a.Direction.RIGHT,f=Kd(r.length+u,n,d,S);g&&($s(g)&&(g.v=f),t.push(g))}}return t}function Kd(r,i,e,t=!0){function n(h){let g=0;for(let S=0;S<h.length;S++)g+=h[S];return g/h.length}const o=n(e),s=n(i);let l=0,c=0;for(let h=0;h<e.length;h++)l+=(e[h]-o)*(i[h]-s),c+=(e[h]-o)*(e[h]-o);let d;c===0?d=t?1:-1:d=l/c;const u=s-d*o;return Math.round((u+d*r)*1e5)/1e5}function qs(r,i,e){var o;const t=[],n=/0|([1-9]+[0-9]*)/g;for(let s=1;s<=i;s++){const l=(s-1)%r.length,c=a.Tools.deepClone(r[l]);jt(c);const d=`${(o=r[r.length-1])==null?void 0:o.v}`,u=d==null?void 0:d.match(n),h=u==null?void 0:u[u.length-1],g=Math.abs(Number(h)+e*s);if(!d||!h)continue;const S=d.lastIndexOf(h),f=d.substr(0,S)+g.toString()+d.substr(S+h.length);c&&(c.v=f,t.push(c))}return t}function Rn(r,i,e,t=0){var l,c;const n=[["日","一","二","三","四","五","六"],["周日","周一","周二","周三","周四","周五","周六"],["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]];if(t>=n.length)return[];const o=n[t],s=[];for(let d=1;d<=i;d++){const u=(d-1)%r.length,h=a.Tools.deepClone(r[u]);jt(h);let g=0;if(((l=r[r.length-1])==null?void 0:l.v)===o[0])g=7+e*d;else{const f=`${(c=r[r.length-1])==null?void 0:c.v}`;if(f){const p=f.substr(f.length-1,1);g=ct(p)+e*d}}g<0&&(g=Math.ceil(Math.abs(g)/7)*7+g);const S=g%7;h&&(h.v=o[S],s.push(h))}return s}function Qs(r,i,e){var n;const t=[];for(let o=1;o<=i;o++){const s=(o-1)%r.length,l=a.Tools.deepClone(r[s]);jt(l);const c=`${(n=r[r.length-1])==null?void 0:n.v}`,d=ct(c)+e*o;let u;d<=0?u="零":u=Ld(d),l&&(l.v=u,t.push(l))}return t}const Kn={enWeek1:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],enWeek2:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],enMonth1:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],enMonth2:["January","February","March","April","May","June","July","August","September","October","November","December"],chnMonth1:["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],chnMonth2:["正月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","腊月"],chHour1:["子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"],chHour2:["子时","丑时","寅时","卯时","辰时","巳时","午时","未时","申时","酉时","戌时","亥时"],chYear1:["甲","乙","丙","丁","戊","己","庚","辛","壬","癸"],chSeason1:["春","夏","秋","冬"],chSeason2:["春季","夏季","秋季","冬季"]};function qd(r){let i=!1;return Object.keys(Kn).forEach(e=>{Kn[e].includes(r)&&(i=!0)}),i}function zi(r){let i="";const e=[];return Object.keys(Kn).forEach(t=>{Kn[t].includes(r)&&(i=t,e.push(...Kn[t]))}),{name:i,series:e}}function Js(r,i,e,t){var s;const n=t.length,o=[];for(let l=1;l<=i;l++){const c=(l-1)%r.length,d=a.Tools.deepClone(r[c]);jt(d);const u=`${(s=r[r.length-1])==null?void 0:s.v}`;let h=t.indexOf(u)+e*l;h<0&&(h+=Math.abs(e)*n);const g=h%n;d&&(d.v=t[g],o.push(d))}return o}function Qd(r,i){const e=[];let t;if(i.startRow<r.startRow)t=a.Direction.UP;else if(i.endRow>r.endRow)t=a.Direction.DOWN;else if(i.startColumn<r.startColumn)t=a.Direction.LEFT;else if(i.endColumn>r.endColumn)t=a.Direction.RIGHT;else return[];if(t===a.Direction.DOWN||t===a.Direction.UP){const n=r.endRow-r.startRow+1,o=i.endRow-i.startRow+1,s=Math.floor(o/n),l=o%n,c={startRow:0,startColumn:0,endRow:r.endRow-r.startRow,endColumn:r.endColumn-r.startColumn};if(t===a.Direction.DOWN){for(let d=0;d<s;d++)e.push({repeatStartCell:{row:r.startRow+(d+1)*n,col:r.startColumn},relativeRange:c});l>0&&e.push({repeatStartCell:{row:r.startRow+(s+1)*n,col:r.startColumn},relativeRange:{startRow:0,startColumn:0,endRow:l-1,endColumn:r.endColumn-r.startColumn}})}else{for(let d=0;d<s;d++)e.push({repeatStartCell:{row:r.startRow-(d+1)*n,col:r.startColumn},relativeRange:c});l>0&&e.push({repeatStartCell:{row:r.startRow-(s+1)*n,col:r.startColumn},relativeRange:{startRow:n-l,endRow:n-1,startColumn:0,endColumn:r.endColumn-r.startColumn}})}}if(t===a.Direction.RIGHT||t===a.Direction.LEFT){const n=r.endColumn-r.startColumn+1,o=i.endColumn-i.startColumn+1,s=Math.floor(o/n),l=o%n,c={startRow:0,startColumn:0,endRow:r.endRow-r.startRow,endColumn:r.endColumn-r.startColumn};if(t===a.Direction.RIGHT){for(let d=0;d<s;d++)e.push({repeatStartCell:{row:r.startRow,col:r.startColumn+(d+1)*n},relativeRange:c});l>0&&e.push({repeatStartCell:{row:r.startRow,col:r.startColumn+(s+1)*n},relativeRange:{startRow:0,startColumn:0,endRow:r.endRow-r.startRow,endColumn:l-1}})}else{for(let d=0;d<s;d++)e.push({repeatStartCell:{row:r.startRow,col:r.startColumn-(d+1)*n},relativeRange:c});l>0&&e.push({repeatStartCell:{row:r.startRow,col:r.startColumn-(s+1)*n},relativeRange:{startRow:0,startColumn:n-l,endRow:r.endRow-r.startRow,endColumn:n-1}})}}return e}function $s(r){return!(a.isFormulaString(r.f)||a.isFormulaId(r.si)||r.t===a.CellValueType.BOOLEAN)}function jt(r){r&&"custom"in r&&delete r.custom}var mn=(r=>(r.Append="APPEND",r.Default="DEFAULT",r.Only="ONLY",r))(mn||{}),Ie=(r=>(r.NUMBER="number",r.DATE="date",r.EXTEND_NUMBER="extendNumber",r.CHN_NUMBER="chnNumber",r.CHN_WEEK2="chnWeek2",r.CHN_WEEK3="chnWeek3",r.LOOP_SERIES="loopSeries",r.FORMULA="formula",r.OTHER="other",r))(Ie||{}),le=(r=>(r.COPY="COPY",r.SERIES="SERIES",r.ONLY_FORMAT="ONLY_FORMAT",r.NO_FORMAT="NO_FORMAT",r))(le||{});const Jd={type:Ie.DATE,priority:1100,match:(r,i)=>{var e;if(r!=null&&r.f||r!=null&&r.si)return!1;if((typeof(r==null?void 0:r.v)=="number"||(r==null?void 0:r.t)===a.CellValueType.NUMBER)&&r.s){if(typeof r.s=="string"){const n=i.get(a.IUniverInstanceService).getFocusedUnit().getStyles().get(r.s),o=(e=n==null?void 0:n.n)==null?void 0:e.pattern;if(o)return a.numfmt.getInfo(o).isDate}else if(r.s.n&&a.numfmt.getInfo(r.s.n.pattern).isDate)return!0}return!1},isContinue:(r,i)=>r.type===Ie.DATE,applyFunctions:{[le.SERIES]:(r,i,e)=>{const{data:t}=r;return e===a.Direction.LEFT||e===a.Direction.UP?(t.reverse(),ei(t,i,e).reverse()):ei(t,i,e)}}},$d={type:Ie.NUMBER,priority:1e3,match:r=>typeof(r==null?void 0:r.v)=="number"||(r==null?void 0:r.t)===a.CellValueType.NUMBER,isContinue:(r,i)=>r.type===Ie.NUMBER,applyFunctions:{[le.SERIES]:(r,i,e)=>{const{data:t}=r;return e===a.Direction.LEFT||e===a.Direction.UP?(t.reverse(),ei(t,i,e).reverse()):ei(t,i,e)}}},$a={type:Ie.OTHER,priority:0,match:()=>!0,isContinue:(r,i)=>r.type===Ie.OTHER},eu={type:Ie.EXTEND_NUMBER,priority:900,match:r=>Vr(`${r==null?void 0:r.v}`||"").isExtendNumber,isContinue:(r,i)=>{var e;if(r.type===Ie.EXTEND_NUMBER){const{beforeTxt:t,afterTxt:n}=Vr(`${(e=r.cellData)==null?void 0:e.v}`||""),{beforeTxt:o,afterTxt:s}=Vr(`${i==null?void 0:i.v}`||"");if(t===o&&n===s)return!0}return!1},applyFunctions:{[le.SERIES]:(r,i,e)=>{var l;const{data:t}=r,n=e===a.Direction.UP||e===a.Direction.LEFT;let o;if(t.length===1)return o=n?-1:1,Ue(qs(t,i,o),n);const s=[];for(let c=0;c<t.length;c++){const d=`${(l=t[c])==null?void 0:l.v}`;d&&s.push(Number(Vr(d).matchTxt))}return n&&(t.reverse(),s.reverse()),cr(s)?(o=s[1]-s[0],Ue(qs(t,i,o),n)):Ft(t,i)}}},tu={type:Ie.CHN_NUMBER,priority:830,match:r=>!!Bd(`${r==null?void 0:r.v}`||""),isContinue:(r,i)=>r.type===Ie.CHN_NUMBER,applyFunctions:{[le.SERIES]:(r,i,e)=>{var c,d,u;const{data:t}=r,n=e===a.Direction.LEFT||e===a.Direction.UP;if(t.length===1){const h=`${(c=t[0])==null?void 0:c.v}`;let g;return n?g=-1:g=1,h&&(h==="日"||ct(h)<7)?Ue(Rn(t,i,g),n):Ue(Qs(t,i,g),n)}let o=!1;for(let h=0;h<t.length;h++)if(((d=t[h])==null?void 0:d.v)==="日"){o=!0;break}const s=[];let l=0;for(let h=0;h<t.length;h++){const g=`${(u=t[h])==null?void 0:u.v}`;g==="日"?h===0?s.push(0):(l++,s.push(l*7)):o&&ct(g)>0&&ct(g)<7?s.push(ct(g)+l*7):s.push(ct(g))}if(n&&(t.reverse(),s.reverse()),cr(s)){if(o||s[s.length-1]<6&&s[0]>0||s[0]<6&&s[s.length-1]>0){const g=s[1]-s[0];return Ue(Rn(t,i,g),n)}const h=s[1]-s[0];return Ue(Qs(t,i,h),n)}return Ft(t,i)}}},nu={type:Ie.CHN_WEEK2,priority:820,match:r=>!!Fd(`${r==null?void 0:r.v}`||""),isContinue:(r,i)=>r.type===Ie.CHN_WEEK2,applyFunctions:{[le.SERIES]:(r,i,e)=>{var l;const{data:t}=r,n=e===a.Direction.LEFT||e===a.Direction.UP;if(t.length===1){let c;return n?c=-1:c=1,Ue(Rn(t,i,c,1),n)}const o=[];let s=0;for(let c=0;c<t.length;c++){const d=`${(l=t[c])==null?void 0:l.v}`,u=d==null?void 0:d.substr(d.length-1,1);d==="周日"?c===0?o.push(0):(s++,o.push(s*7)):o.push(ct(u)+s*7)}if(n&&(t.reverse(),o.reverse()),cr(o)){const c=o[1]-o[0];return Ue(Rn(t,i,c,1),n)}return Ft(t,i)}}},ru={type:Ie.CHN_WEEK3,priority:810,match:r=>jd(`${r==null?void 0:r.v}`||""),isContinue:(r,i)=>r.type===Ie.CHN_WEEK3,applyFunctions:{[le.SERIES]:(r,i,e)=>{var l;const{data:t}=r,n=e===a.Direction.LEFT||e===a.Direction.UP;if(t.length===1){let c;return n?c=-1:c=1,Ue(Rn(t,i,c,2),n)}const o=[];let s=0;for(let c=0;c<t.length;c++){const d=`${(l=t[c])==null?void 0:l.v}`;if(d){const u=d.substr(d.length-1,1);d==="星期日"?c===0?o.push(0):(s++,o.push(s*7)):o.push(ct(u)+s*7)}}if(n&&(t.reverse(),o.reverse()),cr(o)){const c=o[1]-o[0];return Ue(Rn(t,i,c,2),n)}return Ft(t,i)}}},iu={type:Ie.LOOP_SERIES,priority:800,match:r=>qd(`${r==null?void 0:r.v}`||""),isContinue:(r,i)=>{var e;return r.type===Ie.LOOP_SERIES?zi(`${(e=r.cellData)==null?void 0:e.v}`||"").name===zi(`${i==null?void 0:i.v}`||"").name:!1},applyFunctions:{[le.SERIES]:(r,i,e)=>{var c,d;const{data:t}=r,n=e===a.Direction.LEFT||e===a.Direction.UP,{series:o}=zi(`${(c=t[0])==null?void 0:c.v}`||"");if(t.length===1){let u;return n?u=-1:u=1,Ue(Js(t,i,u,o),n)}const s=[];let l=0;for(let u=0;u<t.length;u++){const h=`${(d=t[u])==null?void 0:d.v}`;h&&(h===o[0]?u===0?s.push(0):(l++,s.push(l*o.length)):s.push(o.indexOf(h)+l*7))}if(n&&(t.reverse(),s.reverse()),cr(s)){const u=s[1]-s[0];return Ue(Js(t,i,u,o),n)}return Ft(t,i)}}};function Ue(r,i){return i?r.reverse():r}var ou=Object.defineProperty,su=Object.getOwnPropertyDescriptor,au=(r,i,e,t)=>{for(var n=t>1?void 0:t?su(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&ou(i,e,n),n},on=(r,i)=>(e,t)=>i(e,t,r);exports.AutoFillService=class extends a.Disposable{constructor(e,t,n,o,s,l){super();E(this,"_rules",[]);E(this,"_hooks",[]);E(this,"_applyType$",new O.BehaviorSubject(le.SERIES));E(this,"_isFillingStyle",!0);E(this,"_prevUndos",[]);E(this,"_autoFillLocation$",new O.BehaviorSubject(null));E(this,"autoFillLocation$",this._autoFillLocation$.asObservable());E(this,"_showMenu$",new O.BehaviorSubject(!1));E(this,"showMenu$",this._showMenu$.asObservable());E(this,"_direction",a.Direction.DOWN);E(this,"applyType$",this._applyType$.asObservable());E(this,"_menu$",new O.BehaviorSubject([{label:"autoFill.copy",value:le.COPY,disable:!1},{label:"autoFill.series",value:le.SERIES,disable:!1},{label:"autoFill.formatOnly",value:le.ONLY_FORMAT,disable:!1},{label:"autoFill.noFormat",value:le.NO_FORMAT,disable:!1}]));E(this,"menu$",this._menu$.asObservable());this._commandService=e,this._undoRedoService=t,this._renderManagerService=n,this._univerInstanceService=o,this._selectionManagerService=s,this._injector=l,this._init()}_init(){this._rules=[Jd,$d,eu,tu,nu,ru,iu,$a].sort((e,t)=>t.priority-e.priority),this._isFillingStyle=!0}_getOneByPriority(e){return e.length<=0?[]:[e.reduce((n,o)=>(o.priority||0)>(n.priority||0)?o:n,e[0])]}_initPrevUndo(){this._prevUndos=[]}async triggerAutoFill(e,t,n,o){var S;if(n.startColumn===o.startColumn&&n.startRow===o.startRow&&n.endColumn===o.endColumn&&n.endRow===o.endRow)return!1;if(o.endColumn<n.endColumn&&o.endColumn>n.startColumn)return this._commandService.executeCommand(En.id,{clearRange:{startRow:o.startRow,endRow:o.endRow,startColumn:o.endColumn+1,endColumn:n.endColumn},selectionRange:o});if(o.endRow<n.endRow&&o.endRow>n.startRow)return this._commandService.executeCommand(En.id,{clearRange:{startRow:o.endRow+1,endRow:n.endRow,startColumn:o.startColumn,endColumn:o.endColumn},selectionRange:o});const s={startRow:o.startRow,endRow:o.endRow,startColumn:o.startColumn,endColumn:o.endColumn};let l=null;if(o.startRow<n.startRow)l=a.Direction.UP,s.endRow=n.startRow-1;else if(o.endRow>n.endRow)l=a.Direction.DOWN,s.startRow=n.endRow+1;else if(o.startColumn<n.startColumn)l=a.Direction.LEFT,s.endColumn=n.startColumn-1;else if(o.endColumn>n.endColumn)l=a.Direction.RIGHT,s.startColumn=n.endColumn+1;else return!1;this.direction=l;const c=this._injector.invoke(f=>Sn(n,f)),d=this._injector.invoke(f=>Sn(s,f));if(!c||!d)return!1;this.autoFillLocation={source:c,target:d,unitId:e,subUnitId:t};const u=[];this.getActiveHooks().forEach(f=>{var C;const p=(C=f==null?void 0:f.onBeforeFillData)==null?void 0:C.call(f,{source:c,target:d,unitId:e,subUnitId:t},l);p&&u.unshift(p)}),this._initPrevUndo();for(let f=0;f<u.length;f++){const p=this.menu.find(C=>C.value===u[f]);if(p&&!p.disable)return this.fillData(u[f])}const g=(S=this.menu.find(f=>f.disable===!1))==null?void 0:S.value;return g?this.fillData(g):!1}addHook(e){if(this._hooks.find(t=>t.id===e.id))throw new Error(`Add hook failed, hook id '${e.id}' already exist!`);return e.priority===void 0&&(e.priority=0),e.type===void 0&&(e.type=mn.Append),this._hooks.push(e),a.toDisposable(()=>{const t=this._hooks.findIndex(n=>n===e);t>-1&&this._hooks.splice(t,1)})}registerRule(e){if(this._rules.find(n=>n.type===e.type))throw new Error(`Registry rule failed, type '${e.type}' already exist!`);const t=this._rules.findIndex(n=>n.priority<e.priority);this._rules.splice(t===-1?this._rules.length:t,0,e)}getRules(){return this._rules}getAllHooks(){return this._hooks}getActiveHooks(){const{source:e,target:t,unitId:n,subUnitId:o}=this.autoFillLocation||{};if(!e||!t||!n||!o)return[];const s=this._hooks.filter(u=>{var h;return!((h=u.disable)!=null&&h.call(u,{source:e,target:t,unitId:n,subUnitId:o},this._direction,this.applyType))}),l=s.filter(u=>u.type===mn.Only);if(l.length>0)return this._getOneByPriority(l);const c=this._getOneByPriority(s.filter(u=>u.type===mn.Default)),d=s.filter(u=>u.type===mn.Append)||[];return[...c,...d]}get applyType(){return this._applyType$.getValue()}set applyType(e){this._applyType$.next(e)}get menu(){return this._menu$.getValue()}get direction(){return this._direction}set direction(e){this._direction=e}isFillingStyle(){return this._isFillingStyle}setFillingStyle(e){this._isFillingStyle=e}get autoFillLocation(){return this._autoFillLocation$.getValue()}set autoFillLocation(e){this._autoFillLocation$.next(e)}setDisableApplyType(e,t){this._menu$.next(this._menu$.getValue().map(n=>n.value===e?{...n,disable:t}:n))}setShowMenu(e){this._showMenu$.next(e)}fillData(e){var v,b,R;this.applyType=e;const{source:t,target:n,unitId:o=this._univerInstanceService.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET).getUnitId(),subUnitId:s=(v=this._univerInstanceService.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET).getActiveSheet())==null?void 0:v.getSheetId()}=this.autoFillLocation||{};if(!t||!n||!o||!s)return!1;const l=this.direction;this._prevUndos.length>0&&this._prevUndos.forEach(T=>{this._commandService.syncExecuteCommand(T.id,T.params)}),this._prevUndos=[];const c=a.Rectangle.union(Rt(t),Rt(n)),d=this.getActiveHooks(),u=this._univerInstanceService.getUnit(o,a.UniverInstanceType.UNIVER_SHEET);this._commandService.syncExecuteCommand(m.SetSelectionsOperation.id,{selections:[{primary:{...(R=(b=this._selectionManagerService.getCurrentLastSelection())==null?void 0:b.primary)!=null?R:c},range:{...c,rangeType:a.RANGE_TYPE.NORMAL}}],unitId:o,subUnitId:s});const h=[],g=[];d.forEach(T=>{var P;const{undos:M,redos:y}=((P=T.onFillData)==null?void 0:P.call(T,{source:t,target:n,unitId:o,subUnitId:s},l,e))||{};M&&h.push(...M),y&&g.push(...y)});const S=g.every(T=>this._commandService.syncExecuteCommand(T.id,T.params)),f=[];e!==le.NO_FORMAT&&g.forEach(T=>{if(T.id===m.SetRangeValuesMutation.id){const{cellValue:M}=T.params;new a.ObjectMatrix(M).forValue((P,x,A)=>{const N=Object.keys(u.getStyles().get(A==null?void 0:A.s)||{});(A!=null&&A.p||N.length&&ps.some(D=>N.includes(D)))&&f.push({startRow:P,endRow:P,startColumn:x,endColumn:x})})}});const p=this._getAutoHeightUndoRedos(o,s,f);return p.redos.every(T=>this._commandService.syncExecuteCommand(T.id,T.params))&&(h.push(...p.undos),g.push(...p.redos)),S&&(this._prevUndos=h,this._undoRedoService.pushUndoRedo({unitID:o,undoMutations:h,redoMutations:g})),d.forEach(T=>{var M;(M=T.onAfterFillData)==null||M.call(T,{source:t,target:n,unitId:o,subUnitId:s},l,e)}),this.setShowMenu(!0),!0}_getAutoHeightUndoRedos(e,t,n){var h,g;const o=(h=this._renderManagerService.getRenderById(e))==null?void 0:h.with(exports.SheetSkeletonManagerService),s=(g=o==null?void 0:o.getCurrentParam())==null?void 0:g.skeleton;if(!s)return{redos:[],undos:[]};const l=s.calculateAutoHeightInRange(n),c={subUnitId:t,unitId:e,rowsAutoHeightInfo:l},d=s.worksheet,u=m.SetWorksheetRowAutoHeightMutationFactory(c,d);return{undos:[{id:m.SetWorksheetRowAutoHeightMutation.id,params:u}],redos:[{id:m.SetWorksheetRowAutoHeightMutation.id,params:c}]}}};exports.AutoFillService=au([on(0,a.ICommandService),on(1,a.IUndoRedoService),on(2,w.IRenderManagerService),on(3,a.Inject(a.IUniverInstanceService)),on(4,a.Inject(m.SheetsSelectionsService)),on(5,a.Inject(a.Injector))],exports.AutoFillService);const Et=a.createIdentifier("univer.auto-fill-service"),vt={type:a.CommandType.COMMAND,id:"sheet.command.auto-fill",handler:async(r,i)=>{const e=r.get(Et),t=r.get(a.IUniverInstanceService),{sourceRange:n,targetRange:o}=i,s=m.getSheetCommandTarget(t);if(!s)return!1;const{subUnitId:l,unitId:c}=s;return e.triggerAutoFill(c,l,n,o)}},En={id:"sheet.command.auto-clear-content",type:a.CommandType.COMMAND,handler:async(r,i)=>{const e=r.get(a.IUniverInstanceService),t=r.get(a.ICommandService),n=r.get(a.IUndoRedoService),o=r.get(m.SheetInterceptorService),s=m.getSheetCommandTarget(e);if(!s)return!1;const{unitId:l,subUnitId:c}=s,{clearRange:d,selectionRange:u}=i,h={subUnitId:c,unitId:l,cellValue:m.generateNullCellValue([d])},g=m.SetRangeValuesUndoMutationFactory(r,h),{startColumn:S,startRow:f}=u,p={selections:[{primary:{startColumn:S,startRow:f,endColumn:S,endRow:f,actualRow:f,actualColumn:S,isMerged:!1,isMergedMainCell:!1},range:{...u}}],unitId:l,subUnitId:c};if(t.executeCommand(m.SetSelectionsOperation.id,p),t.syncExecuteCommand(m.SetRangeValuesMutation.id,h)){const v=o.afterCommandExecute({id:m.SetRangeValuesMutation.id,params:h});return a.sequenceExecute(v.redos,t),n.pushUndoRedo({unitID:l,undoMutations:[{id:m.SetRangeValuesMutation.id,params:g},...v.undos],redoMutations:[{id:m.SetRangeValuesMutation.id,params:h},...v.redos]}),!0}return!1}},dr={id:"sheet.command.refill",type:a.CommandType.COMMAND,handler:async(r,i)=>r.get(Et).fillData(i.type)};var lu=Object.defineProperty,cu=Object.getOwnPropertyDescriptor,du=(r,i,e,t)=>{for(var n=t>1?void 0:t?cu(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&lu(i,e,n),n},Mt=(r,i)=>(e,t)=>i(e,t,r);exports.EditorBridgeService=class extends a.Disposable{constructor(e,t,n,o,s,l,c){super();E(this,"_editorUnitId",a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY);E(this,"_editorIsDirty",!1);E(this,"_isDisabled",!1);E(this,"_visible",{visible:!1,eventType:w.DeviceInputEventType.Dblclick,unitId:""});E(this,"_currentEditCell",null);E(this,"_currentEditCellState",null);E(this,"_currentEditCellLayout",null);E(this,"_currentEditCellState$",new O.BehaviorSubject(null));E(this,"currentEditCellState$",this._currentEditCellState$.asObservable());E(this,"_currentEditCellLayout$",new O.BehaviorSubject(null));E(this,"currentEditCellLayout$",this._currentEditCellLayout$.asObservable());E(this,"currentEditCell$",this._currentEditCellState$.pipe(O.switchMap(e=>this._currentEditCellLayout$.pipe(O.map(t=>e&&t?{...e,...t}:null)))));E(this,"_visible$",new O.BehaviorSubject(this._visible));E(this,"visible$",this._visible$.asObservable());E(this,"_afterVisible$",new O.BehaviorSubject(this._visible));E(this,"afterVisible$",this._afterVisible$.asObservable());E(this,"_forceKeepVisible$",new O.BehaviorSubject(!1));E(this,"forceKeepVisible$",this._forceKeepVisible$.asObservable());this._sheetInterceptorService=e,this._renderManagerService=t,this._themeService=n,this._univerInstanceService=o,this._editorService=s,this._refSelectionsService=l,this._contextService=c,this.disposeWithMe(a.toDisposable(()=>{this._currentEditCellState$.complete(),this._currentEditCell=null})),this._univerInstanceService.getTypeOfUnitDisposed$(a.UniverInstanceType.UNIVER_SHEET).subscribe(d=>{var u;d.getUnitId()===((u=this._currentEditCellState)==null?void 0:u.unitId)&&this._clearCurrentEditCellState()})}refreshEditCellState(){const e=this.getLatestEditCellState();if(!e){this._currentEditCellState=null,this._currentEditCellLayout=null,this._currentEditCellState$.next(null),this._currentEditCellLayout$.next(null);return}const{position:t,scaleX:n,scaleY:o,canvasOffset:s,...l}=e;this._currentEditCellState=l,this._currentEditCellLayout={position:t,scaleX:n,scaleY:o,canvasOffset:s},this._currentEditCellState$.next(this._currentEditCellState),this._currentEditCellLayout$.next(this._currentEditCellLayout)}refreshEditCellPosition(e){var y;const t=this._currentEditCell;if(t==null)return;const n=this._renderManagerService.getCurrentTypeOfRenderer(a.UniverInstanceType.UNIVER_SHEET);if(!n)return;const o=(y=n.with(exports.SheetSkeletonManagerService).getSkeletonParam(t.sheetId))==null?void 0:y.skeleton,s=n.with(Ce);if(!o||!this._currentEditCellState)return;const{primary:l,unitId:c,sheetId:d,scene:u,engine:h}=t,g=Jn(o,l);if(g==null)return;const S=a.convertCellToRange(g),f=$.getCanvasOffsetByEngine(h);let{startX:p,startY:C,endX:v,endY:b}=S;const{scaleX:R,scaleY:T}=u.getAncestorScale(),M=u.getViewportScrollXY(s.getViewPort());p=w.convertTransformToOffsetX(p,R,M),C=w.convertTransformToOffsetY(C,T,M),v=w.convertTransformToOffsetX(v,R,M),b=w.convertTransformToOffsetY(b,T,M),e&&this._currentEditCellLayout&&(v=v-p+this._currentEditCellLayout.position.startX,b=b-C+this._currentEditCellLayout.position.startY,p=this._currentEditCellLayout.position.startX,C=this._currentEditCellLayout.position.startY),this._currentEditCellLayout={position:{startX:p,startY:C,endX:v,endY:b},canvasOffset:f,scaleX:R,scaleY:T},this._currentEditCellLayout$.next(this._currentEditCellLayout)}setEditCell(e){this._currentEditCell=e,this._editorService.getFocusEditor()||(this._editorService.focus(a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY),this._contextService.setContextValue(a.EDITOR_ACTIVATED,!1),this._contextService.setContextValue(a.FOCUSING_EDITOR_STANDALONE,!1));const t=this.getLatestEditCellState();if(!t){this._currentEditCellState=t,this._currentEditCellLayout=t,this._currentEditCellState$.next(t),this._currentEditCellLayout$.next(t);return}const{position:n,canvasOffset:o,scaleX:s,scaleY:l,...c}=t;this._currentEditCellState=c,this._currentEditCellLayout={position:n,canvasOffset:o,scaleX:s,scaleY:l},this._currentEditCellState$.next(this._currentEditCellState),this._currentEditCellLayout$.next(this._currentEditCellLayout)}_clearCurrentEditCellState(){this._currentEditCellState=null,this._currentEditCellState$.next(null),this._currentEditCellLayout=null,this._currentEditCellLayout$.next(null)}getEditCellState(){return this._currentEditCellState==null||this._currentEditCellLayout==null?null:{...this._currentEditCellState,...this._currentEditCellLayout}}getEditCellLayout(){return this._currentEditCellLayout}getEditLocation(){return this._currentEditCellState}updateEditLocation(e,t){this._currentEditCellState&&(this._currentEditCellState={...this._currentEditCellState,row:e,column:t})}getLatestEditCellState(){var W,k,U;const e=this._currentEditCell;if(e==null)return;const t=this._renderManagerService.getCurrentTypeOfRenderer(a.UniverInstanceType.UNIVER_SHEET);if(!t)return;const n=t.with(exports.SheetSkeletonManagerService).getCurrentSkeleton(),o=t.with(Ce);if(!n)return;const{primary:s,unitId:l,sheetId:c,scene:d,engine:u}=e,{startRow:h,startColumn:g}=s,S=Jn(n,s);if(S==null)return;const f=a.convertCellToRange(S),p=$.getCanvasOffsetByEngine(u);let{startX:C,startY:v,endX:b,endY:R}=f;const{scaleX:T,scaleY:M}=d.getAncestorScale(),y=d.getViewportScrollXY(o.getViewPort());C=w.convertTransformToOffsetX(C,T,y),v=w.convertTransformToOffsetY(v,M,y),b=w.convertTransformToOffsetX(b,T,y),R=w.convertTransformToOffsetY(R,M,y);const P=this._univerInstanceService.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET),x=P.getActiveSheet();if(!x)return;const A={workbook:P,worksheet:x,unitId:P.getUnitId(),subUnitId:x.getSheetId(),row:h,col:g,origin:x.getCellRaw(h,g)};let N;const D=this._sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(m.BEFORE_CELL_EDIT)(x.getCell(h,g),A);N=D&&n.getCellDocumentModelWithFormula(D);const H=(W=N==null?void 0:N.documentModel)==null?void 0:W.documentStyle.renderConfig;if(H!=null&&(H.cellValueType=a.CellValueType.STRING),!N||N.documentModel==null){const B=n.getBlankCellDocumentModel(D);if(N!=null){const{verticalAlign:F,horizontalAlign:L,wrapStrategy:j,textRotation:z,fill:q}=N,{centerAngle:te,vertexAngle:Q}=w.convertTextRotation(z);B.documentModel.documentStyle.renderConfig={...H,verticalAlign:F,horizontalAlign:L,wrapStrategy:j,background:{rgb:q},centerAngle:te,vertexAngle:Q}}N=B}if(N.fill=N.fill||"#fff",(k=N.documentModel)==null||k.setZoomRatio(Math.max(T,M)),(D==null?void 0:D.isInArrayFormulaRange)===!0){const B=(U=N.documentModel)==null?void 0:U.getBody();B&&(B.textRuns=[{st:0,ed:B.dataStream.length-2,ts:{cl:{rgb:this._themeService.getCurrentTheme().textColorSecondary}}}])}return{position:{startX:C,startY:v,endX:b,endY:R},scaleX:T,scaleY:M,canvasOffset:p,row:h,column:g,unitId:l,sheetId:c,documentLayoutObject:N,editorUnitId:this._editorUnitId,isInArrayFormulaRange:D==null?void 0:D.isInArrayFormulaRange}}getCurrentEditorId(){return this._editorUnitId}changeVisible(e){this._visible=e,e.visible&&(this._editorIsDirty=!1),this._visible$.next(this._visible),this._afterVisible$.next(this._visible)}isVisible(){return this._visible}enableForceKeepVisible(){this._forceKeepVisible$.next(!0)}disableForceKeepVisible(){this._forceKeepVisible$.next(!1)}isForceKeepVisible(){return this._forceKeepVisible$.getValue()}changeEditorDirty(e){this._editorIsDirty=e}getEditorDirty(){return this._editorIsDirty}};exports.EditorBridgeService=du([Mt(0,a.Inject(m.SheetInterceptorService)),Mt(1,w.IRenderManagerService),Mt(2,a.Inject(a.ThemeService)),Mt(3,a.IUniverInstanceService),Mt(4,$.IEditorService),Mt(5,m.IRefSelectionsService),Mt(6,a.IContextService)],exports.EditorBridgeService);const Se=a.createIdentifier("univer.sheet-editor-bridge.service"),ve={id:"sheet.operation.set-cell-edit-visible",type:a.CommandType.OPERATION,handler:(r,i)=>i?(r.get(Se).changeVisible(i),!0):!1},ur={id:"sheet.operation.set-cell-edit-visible-f2",type:a.CommandType.OPERATION,handler:(r,i)=>{const e=r.get(a.ICommandService),n=r.get(a.IUniverInstanceService).getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET);return n?(e.syncExecuteCommand(ve.id,{...i,unitId:n.getUnitId()}),!0):!1}},zt={id:"sheet.operation.set-cell-edit-visible-arrow",type:a.CommandType.OPERATION,handler:()=>!0},Nn={id:"sheet.command.change-zoom-ratio",type:a.CommandType.COMMAND,handler:(r,i)=>{if(!i)return!1;const e=r.get(a.IUniverInstanceService),t=m.getSheetCommandTarget(e);if(!t)return!1;const{worksheet:n,subUnitId:o,unitId:s}=t,{delta:l,reset:c}=i;let d=c?100:Math.round((n.getConfig().zoomRatio+l)*100);d=Math.max(ko[0],d),d=Math.min(ko[1],d);const u=d/100,g=r.get(Se).isVisible();return(g.unitId===s||g.unitId===a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY)&&g.visible?!1:r.get(a.ICommandService).executeCommand(Le.id,{unitId:s,subUnitId:o,zoomRatio:u})}},Dn={id:"sheet.command.set-zoom-ratio",type:a.CommandType.COMMAND,handler:(r,i)=>{if(!i)return!1;const{unitId:e,subUnitId:t,zoomRatio:n}=i,s=r.get(Se).isVisible();return(s.unitId===e||s.unitId===a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY)&&s.visible?!1:r.get(a.ICommandService).executeCommand(Le.id,{unitId:e,subUnitId:t,zoomRatio:n})}};function uu(r){return typeof r.isMainScene>"u"}function Ge(r,i){const e=r instanceof a.Workbook?r:r.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET);if(!e)return null;const t=e.getUnitId();let n,o,s,l;if(uu(i)){const g=i.getRenderById(t);if(g==null)return null;n=g.components,n=g.components,o=g.mainComponent,s=g.scene,l=g.engine}else n=i.components,o=i.mainComponent,s=i.scene,l=i.engine;if(!n||!o)return null;const c=o,d=n.get(me.ROW),u=n.get(me.COLUMN),h=n.get(me.LEFT_TOP);return{spreadsheet:c,spreadsheetRowHeader:d,spreadsheetColumnHeader:u,spreadsheetLeftTopPlaceholder:h,scene:s,engine:l}}function Bo(r,i,e,t){const{startX:n,startY:o,endX:s,endY:l}=t.getCellWithCoordByIndex(r,i);return{startX:n,startY:o,endX:s,endY:l}}function qe(r,i,e,t,n,o){const s=e.getCoordRelativeToViewport(w.Vector2.FromArray([r,i])),{x:l,y:c}=s,d=e.getScrollXYInfoByViewport(s,n),{scaleX:u,scaleY:h}=e.getAncestorScale(),g=t.getCellIndexByOffset(l,c,u,h,d,{closeFirst:o}),{row:S,column:f}=g,p=t.getNoMergeCellWithCoordByIndex(S,f),{startX:C,startY:v,endX:b,endY:R}=p;return{startX:C,startY:v,endX:b,endY:R,row:S,column:f}}function qn(r,i,e,t){const n=e.getCoordRelativeToViewport(w.Vector2.FromArray([r,i])),o=e.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN),s=e.getScrollXYInfoByViewport(n,o),{scaleX:l,scaleY:c}=e.getAncestorScale(),{x:d,y:u}=s,h=r/l+d,g=i/c+u;return{x:h,y:g}}var hu=Object.defineProperty,mu=Object.getOwnPropertyDescriptor,gu=(r,i,e,t)=>{for(var n=t>1?void 0:t?mu(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&hu(i,e,n),n},Wr=(r,i)=>(e,t)=>i(e,t,r);let $n=class extends a.Disposable{constructor(i,e,t,n,o){super();E(this,"_zoom");this._context=i,this._sheetSkeletonManagerService=e,this._commandService=t,this._contextService=n,this._editorBridgeService=o,this._initSkeletonListener(),this._initZoomEventListener()}updateZoom(i,e){const t=this._context.unit.getSheetBySheetId(i);return t?(t.getConfig().zoomRatio=e,t===this._context.unit.getActiveSheet()&&this._updateViewZoom(e),!0):!1}_initZoomEventListener(){const i=this._getSheetObject().scene;this.disposeWithMe(i.onMouseWheel$.subscribeEvent(e=>{if(!e.ctrlKey||!this._contextService.getContextValue(a.FOCUSING_SHEET))return;if(this._editorBridgeService){const d=this._editorBridgeService.isVisible();if((d.unitId===this._context.unitId||d.unitId===a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY)&&d.visible)return}const t=Math.abs(e.deltaX);let n=t<40?.2:t<80?.4:.2;n*=e.deltaY>0?-1:1,i.scaleX<1&&(n/=2);const o=this._context.unit,s=o.getActiveSheet();if(!s)return;const l=s.getZoomRatio();let c=+Number.parseFloat(`${l+n}`).toFixed(1);c=c>=4?4:c<=.1?.1:c,this._commandService.executeCommand(Dn.id,{zoomRatio:Math.round(c*10)/10,unitId:o.getUnitId(),subUnitId:s.getSheetId()}),e.preventDefault()}))}_initSkeletonListener(){this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(i=>{if(i==null)return;const t=this._context.unit.getActiveSheet();if(!t)return;const n=t.getZoomRatio()||1;this._zoom!==n&&this._updateViewZoom(n),this._zoom=n}))}_updateViewZoom(i){const e=this._getSheetObject();e==null||e.scene.scale(i,i),e==null||e.spreadsheet.makeForceDirty()}_getSheetObject(){return Ge(this._context.unit,this._context)}};$n=gu([Wr(1,a.Inject(exports.SheetSkeletonManagerService)),Wr(2,a.ICommandService),Wr(3,a.IContextService),Wr(4,a.Optional(Se))],$n);const Le={id:"sheet.operation.set-zoom-ratio",type:a.CommandType.OPERATION,handler:(r,i)=>{const t=r.get(w.IRenderManagerService).getRenderById(i.unitId);return t?t.with($n).updateZoom(i.subUnitId,i.zoomRatio):!1}};var Su=Object.defineProperty,fu=Object.getOwnPropertyDescriptor,pu=(r,i,e,t)=>{for(var n=t>1?void 0:t?fu(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&Su(i,e,n),n},Xi=(r,i)=>(e,t)=>i(e,t,r);const ea="univer-sheet-main-canvas";exports.SheetsRenderService=class extends a.RxDisposable{constructor(e,t,n){super();E(this,"_skeletonChangeMutations",new Set);this._contextService=e,this._instanceSrv=t,this._renderManagerService=n,Promise.resolve().then(()=>this._init())}registerSkeletonChangingMutations(e){return this._skeletonChangeMutations.has(e)?a.toDisposable(()=>{}):(this._skeletonChangeMutations.add(e),a.toDisposable(()=>this._skeletonChangeMutations.delete(e)))}checkMutationShouldTriggerRerender(e){return this._skeletonChangeMutations.has(e)}_init(){this._initWorkbookListener(),this._initContextListener()}_initWorkbookListener(){this._instanceSrv.getTypeOfUnitAdded$(a.UniverInstanceType.UNIVER_SHEET).pipe(O.takeUntil(this.dispose$)).subscribe(e=>this._createRenderer(e)),this._instanceSrv.getAllUnitsForType(a.UniverInstanceType.UNIVER_SHEET).forEach(e=>this._createRenderer(e)),this._instanceSrv.getTypeOfUnitDisposed$(a.UniverInstanceType.UNIVER_SHEET).pipe(O.takeUntil(this.dispose$)).subscribe(e=>this._disposeRenderer(e)),this.disposeWithMe(this._instanceSrv.getCurrentTypeOfUnit$(a.UniverInstanceType.UNIVER_SHEET).subscribe(e=>{e&&this._renderManagerService.setCurrent(e.getUnitId())}))}_createRenderer(e){const t=e.getUnitId();this._renderManagerService.created$.subscribe(n=>{n.unitId===t&&(n.engine.getCanvas().setId(`${ea}_${t}`),n.engine.getCanvas().getContext().setId(`${ea}_${t}`))}),this._renderManagerService.createRender(t)}_disposeRenderer(e){const t=e.getUnitId();this._renderManagerService.removeRender(t)}_initContextListener(){this.disposeWithMe(this._contextService.subscribeContextValue$(w.RENDER_RAW_FORMULA_KEY).pipe(O.distinctUntilChanged()).subscribe(()=>{this._renderManagerService.getRenderAll().forEach(e=>{e.mainComponent instanceof w.Spreadsheet&&e.mainComponent.makeForceDirty(!0)})}))}};exports.SheetsRenderService=pu([Xi(0,a.IContextService),Xi(1,a.IUniverInstanceService),Xi(2,w.IRenderManagerService)],exports.SheetsRenderService);var Cu=Object.defineProperty,vu=Object.getOwnPropertyDescriptor,_u=(r,i,e,t)=>{for(var n=t>1?void 0:t?vu(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&Cu(i,e,n),n},Pt=(r,i)=>(e,t)=>i(e,t,r);exports.AutoFillController=class extends a.Disposable{constructor(e,t,n,o,s,l,c){super();E(this,"_beforeApplyData",[]);E(this,"_currentLocation",null);E(this,"_copyData",[]);E(this,"_defaultHook");this._univerInstanceService=e,this._commandService=t,this._autoFillService=n,this._editorBridgeService=o,this._renderManagerService=s,this._injector=l,this._sheetsRenderService=c,this._defaultHook={id:"default",type:mn.Default,priority:0,onBeforeFillData:(d,u)=>this._presetAndCacheData(d,u),onFillData:(d,u,h)=>this._fillData(d,u,h)},this._init()}_init(){this._initDefaultHook(),this._initSelectionControlFillChanged(),this._initQuitListener(),this._initSkeletonChange()}_initSkeletonChange(){this.disposeWithMe(this._sheetsRenderService.registerSkeletonChangingMutations(vt.id))}_initDefaultHook(){this.disposeWithMe(this._autoFillService.addHook(this._defaultHook))}_initQuitListener(){const e=[ve.id,En.id,m.SetRangeValuesCommand.id,Le.id,m.SetWorksheetActiveOperation.id,m.MoveRangeMutation.id,m.RemoveRowMutation.id,m.RemoveColMutation.id,m.InsertRowMutation.id,m.InsertColMutation.id,m.MoveRowsMutation.id,m.MoveColsMutation.id,m.SetWorksheetColWidthMutation.id,m.SetWorksheetRowHeightMutation.id,m.SetRangeValuesMutation.id];this.disposeWithMe(this._commandService.onCommandExecuted((t,n)=>{var c,d;const o=n==null?void 0:n.fromCollab,s=n==null?void 0:n.fromSync,l=n==null?void 0:n.fromFormula;e.includes(t.id)&&!o&&!s&&!l&&t.params.trigger!==vt.id&&t.params.trigger!==dr.id&&this._quit(),t.id===m.RemoveSheetMutation.id&&t.params.unitId===((c=this._currentLocation)==null?void 0:c.unitId)&&t.params.subUnitId===((d=this._currentLocation)==null?void 0:d.subUnitId)&&this._quit()})),this.disposeWithMe(this._univerInstanceService.unitDisposed$.subscribe(t=>{var n;t.getUnitId()===((n=this._currentLocation)==null?void 0:n.unitId)&&this._quit()}))}_quit(){this._currentLocation=null,this._beforeApplyData=[],this._copyData=[],this._autoFillService.setShowMenu(!1)}_initSelectionControlFillChanged(){const e=new a.DisposableCollection,t=()=>{e.dispose();const n=this._renderManagerService.getCurrentTypeOfRenderer(a.UniverInstanceType.UNIVER_SHEET);if(!n)return;n.with(Ce).getSelectionControls().forEach(l=>{e.add(l.selectionFilled$.subscribe(c=>{if(c==null||c.startColumn===-1||c.startRow===-1||c.endColumn===-1||c.endRow===-1)return;const d={startColumn:l.model.startColumn,endColumn:l.model.endColumn,startRow:l.model.startRow,endRow:l.model.endRow},u={startColumn:c.startColumn,endColumn:c.endColumn,startRow:c.startRow,endRow:c.endRow};this._commandService.executeCommand(vt.id,{sourceRange:d,targetRange:u})})),e.add(l.fillControl.onDblclick$.subscribeEvent(()=>{const c={startColumn:l.model.startColumn,endColumn:l.model.endColumn,startRow:l.model.startRow,endRow:l.model.endRow};this._handleDbClickFill(c)})),e.add(l.fillControl.onPointerDown$.subscribeEvent(()=>{this._editorBridgeService.isVisible().visible&&this._commandService.syncExecuteCommand(ve.id,{visible:!1,eventType:w.DeviceInputEventType.PointerDown,unitId:n.unitId})}))})};t(),this.disposeWithMe(this._commandService.onCommandExecuted(n=>{n.id===m.SetSelectionsOperation.id&&t()})),this.disposeWithMe(this._univerInstanceService.getCurrentTypeOfUnit$(a.UniverInstanceType.UNIVER_SHEET).subscribe(()=>t()))}_handleDbClickFill(e){const t=this._detectFillRange(e);t.endRow<=e.endRow||this._commandService.executeCommand(vt.id,{sourceRange:e,targetRange:t})}_detectFillRange(e){var g,S,f,p,C,v;const{startRow:t,endRow:n,startColumn:o,endColumn:s}=e,l=(g=this._univerInstanceService.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET))==null?void 0:g.getActiveSheet();if(!l)return e;const c=l.getCellMatrix(),d=l.getMaxRows()-1,u=l.getMaxColumns()-1;let h=n;if(o>0&&((S=c.getValue(t,o-1))==null?void 0:S.v)!=null){let b=t;for(;((f=c.getValue(b,o-1))==null?void 0:f.v)!=null&&b<d;)b+=1;h=b-1}else if(s<u&&((p=c.getValue(n,s+1))==null?void 0:p.v)!=null){let b=t;for(;((C=c.getValue(b,s+1))==null?void 0:C.v)!=null&&b<d;)b+=1;h=b-1}for(let b=n+1;b<=h;b++)for(let R=o;R<=s;R++)if(((v=c.getValue(b,R))==null?void 0:v.v)!=null){h=b-1;break}return{startColumn:o,endColumn:s,startRow:t,endRow:h}}_getApplyData(e,t,n,o,s,l=!0){const c=[],d=Math.floor(n/t),u=n%t,h=this._autoFillService.getRules();if(!l&&s===le.ONLY_FORMAT)return console.error("ERROR: only format can not be applied when hasStyle is false"),[];const g={};h.forEach(S=>{g[S.type]=[]}),h.forEach(S=>{const{type:f,applyFunctions:p={}}=S,C=e[f];C&&C.forEach(v=>{const b=zd(v.index,u),R=v.index.length*d+b,T=this._applyFunctions(v,R,o,s,p,e),M=Xd(t,n,v.index);g[f].push({data:T,index:M})})});for(let S=0;S<n;S++)h.forEach(f=>{const{type:p}=f,C=g[p];for(let v=0;v<C.length;v++)S in C[v].index&&c.push(C[v].data[C[v].index[S]])});return c}_applyFunctions(e,t,n,o,s,l){const{data:c}=e,d=n===a.Direction.UP||n===a.Direction.LEFT;if(o===le.COPY){const u=s==null?void 0:s[le.COPY];return u?u(e,t,n,l):(d&&c.reverse(),Ft(c,t))}if(o===le.SERIES){const u=s==null?void 0:s[le.SERIES];return u?u(e,t,n,l):(d&&c.reverse(),s!=null&&s[le.COPY]?s[le.COPY](e,t,n,l):Ft(c,t))}if(o===le.ONLY_FORMAT){const u=s==null?void 0:s[le.ONLY_FORMAT];return u?u(e,t,n,l):Yd(c,t)}}_getCopyData(e,t){const n=this._univerInstanceService.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET).getActiveSheet();if(!n)throw new Error("No active sheet found");const o=n.getCellMatrix(),s=this._autoFillService.getRules(),l=[],c=t===a.Direction.DOWN||t===a.Direction.UP;let d,u;return c?(d=e.cols,u=e.rows):(d=e.rows,u=e.cols),d.forEach(h=>{const g=this._getEmptyCopyDataPiece(),S={type:void 0,cellData:void 0};u.forEach(f=>{let p;c?p=o.getValue(f,h):p=o.getValue(h,f);const{type:C,isContinue:v}=s.find(b=>b.match(p,this._injector))||$a;if(v(S,p)){const b=g[C],R=b[b.length-1];R.data.push(p),R.index.push(f-u[0])}else{const b=g[C];b?b.push({data:[p],index:[f-u[0]]}):g[C]=[{data:[p],index:[f-u[0]]}]}S.type=C,S.cellData=p}),l.push(g)}),l}_getEmptyCopyDataPiece(){const e={};return this._autoFillService.getRules().forEach(t=>{e[t.type]=[]}),e}_getMergeApplyData(e,t,n,o){const s=this._univerInstanceService.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET).getActiveSheet();if(!s)throw new Error("No active sheet found");const l=[];for(let c=e.startRow;c<=e.endRow;c++)for(let d=e.startColumn;d<=e.endColumn;d++){const{isMergedMainCell:u,startRow:h,startColumn:g,endRow:S,endColumn:f}=s.getCellInfoInMergeData(c,d);if(u){if(n===a.Direction.DOWN){let p=h+o,C=S+o;for(;C<=t.endRow;)l.push({startRow:p,startColumn:g,endRow:C,endColumn:f}),p+=o,C+=o}else if(n===a.Direction.UP){let p=h-o,C=S-o;for(;p>=t.startRow;)l.push({startRow:p,startColumn:g,endRow:C,endColumn:f}),p-=o,C-=o}else if(n===a.Direction.RIGHT){let p=g+o,C=f+o;for(;C<=t.endColumn;)l.push({startRow:h,startColumn:p,endRow:S,endColumn:C}),p+=o,C+=o}else if(n===a.Direction.LEFT){let p=g-o,C=f-o;for(;p>=t.startColumn;)l.push({startRow:h,startColumn:p,endRow:S,endColumn:C}),p-=o,C-=o}}}return l}_presetAndCacheData(e,t){const{source:n,target:o}=e,s=this._univerInstanceService.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET).getActiveSheet();if(!s)throw new Error("No active sheet found");const l=s.getCellMatrix(),c=[];return o.rows.forEach(d=>{const u=[];o.cols.forEach(h=>{u.push(a.Tools.deepClone(l.getValue(d,h)))}),c.push(u)}),this._beforeApplyData=c,this._copyData=this._getCopyData(n,t),this._currentLocation=e,this._shouldDisableSeries(this._copyData)?(this._autoFillService.setDisableApplyType(le.SERIES,!0),le.COPY):(this._autoFillService.setDisableApplyType(le.SERIES,!1),this._getPreferredApplyType(this._copyData))}_fillData(e,t,n){var W,k;const o=[],s=[];let l=!0;n===le.NO_FORMAT&&(l=!1,n=le.SERIES);const{source:c,target:d,unitId:u,subUnitId:h}=e;if(!c||!d||t==null)return{undos:o,redos:s};const g=Rt(c),S=Rt(d),{cols:f,rows:p}=d,{cols:C,rows:v}=c,b=this._copyData;let R;t===a.Direction.DOWN||t===a.Direction.UP?R=v.length:R=C.length;const T=[];if(t===a.Direction.DOWN||t===a.Direction.UP){const U=p.length,B=[];f.forEach((F,L)=>{const j=b[L],z=this._getApplyData(j,R,U,t,n,l);B.push(z)});for(let F=0;F<B[0].length;F++){const L=[];for(let j=0;j<B.length;j++)L.push({s:null,...B[j][F]});T.push(L)}}else{const U=f.length;p.forEach((B,F)=>{const L=b[F],j=this._getApplyData(L,R,U,t,n,l),z=[];for(let q=0;q<j.length;q++)z.push({s:null,...j[q]});T.push(z)})}let M=[];const y=this._univerInstanceService.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET).getStyles();if(l?(M=this._getMergeApplyData(g,S,t,R),T.forEach(U=>{U.forEach(B=>{B&&y&&y&&(B.s=y.getStyleByCell(B))})})):T.forEach((U,B)=>{U.forEach((F,L)=>{F&&y&&(F.s=y.getStyleByCell(this._beforeApplyData[B][L])||null)})}),n===le.ONLY_FORMAT&&T.forEach((U,B)=>{U.forEach((F,L)=>{if(F){const j=this._beforeApplyData[B][L]||{};F.f=j.f,F.si=j.si,F.t=j.t,F.v=j.v}})}),l){const U=[],B=(k=(W=this._univerInstanceService.getUniverSheetInstance(u))==null?void 0:W.getSheetBySheetId(h))==null?void 0:k.getMergeData();B&&B.forEach(j=>{a.Rectangle.intersects(j,S)&&U.push(j)});const F={unitId:u,subUnitId:h,ranges:U},L=this._injector.invoke(m.RemoveMergeUndoMutationFactory,F);U.length&&(s.push({id:m.RemoveWorksheetMergeMutation.id,params:F}),o.unshift({id:m.AddWorksheetMergeMutation.id,params:L}))}const P={subUnitId:h,unitId:u,cellValue:Vd([d])},x=this._injector.invoke(m.SetRangeValuesUndoMutationFactory,P);s.push({id:m.SetRangeValuesMutation.id,params:P}),o.unshift({id:m.SetRangeValuesMutation.id,params:x});const A=new a.ObjectMatrix;p.forEach((U,B)=>{f.forEach((F,L)=>{T[B][L]&&A.setValue(U,F,T[B][L])})});const N=A.getMatrix(),D={subUnitId:h,unitId:u,cellValue:N},H=this._injector.invoke(m.SetRangeValuesUndoMutationFactory,D);if(this._autoFillService.getActiveHooks().forEach(U=>{var B;(B=U.onBeforeSubmit)==null||B.call(U,e,t,n,N)}),o.unshift({id:m.SetRangeValuesMutation.id,params:H}),s.push({id:m.SetRangeValuesMutation.id,params:D}),M!=null&&M.length){const U=m.getAddMergeMutationRangeByType(M),B={unitId:u,subUnitId:h,ranges:U},F=this._injector.invoke(m.AddMergeUndoMutationFactory,B);o.unshift({id:m.RemoveWorksheetMergeMutation.id,params:F}),s.push({id:m.AddWorksheetMergeMutation.id,params:B})}return{undos:o,redos:s}}_shouldDisableSeries(e){return e.every(t=>Object.keys(t).every(n=>{var o;return((o=t[n])==null?void 0:o.length)===0||[Ie.OTHER,Ie.FORMULA].includes(n)}))}_getPreferredApplyType(e){return e.every(n=>Object.keys(n).every(o=>{var s,l;return((s=n[o])==null?void 0:s.length)===0||((l=n[o])==null?void 0:l.length)===1&&n[o][0].data.length===1&&Ie.NUMBER===o}))?le.COPY:le.SERIES}};exports.AutoFillController=_u([Pt(0,a.IUniverInstanceService),Pt(1,a.ICommandService),Pt(2,Et),Pt(3,Se),Pt(4,w.IRenderManagerService),Pt(5,a.Inject(a.Injector)),Pt(6,a.Inject(exports.SheetsRenderService))],exports.AutoFillController);var Iu=Object.defineProperty,bu=Object.getOwnPropertyDescriptor,Ru=(r,i,e,t)=>{for(var n=t>1?void 0:t?bu(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&Iu(i,e,n),n},Yi=(r,i)=>(e,t)=>i(e,t,r);exports.CellCustomRenderController=class extends a.Disposable{constructor(e,t,n,o){super();E(this,"_enterActiveRender");this._context=e,this._sheetSkeletonManagerService=t,this._renderManagerService=n,this._permissionService=o,this._initEventBinding()}_initEventBinding(){const e=new a.DisposableCollection,t=this._context.unit,n=o=>{if(e.dispose(),!o)return;const s=this._context.unitId,{skeleton:l}=o,c=this._renderManagerService.getRenderById(s);if(c&&c.mainComponent){const d=c.mainComponent,u=S=>{const{offsetX:f,offsetY:p}=S,C=c.scene,v=t.getActiveSheet();if(!v)return;const b=C.getActiveViewportByCoord(w.Vector2.FromArray([f,p]));if(!b)return;const{scaleX:R,scaleY:T}=C.getAncestorScale(),M={x:b.viewportScrollX,y:b.viewportScrollY},y=l.getCellIndexByOffset(f,p,R,T,M),P=l.worksheet.getMergedCell(y.row,y.column),x={actualRow:P?P.startRow:y.row,actualCol:P?P.startColumn:y.column,mergeCell:P,row:y.row,col:y.column};if(!x||!l)return;const A=v.getCell(x.actualRow,x.actualCol);if(!A)return;const N=A.customRender;if(!(N!=null&&N.length))return;const D=x.actualRow,H=x.actualCol,W=N.sort(a.sortRules),k=v.getSheetId(),U={data:A,style:l.getStyles().getStyleByCell(A),primaryWithCoord:l.getCellWithCoordByIndex(x.actualRow,x.actualCol),unitId:s,subUnitId:k,row:D,col:H,workbook:t,worksheet:v},B={x:M.x+f/R,y:M.y+p/T},F=W.find(L=>{var j;return(j=L.isHit)==null?void 0:j.call(L,B,U)});if(F)return[F,U]},h=d.onPointerDown$.subscribeEvent(S=>{var p,C,v,b;const f=u(S);if(f){const[R,T]=f,{row:M,col:y,worksheet:P,unitId:x,subUnitId:A}=T;if(!((p=this._permissionService.composePermission([new m.WorkbookEditablePermission(x).id,new m.WorksheetEditPermission(x,A).id]))==null?void 0:p.every(H=>H.value)))return!1;const D=(v=(C=P.getCell(M,y))==null?void 0:C.selectionProtection)==null?void 0:v[0];if((D==null?void 0:D[m.UnitAction.Edit])===!1)return!1;(b=R.onPointerDown)==null||b.call(R,T,S)}}),g=a.fromEventSubject(d.onPointerMove$).pipe(O.throttleTime(30)).subscribe(S=>{var p,C,v,b,R,T;const f=u(S);if(f){const[M,y]=f;this._enterActiveRender?this._enterActiveRender.render!==M&&((C=(p=this._enterActiveRender.render).onPointerLeave)==null||C.call(p,this._enterActiveRender.cellContext,S),this._enterActiveRender={render:M,cellContext:y},(v=M.onPointerEnter)==null||v.call(M,y,S)):(this._enterActiveRender={render:M,cellContext:y},(b=M.onPointerEnter)==null||b.call(M,y,S))}else this._enterActiveRender&&((T=(R=this._enterActiveRender.render).onPointerLeave)==null||T.call(R,this._enterActiveRender.cellContext,S),this._enterActiveRender=null)});h&&e.add(h),g&&e.add(g)}};this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(n)),n(this._sheetSkeletonManagerService.getCurrentParam()),this.disposeWithMe(e)}};exports.CellCustomRenderController=Ru([Yi(1,a.Inject(exports.SheetSkeletonManagerService)),Yi(2,w.IRenderManagerService),Yi(3,a.IPermissionService)],exports.CellCustomRenderController);class Cs extends a.Disposable{constructor(){super(...arguments);E(this,"rangesStack",[]);E(this,"selectedRangeWorksheet","")}}var Eu=Object.defineProperty,Tu=Object.getOwnPropertyDescriptor,wu=(r,i,e,t)=>{for(var n=t>1?void 0:t?Tu(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&Eu(i,e,n),n},ta=(r,i)=>(e,t)=>i(e,t,r);let Tn=class{constructor(r,i){E(this,"_current",null);E(this,"_shortcutParam",[]);this._univerInstanceService=r,this._localeService=i}dispose(){this._shortcutParam=[]}getCurrentBySearch(r){return this._getCurrentBySearch(r)}getCurrent(){return this._getCurrentBySearch(this._current)}addOrUpdate(r){const i=this._getCurrentBySearch({unitId:r.unitId,sheetId:r.sheetId,keycode:r.keycode});if(i!=null){const e=this._shortcutParam.indexOf(i);this._shortcutParam.splice(e,1)}return this._shortcutParam.push(r),i}remove(r){if(r==null)return;const i=this._getCurrentBySearch(r);if(i==null)return;const e=this._shortcutParam.indexOf(i);return this._shortcutParam.splice(e,1)[0]}_getCurrentBySearch(r){return r==null?void 0:this._shortcutParam.find(e=>e.unitId===r.unitId&&e.sheetId===r.sheetId&&e.keycode===r.keycode)}};Tn=wu([ta(0,a.IUniverInstanceService),ta(1,a.Inject(a.LocaleService))],Tn);var xi=(r=>(r.MergeAll="mergeAll",r.MergeVertical="mergeVertical",r.MergeHorizontal="mergeHorizontal",r))(xi||{});function _t(r,i,e,t,n=!0,o=1,s=!0){let l={...r};t==null&&(t={startRow:0,endRow:e.getRowCount()-1,startColumn:0,endColumn:e.getColumnCount()-1});let c;switch(i){case a.Direction.UP:for(c=r.startRow-o;c>-1&&!e.getRowVisible(c);)c-=1;c>=t.startRow?(l.startRow=c,l.endRow=c):s&&(l.startRow=t.endRow,l.endRow=t.endRow,n&&(l=_t(l,a.Direction.LEFT,e,t,!1)));break;case a.Direction.DOWN:for(c=r.endRow+o;c<e.getRowCount()&&!e.getRowVisible(c);)c+=1;c<=t.endRow?(l.startRow=c,l.endRow=c):s&&(l.startRow=t.startRow,l.endRow=t.startRow,n&&(l=_t(l,a.Direction.RIGHT,e,t,!1)));break;case a.Direction.LEFT:for(c=r.startColumn-o;c>-1&&!e.getColVisible(c);)c-=1;c>=t.startColumn?(l.startColumn=c,l.endColumn=c):s&&(l.startColumn=t.endColumn,l.endColumn=t.endColumn,n&&(l=_t(l,a.Direction.UP,e,t,!1)));break;case a.Direction.RIGHT:for(c=r.endColumn+o;c<e.getColumnCount()&&!e.getColVisible(c);)c+=1;c<=t.endColumn?(l.startColumn=c,l.endColumn=c):s&&(l.startColumn=t.startColumn,l.endColumn=t.startColumn,n&&(l=_t(l,a.Direction.DOWN,e,t,!1)));break}return l}function vs(r,i,e){const t={...r},{startRow:n,startColumn:o,endRow:s,endColumn:l}=er(r,i,e);let c=Ln(e,n,o,s,l).hasValue,d=!0,u=!0;for(;u;){if(a.Direction.UP===i){let h=t.startRow-1;for(;h>-1&&!e.getRowVisible(h);)h-=1;if(h===-1){u=!1;break}const{hasValue:g,matrix:S}=Ln(e,h,t.startColumn,h,t.endColumn);if(c&&!g&&!d){u=!1;break}else{if(S.getLength()!==0){let f=h;S.forValue(p=>{f=Math.min(p,f)}),t.startRow=f}else t.startRow=h;if(t.endRow=t.startRow,!c&&g){u=!1;break}c=g,d=!1}}if(a.Direction.DOWN===i){let h=t.endRow+1;for(;h<e.getRowCount()&&!e.getRowVisible(h);)h+=1;if(h===e.getRowCount()){u=!1;break}const{hasValue:g,matrix:S}=Ln(e,h,t.startColumn,h,t.endColumn);if(c&&!g&&!d){u=!1;break}else{if(S.getLength()!==0){let f=h;S.forValue((p,C,v)=>{f=Math.max(p+(v.rowSpan||1)-1,f)}),t.endRow=f}else t.endRow=h;if(t.startRow=t.endRow,!c&&g){u=!1;break}c=g,d=!1}}if(a.Direction.LEFT===i){let h=t.startColumn-1;for(;h>-1&&!e.getColVisible(h);)h-=1;if(h===-1){u=!1;break}const{hasValue:g,matrix:S}=Ln(e,t.startRow,h,t.endRow,h);if(c&&!g&&!d){u=!1;break}else{if(S.getLength()!==0){let f=h;S.forValue((p,C)=>{f=Math.min(C,f)}),t.startColumn=f}else t.startColumn=h;if(t.endColumn=t.startColumn,!c&&g){u=!1;break}c=g,d=!1}}if(a.Direction.RIGHT===i){let h=t.endColumn+1;for(;h<e.getColumnCount()&&!e.getColVisible(h);)h+=1;if(h===e.getColumnCount()){u=!1;break}const{hasValue:g,matrix:S}=Ln(e,t.startRow,h,t.endRow,h);if(c&&!g&&!d){u=!1;break}else{if(S.getLength()!==0){let f=h;S.forValue((p,C,v)=>{f=Math.max(C+(v.colSpan||1)-1,f)}),t.endColumn=f}else t.endColumn=h;if(t.startColumn=t.endColumn,!c&&g){u=!1;break}c=g,d=!1}}}return m.alignToMergedCellsBorders(t,e,!0)}function el(r,i,e){return _t(r,i,e,void 0,!1,1,!1)}function yu(r,i,e){const t=vs(r,i,e);return m.alignToMergedCellsBorders(a.Rectangle.union(t,r),e,!0)}function Mu(r,i,e){const t=el(r,i,e),n={startRow:Math.min(r.startRow,t.startRow),startColumn:Math.min(r.startColumn,t.startColumn),endRow:Math.max(r.endRow,t.endRow),endColumn:Math.max(r.endColumn,t.endColumn)};return m.alignToMergedCellsBorders(a.Rectangle.union(r,n),e)}function Pu(r,i,e,t){const n=a.getReverseDirection(e),o=vs(er(r,n,t),e,t);return e===a.Direction.UP&&o.startRow<=r.startRow?m.alignToMergedCellsBorders({...i,startColumn:r.startColumn,endColumn:r.endColumn},t,!0):e===a.Direction.DOWN&&o.endRow>=r.endRow?m.alignToMergedCellsBorders({...i,startColumn:r.startColumn,endColumn:r.endColumn},t,!0):e===a.Direction.LEFT&&o.startColumn<=r.startColumn?m.alignToMergedCellsBorders({...i,startRow:r.startRow,endRow:r.endRow},t,!0):e===a.Direction.RIGHT&&o.endColumn>=r.endColumn?m.alignToMergedCellsBorders({...i,startRow:r.startRow,endRow:r.endRow},t,!0):a.Rectangle.union(a.Rectangle.clone(i),o)}function Ou(r,i,e){const t=a.getReverseDirection(i),n=er(r,t,e),o=er(r,i,e),s=el(n,i,e);return m.alignToMergedCellsBorders(a.Rectangle.union(o,s),e,!1)}function na(r){return{startRow:0,startColumn:0,endRow:r.getRowCount()-1,endColumn:r.getColumnCount()-1,rangeType:a.RANGE_TYPE.ALL}}function er(r,i,e){let t;switch(i){case a.Direction.UP:t={startRow:r.startRow,startColumn:r.startColumn,endRow:r.startRow,endColumn:r.endColumn,rangeType:a.RANGE_TYPE.NORMAL};break;case a.Direction.DOWN:t={startRow:r.endRow,startColumn:r.startColumn,endRow:r.endRow,endColumn:r.endColumn,rangeType:a.RANGE_TYPE.NORMAL};break;case a.Direction.LEFT:t={startRow:r.startRow,startColumn:r.startColumn,endRow:r.endRow,endColumn:r.startColumn,rangeType:a.RANGE_TYPE.NORMAL};break;case a.Direction.RIGHT:t={startRow:r.startRow,startColumn:r.endColumn,endRow:r.endRow,endColumn:r.endColumn,rangeType:a.RANGE_TYPE.NORMAL};break;default:throw new Error("Invalid direction")}return m.alignToMergedCellsBorders(t,e,!1)}function Ln(r,i,e,t,n){let o=!1;const s=r.getMatrixWithMergedCells(i,e,t,n).forValue((l,c,d)=>{if(Au(d))return o=!0,!1});return{hasValue:o,matrix:s}}function Fo(r,i,e){const t=a.Rectangle.clone(r);if(i==null)return t;switch(e){case a.Direction.UP:case a.Direction.DOWN:t.startColumn=t.endColumn=i.actualColumn;break;case a.Direction.LEFT:case a.Direction.RIGHT:t.startRow=t.endRow=i.actualRow;break}return t}function xu(r,i,e){var l,c,d,u;const{primary:t,range:n}=r,o=a.Rectangle.clone(n);switch(i){case a.Direction.UP:case a.Direction.DOWN:o.startRow=(l=t==null?void 0:t.startRow)!=null?l:n.startRow,o.endRow=(c=t==null?void 0:t.endRow)!=null?c:n.startRow;break;case a.Direction.LEFT:case a.Direction.RIGHT:o.startColumn=(d=t==null?void 0:t.startColumn)!=null?d:n.startColumn,o.endColumn=(u=t==null?void 0:t.endColumn)!=null?u:n.startColumn;break}const s=er(o,i,e);switch(i){case a.Direction.DOWN:return n.startRow<s.startRow;case a.Direction.UP:return n.endRow>s.endRow;case a.Direction.LEFT:return s.endColumn<n.endColumn;case a.Direction.RIGHT:return s.startColumn>n.startColumn}}function tl(r,i){let e=r[0].startRow,t=r[0].endRow;r.forEach(o=>{const{startRow:s,endRow:l}=o;e=Math.min(e,s),t=Math.max(t,l)});const n=new Array(t-e+1).fill(!1);return i.forEach(o=>{const{startRow:s,endRow:l}=o;for(let c=Math.max(s,e);c<=Math.min(l,t);c++)n[c-e]=!0}),r.every(o=>{const{startRow:s,endRow:l}=o;for(let c=s;c<=l;c++)if(!n[c-e])return!1;return!0})}function nl(r,i){let e=r[0].startColumn,t=r[0].endColumn;r.forEach(o=>{const{startColumn:s,endColumn:l}=o;e=Math.min(e,s),t=Math.max(t,l)});const n=new Array(t-e+1).fill(!1);return i.forEach(o=>{const{startColumn:s,endColumn:l}=o;for(let c=Math.max(s,e);c<=Math.min(l,t);c++)n[c-e]=!0}),r.every(o=>{const{startColumn:s,endColumn:l}=o;for(let c=s;c<=l;c++)if(!n[c-e])return!1;return!0})}function _s(r,i){return i?r==="mergeAll"?i.filter(e=>!(e.startRow===e.endRow&&e.startColumn===e.endColumn)):r==="mergeVertical"?i.filter(e=>e.startRow!==e.endRow):r==="mergeHorizontal"?i.filter(e=>e.startColumn!==e.endColumn):i:null}function Au(r){return r.v!==void 0&&r.v!==null&&r.v!==""||r.p!==void 0}var Je=(r=>(r[r.moveStopeOne=0]="moveStopeOne",r[r.moveGap=1]="moveGap",r[r.moveStepPage=2]="moveStepPage",r[r.moveStepEnd=3]="moveStepEnd",r))(Je||{});const xe={id:"sheet.command.move-selection",type:a.CommandType.COMMAND,handler:(r,i)=>{if(!i)return!1;const e=m.getSheetCommandTarget(r.get(a.IUniverInstanceService));if(!e)return!1;const{workbook:t,worksheet:n}=e,o=m.getSelectionsService(r,i.fromCurrentSelection).getCurrentLastSelection();if(!o)return!1;const{direction:s,jumpOver:l,extra:c}=i,{range:d,primary:u}=o,h=Fo(d,u,s),g=l===1?vs(h,s,n):_t(h,s,n),S=m.getCellAtRowCol(g.startRow,g.startColumn,n);if(a.Rectangle.equals(S,h))return!1;const f=t.getUnitId();r.get(Tn).remove({unitId:f,sheetId:n.getSheetId(),keycode:_.KeyCode.TAB});const p=[{range:a.Rectangle.clone(S),primary:{startRow:S.startRow,startColumn:S.startColumn,endRow:S.endRow,endColumn:S.endColumn,actualRow:g.startRow,actualColumn:g.startColumn,isMerged:S.isMerged,isMergedMainCell:S.startRow===g.startRow&&S.startColumn===g.startColumn}}];return r.get(a.ICommandService).executeCommand(m.SetSelectionsOperation.id,{unitId:t.getUnitId(),subUnitId:n.getSheetId(),selections:p,type:m.SelectionMoveType.MOVE_END,extra:c,reveal:!0})}},$e={id:"sheet.command.move-selection-enter-tab",type:a.CommandType.COMMAND,handler:(r,i)=>{var U;if(!i)return!1;const e=m.getSheetCommandTarget(r.get(a.IUniverInstanceService));if(!e)return!1;const{workbook:t,worksheet:n}=e,o=m.getSelectionsService(r),{direction:s,keycode:l}=i,c=s===a.Direction.LEFT||s===a.Direction.UP,d=o.getCurrentSelections().concat(),u=d.findIndex(B=>B.primary),h=d[u];if(!h)return!1;const g=c?-1:1,S=u+g!==d.length?d[u+g]:d[0],f=d.findIndex(B=>B===S),p=t.getUnitId(),C=n.getSheetId();if(!h)return!1;const{range:v}=h,b=h.primary;let R=Fo(v,b,s);const T=r.get(Tn),M=T.getCurrentBySearch({unitId:p,sheetId:C,keycode:_.KeyCode.TAB});let y;const{startRow:P,endRow:x,startColumn:A,endColumn:N}=v,D=c?b.startRow===P&&b.startColumn===A:b.endRow===x&&b.endColumn===N;if(a.Rectangle.equals(v,b)){if(l===_.KeyCode.TAB)M==null&&T.addOrUpdate({unitId:p,sheetId:C,keycode:_.KeyCode.TAB,selection:h});else{const L=M==null?void 0:M.selection;if(L!=null){const{range:j,primary:z}=L;R=Fo(j,z,s)}T.remove({unitId:p,sheetId:C,keycode:_.KeyCode.TAB})}const B=_t(R,s,n),F=m.getCellAtRowCol(B.startRow,B.startColumn,n);if(a.Rectangle.equals(F,R))return!1;y={range:a.Rectangle.clone(F),primary:{startRow:F.startRow,startColumn:F.startColumn,endRow:F.endRow,endColumn:F.endColumn,actualRow:B.startRow,actualColumn:B.startColumn,isMerged:F.isMerged,isMergedMainCell:F.startRow===B.startRow&&F.startColumn===B.startColumn}}}else{T.remove({unitId:p,sheetId:C,keycode:_.KeyCode.TAB});const B=a.Tools.deepClone(b),F=D?S.range:_t({startRow:B.startRow,startColumn:B.startColumn,endRow:B.endRow,endColumn:B.endColumn},s,n,{startRow:P,endRow:x,startColumn:A,endColumn:N}),L=D&&c,j=L?m.getCellAtRowCol(F.endRow,F.endColumn,n):m.getCellAtRowCol(F.startRow,F.startColumn,n);y={range:D?S.range:a.Rectangle.clone(v),primary:{startRow:j.startRow,startColumn:j.startColumn,endRow:j.endRow,endColumn:j.endColumn,actualRow:L?j.startRow:F.startRow,actualColumn:L?j.startColumn:F.startColumn,isMerged:j.isMerged,isMergedMainCell:j.startRow===F.startRow&&j.startColumn===F.startColumn},style:D?S.style:h.style}}D?(d[u].primary=null,d[f]=y,o.setSelections(p,C,[],m.SelectionMoveType.MOVE_END)):d[u]=y;const H=r.get(a.ICommandService).executeCommand(m.SetSelectionsOperation.id,{unitId:p,subUnitId:C,type:m.SelectionMoveType.MOVE_END,selections:d,reveal:!0,extra:i.extra}),k=(U=r.get(w.IRenderManagerService).getRenderById(p))==null?void 0:U.with(Ce);return k==null||k.refreshSelectionMoveEnd(),H}},He={id:"sheet.command.expand-selection",type:a.CommandType.COMMAND,handler:(r,i)=>{if(!i)return!1;const e=m.getSheetCommandTarget(r.get(a.IUniverInstanceService));if(!e)return!1;const{worksheet:t,unitId:n,subUnitId:o}=e,s=m.getSelectionsService(r).getCurrentLastSelection();if(!s)return!1;const{range:l,primary:c}=s,{jumpOver:d,direction:u,extra:h}=i,S=xu(s,u,t)?d===1?Pu(l,{...a.Rectangle.clone(c),rangeType:a.RANGE_TYPE.NORMAL},u,t):Ou(l,u,t):d===1?yu(l,u,t):Mu(l,u,t);return S.rangeType=s.range.rangeType,a.Rectangle.equals(S,l)?!1:r.get(a.ICommandService).syncExecuteCommand(m.SetSelectionsOperation.id,{unitId:n,subUnitId:o,type:m.SelectionMoveType.ONLY_SET,selections:[{range:S,primary:c}],extra:h})}},Ai={id:"sheet.command.select-all",type:a.CommandType.COMMAND,handler:async(r,i={expandToGapFirst:!0,loop:!1})=>{const e=m.getSheetCommandTarget(r.get(a.IUniverInstanceService));if(!e)return!1;const t=m.getSelectionsService(r).getCurrentLastSelection();if(!t)return!1;const n=r.get(Cs),{worksheet:o,unitId:s,subUnitId:l}=e,c=`${s}|${l}`;c!==n.selectedRangeWorksheet&&(n.rangesStack=[],n.selectedRangeWorksheet=c);const d=o.getMaxRows(),u=o.getMaxColumns(),{expandToGapFirst:h,loop:g}=i,{range:S,primary:f}=t,p=S.endColumn===u-1&&S.endRow===d-1&&S.startRow===0&&S.startColumn===0;n.rangesStack.some(v=>a.Rectangle.equals(v,S))||(n.rangesStack=[],n.rangesStack.push(S));let C;if(p)if(g){if(n.rangesStack.findIndex(b=>a.Rectangle.equals(b,S))!==n.rangesStack.length-1)return!1;C=n.rangesStack[0]}else return!1;else h?(C=m.expandToContinuousRange(S,{left:!0,right:!0,up:!0,down:!0},o),a.Rectangle.equals(C,S)&&(C=na(o))):C=na(o);return n.rangesStack.some(v=>a.Rectangle.equals(v,C))||n.rangesStack.push(C),r.get(a.ICommandService).executeCommand(m.SetSelectionsOperation.id,{unitId:s,subUnitId:l,reveal:!0,selections:[{range:C,primary:f}]})}};var Nu=Object.defineProperty,Du=Object.getOwnPropertyDescriptor,Hu=(r,i,e,t)=>{for(var n=t>1?void 0:t?Du(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&Nu(i,e,n),n},ku=(r,i)=>(e,t)=>i(e,t,r);exports.SheetScrollManagerService=class{constructor(i,e){E(this,"_scrollStateMap",new Map);E(this,"_rawScrollInfo$",new O.BehaviorSubject(null));E(this,"rawScrollInfo$",this._rawScrollInfo$.asObservable());E(this,"validViewportScrollInfo$",new O.BehaviorSubject(null));E(this,"_searchParamForScroll",null);this._context=i,this._sheetSkeletonManagerService=e}dispose(){this._rawScrollInfo$.complete()}calcViewportScrollFromRowColOffset(i){if(!i)return{viewportScrollX:0,viewportScrollY:0};let{sheetViewStartColumn:e,sheetViewStartRow:t,offsetX:n,offsetY:o}=i;t=t||0,o=o||0;const s=this._sheetSkeletonManagerService.getCurrentSkeleton(),l=(s==null?void 0:s.rowHeightAccumulation[t-1])||0,d=((s==null?void 0:s.columnWidthAccumulation[e-1])||0)+n,u=l+o;return{viewportScrollX:d,viewportScrollY:u}}setSearchParam(i){this._searchParamForScroll=i}getScrollStateByParam(i){return this._getCurrentScroll(i)}getCurrentScrollState(){return this._getCurrentScroll(this._searchParamForScroll)}setValidScrollState(i){this._setScrollState(i)}emitRawScrollParam(i){this._emitRawScroll(i)}setValidScrollStateToCurrSheet(i){if(this._searchParamForScroll==null)return;this._setScrollState({...this._searchParamForScroll,...i});const e=this._searchParamForScroll.sheetId,t=this._sheetSkeletonManagerService.getSkeleton(e);t&&t.setScroll(i.viewportScrollX,i.viewportScrollY)}clear(){this._searchParamForScroll!=null&&this._clearByParamAndNotify(this._searchParamForScroll)}_setScrollState(i){const{unitId:e,sheetId:t,sheetViewStartColumn:n,sheetViewStartRow:o,offsetX:s,offsetY:l}=i;this._scrollStateMap.has(e)||this._scrollStateMap.set(e,new Map);const c=this._scrollStateMap.get(e),d={sheetViewStartRow:o,sheetViewStartColumn:n,offsetX:s,offsetY:l};c.set(t,d)}_clearByParamAndNotify(i){this._setScrollState({...i,sheetViewStartRow:0,sheetViewStartColumn:0,offsetX:0,offsetY:0}),this._emitRawScroll({sheetViewStartRow:0,sheetViewStartColumn:0,offsetX:0,offsetY:0})}_getCurrentScroll(i){var s;const e={sheetViewStartRow:0,sheetViewStartColumn:0,offsetX:0,offsetY:0};if(i==null)return e;const{unitId:t,sheetId:n}=i;return((s=this._scrollStateMap.get(t))==null?void 0:s.get(n))||e}_emitRawScroll(i){this._rawScrollInfo$.next(i)}};exports.SheetScrollManagerService=Hu([ku(1,a.Inject(exports.SheetSkeletonManagerService))],exports.SheetScrollManagerService);const je={id:"sheet.operation.set-scroll",type:a.CommandType.OPERATION,handler:(r,i)=>{if(!i)return!1;const{unitId:e,sheetId:t,offsetX:n,offsetY:o,sheetViewStartColumn:s,sheetViewStartRow:l}=i;return r.get(w.IRenderManagerService).getRenderById(e).with(exports.SheetScrollManagerService).emitRawScrollParam({unitId:e,sheetId:t,offsetX:n,offsetY:o,sheetViewStartRow:l,sheetViewStartColumn:s}),!0}},wn={id:"sheet.command.set-scroll-relative",type:a.CommandType.COMMAND,handler:async(r,i)=>{const e=r.get(a.ICommandService),t=r.get(a.IUniverInstanceService),n=r.get(w.IRenderManagerService),o=m.getSheetCommandTarget(t);if(!o)return!1;const{unitId:s,subUnitId:l}=o,d=n.getRenderById(s).with(exports.SheetScrollManagerService).getCurrentScrollState(),{offsetX:u=0,offsetY:h=0}=i||{},{sheetViewStartRow:g=0,sheetViewStartColumn:S=0,offsetX:f=0,offsetY:p=0}=d||{};return e.executeCommand(je.id,{unitId:s,sheetId:l,sheetViewStartRow:g,sheetViewStartColumn:S,offsetX:f+u,offsetY:p+h})}},ze={id:"sheet.command.scroll-view",type:a.CommandType.COMMAND,handler:(r,i)=>{if(!i)return!1;const e=r.get(a.IUniverInstanceService),t=r.get(w.IRenderManagerService),n=m.getSheetCommandTarget(e);if(!n)return!1;const{workbook:o,worksheet:s,unitId:l}=n,d=t.getRenderById(l).with(exports.SheetScrollManagerService).getCurrentScrollState();if(!s)return!1;const{sheetViewStartRow:u,sheetViewStartColumn:h,offsetX:g,offsetY:S}=i,{sheetViewStartColumn:f,sheetViewStartRow:p,offsetX:C,offsetY:v}=d||{},{xSplit:b,ySplit:R}=n.worksheet.getConfig().freeze;return r.get(a.ICommandService).syncExecuteCommand(je.id,{unitId:o.getUnitId(),sheetId:s.getSheetId(),sheetViewStartRow:u!=null?u:p!=null?p:0+R,sheetViewStartColumn:h!=null?h:f!=null?f:0+b,offsetX:g!=null?g:C,offsetY:S!=null?S:v})}},Is={id:"sheet.command.scroll-to-cell",type:a.CommandType.COMMAND,handler:(r,i)=>{const e=r.get(a.IUniverInstanceService);return r.get(w.IRenderManagerService).getRenderById(e.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET).getUnitId()).with(exports.SheetsScrollRenderController).scrollToRange(i.range,i.forceTop,i.forceLeft)}},Vu={id:"sheet.command.scroll-view-reset",type:a.CommandType.COMMAND,handler:async r=>{const i=r.get(a.IUniverInstanceService),e=m.getSheetCommandTarget(i);if(!e)return!1;const{subUnitId:t,unitId:n}=e;return r.get(a.ICommandService).executeCommand(je.id,{unitId:n,sheetId:t,sheetViewStartRow:0,sheetViewStartColumn:0})}};var Wu=Object.defineProperty,Uu=Object.getOwnPropertyDescriptor,Lu=(r,i,e,t)=>{for(var n=t>1?void 0:t?Uu(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&Wu(i,e,n),n},sn=(r,i)=>(e,t)=>i(e,t,r);xe.id,$e.id;const Bu=3;exports.SheetsScrollRenderController=class extends a.Disposable{constructor(i,e,t,n,o,s,l){super(),this._context=i,this._injector=e,this._sheetSkeletonManagerService=t,this._contextService=n,this._commandService=o,this._renderManagerService=s,this._scrollManagerService=l,this._initCommandListener(),this._wheelEventListener(),this._scrollBarEventListener(),this._initSkeletonListener()}_wheelEventListener(){const{scene:i}=this._context;if(!i)return;const e=i.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN);e&&this.disposeWithMe(i.onMouseWheel$.subscribeEvent((t,n)=>{if(t.ctrlKey||!this._contextService.getContextValue(a.FOCUSING_SHEET))return;let o=0,s=0;o=t.deltaX,t.shiftKey?o=(t.deltaY||t.deltaX)*Bu:s=t.deltaY,this._commandService.executeCommand(wn.id,{offsetX:o,offsetY:s}),this._context.scene.makeDirty(!0);const l=e.limitedScroll(e.scrollX+o,e.scrollY+s);l&&!l.isLimitedX&&!l.isLimitedY&&(t.preventDefault(),i.getParent().classType===w.RENDER_CLASS_TYPE.SCENE_VIEWER&&n.stopPropagation()),e.isWheelPreventDefaultX&&e.isWheelPreventDefaultY&&t.preventDefault()}))}_scrollBarEventListener(){const{scene:i}=this._context;if(i==null)return;const e=i.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN);e&&(this.disposeWithMe(a.toDisposable(this._scrollManagerService.rawScrollInfo$.subscribe(t=>{var S;const n=(S=this._sheetSkeletonManagerService.getCurrentParam())==null?void 0:S.skeleton;if(!n)return;if(t==null){e.scrollToViewportPos({viewportScrollX:0,viewportScrollY:0});return}const{sheetViewStartRow:o,sheetViewStartColumn:s,offsetX:l,offsetY:c}=t,{startX:d,startY:u}=n.getCellWithCoordByIndex(o,s,!1),h=d+l,g=u+c;e.scrollToViewportPos({viewportScrollX:h,viewportScrollY:g})}))),this.disposeWithMe(e.onScrollAfter$.subscribeEvent(t=>{var p;const n=(p=this._sheetSkeletonManagerService.getCurrentParam())==null?void 0:p.skeleton;if(n==null||t.isTrigger===!1)return;const o=this._getSheetObject();if(n==null||o==null)return;const{viewportScrollX:s,viewportScrollY:l,scrollX:c,scrollY:d}=t,{row:u,column:h,rowOffset:g,columnOffset:S}=n.getOffsetRelativeToRowCol(s,l),f={sheetViewStartRow:u,sheetViewStartColumn:h,offsetX:S,offsetY:g,viewportScrollX:s,viewportScrollY:l,scrollX:c,scrollY:d};this._scrollManagerService.setValidScrollStateToCurrSheet(f),this._scrollManagerService.validViewportScrollInfo$.next({...f,viewportScrollX:s,viewportScrollY:l,scrollX:c,scrollY:d})})),this.disposeWithMe(e.onScrollByBar$.subscribeEvent(t=>{var g;const n=(g=this._sheetSkeletonManagerService.getCurrentParam())==null?void 0:g.skeleton;if(n==null||t.isTrigger===!1)return;const o=this._getSheetObject();if(n==null||o==null)return;const{viewportScrollX:s=0,viewportScrollY:l=0}=t;this._getFreeze();const{row:c,column:d,rowOffset:u,columnOffset:h}=n.getOffsetRelativeToRowCol(s,l);this._commandService.executeCommand(ze.id,{sheetViewStartRow:c,sheetViewStartColumn:d,offsetX:h,offsetY:u})})))}_initSkeletonListener(){this.disposeWithMe(a.toDisposable(this._sheetSkeletonManagerService.currentSkeletonBefore$.subscribe(i=>{if(i==null)return;const e={unitId:i.unitId,sheetId:i.sheetId};if(this._scrollManagerService.setSearchParam(e),!this._getSheetObject())return;const n=this._scrollManagerService.getScrollStateByParam(e),{viewportScrollX:o,viewportScrollY:s}=this._scrollManagerService.calcViewportScrollFromRowColOffset(n);n&&this._updateViewportScroll(o,s)})))}_updateViewportScroll(i=0,e=0){const t=this._getSheetObject();if(!t)return;const n=t.scene,o=n.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN),s=n.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_COLUMN_RIGHT),l=n.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_ROW_BOTTOM),c=n.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT),d=n.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP);o&&(o.viewportScrollX=i,o.viewportScrollY=e),l&&(l.viewportScrollY=e),s&&(s.viewportScrollX=i),c&&(c.viewportScrollY=e),d&&(d.viewportScrollX=i)}scrollToRange(i,e,t){let{endRow:n,endColumn:o,startColumn:s,startRow:l}=i;const c=this._getViewportBounding();if(i.rangeType===a.RANGE_TYPE.ROW?(s=0,o=0):i.rangeType===a.RANGE_TYPE.COLUMN&&(l=0,n=0),c&&!e&&!t){const d=c.startRow>n?l:n,u=c.startColumn>o?s:o;return this._scrollToCell(d,u)}else return this._scrollToCell(l,s,e,t)}scrollToCell(i,e){const t=this._context.unit.getActiveSheet(),{ySplit:n,xSplit:o}=t.getFreeze();return this._commandService.syncExecuteCommand(ze.id,{sheetViewStartRow:i-n,sheetViewStartColumn:e-o,offsetX:0,offsetY:0})}_initCommandListener(){this.disposeWithMe(this._commandService.onCommandExecuted(i=>{switch(i.id){case m.SetSelectionsOperation.id:{const e=i.params;e.unitId===this._context.unitId&&e.reveal&&this._scrollToSelection()}break;case m.ScrollToCellOperation.id:{const e=i.params;if(e.unitId===this._context.unitId){const t=e.range;this.scrollToRange(t)}}break;case He.id:{const e=i.params;this._scrollToSelectionForExpand(e)}break}}))}_scrollToSelectionForExpand(i){setTimeout(()=>{const e=this._getSelectionsService().getCurrentLastSelection();if(e==null)return;const{startRow:t,startColumn:n,endRow:o,endColumn:s}=e.range,l=this._getViewportBounding();if(l==null)return;const{startRow:c,startColumn:d,endRow:u,endColumn:h}=l;let g=0,S=0;t>c?g=o:o<u?g=t:g=c,n>d?S=s:s<h?S=n:S=d,i.direction===a.Direction.DOWN?g=o:i.direction===a.Direction.UP?g=t:i.direction===a.Direction.RIGHT?S=s:i.direction===a.Direction.LEFT&&(S=n),this._scrollToCell(g,S)},0)}_getFreeze(){var e;const i=(e=this._sheetSkeletonManagerService.getCurrentParam())==null?void 0:e.skeleton.getWorksheetConfig();if(i!=null)return i.freeze}_updateSceneSize(i){var h;if(i==null)return;const{unitId:e}=this._context,{skeleton:t}=i,n=(h=this._renderManagerService.getRenderById(e))==null?void 0:h.scene;if(t==null||n==null)return;const{rowTotalHeight:o,columnTotalWidth:s,rowHeaderWidthAndMarginLeft:l,columnHeaderHeightAndMarginTop:c}=t;this._context.unit.getActiveSheet()&&(n==null||n.transformByState({width:l+s,height:c+o}))}_getSheetObject(){return Ge(this._context.unit,this._context)}_scrollToSelectionByDirection(i){const e=this._getViewportBounding();if(e==null)return!1;const{startRow:t,startColumn:n,endRow:o,endColumn:s}=e;let l=0,c=0;const{startRow:d,startColumn:u,endRow:h,endColumn:g}=i;d>=t&&(l=h),h<=o&&(l=d),u>=n&&(c=g),g<=s&&(c=u),this._scrollToCell(l,c)}_scrollToSelection(i=!0){var d;const e=this._getSelectionsService().getCurrentLastSelection();if(!e)return;const{startRow:t,startColumn:n,actualRow:o,actualColumn:s}=(d=e.primary)!=null?d:e.range,l=i&&o!=null?o:t,c=i&&s!=null?s:n;this._scrollToCell(l,c)}_getSelectionsService(){return m.getSelectionsService(this._injector)}_getViewportBounding(){var o,s;const i=(o=this._getSheetObject())==null?void 0:o.scene;if(i==null)return;const e=i.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN);if(e==null)return;const t=(s=this._sheetSkeletonManagerService.getCurrentParam())==null?void 0:s.skeleton;if(t==null)return;const n=e.calcViewportInfo();return t.getRangeByViewBound(n.viewBound)}_scrollToCell(i,e,t=!1,n=!1){var N,D,H;const{rowHeightAccumulation:o,columnWidthAccumulation:s}=(N=this._sheetSkeletonManagerService.getCurrentSkeleton())!=null?N:{};if(o==null||s==null)return!1;const l=(D=this._getSheetObject())==null?void 0:D.scene;if(l==null)return!1;const c=l.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN);if(c==null)return!1;const d=(H=this._sheetSkeletonManagerService.getCurrentParam())==null?void 0:H.skeleton;if(d==null)return!1;const u=this._context.unit.getActiveSheet();if(!u)return!1;i=a.Tools.clamp(i,0,o.length-1),e=a.Tools.clamp(e,0,s.length-1);const{startColumn:h,startRow:g,ySplit:S,xSplit:f}=u.getFreeze(),p=this._getViewportBounding();if(p==null)return!1;const{startRow:C,startColumn:v,endRow:b,endColumn:R}=p;let T,M;if(i>=g&&e>=h-S&&(i<=C&&(T=i,t=!0),i>=b)){const W=o[i]-c.height;for(let k=C;k<=i&&(T=k+1,!(o[k]>=W));k++);}if(e>=h&&i>=g-S&&(e<=v&&(M=e,n=!0),e>=R)){const W=s[e]-c.width;for(let k=v;k<=e&&(M=k+1,!(s[k]>=W));k++);}if(T===void 0&&M===void 0)return!1;let{offsetX:y,offsetY:P,sheetViewStartRow:x,sheetViewStartColumn:A}=this._scrollManagerService.getCurrentScrollState()||{};if(T=T?Math.min(T,i):x+S,M=M?Math.min(M,e):A+f,n){y=0,M=e;const W=d.getHiddenColumnsInRange({startColumn:M-f,endColumn:M});M=M-W.length}if(t){P=0,T=i;const W=d.getHiddenRowsInRange({startRow:T-S,endRow:T});T=T-W.length}return this._commandService.syncExecuteCommand(ze.id,{sheetViewStartRow:Math.max(0,T-S),sheetViewStartColumn:Math.max(0,M-f),offsetX:y,offsetY:P})}};exports.SheetsScrollRenderController=Lu([sn(1,a.Inject(a.Injector)),sn(2,a.Inject(exports.SheetSkeletonManagerService)),sn(3,a.IContextService),sn(4,a.ICommandService),sn(5,w.IRenderManagerService),sn(6,a.Inject(exports.SheetScrollManagerService))],exports.SheetsScrollRenderController);const bs={id:"sheet.operation.scroll-to-range",type:a.CommandType.OPERATION,handler:(r,i)=>{if(!i)return!1;const e=r.get(a.IUniverInstanceService);return r.get(w.IRenderManagerService).getRenderById(e.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET).getUnitId()).with(exports.SheetsScrollRenderController).scrollToRange(i.range,i.forceTop,i.forceLeft)}};class rl{constructor(){E(this,"_state",null);E(this,"_rect",null);E(this,"_state$",new O.BehaviorSubject(null));E(this,"state$",this._state$.asObservable());E(this,"_rect$",new O.BehaviorSubject(null));E(this,"rect$",this._rect$.asObservable());E(this,"_focus",!1);E(this,"_focus$",new O.BehaviorSubject(this._focus));E(this,"focus$",this._focus$.asObservable())}dispose(){this._state$.complete(),this._state=null,this._rect$.complete(),this._rect=null}setState(i){this._state=i,this._refresh(i)}getRect(){return this._rect}setRect(i){this._rect=i,this._rect$.next(i)}getState(){return this._state}setFocus(i=!1){this._focus=i,this._focus$.next(i)}_refresh(i){this._state$.next(i)}}const hr=a.createIdentifier("univer.sheet-cell-editor-manager.service"),Fu="36",ju="univer-sheet-container",il={sheetFooterBarHeight:Fu,sheetContainer:ju};var zu=Object.defineProperty,Xu=Object.getOwnPropertyDescriptor,Yu=(r,i,e,t)=>{for(var n=t>1?void 0:t?Xu(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&zu(i,e,n),n},an=(r,i)=>(e,t)=>i(e,t,r);const ra=5,ia=2;exports.SheetCellEditorResizeService=class extends a.Disposable{constructor(i,e,t,n,o,s,l){super(),this._context=i,this._layoutService=e,this._cellEditorManagerService=t,this._editorBridgeService=n,this._renderManagerService=o,this._sheetSkeletonManagerService=s,this._univerInstanceService=l}fitTextSize(i){var A,N,D,H;const e=this._editorBridgeService.getEditCellState();if(!e)return;const{position:t,documentLayoutObject:n,canvasOffset:o,scaleX:s,scaleY:l}=e,{startX:c,startY:d,endX:u,endY:h}=t,g=this._univerInstanceService.getUnit(a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.UniverInstanceType.UNIVER_DOC);if(g==null)return;const S=this._getEditorSkeleton();if(!S)return;const f=this._predictingSize(t,o,S,n,s,l);if(!f)return;let{actualWidth:p,actualHeight:C}=f;const{verticalAlign:v,horizontalAlign:b,paddingData:R,fill:T}=n;p=p+((A=R.l)!=null?A:0)+((N=R.r)!=null?N:0),C=C+((D=R.t)!=null?D:0)+((H=R.b)!=null?H:0);let M=u-c,y=h-d;M<p&&(M=Math.ceil(p)),y<C&&(y=Math.ceil(C));let P=0;v===a.VerticalAlign.MIDDLE?P=(y-C)/2/l:v===a.VerticalAlign.TOP?P=R.t||0:P=(y-C)/l;let x=0;b===a.HorizontalAlign.CENTER?x=(M-p)/2/s:b===a.HorizontalAlign.RIGHT?x=(M-p)/s:x=R.l||0,P=P<(R.t||0)?R.t||0:P,x=x<(R.l||0)?R.l||0:x,g.updateDocumentDataMargin({t:P,l:x}),S.calculate(),this._editAreaProcessing(M,y,t,o,T,s,l,b,i)}_predictingSize(i,e,t,n,o=1,s=1){const{startX:l,endX:c}=i,{textRotation:d,wrapStrategy:u,paddingData:h}=n,g=this._univerInstanceService.getUnit(a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.UniverInstanceType.UNIVER_DOC),{vertexAngle:S}=w.convertTextRotation(d);if(u===a.WrapStrategy.WRAP&&S===0){g==null||g.updateDocumentDataPageSize(c-l),g==null||g.updateDocumentDataMargin({l:h.l,t:h.t}),t.calculate();const{actualWidth:v,actualHeight:b}=t.getActualSize();return{actualWidth:v*o,actualHeight:b*s}}const f=this._getEditorMaxSize(i,e,n.horizontalAlign);if(!f)return;g==null||g.updateDocumentDataPageSize(f.width/o),t.calculate();const p=t.getActualSize();let C=c-l;return C<p.actualWidth*o+ra*o&&(C=p.actualWidth*o+ra*o),g==null||g.updateDocumentDataPageSize(C/o),g==null||g.updateDocumentRenderConfig({horizontalAlign:a.HorizontalAlign.UNSPECIFIED,cellValueType:void 0}),{actualWidth:p.actualWidth*o,actualHeight:p.actualHeight*s}}_getEditorMaxSize(i,e,t){if(this._getEditorObject()==null)return;function o(b){return Number.parseInt(b.replace("px",""))}const l=this._context.engine.getCanvasElement(),c=l.getBoundingClientRect(),d=o(l.style.width),{width:u}=c,h=u/d,{startX:g,startY:S,endX:f}=i,p=this._context.engine.width,C=document.body.clientHeight-S-Number.parseFloat(il.sheetFooterBarHeight)-e.top-ia*2;let v=u-g;if(t===a.HorizontalAlign.CENTER){const b=p-f,R=g;v=f-g+Math.min(R,b)*2}else t===a.HorizontalAlign.RIGHT&&(v=f);return{height:C,width:v-ia,scaleAdjust:h}}_editAreaProcessing(i,e,t,n,o,s=1,l=1,c,d){var k;const u=this._getEditorObject();if(u==null)return;const g=this._context.engine.getCanvasElement();let{startX:S,startY:f}=t;const{document:p,scene:C,engine:v}=u,b=C.getViewport($.VIEWPORT_KEY.VIEW_MAIN),R=this._getEditorMaxSize(t,n,c),{height:T,width:M,scaleAdjust:y}=R;let P=e,x=b==null?void 0:b.getScrollBar();P>T?(x==null?b&&new w.ScrollBar(b,{enableHorizontal:!1,barSize:8}):b==null||b.resetCanvasSizeAndUpdateScroll(),b==null||b.scrollToViewportPos({viewportScrollY:P-T}),P=T):(x=null,(k=b==null?void 0:b.getScrollBar())==null||k.dispose()),i+=(x==null?void 0:x.barSize)||0,i>M&&(i=M),this._addBackground(C,i/s,e/l,o);const{scaleX:A,scaleY:N}=C.getPrecisionScale();C.transformByState({width:i*y/s,height:e*y/l,scaleX:s*y,scaleY:l*y}),p.resize(i*y/s,e*y/l),setTimeout(()=>{v.resizeBySize(w.fixLineWidthByScale(i,A),w.fixLineWidthByScale(P,N)),d==null||d()},0);const D=this._layoutService.getContentElement().getBoundingClientRect(),H=g.getBoundingClientRect();S=S*y+(H.left-D.left),f=f*y+(H.top-D.top);const W=t.endX-t.startX;c===a.HorizontalAlign.RIGHT?S+=(W-i)*y:c===a.HorizontalAlign.CENTER&&(S+=(W-i*y)/2),this._cellEditorManagerService.setState({startX:S,startY:f,endX:i*y+S,endY:P*y+f,show:!0})}_addBackground(i,e,t,n){const o="_backgroundRectHelperColor_",s=i.getObject(o);s==null&&n==null||(s==null?i.addObjects([new w.Rect(o,{width:e,height:t,fill:n,evented:!1})],$.DOCS_COMPONENT_MAIN_LAYER_INDEX):n==null?s.dispose():(s.setProps({fill:n}),s.transformByState({width:e,height:t})))}resizeCellEditor(i){var y;const e=this._cellEditorManagerService.getState();if(!e||!this._editorBridgeService.isVisible().visible)return;this._editorBridgeService.refreshEditCellPosition(!0);const t=this._editorBridgeService.getEditCellState();if(!t)return;const n=(y=this._sheetSkeletonManagerService.getSkeletonParam(t.sheetId))==null?void 0:y.skeleton;if(!n)return;const{row:o,column:s,scaleX:l,scaleY:c,position:d,canvasOffset:u,documentLayoutObject:h}=t,{horizontalAlign:g}=h,S=this._getEditorMaxSize(d,u,g);if(!S)return;const{height:f,width:p,scaleAdjust:C}=S,v=n.getCellWithCoordByIndex(o,s),b=Math.min((v.mergeInfo.endY-v.mergeInfo.startY)*c,f)*C,R=Math.min((v.mergeInfo.endX-v.mergeInfo.startX)*l,p)*C,T=e.endY-e.startY,M=e.endX-e.startX;if(T!==b||M!==R){if(this._editorBridgeService.refreshEditCellPosition(!0),!this._getEditorSkeleton())return;this.fitTextSize(i)}}_getEditorObject(){return wi(this._editorBridgeService.getCurrentEditorId(),this._renderManagerService)}_getEditorSkeleton(){var i;return(i=this._renderManagerService.getRenderById(a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY))==null?void 0:i.with(Re.DocSkeletonManagerService).getSkeleton()}};exports.SheetCellEditorResizeService=Yu([an(1,_.ILayoutService),an(2,hr),an(3,Se),an(4,w.IRenderManagerService),an(5,a.Inject(exports.SheetSkeletonManagerService)),an(6,a.IUniverInstanceService)],exports.SheetCellEditorResizeService);function Rs(r){return r.getContextValue(a.FOCUSING_SHEET)}function ce(r){return r.getContextValue(a.FOCUSING_SHEET)&&r.getContextValue(a.FOCUSING_UNIVER_EDITOR)&&!r.getContextValue(a.EDITOR_ACTIVATED)&&!r.getContextValue(a.FOCUSING_COMMON_DRAWINGS)}function ol(r){return r.getContextValue(a.FOCUSING_SHEET)&&r.getContextValue(a.FOCUSING_UNIVER_EDITOR)&&!r.getContextValue(a.EDITOR_ACTIVATED)&&!r.getContextValue(a.FOCUSING_FX_BAR_EDITOR)&&!r.getContextValue(a.FOCUSING_COMMON_DRAWINGS)}function Gu(r){return r.getContextValue(a.FOCUSING_SHEET)&&r.getContextValue(a.FOCUSING_UNIVER_EDITOR)&&r.getContextValue(a.EDITOR_ACTIVATED)}function Zu(r){return r.getContextValue(a.FOCUSING_UNIVER_EDITOR)&&r.getContextValue(a.EDITOR_ACTIVATED)}function sl(r){return r.getContextValue(a.FOCUSING_FX_BAR_EDITOR)&&r.getContextValue(a.FOCUSING_UNIVER_EDITOR)}function Ku(r){return r.getContextValue(a.FOCUSING_SHEET)&&r.getContextValue(a.EDITOR_ACTIVATED)&&r.getContextValue(a.FOCUSING_EDITOR_INPUT_FORMULA)&&r.getContextValue(a.FOCUSING_UNIVER_EDITOR)}function tr(r){return r.getContextValue(a.FOCUSING_SHEET)&&r.getContextValue(a.FOCUSING_UNIVER_EDITOR)&&r.getContextValue(a.EDITOR_ACTIVATED)&&!r.getContextValue(a.FOCUSING_EDITOR_INPUT_FORMULA)&&!r.getContextValue(a.FOCUSING_EDITOR_STANDALONE)}const al=[_.KeyCode.ARROW_DOWN,_.KeyCode.ARROW_UP,_.KeyCode.ARROW_LEFT,_.KeyCode.ARROW_RIGHT],qu=[_.KeyCode.ENTER,_.KeyCode.TAB,...al];function ll(){const r=[];for(const i of al)r.push({id:zt.id,binding:i,preconditions:e=>tr(e),staticParameters:{visible:!1,eventType:w.DeviceInputEventType.Keyboard,keycode:i,isShift:!1}}),r.push({id:zt.id,binding:i|_.MetaKeys.SHIFT,preconditions:e=>tr(e),staticParameters:{visible:!1,eventType:w.DeviceInputEventType.Keyboard,keycode:i,isShift:!0}});return r}const cl={id:ur.id,binding:_.KeyCode.F2,description:"shortcut.sheet.start-editing",group:"4_sheet-edit",preconditions:ce,staticParameters:{visible:!0,eventType:w.DeviceInputEventType.Keyboard,keycode:_.KeyCode.F2}},dl={id:ve.id,binding:_.KeyCode.ENTER,description:"shortcut.sheet.toggle-editing",group:"4_sheet-edit",preconditions:r=>tr(r),staticParameters:{visible:!1,eventType:w.DeviceInputEventType.Keyboard,keycode:_.KeyCode.ENTER}},ul={id:ve.id,binding:_.KeyCode.TAB,preconditions:r=>tr(r),staticParameters:{visible:!1,eventType:w.DeviceInputEventType.Keyboard,keycode:_.KeyCode.TAB}},hl={id:ve.id,binding:_.KeyCode.ESC,description:"shortcut.sheet.abort-editing",group:"4_sheet-edit",preconditions:r=>tr(r),staticParameters:{visible:!1,eventType:w.DeviceInputEventType.Keyboard,keycode:_.KeyCode.ESC}},ml={id:$.BreakLineCommand.id,description:"shortcut.sheet.break-line",group:"4_sheet-edit",preconditions:r=>Gu(r),binding:_.KeyCode.ENTER|_.MetaKeys.ALT},gl={id:$.DeleteLeftCommand.id,preconditions:r=>Zu(r)||sl(r),binding:_.KeyCode.BACKSPACE},Sl={id:ve.id,description:"shortcut.sheet.delete-and-start-editing",group:"4_sheet-edit",preconditions:r=>ce(r)&&!sl(r),binding:_.KeyCode.BACKSPACE,staticParameters:{visible:!0,eventType:w.DeviceInputEventType.Keyboard,keycode:_.KeyCode.BACKSPACE}};function Qu(r){return r.startsWith("'")}function Ju(r){return r.slice(1)}const $u={"0":"0","1":"1","2":"2","3":"3","4":"4","5":"5","6":"6","7":"7","8":"8","9":"9",A:"A",B:"B",C:"C",D:"D",E:"E",F:"F",G:"G",H:"H",I:"I",J:"J",K:"K",L:"L",M:"M",N:"N",O:"O",P:"P",Q:"Q",R:"R",S:"S",T:"T",U:"U",V:"V",W:"W",X:"X",Y:"Y",Z:"Z",a:"a",b:"b",c:"c",d:"d",e:"e",f:"f",g:"g",h:"h",i:"i",j:"j",k:"k",l:"l",m:"m",n:"n",o:"o",p:"p",q:"q",r:"r",s:"s",t:"t",u:"u",v:"v",w:"w",x:"x",y:"y",z:"z","%":"%","-":"-",".":".",":":":","/":"/","+":"+","@":"@","&":"&","*":"*","#":"#","=":"=",">":">","<":"<","$":"$",""":'"',"'":"'","(":"(",")":")"," ":" ",",":",","!":"!","?":"?",";":";","[":"[","]":"]","{":"{","}":"}"},jo={true:"TRUE",false:"FALSE"};function eh(r){return[a.LocaleType.ZH_CN,a.LocaleType.ZH_TW].includes(r)}function th(r,i,e,t){let n=r;if(eh(e)){if(r.startsWith("'")||r.startsWith("'"))return`'${r.slice(1)}`;n=nh(n)}if(n.startsWith("="))return rh(r,n,i,t);const o=n.toLowerCase();return jo[o]?jo[o]:a.numfmt.parseValue(n)==null?r:n}function nh(r){const i=/['"].*?['"]/g,e=[],t=r.split(i);r.replace(i,o=>(e.push(o),""));let n=t.join("").split("").map(sh).join("");return e.forEach((o,s)=>{n=n.slice(0,s*2)+o+n.slice((s+1)*2)}),n}function rh(r,i,e,t){const n=e.sequenceNodesBuilder(i);if(!n)return r;let o=i,s=0;return n.forEach((l,c)=>{if(typeof l=="object"){const d=l.token;if(jo[d.toLowerCase()]){const u=l.startIndex+s+1,h=l.endIndex+s+2;o=ln(d.toLocaleUpperCase(),o,u,h)}else if(l.nodeType===K.sequenceNodeType.FUNCTION&&ih(d,t,n,c)||l.nodeType===K.sequenceNodeType.REFERENCE){const u=d.indexOf("!");if(u>-1){const h=d.substring(u+1),g=l.startIndex+s+(u+1)+1,S=l.endIndex+s+2;o=ln(h.toLocaleUpperCase(),o,g,S)}else{const h=l.startIndex+s+1,g=l.endIndex+s+2;o=ln(d.toLocaleUpperCase(),o,h,g)}}else if(d.startsWith('"')&&d.endsWith('"')||d.startsWith("'")&&d.endsWith("'")){const u=l.startIndex+s+2,h=l.endIndex+s+1;o=ln(r.slice(u,h),o,u,h)}else if(l.nodeType!==K.sequenceNodeType.ARRAY){const u=a.numfmt.parseValue(d);if(u==null){const h=l.startIndex+s+1,g=l.endIndex+s+2;o=ln(r.slice(h,g),o,h,g)}else if(typeof u.v=="number"&&(u.z===void 0||!a.numfmt.isDate(u.z))){const h=`${u.v}`,g=l.startIndex+s+1,S=l.endIndex+s+2,{processedString:f,offset:p}=ah(d,h);o=ln(f,o,g,S),s+=p}}}}),o}function ih(r,i,e,t){const n=i.getDescriptions();return e[t+1]!==K.matchToken.OPEN_BRACKET?!1:n.get(oh(r).toLocaleUpperCase())!==void 0}function oh(r){const i=/^@+/;return r.replace(i,"")}function sh(r){return $u[r]||r}function ln(r,i,e,t){return i.substring(0,e)+r+i.substring(t)}function ah(r,i){var s,l;const e=((s=r.match(/^\s*/))==null?void 0:s[0])||"",t=((l=r.match(/\s*$/))==null?void 0:l[0])||"",n=e+i+t,o=n.length-r.length;return{processedString:n,offset:o}}const fl="range_selector",pl=r=>r.includes(fl);var lh=Object.defineProperty,ch=Object.getOwnPropertyDescriptor,dh=(r,i,e,t)=>{for(var n=t>1?void 0:t?ch(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&lh(i,e,n),n},ye=(r,i)=>(e,t)=>i(e,t,r);const oa=-1e3;exports.EditingRenderController=class extends a.Disposable{constructor(e,t,n,o,s,l,c,d,u,h,g,S,f,p,C,v){super();E(this,"_cursorChange",0);E(this,"_isUnitEditing",!1);E(this,"_workbookSelections");E(this,"_d");E(this,"_cursorTimeout");this._context=e,this._undoRedoService=n,this._contextService=o,this._renderManagerService=s,this._editorBridgeService=l,this._cellEditorManagerService=c,this._lexerTreeBuilder=d,this._functionService=u,this._textSelectionManagerService=h,this._commandService=g,this._localService=S,this._editorService=f,this._sheetCellEditorResizeService=p,this._univerInstanceService=C,this._sheetInterceptorService=v,this._workbookSelections=t.getWorkbookSelections(this._context.unitId),this.disposeWithMe(this._univerInstanceService.getCurrentTypeOfUnit$(a.UniverInstanceType.UNIVER_SHEET).subscribe(b=>{(b==null?void 0:b.getUnitId())===this._context.unitId?this._d=this._init():(this._disposeCurrent(),this._isUnitEditing&&(this._handleEditorInvisible({visible:!1,eventType:w.DeviceInputEventType.Keyboard,keycode:_.KeyCode.ESC,unitId:this._context.unitId}),this._isUnitEditing=!1))})),this._initEditorVisibilityListener()}dispose(){super.dispose(),this._disposeCurrent()}_disposeCurrent(){var e;(e=this._d)==null||e.dispose(),this._d=null}_init(){const e=new a.DisposableCollection;return this._subscribeToCurrentCell(e),this._initialCursorSync(e),this._listenEditorFocus(e),this._commandExecutedListener(e),this._initSkeletonListener(e),e.add(()=>{clearTimeout(this._cursorTimeout)}),this._cursorTimeout=setTimeout(()=>{this._cursorStateListener(e)},1e3),e}_initEditorVisibilityListener(){this.disposeWithMe(this._editorBridgeService.visible$.pipe(O.distinctUntilChanged((e,t)=>e.visible===t.visible)).subscribe(e=>{(e.unitId===a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY||e.unitId===a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY||e.unitId===this._context.unitId)&&e.visible?(this._isUnitEditing=!0,this._handleEditorVisible(e)):this._isUnitEditing&&(this._handleEditorInvisible(e),this._isUnitEditing=!1)}))}_listenEditorFocus(e){const t=this._getEditorObject();t&&e.add(t.document.onPointerDown$.subscribeEvent(()=>{if(this._isUnitEditing&&this._editorBridgeService.isVisible()){const n=this._editorBridgeService.getEditCellState(),o=this._editorBridgeService.getCurrentEditorId();if(!n||!o)return;this._sheetCellEditorResizeService.fitTextSize()}}))}_initialCursorSync(e){e.add(this._cellEditorManagerService.focus$.pipe(O.filter(t=>!!t)).subscribe(()=>{var n;const t=(n=this._renderManagerService.getCurrentTypeOfRenderer(a.UniverInstanceType.UNIVER_DOC))==null?void 0:n.with($.DocSelectionRenderService);t&&t.sync()}))}_initSkeletonListener(e){const t=new Set(m.COMMAND_LISTENER_SKELETON_CHANGE);t.delete(m.SetWorksheetActiveOperation.id),e.add(this._commandService.onCommandExecuted(n=>{if(t.has(n.id)){switch(n.id){case m.MoveRowsMutation.id:case m.MoveColsMutation.id:case m.RemoveColMutation.id:case m.RemoveRowMutation.id:case m.InsertColMutation.id:case m.InsertRowMutation.id:{const o=this._editorBridgeService.getEditLocation();if(!o)break;const s={startRow:o.row,startColumn:o.column,endRow:o.row,endColumn:o.column},l=m.adjustRangeOnMutation(s,n);if(!l)break;const c=l.startRow,d=l.startColumn;this._editorBridgeService.updateEditLocation(c,d);break}}this._sheetCellEditorResizeService.resizeCellEditor(()=>{this._textSelectionManagerService.refreshSelection({unitId:a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,subUnitId:a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY})})}}))}_subscribeToCurrentCell(e){e.add(this._editorBridgeService.currentEditCellState$.subscribe(t=>{var b,R;if(t==null||this._editorBridgeService.isForceKeepVisible())return;const n=this._editorBridgeService.getEditCellState();if(n==null)return;const{position:o,documentLayoutObject:s,scaleX:l,editorUnitId:c}=n;if(this._contextService.getContextValue(a.FOCUSING_EDITOR_STANDALONE))return;const d=this._getDocumentDataModel();if(d==null)return;const{startX:u,endX:h}=o,{textRotation:g,wrapStrategy:S,documentModel:f}=s,{vertexAngle:p}=w.convertTextRotation(g);S===a.WrapStrategy.WRAP&&p===0&&d.updateDocumentDataPageSize((h-u)/l),this._commandService.syncExecuteCommand($.ReplaceSnapshotCommand.id,{unitId:c,snapshot:f.getSnapshot()}),this._contextService.setContextValue(a.FOCUSING_EDITOR_BUT_HIDDEN,!0),this._textSelectionManagerService.replaceDocRanges([{startOffset:0,endOffset:0}],{unitId:a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,subUnitId:a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY});const C=(b=this._renderManagerService.getRenderById(a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY))==null?void 0:b.with($.DocSelectionRenderService),v=(R=this._renderManagerService.getRenderById(a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY))==null?void 0:R.with($.DocSelectionRenderService);(C!=null&&C.canFocusing||v!=null&&v.canFocusing)&&(this._univerInstanceService.setCurrentUnitForType(a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY),C==null||C.activate(oa,oa,!0))}))}_commandExecutedListener(e){e.add(this._commandService.onCommandExecuted(n=>{if(n.id===Re.RichTextEditingMutation.id){const o=n.params,{unitId:s}=o;if(!this._isCurrentSheetFocused()||pl(s)||(this._editorBridgeService.changeEditorDirty(!0),!this._editorBridgeService.isVisible().visible))return;s===a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY&&this._sheetCellEditorResizeService.fitTextSize()}}));const t=[zt.id];e.add(this._commandService.onCommandExecuted(n=>{if(t.includes(n.id)){const o=n.params,{keycode:s,isShift:l}=o;if(s!=null&&(this._cursorChange===2||this._contextService.getContextValue(a.FOCUSING_FX_BAR_EDITOR))){this._moveInEditor(s,l);return}this._commandService.syncExecuteCommand(ve.id,o)}n.id===ur.id&&(this._cursorChange=2)}))}_handleEditorVisible(e){var S,f,p;const{eventType:t,keycode:n}=e;this._cursorChange=t===w.DeviceInputEventType.PointerDown||t===w.DeviceInputEventType.Dblclick?2:1;const o=this._editorBridgeService.getEditLocation();if(o==null)return;this._commandService.syncExecuteCommand(bs.id,{range:{startRow:o.row,startColumn:o.column,endRow:o.row,endColumn:o.column}}),this._editorBridgeService.refreshEditCellPosition(!1);const{unitId:s,isInArrayFormulaRange:l=!1}=o,c=this._getEditorObject();if(c==null)return;const{document:d,scene:u}=c;this._contextService.setContextValue(a.EDITOR_ACTIVATED,!0);const h=this._getDocumentDataModel(),g=this._getEditorSkeleton(a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY);if(!(!g||!h)){if(this._sheetCellEditorResizeService.fitTextSize(()=>{const C=u.getViewport($.VIEWPORT_KEY.VIEW_MAIN);C==null||C.scrollToViewportPos({viewportScrollX:Number.POSITIVE_INFINITY,viewportScrollY:Number.POSITIVE_INFINITY})}),t===w.DeviceInputEventType.Keyboard&&n===_.KeyCode.F2){d.makeDirty(),this._textSelectionManagerService.replaceDocRanges([{startOffset:0,endOffset:0}]);const C=((f=(S=h.getBody())==null?void 0:S.dataStream.length)!=null?f:2)-2;this._textSelectionManagerService.replaceDocRanges([{startOffset:C,endOffset:C}],{unitId:a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,subUnitId:a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY})}else if(t===w.DeviceInputEventType.Keyboard||t===w.DeviceInputEventType.Dblclick&&l)this._emptyDocumentDataModel(h.getSnapshot().documentStyle,!!l),d.makeDirty(),(n===_.KeyCode.BACKSPACE||t===w.DeviceInputEventType.Dblclick)&&(g.calculate(),this._editorBridgeService.changeEditorDirty(!0)),this._textSelectionManagerService.replaceDocRanges([{startOffset:0,endOffset:0}],{unitId:a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,subUnitId:a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY});else if(t===w.DeviceInputEventType.Dblclick){if(this._contextService.getContextValue(a.FOCUSING_EDITOR_INPUT_FORMULA))return;const C=h.getBody().dataStream.length-2||0;this._textSelectionManagerService.replaceDocRanges([{startOffset:C,endOffset:C}])}(p=this._renderManagerService.getRenderById(s))==null||p.scene.resetCursor()}}async _handleEditorInvisible(e){const t=this._editorBridgeService.getEditCellState(),n=this._univerInstanceService.getUnit(a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY),o=a.Tools.deepClone(n==null?void 0:n.getSnapshot());let{keycode:s}=e;if(this._cursorChange=0,this._exitInput(e),t==null)return;this._editorBridgeService.getEditorDirty()===!1&&(s=_.KeyCode.ESC);const d=this._context.unit.getActiveSheet(),u=this._context.unitId,h=d.getSheetId(),{unitId:g,sheetId:S}=t;if(u===g&&S!==h&&await this._commandService.executeCommand(m.SetWorksheetActivateCommand.id,{subUnitId:S,unitId:g}),s===_.KeyCode.ESC){this._editorBridgeService.isForceKeepVisible()&&this._editorBridgeService.disableForceKeepVisible();const f=this._workbookSelections.getCurrentSelections();f&&(this._contextService.setContextValue(m.REF_SELECTIONS_ENABLED,!1),this._commandService.syncExecuteCommand(m.SetSelectionsOperation.id,{unitId:this._context.unit.getUnitId(),subUnitId:S,selections:f}));return}o&&await this._submitCellData(o),this._moveCursor(s)}_getEditorObject(){return wi(this._editorBridgeService.getCurrentEditorId(),this._renderManagerService)}submitCellData(e){return this._submitCellData(e.getSnapshot())}async _submitCellData(e){const t=this._editorBridgeService.getEditCellState();if(t==null)return;const{unitId:n,sheetId:o,row:s,column:l}=t,c=this._context.unit;let d=c.getActiveSheet();if(!c.getSheetBySheetId(o))return;d=c.getActiveSheet();const h=uh({...d.getCellRaw(s,l)||{}},e,this._lexerTreeBuilder,this._localService,this._functionService,c.getStyles());if(!h)return;const g=await this._sheetInterceptorService.onWriteCell(c,d,s,l,h);g!==d.getCellRaw(s,l)&&this._commandService.executeCommand(m.SetRangeValuesCommand.id,{subUnitId:o,unitId:n,range:{startRow:s,startColumn:l,endRow:s,endColumn:l},value:g})}_exitInput(e){var o;this._contextService.setContextValue(a.FOCUSING_EDITOR_INPUT_FORMULA,!1),this._contextService.setContextValue(a.EDITOR_ACTIVATED,!1),this._contextService.setContextValue(a.FOCUSING_EDITOR_BUT_HIDDEN,!1),this._contextService.setContextValue(a.FOCUSING_FX_BAR_EDITOR,!1),this._cellEditorManagerService.setState({show:e.visible});const t=this._getEditorObject();(o=t==null?void 0:t.scene.getViewport($.VIEWPORT_KEY.VIEW_MAIN))==null||o.scrollToViewportPos({viewportScrollX:0,viewportScrollY:0});const n=this._editorBridgeService.getCurrentEditorId();n==null||!this._editorService.isSheetEditor(n)||(this._undoRedoService.clearUndoRedo(n),this._undoRedoService.clearUndoRedo(a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY))}_moveCursor(e){if(e==null||!qu.includes(e))return;let t=a.Direction.LEFT;switch(e){case _.KeyCode.ENTER:t=a.Direction.DOWN;break;case _.KeyCode.TAB:t=a.Direction.RIGHT;break;case _.KeyCode.ARROW_DOWN:t=a.Direction.DOWN;break;case _.KeyCode.ARROW_UP:t=a.Direction.UP;break;case _.KeyCode.ARROW_LEFT:t=a.Direction.LEFT;break;case _.KeyCode.ARROW_RIGHT:t=a.Direction.RIGHT;break}e===_.KeyCode.ENTER||e===_.KeyCode.TAB?this._commandService.executeCommand($e.id,{keycode:e,direction:t}):this._commandService.executeCommand(xe.id,{direction:t})}_cursorStateListener(e){const t=this._getEditorObject();if(!(t!=null&&t.document))return;const{document:n}=t;e.add(a.toDisposable(n.onPointerDown$.subscribeEvent(()=>{this._cursorChange===1&&(this._cursorChange=2)})))}_moveInEditor(e,t){let n=a.Direction.LEFT;e===_.KeyCode.ARROW_DOWN?n=a.Direction.DOWN:e===_.KeyCode.ARROW_UP?n=a.Direction.UP:e===_.KeyCode.ARROW_RIGHT&&(n=a.Direction.RIGHT),t?this._commandService.executeCommand($.MoveSelectionOperation.id,{direction:n}):this._commandService.executeCommand($.MoveCursorOperation.id,{direction:n})}_getDocumentDataModel(){return this._univerInstanceService.getUnit(a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.UniverInstanceType.UNIVER_DOC)}_isCurrentSheetFocused(){var e;return((e=this._univerInstanceService.getFocusedUnit())==null?void 0:e.getUnitId())===this._context.unitId}_getEditorSkeleton(e){var t;return(t=this._renderManagerService.getRenderById(e))==null?void 0:t.with(Re.DocSkeletonManagerService).getSkeleton()}_getEditorViewModel(e){var t;return(t=this._renderManagerService.getRenderById(e))==null?void 0:t.with(Re.DocSkeletonManagerService).getViewModel()}_emptyDocumentDataModel(e,t){const n=(l,c)=>{const d=a.Tools.deepClone(l.getSnapshot()),u=this._getEditorViewModel(l.getUnitId());u!=null&&(hh(d.body,t),c&&(d.documentStyle=e),d.drawings={},d.drawingsOrder=[],l.reset(d),u.reset(l))},o=this._getDocumentDataModel();o&&n(o,!0);const s=this._univerInstanceService.getUnit(a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,a.UniverInstanceType.UNIVER_DOC);s&&n(s)}};exports.EditingRenderController=dh([ye(1,a.Inject(m.SheetsSelectionsService)),ye(2,a.IUndoRedoService),ye(3,a.IContextService),ye(4,w.IRenderManagerService),ye(5,Se),ye(6,hr),ye(7,a.Inject(K.LexerTreeBuilder)),ye(8,K.IFunctionService),ye(9,a.Inject(Re.DocSelectionManagerService)),ye(10,a.ICommandService),ye(11,a.Inject(a.LocaleService)),ye(12,$.IEditorService),ye(13,a.Inject(exports.SheetCellEditorResizeService)),ye(14,a.IUniverInstanceService),ye(15,a.Inject(m.SheetInterceptorService))],exports.EditingRenderController);function uh(r,i,e,t,n,o){var h,g,S;if((i==null?void 0:i.body)==null)return null;const{body:s}=i;r.t=void 0;const l=s.dataStream;let d=l.substring(l.length-2,l.length)===a.DEFAULT_EMPTY_DOCUMENT_VALUE?l.substring(0,l.length-2):l;const u=t.getCurrentLocale();if(d=th(d,e,u,n),(h=i.drawingsOrder)!=null&&h.length)r.v="",r.f=null,r.si=null,r.p=i,r.t=a.CellValueType.STRING;else if(r.s&&((S=(g=o==null?void 0:o.get(r.s))==null?void 0:g.n)==null?void 0:S.pattern)===bd.DEFAULT_TEXT_FORMAT){r.v=d,r.f=null,r.si=null,r.p=null,r.t=a.CellValueType.STRING,zo(s)&&s.dataStream!==`\r
1
+ "use strict";var _d=Object.defineProperty;var Id=(r,i,e)=>i in r?_d(r,i,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[i]=e;var E=(r,i,e)=>Id(r,typeof i!="symbol"?i+"":i,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const $=require("@univerjs/docs-ui"),a=require("@univerjs/core"),w=require("@univerjs/engine-render"),_=require("@univerjs/ui"),V=require("react"),O=require("rxjs"),m=require("@univerjs/sheets"),Re=require("@univerjs/docs"),K=require("@univerjs/engine-formula"),bd=require("@univerjs/engine-numfmt"),Z=require("@univerjs/design"),I=require("react/jsx-runtime"),Rd=require("@univerjs/telemetry"),Ka=require("rxjs/operators");function wi(r,i){if(r==null)return;const e=i.getRenderById(r);if(e==null)return;const{mainComponent:t,scene:n,engine:o,components:s}=e,l=t,c=s.get($.DOCS_VIEW_KEY.BACKGROUND);return{document:l,docBackground:c,scene:n,engine:o}}const ko=[10,400],hs="RANGE_SELECTOR_COMPONENT_KEY",yi="EMBEDDING_FORMULA_EDITOR_COMPONENT_KEY";var me=(r=>(r.MAIN="__SpreadsheetRender__",r.ROW="__SpreadsheetRowHeader__",r.COLUMN="__SpreadsheetColumnHeader__",r.LEFT_TOP="__SpreadsheetLeftTopPlaceholder__",r))(me||{});const zs=0,Jr=1,It=10,Ed=11,Xs=12,Mi={id:"sheet.command.set-row-header-width",type:a.CommandType.COMMAND,handler:async(r,i)=>{if(!i)return!1;const e=r.get(w.IRenderManagerService);if(!e)return!1;const{unitId:t,subUnitId:n,size:o}=i,s=e.getRenderById(t);if(s){e.getRenderById(t).with(exports.SheetSkeletonManagerService).setRowHeaderSize(s,n,o);const{components:c}=s,d=c.get(me.ROW);d&&d.setCustomHeader({headerStyle:{size:o}})}return!0}},Pi={id:"sheet.command.set-col-header-height",type:a.CommandType.COMMAND,handler:async(r,i)=>{if(!i)return!1;const e=r.get(w.IRenderManagerService);if(!e)return!1;const{unitId:t,subUnitId:n,size:o}=i,s=e.getRenderById(t);if(s){e.getRenderById(t).with(exports.SheetSkeletonManagerService).setColumnHeaderSize(s,n,o);const{components:c}=s,d=c.get(me.COLUMN);d&&d.setCustomHeader({headerStyle:{size:o}})}return!0}},Vo=a.createInterceptorKey("rangeMovePermissionCheck"),Wo=a.createInterceptorKey("rangeFillPermissionCheck");var ms=(r=>(r[r.FORMULA_EDITOR_SHOW=100]="FORMULA_EDITOR_SHOW",r[r.MARK_SELECTION=1e4]="MARK_SELECTION",r))(ms||{});function bt(r){const i=r.getCurrentTheme(),e=new a.ColorKit(i.primaryColor).setAlpha(.07).toRgbString();return{strokeWidth:1,stroke:i.primaryColor,fill:e,widgets:{},widgetSize:6,widgetStrokeWidth:1,widgetStroke:i.colorWhite,autofillSize:6,autofillStrokeWidth:1,autofillStroke:i.colorWhite,rowHeaderFill:e,rowHeaderStroke:i.primaryColor,rowHeaderStrokeWidth:1,columnHeaderFill:e,columnHeaderStroke:i.primaryColor,columnHeaderStrokeWidth:1,expandCornerSize:40}}class Td{constructor(){E(this,"_startColumn",-1);E(this,"_startRow",-1);E(this,"_endColumn",-1);E(this,"_endRow",-1);E(this,"_startX",0);E(this,"_startY",0);E(this,"_endX",0);E(this,"_endY",0);E(this,"_primary");E(this,"_rangeType",a.RANGE_TYPE.NORMAL)}get startColumn(){return this._startColumn}get startRow(){return this._startRow}get endColumn(){return this._endColumn}get endRow(){return this._endRow}get startX(){return this._startX}get startY(){return this._startY}get endX(){return this._endX}get endY(){return this._endY}get currentCell(){return this._primary}get rangeType(){return this._rangeType}isEqual(i){const{startColumn:e,startRow:t,endColumn:n,endRow:o}=this,{startColumn:s,startRow:l,endColumn:c,endRow:d}=i;return e===s&&t===l&&n===c&&o===d}isInclude(i){const{startColumn:e,startRow:t,endColumn:n,endRow:o}=this,{startColumn:s,startRow:l,endColumn:c,endRow:d}=i;return!(c<e||s>n||l>o||d<t)}highlightToSelection(){if(this._primary)return a.convertCellToRange(this._primary)}getRange(){return{startColumn:this._startColumn,startRow:this._startRow,endColumn:this._endColumn,endRow:this._endRow,startX:this._startX,startY:this._startY,endX:this._endX,endY:this._endY,rangeType:this.rangeType}}getCell(){return this._primary}getRangeType(){return this._rangeType}setRangeType(i){this._rangeType=i}getValue(){return{rangeWithCoord:this.getRange(),primaryWithCoord:this._primary}}setValue(i,e){const{startColumn:t,startRow:n,endColumn:o,endRow:s,startX:l,startY:c,endX:d,endY:u,rangeType:h}=i;this._startColumn=t,this._startRow=n,this._endColumn=o,this._endRow=s,this._startX=l,this._startY=c,this._endX=d,this._endY=u,h!==void 0&&(this._rangeType=h),e!==void 0&&this.setCurrentCell(e)}setCurrentCell(i){this._primary=i}clearCurrentCell(){this._primary=null}}function bn(r,i){const{startRow:e,startColumn:t,endRow:n,endColumn:o,rangeType:s}=i,l=n<e?n:e,c=n<e?e:n,d=o<t?o:t,u=o<t?t:o,h=r.getNoMergeCellWithCoordByIndex(l,d),g=r.getNoMergeCellWithCoordByIndex(c,u);return{startRow:e,startColumn:t,endRow:n,endColumn:o,rangeType:s,startY:(h==null?void 0:h.startY)||0,endY:(g==null?void 0:g.endY)||0,startX:(h==null?void 0:h.startX)||0,endX:(g==null?void 0:g.endX)||0}}function Me(r,i){const{range:e,primary:t,style:n}=r,o=bn(i,e),s=t&&Jn(i,t);return{rangeWithCoord:o,primaryWithCoord:s,style:n}}function Jn(r,i){const{actualRow:e,actualColumn:t,isMerged:n,isMergedMainCell:o,startRow:s,startColumn:l,endRow:c,endColumn:d}=i,u=r.getNoMergeCellWithCoordByIndex(e,t),h=r.getNoMergeCellWithCoordByIndex(s,l),g=r.getNoMergeCellWithCoordByIndex(c,d);return{actualRow:e,actualColumn:t,isMerged:n,isMergedMainCell:o,startX:u.startX,startY:u.startY,endX:u.endX,endY:u.endY,mergeInfo:{startRow:s,startColumn:l,endRow:c,endColumn:d,startY:(h==null?void 0:h.startY)||0,endY:(g==null?void 0:g.endY)||0,startX:(h==null?void 0:h.startX)||0,endX:(g==null?void 0:g.endX)||0}}}const Ys="__SpreadsheetHelperSelectionTempRect",wd=35;class qa{constructor(i,e){E(this,"_startOffsetX",0);E(this,"_startOffsetY",0);E(this,"_relativeSelectionPositionRow",0);E(this,"_relativeSelectionPositionColumn",0);E(this,"_relativeSelectionRowLength",0);E(this,"_relativeSelectionColumnLength",0);E(this,"_scenePointerMoveSub");E(this,"_scenePointerUpSub");E(this,"_helperSelection");E(this,"_scrollTimer");E(this,"_activeViewport");E(this,"_targetSelection",{startY:0,endY:0,startX:0,endX:0,startRow:-1,endRow:-1,startColumn:-1,endColumn:-1});E(this,"_isInMergeState",!1);E(this,"_fillControlColors",[]);E(this,"_skeleton");E(this,"_scene");E(this,"_themeService");E(this,"_injector");E(this,"_selectionHooks");this._control=i,this._skeleton=e.skeleton,this._scene=e.scene,this._themeService=e.themeService,this._injector=e.injector,this._selectionHooks=e.selectionHooks,this._initialControl(),this._initialWidget(),this._initialAutoFill(),this._control.dispose$.subscribe(()=>{this.dispose()})}get isHelperSelection(){return this._control.isHelperSelection}dispose(){var i,e;(i=this._scrollTimer)==null||i.dispose(),this._fillControlColors=[],this._clearObserverEvent(),(e=this._helperSelection)==null||e.dispose()}_getFreeze(){var t,n;return(n=(t=this._injector.get(w.IRenderManagerService).withCurrentTypeOfUnit(a.UniverInstanceType.UNIVER_SHEET,exports.SheetSkeletonManagerService))==null?void 0:t.getCurrentParam())==null?void 0:n.skeleton.getWorksheetConfig().freeze}_isSelectionInViewport(i,e){const t=this._getFreeze()||{startRow:-1,startColumn:-1,xSplit:0,ySplit:0};switch(e.viewportKey){case w.SHEET_VIEWPORT_KEY.VIEW_MAIN:return i.endRow>=t.startRow&&i.endColumn>=t.startColumn;case w.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP:case w.SHEET_VIEWPORT_KEY.VIEW_COLUMN_RIGHT:return i.endColumn>=t.startColumn&&i.startRow<t.startRow;case w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT:case w.SHEET_VIEWPORT_KEY.VIEW_ROW_BOTTOM:return i.endRow>=t.startRow&&i.startColumn<t.startColumn;case w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP:case w.SHEET_VIEWPORT_KEY.VIEW_COLUMN_LEFT:case w.SHEET_VIEWPORT_KEY.VIEW_ROW_TOP:case w.SHEET_VIEWPORT_KEY.VIEW_LEFT_TOP:return i.startRow<t.startRow&&i.startColumn<t.startColumn}}_clearObserverEvent(){var i,e;(i=this._scenePointerMoveSub)==null||i.unsubscribe(),(e=this._scenePointerUpSub)==null||e.unsubscribe(),this._scenePointerMoveSub=null,this._scenePointerUpSub=null}_initialControl(){const{leftControl:i,rightControl:e,topControl:t,bottomControl:n}=this._control;[i,e,t,n].forEach(o=>{o.onPointerEnter$.subscribeEvent(()=>{var l;((l=this._injector.get(Ce,a.Quantity.OPTIONAL))==null?void 0:l.interceptor.fetchThroughInterceptors(Vo)(!1,null))!==!1&&o.setCursor(w.CURSOR_TYPE.MOVE)}),o.onPointerLeave$.subscribeEvent(()=>{o.resetCursor()}),o.onPointerDown$.subscribeEvent(this._controlPointerDownHandler.bind(this))})}_controlMoving(i,e){var A;const t=this._scene,n=t.getScrollXYInfoByViewport(w.Vector2.FromArray([i,e])),{scaleX:o,scaleY:s}=t.getAncestorScale(),l=this._skeleton.getCellIndexByOffset(i,e,o,s,n),{row:c,column:d}=l,u=this._skeleton.getRowCount()-1,h=this._skeleton.getColumnCount()-1;let g=Math.max(0,c+this._relativeSelectionPositionRow),S=g+this._relativeSelectionRowLength;S>u&&(S=u,S-g<this._relativeSelectionRowLength&&(g=S-this._relativeSelectionRowLength));let f=Math.max(0,d+this._relativeSelectionPositionColumn),p=f+this._relativeSelectionColumnLength;p>h&&(p=h,p-f<this._relativeSelectionColumnLength&&(f=p-this._relativeSelectionColumnLength));const C=this._skeleton.worksheet.getCellInfoInMergeData(g,f),b=Me({range:{startRow:g,endRow:S,startColumn:f,endColumn:p},primary:C,style:null},this._skeleton),R=this._skeleton.getNoMergeCellWithCoordByIndex(g,f),T=this._skeleton.getNoMergeCellWithCoordByIndex(S,p),M=(R==null?void 0:R.startY)||0,y=(T==null?void 0:T.endY)||0,P=(R==null?void 0:R.startX)||0,x=(T==null?void 0:T.endX)||0;(A=this._helperSelection)==null||A.transformByState({left:P,top:M,width:x-P,height:y-M}),this._targetSelection={...b.rangeWithCoord},this._control.selectionMoving$.next(b.rangeWithCoord)}_controlPointerDownHandler(i){const{offsetX:e,offsetY:t}=i,n=this._scene,o=n.getCoordRelativeToViewport(w.Vector2.FromArray([e,t])),{x:s,y:l}=o,c=n.getScrollXYInfoByViewport(o),{scaleX:d,scaleY:u}=n.getAncestorScale(),h=this._skeleton.getCellIndexByOffset(s,l,d,u,c);this._startOffsetX=s,this._startOffsetY=l;const{row:g,column:S}=h,{startRow:f,startColumn:p,endRow:C,endColumn:v}=this._control.model;let b=0,R=0;g<f?b-=1:g>C&&(b+=1),S<p?R-=1:S>v&&(R+=1),this._relativeSelectionPositionRow=f-g+b,this._relativeSelectionPositionColumn=p-S+R,this._relativeSelectionRowLength=C-f,this._relativeSelectionColumnLength=v-p;const T=this._control.currentStyle,M=this._getScale();this.isHelperSelection&&(this._helperSelection=new w.Rect(Ys,{stroke:T.stroke,strokeWidth:T.strokeWidth/M}),n.addObject(this._helperSelection));const y=n.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN),P=w.ScrollTimer.create(n);this._scrollTimer=P,P.startScroll(s,l,y),n.disableObjectsEvent(),this._scenePointerMoveSub=n.onPointerMove$.subscribeEvent(x=>{var k;const{offsetX:A,offsetY:N}=x;if(((k=this._injector.get(Ce,a.Quantity.OPTIONAL))==null?void 0:k.interceptor.fetchThroughInterceptors(Vo)(!1,null))===!1)return;const{x:H,y:W}=n.getCoordRelativeToViewport(w.Vector2.FromArray([A,N]));this._controlMoving(H,W),n.setCursor(w.CURSOR_TYPE.MOVE),P.scrolling(H,W,()=>{this._controlMoving(H,W)})}),this._scenePointerUpSub=n.onPointerUp$.subscribeEvent(()=>{var A,N,D,H;(A=this._helperSelection)==null||A.dispose();const x=this._scene;x.resetCursor(),this._clearObserverEvent(),x.enableObjectsEvent(),(N=this._scrollTimer)==null||N.dispose(),this._control.selectionMoveEnd$.next(this._targetSelection),(H=(D=this._selectionHooks).selectionMoveEnd)==null||H.call(D)})}_initialWidget(){const{topLeftWidget:i,topCenterWidget:e,topRightWidget:t,middleLeftWidget:n,middleRightWidget:o,bottomLeftWidget:s,bottomCenterWidget:l,bottomRightWidget:c}=this._control,d=[w.CURSOR_TYPE.NORTH_WEST_RESIZE,w.CURSOR_TYPE.NORTH_RESIZE,w.CURSOR_TYPE.NORTH_EAST_RESIZE,w.CURSOR_TYPE.WEST_RESIZE,w.CURSOR_TYPE.EAST_RESIZE,w.CURSOR_TYPE.SOUTH_WEST_RESIZE,w.CURSOR_TYPE.SOUTH_RESIZE,w.CURSOR_TYPE.SOUTH_EAST_RESIZE];[i,e,t,n,o,s,l,c].forEach((u,h)=>{u.onPointerEnter$.subscribeEvent(()=>{u.setCursor(d[h])}),u.onPointerLeave$.subscribeEvent(()=>{u.resetCursor()}),u.onPointerDown$.subscribeEvent(g=>{this._widgetPointerDownEvent(g,d[h])})})}_widgetPointerDownEvent(i,e){const t=this._scene,{offsetX:n,offsetY:o}=i,s=t.getCoordRelativeToViewport(w.Vector2.FromArray([n,o])),{x:l,y:c}=s;this._startOffsetX=n,this._startOffsetY=o;const{startRow:d,startColumn:u,endRow:h,endColumn:g}=this._control.model,S=Math.min(d,h),f=Math.min(u,g),p=Math.max(d,h),C=Math.max(u,g);this._relativeSelectionPositionRow=S,this._relativeSelectionPositionColumn=f,this._relativeSelectionRowLength=p-S,this._relativeSelectionColumnLength=C-f,e===w.CURSOR_TYPE.NORTH_WEST_RESIZE?(this._relativeSelectionPositionRow=p,this._relativeSelectionPositionColumn=C):e===w.CURSOR_TYPE.NORTH_RESIZE?this._relativeSelectionPositionRow=p:e===w.CURSOR_TYPE.NORTH_EAST_RESIZE?this._relativeSelectionPositionRow=p:e===w.CURSOR_TYPE.WEST_RESIZE?this._relativeSelectionPositionColumn=C:e===w.CURSOR_TYPE.SOUTH_WEST_RESIZE?this._relativeSelectionPositionColumn=C:e===w.CURSOR_TYPE.SOUTH_RESIZE&&(this._relativeSelectionPositionRow=S);const v=w.ScrollTimer.create(t),b=t.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN);v.startScroll(l,c,b),this._scrollTimer=v,t.disableObjectsEvent(),this._scenePointerMoveSub=t.onPointerMove$.subscribeEvent(R=>{const{offsetX:T,offsetY:M}=R,{x:y,y:P}=t.getCoordRelativeToViewport(w.Vector2.FromArray([T,M]));this._widgetMoving(y,P,e),t.setCursor(e),v.scrolling(y,P,()=>{this._widgetMoving(y,P,e)})}),this._scenePointerUpSub=t.onPointerUp$.subscribeEvent(()=>{var T,M,y;const R=this._scene;R.resetCursor(),this._clearObserverEvent(),R.enableObjectsEvent(),(T=this._scrollTimer)==null||T.dispose(),this._control.selectionScaled$.next(this._targetSelection),(y=(M=this._selectionHooks).selectionMoveEnd)==null||y.call(M)})}_widgetMoving(i,e,t){const n=this._scene,o=n.getScrollXYInfoByViewport(w.Vector2.FromArray([this._startOffsetX,this._startOffsetY])),{scaleX:s,scaleY:l}=n.getAncestorScale(),c=this._skeleton.getCellIndexByOffset(i,e,s,l,o),{row:d,column:u}=c;let h=this._relativeSelectionPositionRow,g=this._relativeSelectionPositionColumn,S=d,f=u;t===w.CURSOR_TYPE.NORTH_WEST_RESIZE?(h=d,g=u,S=this._relativeSelectionPositionRow,f=this._relativeSelectionPositionColumn):t===w.CURSOR_TYPE.NORTH_RESIZE?(h=d,g=this._relativeSelectionPositionColumn,S=this._relativeSelectionPositionRow,f=this._relativeSelectionPositionColumn+this._relativeSelectionColumnLength):t===w.CURSOR_TYPE.NORTH_EAST_RESIZE?(h=d,g=this._relativeSelectionPositionColumn,S=this._relativeSelectionPositionRow,f=u):t===w.CURSOR_TYPE.WEST_RESIZE?(h=this._relativeSelectionPositionRow,g=u,S=this._relativeSelectionPositionRow+this._relativeSelectionRowLength,f=this._relativeSelectionPositionColumn):t===w.CURSOR_TYPE.EAST_RESIZE?S=this._relativeSelectionPositionRow+this._relativeSelectionRowLength:t===w.CURSOR_TYPE.SOUTH_WEST_RESIZE?(h=this._relativeSelectionPositionRow,g=u,S=d,f=this._relativeSelectionPositionColumn):t===w.CURSOR_TYPE.SOUTH_RESIZE&&(h=this._relativeSelectionPositionRow,g=this._relativeSelectionPositionColumn,S=d,f=this._relativeSelectionPositionColumn+this._relativeSelectionColumnLength);const p=this._swapPositions(h,g,S,f),C=this._skeleton.getCellWithMergeInfoByIndex(h,g),b=Me({range:p,primary:C,style:null},this._skeleton);this._targetSelection={...b.rangeWithCoord},this._control.updateRangeBySelectionWithCoord(b),this._control.selectionScaling$.next(this._targetSelection)}_initialAutoFill(){const{fillControl:i}=this._control;i.onPointerEnter$.subscribeEvent(e=>{this._injector.get(Ce).interceptor.fetchThroughInterceptors(Wo)(!1,{x:e.offsetX,y:e.offsetY,skeleton:this._skeleton,scene:this._scene})&&i.setCursor(w.CURSOR_TYPE.CROSSHAIR)}),i.onPointerLeave$.subscribeEvent(()=>{i.resetCursor()}),i.onPointerDown$.subscribeEvent(this._autoFillForPointerdown.bind(this))}_autoFillMoving(i,e){var N,D,H;const t=this._scene,n=t.getViewportScrollXY(this._activeViewport),{scaleX:o,scaleY:s}=t.getAncestorScale(),l=this._skeleton.getCellIndexByOffset(i,e,o,s,n),{row:c,column:d}=l,u=t.getCoordRelativeToViewport(w.Vector2.FromArray([i,e])),h=this._skeleton.getRowCount()-1,g=this._skeleton.getColumnCount()-1;let S=this._relativeSelectionPositionRow,f=this._relativeSelectionPositionColumn,p=this._relativeSelectionPositionRow+this._relativeSelectionRowLength,C=this._relativeSelectionPositionColumn+this._relativeSelectionColumnLength,v=!1,b=!0;if((d<f||d>C)&&c>=S&&c<=p){const W=this._fillRuler(d,f,C,this._relativeSelectionColumnLength,g);f=W.startRowOrColumn,C=W.endRowOrColumn,v=W.isLighten,b=!1}else if((c<S||c>p)&&d>=f&&d<=C){const W=this._fillRuler(c,S,p,this._relativeSelectionRowLength,h);S=W.startRowOrColumn,p=W.endRowOrColumn,v=W.isLighten}else if(Math.abs(this._startOffsetX-u.x-n.x)/2>Math.abs(this._startOffsetY-u.y-n.y)){const W=this._fillRuler(d,f,C,this._relativeSelectionColumnLength,g);f=W.startRowOrColumn,C=W.endRowOrColumn,v=W.isLighten,b=!1}else{const W=this._fillRuler(c,S,p,this._relativeSelectionRowLength,h);S=W.startRowOrColumn,p=W.endRowOrColumn,v=W.isLighten}const R=this._skeleton.getNoMergeCellWithCoordByIndex(S,f),T=this._skeleton.getNoMergeCellWithCoordByIndex(p,C),M=(R==null?void 0:R.startY)||0,y=(T==null?void 0:T.endY)||0,P=(R==null?void 0:R.startX)||0,x=(T==null?void 0:T.endX)||0;v?this._controlHandler((W,k)=>{const U=new a.ColorKit(this._fillControlColors[k]).lighten(wd).toRgbString();W.setProps({fill:U})}):this._controlHandler((W,k)=>{W.setProps({fill:this._fillControlColors[k]})});const A=m.SELECTION_CONTROL_BORDER_BUFFER_WIDTH/this._getScale();S===p&&b===!0||f===C&&b===!1?(N=this._helperSelection)==null||N.hide():((D=this._helperSelection)==null||D.transformByState({left:P-A/2,top:M-A/2,width:x-P,height:y-M}),(H=this._helperSelection)==null||H.show()),this._targetSelection={startY:M,endY:y,startX:P,endX:x,startRow:S,endRow:p,startColumn:f,endColumn:C},this._control.selectionFilling$.next(this._targetSelection)}_autoFillForPointerdown(i){const{offsetX:e,offsetY:t}=i,n=this._scene,o=n.getCoordRelativeToViewport(w.Vector2.FromArray([e,t])),{x:s,y:l}=o;this._startOffsetX=s,this._startOffsetY=l;const{startRow:c,startColumn:d,endRow:u,endColumn:h}=this._control.model;this._isInMergeState=this._hasMergeInRange(c,d,u,h),this._relativeSelectionPositionRow=c,this._relativeSelectionPositionColumn=d,this._relativeSelectionRowLength=u-c,this._relativeSelectionColumnLength=h-d;const g=this._control.currentStyle;let S=g==null?void 0:g.stroke,f=g==null?void 0:g.strokeWidth;const p=bt(this._themeService);S==null&&(S=p.stroke),f==null&&(f=p.strokeWidth);const C=this._getScale();f/=C;const v=m.SELECTION_CONTROL_BORDER_BUFFER_WIDTH/C,b=new a.ColorKit(S).darken(2).toRgbString();this.isHelperSelection&&(this._helperSelection=new w.Rect(Ys,{stroke:b,strokeWidth:f+v/2}),n.addObject(this._helperSelection)),this._activeViewport=n.getActiveViewportByCoord(w.Vector2.FromArray([e,t]));const R=n.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN),T=w.ScrollTimer.create(n,this._activeViewport.viewportKey===w.SHEET_VIEWPORT_KEY.VIEW_MAIN?w.ScrollTimerType.ALL:w.ScrollTimerType.NONE);T.startScroll(s,l,R),this._scrollTimer=T,n.disableObjectsEvent(),this._controlHandler(M=>{this._fillControlColors.push(M.fill)}),this._scenePointerMoveSub=n.onPointerMove$.subscribeEvent(M=>{var W;const{offsetX:y,offsetY:P}=M,x=n.getActiveViewportByCoord(w.Vector2.FromArray([y,P]));if(!this._injector.get(Ce).interceptor.fetchThroughInterceptors(Wo)(!1,{x:i.offsetX,y:i.offsetY,skeleton:this._skeleton,scene:this._scene}))return;const{x:N,y:D}=n.getCoordRelativeToViewport(w.Vector2.FromArray([y,P]));this._autoFillMoving(N,D),n.setCursor(w.CURSOR_TYPE.CROSSHAIR);const H=this._targetSelection;if(R&&x&&((W=this._activeViewport)==null?void 0:W.viewportKey)!==(x==null?void 0:x.viewportKey)){let k;H.startRow!==c?(T.scrollTimerType=w.ScrollTimerType.Y,k={...H,endRow:H.startRow}):H.endRow!==u?(T.scrollTimerType=w.ScrollTimerType.Y,k={...H,startRow:H.endRow}):H.startColumn!==d?(T.scrollTimerType=w.ScrollTimerType.X,k={...H,endColumn:H.startColumn}):(T.scrollTimerType=w.ScrollTimerType.X,k={...H,startColumn:H.endColumn}),this._isSelectionInViewport(k,x)&&(R.scrollToBarPos({x:T.scrollTimerType===w.ScrollTimerType.X?0:void 0,y:T.scrollTimerType===w.ScrollTimerType.Y?0:void 0}),this._activeViewport=x)}T.scrolling(N,D,()=>{this._autoFillMoving(N,D)})}),this._scenePointerUpSub=n.onPointerUp$.subscribeEvent(()=>{var y,P;(y=this._helperSelection)==null||y.dispose();const M=this._scene;M.resetCursor(),this._clearObserverEvent(),M.enableObjectsEvent(),(P=this._scrollTimer)==null||P.dispose(),this._control.refreshSelectionFilled(this._targetSelection),this._isInMergeState=!1,this._controlHandler((x,A)=>{x.setProps({fill:this._fillControlColors[A]})}),this._fillControlColors=[]})}_hasMergeInRange(i,e,t,n){return this._skeleton.worksheet.getMergedCellRange(i,e,t,n).length>0}_swapPositions(i,e,t,n){const o=Math.min(i,t),s=Math.min(e,n),l=Math.max(i,t),c=Math.max(e,n);return{startRow:o,startColumn:s,endRow:l,endColumn:c}}_controlHandler(i){const{leftControl:e,rightControl:t,topControl:n,bottomControl:o,backgroundControlTop:s,backgroundControlMiddleLeft:l,backgroundControlMiddleRight:c,backgroundControlBottom:d,fillControl:u}=this._control,h=[e,t,n,o,s,l,c,d,u];for(let g=0,S=h.length;g<S;g++){const f=h[g];i(f,g)}}_fillRuler(i,e,t,n,o){let s=!1;if(i<e)if(this._isInMergeState&&i<e){const l=e-i,c=n+1,d=Math.ceil(l/c);let u=e-d*c;u<0&&(u=e-(d-1)*c),e=u}else e=i;else if(i>=e&&i<=t)s=!0,this._isInMergeState||(t=i);else if(this._isInMergeState&&i>t){const l=i-t,c=n+1,d=Math.ceil(l/c);let u=t+d*c;u>o&&(u=t+(d-1)*c),t=u}else t=i;return{rowOrColumn:i,startRowOrColumn:e,endRowOrColumn:t,isLighten:s}}_getScale(){const{scaleX:i,scaleY:e}=this._scene.getAncestorScale();return Math.max(i,e)}}var Uo=(r=>(r.Selection="__SpreadsheetSelectionShape__",r.top="__SpreadsheetSelectionTopControl__",r.bottom="__SpreadsheetSelectionBottomControl__",r.left="__SpreadsheetSelectionShapeLeftControl__",r.right="__SpreadsheetSelectionShapeRightControl__",r.backgroundTop="__SpreadsheetSelectionBackgroundControlTop__",r.backgroundMiddleLeft="__SpreadsheetSelectionBackgroundControlMiddleLeft__",r.backgroundMiddleRight="__SpreadsheetSelectionBackgroundControlMiddleRight__",r.backgroundBottom="__SpreadsheetSelectionBackgroundControlBottom__",r.fill="__SpreadsheetSelectionFillControl__",r.fillTopLeft="__SpreadsheetSelectionFillControlTopLeft__",r.fillBottomRight="__SpreadsheetSelectionFillControlBottomRight__",r.fillTopLeftInner="__SpreadsheetSelectionFillControlTopLeftInner__",r.fillBottomRightInner="__SpreadsheetSelectionFillControlBottomRightInner__",r.lineMain="__SpreadsheetDragLineMainControl__",r.lineContent="__SpreadsheetDragLineContentControl__",r.line="__SpreadsheetDragLineControl__",r.dash="__SpreadsheetDragDashControl__",r.rowHeaderBackground="__SpreadSheetSelectionRowHeaderBackground__",r.rowHeaderBorder="__SpreadSheetSelectionRowHeaderBorder__",r.rowHeaderGroup="__SpreadSheetSelectionRowHeaderGroup__",r.columnHeaderBackground="__SpreadSheetSelectionColumnHeaderBackground__",r.columnHeaderBorder="__SpreadSheetSelectionColumnHeaderBorder__",r.columnHeaderGroup="__SpreadSheetSelectionColumnHeaderGroup__",r.topLeftWidget="__SpreadSheetSelectionTopLeftWidget__",r.topCenterWidget="__SpreadSheetSelectionTopCenterWidget__",r.topRightWidget="__SpreadSheetSelectionTopRightWidget__",r.middleLeftWidget="__SpreadSheetSelectionMiddleLeftWidget__",r.middleRightWidget="__SpreadSheetSelectionMiddleRightWidget__",r.bottomLeftWidget="__SpreadSheetSelectionBottomLeftWidget__",r.bottomCenterWidget="__SpreadSheetSelectionBottomCenterWidget__",r.bottomRightWidget="__SpreadSheetSelectionBottomRightWidget__",r))(Uo||{});const Gs=.3;class lr extends a.Disposable{constructor(e,t,n,o){var s,l,c,d;super();E(this,"_isHelperSelection",!0);E(this,"_enableAutoFill",!0);E(this,"_showAutoFill",!1);E(this,"_highlightHeader",!0);E(this,"_selectionRenderModel");E(this,"_leftBorder");E(this,"_rightBorder");E(this,"_topBorder");E(this,"_bottomBorder");E(this,"_backgroundControlTop");E(this,"_backgroundControlBottom");E(this,"_backgroundControlMiddleLeft");E(this,"_backgroundControlMiddleRight");E(this,"_autoFillControl");E(this,"_selectionShapeGroup");E(this,"_rowHeaderBackground");E(this,"_rowHeaderBorder");E(this,"_rowHeaderGroup");E(this,"_columnHeaderBackground");E(this,"_columnHeaderBorder");E(this,"_columnHeaderGroup");E(this,"_dashedRect");E(this,"_topLeftWidget");E(this,"_topCenterWidget");E(this,"_topRightWidget");E(this,"_middleLeftWidget");E(this,"_middleRightWidget");E(this,"_bottomLeftWidget");E(this,"_bottomCenterWidget");E(this,"_bottomRightWidget");E(this,"_defaultStyle");E(this,"_currentStyle");E(this,"_rowHeaderWidth",0);E(this,"_columnHeaderHeight",0);E(this,"_widgetRects",[]);E(this,"_controlExtension");E(this,"_dispose$",new O.BehaviorSubject(this));E(this,"dispose$",this._dispose$.asObservable());E(this,"selectionMoving$",new O.Subject);E(this,"selectionMoveEnd$",new O.Subject);E(this,"selectionScaling$",new O.Subject);E(this,"selectionScaled$",new O.Subject);E(this,"selectionFilling$",new O.Subject);E(this,"_selectionFilled$",new O.Subject);E(this,"selectionFilled$",this._selectionFilled$.asObservable());E(this,"_antLineOffset",0);E(this,"_antRequestNewFrame",-1);this._scene=e,this._zIndex=t,this._themeService=n,this._enableAutoFill=(s=o==null?void 0:o.enableAutoFill)!=null?s:!0,this._highlightHeader=(l=o==null?void 0:o.highlightHeader)!=null?l:!0,this._rowHeaderWidth=(c=o==null?void 0:o.rowHeaderWidth)!=null?c:0,this._columnHeaderHeight=(d=o==null?void 0:o.columnHeaderHeight)!=null?d:0,this._initializeSheetBody(),this._initialHeader()}_initializeSheetBody(){this._defaultStyle=bt(this._themeService),this._currentStyle=bt(this._themeService),this._selectionRenderModel=new Td;const e=this._zIndex;this._leftBorder=new w.Rect("__SpreadsheetSelectionShapeLeftControl__"+e,{zIndex:e}),this._rightBorder=new w.Rect("__SpreadsheetSelectionShapeRightControl__"+e,{zIndex:e}),this._topBorder=new w.Rect("__SpreadsheetSelectionTopControl__"+e,{zIndex:e}),this._bottomBorder=new w.Rect("__SpreadsheetSelectionBottomControl__"+e,{zIndex:e}),this._backgroundControlTop=new w.Rect("__SpreadsheetSelectionBackgroundControlTop__"+e,{zIndex:e-1,evented:!1}),this._backgroundControlBottom=new w.Rect("__SpreadsheetSelectionBackgroundControlBottom__"+e,{zIndex:e-1,evented:!1}),this._backgroundControlMiddleLeft=new w.Rect("__SpreadsheetSelectionBackgroundControlMiddleLeft__"+e,{zIndex:e-1,evented:!1}),this._backgroundControlMiddleRight=new w.Rect("__SpreadsheetSelectionBackgroundControlMiddleRight__"+e,{zIndex:e-1,evented:!1}),this._autoFillControl=new w.Rect("__SpreadsheetSelectionFillControl__"+e,{zIndex:e+1}),this._autoFillControl.hide(),this._dashedRect=new w.DashedRect("__SpreadsheetDragDashControl__"+e,{zIndex:e+2,evented:!1,stroke:"#fff"});const t=[this._autoFillControl,this._leftBorder,this._rightBorder,this._topBorder,this._bottomBorder,this._backgroundControlTop,this._backgroundControlMiddleLeft,this._backgroundControlMiddleRight,this._backgroundControlBottom,this._dashedRect];this._widgetRects=this._initialWidget(),this._selectionShapeGroup=new w.Group("__SpreadsheetSelectionShape__"+e,...t,...this._widgetRects),this._selectionShapeGroup.hide(),this._selectionShapeGroup.evented=!1,this._selectionShapeGroup.zIndex=e;const n=this.getScene();n.addObject(this._selectionShapeGroup,Jr),this.disposeWithMe(a.toDisposable(n.onTransformChange$.subscribeEvent(o=>{o.type===w.TRANSFORM_CHANGE_OBSERVABLE_TYPE.scale&&(this._updateLayoutOfSelectionControl(this._currentStyle),this._updateControlCoord())})))}_initialHeader(){const e=this._zIndex;this._rowHeaderBackground=new w.Rect("__SpreadSheetSelectionRowHeaderBackground__"+e,{zIndex:e-1,evented:!1}),this._rowHeaderBorder=new w.Rect("__SpreadSheetSelectionRowHeaderBorder__"+e,{zIndex:e-1,evented:!1}),this._rowHeaderGroup=new w.Group("__SpreadSheetSelectionRowHeaderGroup__"+e,this._rowHeaderBackground,this._rowHeaderBorder),this._rowHeaderGroup.hide(),this._rowHeaderGroup.evented=!1,this._rowHeaderGroup.zIndex=e,this._columnHeaderBackground=new w.Rect("__SpreadSheetSelectionColumnHeaderBackground__"+e,{zIndex:e-1,evented:!1}),this._columnHeaderBorder=new w.Rect("__SpreadSheetSelectionColumnHeaderBorder__"+e,{zIndex:e-1,evented:!1}),this._columnHeaderGroup=new w.Group("__SpreadSheetSelectionColumnHeaderGroup__"+e,this._columnHeaderBackground,this._columnHeaderBorder),this._columnHeaderGroup.hide(),this._columnHeaderGroup.evented=!1,this._columnHeaderGroup.zIndex=e,this.getScene().addObjects([this._rowHeaderGroup,this._columnHeaderGroup],Ed)}_initialWidget(){const e=this._zIndex;return this._topLeftWidget=new w.Rect("__SpreadSheetSelectionTopLeftWidget__"+e,{zIndex:e+1}),this._topCenterWidget=new w.Rect("__SpreadSheetSelectionTopCenterWidget__"+e,{zIndex:e+1}),this._topRightWidget=new w.Rect("__SpreadSheetSelectionTopRightWidget__"+e,{zIndex:e+1}),this._middleLeftWidget=new w.Rect("__SpreadSheetSelectionMiddleLeftWidget__"+e,{zIndex:e+1}),this._middleRightWidget=new w.Rect("__SpreadSheetSelectionMiddleRightWidget__"+e,{zIndex:e+1}),this._bottomLeftWidget=new w.Rect("__SpreadSheetSelectionBottomLeftWidget__"+e,{zIndex:e+1}),this._bottomCenterWidget=new w.Rect("__SpreadSheetSelectionBottomCenterWidget__"+e,{zIndex:e+1}),this._bottomRightWidget=new w.Rect("__SpreadSheetSelectionBottomRightWidget__"+e,{zIndex:e+1}),[this._topLeftWidget,this._topCenterWidget,this._topRightWidget,this._middleLeftWidget,this._middleRightWidget,this._bottomLeftWidget,this._bottomCenterWidget,this._bottomRightWidget]}get zIndex(){return this._zIndex}get leftControl(){return this._leftBorder}get rightControl(){return this._rightBorder}get topControl(){return this._topBorder}get bottomControl(){return this._bottomBorder}get fillControl(){return this._autoFillControl}get backgroundControlTop(){return this._backgroundControlTop}get backgroundControlBottom(){return this._backgroundControlBottom}get backgroundControlMiddleLeft(){return this._backgroundControlMiddleLeft}get backgroundControlMiddleRight(){return this._backgroundControlMiddleRight}get selectionShape(){return this._selectionShapeGroup}get columnHeaderGroup(){return this._columnHeaderGroup}get rowHeaderGroup(){return this._rowHeaderGroup}get selectionShapeGroup(){return this._selectionShapeGroup}get model(){return this._selectionRenderModel}get topLeftWidget(){return this._topLeftWidget}get topCenterWidget(){return this._topCenterWidget}get topRightWidget(){return this._topRightWidget}get middleLeftWidget(){return this._middleLeftWidget}get middleRightWidget(){return this._middleRightWidget}get bottomLeftWidget(){return this._bottomLeftWidget}get bottomCenterWidget(){return this._bottomCenterWidget}get bottomRightWidget(){return this._bottomRightWidget}get themeService(){return this._themeService}get selectionModel(){return this._selectionRenderModel}set selectionModel(e){this._selectionRenderModel=e}get currentStyle(){return this._currentStyle}set currentStyle(e){this._currentStyle=e}get dashedRect(){return this._dashedRect}get isHelperSelection(){return this._isHelperSelection}get rowHeaderWidth(){return this._rowHeaderWidth}set rowHeaderWidth(e){this._rowHeaderWidth=e}get columnHeaderHeight(){return this._columnHeaderHeight}set columnHeaderHeight(e){this._columnHeaderHeight=e}setControlExtension(e){this._controlExtension=new qa(this,e)}setEvent(e){this.leftControl.evented=e,this.rightControl.evented=e,this.topControl.evented=e,this.bottomControl.evented=e}refreshSelectionFilled(e){this._selectionFilled$.next(e)}_updateLayoutOfSelectionControl(e){e&&(this.currentStyle=Object.assign({},this._defaultStyle,e));const t=this.currentStyle,{stroke:n,widgets:o,autofillStroke:s,strokeDash:l,isAnimationDash:c}=t;let d=t.autofillSize,u=t.strokeWidth,h=t.autofillStrokeWidth;const g=this._getScale(),S=(u+m.SELECTION_CONTROL_BORDER_BUFFER_WIDTH)/2/g;u/=g,d/=g,h/=g<1?1:g;const f=m.SELECTION_CONTROL_BORDER_BUFFER_WIDTH/g,p=w.FIX_ONE_PIXEL_BLUR_OFFSET/g,{startX:C,startY:v,endX:b,endY:R}=this._selectionRenderModel;if(this.leftControl.transformByState({height:R-v,left:-S+p,width:u,strokeWidth:f,top:-f/2+p}),this.leftControl.setProps({fill:n,stroke:m.SELECTION_CONTROL_BORDER_BUFFER_COLOR}),this.rightControl.transformByState({height:R-v,left:b-C-S+p,width:u,strokeWidth:f,top:-f/2+p}),this.rightControl.setProps({fill:n,stroke:m.SELECTION_CONTROL_BORDER_BUFFER_COLOR}),this.topControl.transformByState({width:b-C+u,top:-S+p,left:-S+p,height:u,strokeWidth:f}),this.topControl.setProps({fill:n,stroke:m.SELECTION_CONTROL_BORDER_BUFFER_COLOR}),this.bottomControl.transformByState({width:b-C+u,top:R-v-S+p,height:u,left:-S+p,strokeWidth:f}),this.bottomControl.setProps({fill:n,stroke:m.SELECTION_CONTROL_BORDER_BUFFER_COLOR}),l==null)this.dashedRect.hide(),this._stopAntLineAnimation();else{const T=t.strokeWidth*2/g;this.dashedRect.transformByState({height:R-v,width:b-C,strokeWidth:T,left:-T/2+p,top:-T/2+p}),this.dashedRect.setProps({strokeDashArray:[0,l/g]}),this._stopAntLineAnimation(),c!==!1&&this._startAntLineAnimation(),this.dashedRect.show()}if(this._showAutoFill=this._showAutoFill&&this._enableAutoFill,this._showAutoFill&&!this._hasWidgets(o)){const T={fill:n,stroke:s,strokeScaleEnabled:!1},M={width:d-h,height:d-h,left:b-C-d/2+h/2-p,top:R-v-d/2+h/2-p,strokeWidth:h};this._autoFillControl.setProps(T),this._autoFillControl.transformByState(M),this._autoFillControl.show()}else this._autoFillControl.hide();this._updateBackgroundControl(t),this._updateHeaderBackground(t),this._updateWidgets(t)}_updateControlCoord(){const{startX:e,startY:t}=this._selectionRenderModel;this.selectionShapeGroup.show(),this.selectionShapeGroup.translate(e,t),this.selectionShapeGroup.makeDirtyNoDebounce(!0)}updateStyle(e){this._updateLayoutOfSelectionControl(e),this._updateControlCoord()}updateRange(e,t){this._selectionRenderModel.setValue(e,t),this._showAutoFill=t!==null,this._updateLayoutOfSelectionControl(),this._updateControlCoord()}updateRangeBySelectionWithCoord(e,t){t&&(this._rowHeaderWidth=t.getWorksheetConfig().rowHeader.width,this._columnHeaderHeight=t.getWorksheetConfig().columnHeader.height),this._selectionRenderModel.setValue(e.rangeWithCoord,e.primaryWithCoord),this._showAutoFill=e.primaryWithCoord!==null,this._updateLayoutOfSelectionControl(e.style),this._updateControlCoord()}update(e,t=0,n=0,o,s){this._rowHeaderWidth=t,this._columnHeaderHeight=n,this.updateRangeBySelectionWithCoord({rangeWithCoord:e,primaryWithCoord:s,style:o}),o&&this.updateStyle(o)}updateCurrCell(e){this._showAutoFill=e!==null,this._selectionRenderModel.setCurrentCell(e)}clearHighlight(){this._showAutoFill=!1,this._selectionRenderModel.clearCurrentCell(),this._updateLayoutOfSelectionControl(this._currentStyle)}getScene(){return this._scene}dispose(){var e,t,n,o,s,l,c,d,u,h,g,S,f,p,C,v,b,R,T,M,y,P,x,A,N;(e=this._leftBorder)==null||e.dispose(),(t=this._rightBorder)==null||t.dispose(),(n=this._topBorder)==null||n.dispose(),(o=this._bottomBorder)==null||o.dispose(),(s=this._backgroundControlTop)==null||s.dispose(),(l=this._backgroundControlMiddleLeft)==null||l.dispose(),(c=this._backgroundControlMiddleRight)==null||c.dispose(),(d=this._backgroundControlBottom)==null||d.dispose(),this._autoFillControl.dispose(),(u=this._selectionShapeGroup)==null||u.dispose(),(h=this._rowHeaderBackground)==null||h.dispose(),(g=this._rowHeaderBorder)==null||g.dispose(),(S=this._rowHeaderGroup)==null||S.dispose(),(f=this._rowHeaderBackground)==null||f.dispose(),(p=this._columnHeaderBackground)==null||p.dispose(),(C=this._columnHeaderBorder)==null||C.dispose(),(v=this._columnHeaderGroup)==null||v.dispose(),(b=this._topLeftWidget)==null||b.dispose(),(R=this._topCenterWidget)==null||R.dispose(),(T=this._topRightWidget)==null||T.dispose(),(M=this._middleLeftWidget)==null||M.dispose(),(y=this._middleRightWidget)==null||y.dispose(),(P=this._bottomLeftWidget)==null||P.dispose(),(x=this._bottomCenterWidget)==null||x.dispose(),(A=this._bottomRightWidget)==null||A.dispose(),(N=this._controlExtension)==null||N.dispose(),super.dispose(),this._dispose$.next(this),this._dispose$.complete()}getCurrentCellInfo(){const e=this.model.currentCell;if(e){let t;if(e.isMerged){const n=e.mergeInfo;t={startRow:n.startRow,endRow:n.endRow,startColumn:n.startColumn,endColumn:n.endColumn,startX:n.startX,endX:n.endX,startY:n.startY,endY:n.endY}}else{const{actualRow:n,actualColumn:o,startX:s,endX:l,startY:c,endY:d}=e;t={startRow:n,endRow:n,startColumn:o,endColumn:o,startX:s,endX:l,startY:c,endY:d}}return t}}getValue(){return{...this._selectionRenderModel.getValue(),style:this._currentStyle}}getRange(){return this._selectionRenderModel.getValue().rangeWithCoord}enableHelperSelection(){this._isHelperSelection=!0}disableHelperSelection(){this._isHelperSelection=!1}_updateHeaderBackground(e){const{startX:t,startY:n,endX:o,endY:s,rangeType:l}=this._selectionRenderModel,c=this._currentStyle,d=this._getScale(),{stroke:u,rowHeaderFill:h=c.rowHeaderFill,rowHeaderStroke:g=c.rowHeaderStroke,columnHeaderFill:S=c.columnHeaderFill,columnHeaderStroke:f=c.columnHeaderStroke}=e;let{rowHeaderStrokeWidth:p=c.rowHeaderStrokeWidth,columnHeaderStrokeWidth:C=c.columnHeaderStrokeWidth}=e;p/=d,C/=d;const v=this._rowHeaderWidth,b=this._columnHeaderHeight;if(this._highlightHeader&&b>0){let R=S;l===a.RANGE_TYPE.COLUMN&&(R=new a.ColorKit(u).setAlpha(Gs).toString()),this._columnHeaderBackground.setProps({fill:R}),this._columnHeaderBackground.resize(o-t,b),this._columnHeaderBorder.setProps({fill:f}),this._columnHeaderBorder.transformByState({width:o-t,height:C,top:b-C+1/d}),this._columnHeaderGroup.show(),this._columnHeaderGroup.translate(t,0)}else this._columnHeaderGroup.hide();if(this._columnHeaderGroup.makeDirty(!0),this._highlightHeader&&v>0){let R=h;l===a.RANGE_TYPE.ROW&&(R=new a.ColorKit(u).setAlpha(Gs).toString()),this._rowHeaderBackground.setProps({fill:R}),this._rowHeaderBackground.resize(v,s-n),this._rowHeaderBorder.setProps({fill:g}),this._rowHeaderBorder.transformByState({width:p,height:s-n,left:v-p+1/d}),this._rowHeaderGroup.show(),this._rowHeaderGroup.translate(0,n)}else this._rowHeaderGroup.hide();this._rowHeaderGroup.makeDirty(!0)}_updateBackgroundControl(e){const{startX:t,startY:n,endX:o,endY:s}=this._selectionRenderModel,l=this._getScale(),{fill:c=e.fill}=e;let{strokeWidth:d}=e;d/=l;const u=this._selectionRenderModel.highlightToSelection();if(!u){this._backgroundControlTop.resize(o-t,s-n),this._backgroundControlTop.setProps({fill:c}),this._backgroundControlBottom.resize(0,0),this._backgroundControlMiddleLeft.resize(0,0),this._backgroundControlMiddleRight.resize(0,0);return}const{startX:h,startY:g,endX:S,endY:f}=u,p=d/2,C={left:-p,top:-p,width:o-t+p*2,height:g-n+p};C.height<0&&(C.width=0,C.height=0),this._backgroundControlTop.transformByState(C);const v={left:-p,top:g-n,width:h-t+p,height:f-g};v.width<0&&(v.width=0,v.height=0),this._backgroundControlMiddleLeft.transformByState(v);const b={left:S-t-p,top:g-n,width:o-S+p*2,height:f-g};b.width<0&&(b.width=0,b.height=0),this._backgroundControlMiddleRight.transformByState(b);const R={left:-p,top:f-n,width:o-t+p*2,height:s-f+p};R.height<0&&(R.width=0,R.height=0),this._backgroundControlBottom.transformByState(R),this._backgroundControlTop.setProps({fill:c}),this._backgroundControlMiddleLeft.setProps({fill:c}),this._backgroundControlMiddleRight.setProps({fill:c}),this._backgroundControlBottom.setProps({fill:c})}_updateWidgets(e){const{startX:t,startY:n,endX:o,endY:s}=this._selectionRenderModel,{stroke:l=e.stroke,widgets:c=e.widgets,widgetStroke:d=e.widgetStroke}=e,u=this._getScale();let{widgetSize:h,widgetStrokeWidth:g}=e;h/=u,g/=u;const S={left:-h/2+g/2,center:(o-t)/2-h/2+g/2,right:o-t-h/2+g/2,top:-h/2,middle:(s-n)/2-h/2,bottom:s-n-h/2+g/2},f=h-g;this._widgetRects.forEach(p=>{p.setProps({fill:l,stroke:d})}),c.tl===!0?(this._topLeftWidget.transformByState({height:f,width:f,left:S.left,top:S.top,strokeWidth:g}),this._topLeftWidget.show()):this._topLeftWidget.hide(),c.tc===!0?(this._topCenterWidget.transformByState({height:f,width:f,left:S.center,top:S.top,strokeWidth:g}),this._topCenterWidget.show()):this._topCenterWidget.hide(),c.tr===!0?(this._topRightWidget.transformByState({height:f,width:f,left:S.right,top:S.top,strokeWidth:g}),this._topRightWidget.show()):this._topRightWidget.hide(),c.ml===!0?(this._middleLeftWidget.transformByState({height:f,width:f,left:S.left,top:S.middle,strokeWidth:g}),this._middleLeftWidget.show()):this._middleLeftWidget.hide(),c.mr===!0?(this._middleRightWidget.transformByState({height:f,width:f,left:S.right,top:S.middle,strokeWidth:g}),this._middleRightWidget.show()):this._middleRightWidget.hide(),c.bl===!0?(this._bottomLeftWidget.transformByState({height:f,width:f,left:S.left,top:S.bottom,strokeWidth:g}),this._bottomLeftWidget.show()):this._bottomLeftWidget.hide(),c.bc===!0?(this._bottomCenterWidget.transformByState({height:f,width:f,left:S.center,top:S.bottom,strokeWidth:g}),this._bottomCenterWidget.show()):this._bottomCenterWidget.hide(),c.br===!0?(this._bottomRightWidget.transformByState({height:f,width:f,left:S.right,top:S.bottom,strokeWidth:g}),this._bottomRightWidget.show()):this._bottomRightWidget.hide()}_hasWidgets(e){if(e==null)return!1;const t=Object.keys(e);if(t.length===0)return!1;for(const n of t)if(e[n]===!0)return!0;return!1}_getScale(){const{scaleX:e,scaleY:t}=this._scene.getAncestorScale();return Math.max(e,t)}_stopAntLineAnimation(){this._antLineOffset=0,w.cancelRequestFrame(this._antRequestNewFrame)}_startAntLineAnimation(){const e=this._getScale();this._antLineOffset+=.6/e,this._antLineOffset>160/e&&(this._antLineOffset=0),this.dashedRect.setProps({strokeDashOffset:-this._antLineOffset}),this._antRequestNewFrame=w.requestNewFrame(()=>{this._startAntLineAnimation()})}}class yd extends w.Layer{render(i,e=!1){const t=a.Tools.now();return super.render(i,e),this._afterRender(t),this}_afterRender(i){this.scene.getEngine().renderFrameTimeMetric$.next(["selectionLayer",a.Tools.now()-i])}}const Ce=a.createIdentifier("univer.sheet.selection-render-service");class gs extends a.Disposable{constructor(e,t,n,o,s){super();E(this,"_downObserver");E(this,"_scenePointerMoveSub");E(this,"_scenePointerUpSub");E(this,"_controlFillConfig$",new O.BehaviorSubject(null));E(this,"controlFillConfig$",this._controlFillConfig$.asObservable());E(this,"_selectionControls",[]);E(this,"_startRangeWhenPointerDown",{startY:0,endY:0,startX:0,endX:0,startRow:-1,endRow:-1,startColumn:-1,endColumn:-1});E(this,"_activeControlIndex",-1);E(this,"_startViewportPosX",0);E(this,"_startViewportPosY",0);E(this,"_scrollTimer");E(this,"_cancelDownSubscription");E(this,"_cancelUpSubscription");E(this,"_skeleton");E(this,"_scene");E(this,"_highlightHeader",!0);E(this,"_rangeType",a.RANGE_TYPE.NORMAL);E(this,"_selectionStyle");E(this,"_remainLastEnabled",!1);E(this,"_skipLastEnabled",!1);E(this,"_singleSelectionEnabled",!1);E(this,"_selectionMoveEnd$",new O.BehaviorSubject([]));E(this,"selectionMoveEnd$",this._selectionMoveEnd$.asObservable());E(this,"_selectionMoving$",new O.Subject);E(this,"selectionMoving$",this._selectionMoving$.asObservable());E(this,"_selectionMoveStart$",new O.Subject);E(this,"selectionMoveStart$",this._selectionMoveStart$.asObservable());E(this,"_selectionMoving",!1);E(this,"_selectionTheme");E(this,"_activeViewport");E(this,"interceptor",new a.InterceptorManager({RANGE_MOVE_PERMISSION_CHECK:Vo,RANGE_FILL_PERMISSION_CHECK:Wo}));E(this,"_escapeShortcutDisposable",null);this._injector=e,this._themeService=t,this._shortcutService=n,this._sheetSkeletonManagerService=o,this.contextService=s,this._initSelectionThemeFromThemeService(),this._initMoving()}get selectionMoving(){return this._selectionMoving}get _shouldDetectMergedCells(){return this._rangeType===a.RANGE_TYPE.NORMAL}_initMoving(){this.disposeWithMe(this._selectionMoving$.subscribe(()=>{this._selectionMoving=!0})),this.disposeWithMe(this._selectionMoveEnd$.subscribe(()=>{this._selectionMoving=!1}))}_setSelectionStyle(e){this._selectionStyle=e}getViewPort(){return this._activeViewport}setSingleSelectionEnabled(e=!1){this._singleSelectionEnabled=e}newSelectionControl(e,t,n){const o=this.getSelectionControls().length,{rowHeaderWidth:s,columnHeaderHeight:l}=t,c=new lr(e,o,this._selectionTheme,{highlightHeader:this._highlightHeader,rowHeaderWidth:s,columnHeaderHeight:l});this._selectionControls.push(c);const d=Me(n,t);return c.updateRangeBySelectionWithCoord(d,t),c.setControlExtension({skeleton:t,scene:e,themeService:this._selectionTheme,injector:this._injector,selectionHooks:{selectionMoveEnd:()=>{this._selectionMoveEnd$.next(this.getSelectionDataWithStyle())}}}),c}resetSelectionsByModelData(e){const t=this.getSelectionControls(),n=this._skeleton;if(!(!t||!n)){for(let o=0,s=e.length;o<s;o++){const l=e[o],c=Me(l,this._skeleton),d=t[o];d?d.updateRangeBySelectionWithCoord(c,n):this.isSelectionEnabled()&&this.newSelectionControl(this._scene,n,l)}e.length<t.length&&t.splice(e.length).forEach(s=>s.dispose())}}refreshSelectionMoveStart(){this._selectionMoveStart$.next(this.getSelectionDataWithStyle())}refreshSelectionMoveEnd(){this._selectionMoveEnd$.next(this.getSelectionDataWithStyle())}_initSelectionThemeFromThemeService(){const e=this._themeService.getCurrentTheme();this._selectionTheme=new a.ThemeService,this._selectionTheme.setTheme(e)}setSelectionTheme(e){this._selectionTheme.setTheme(e)}_changeRuntime(e,t,n){this._skeleton=e,this._scene=t,this._activeViewport=n||(t==null?void 0:t.getViewports()[0]),t.findLayerByZIndex(Jr)||t.addLayer(new yd(t,[],Jr))}getSkeleton(){return this._skeleton}getSelectionDataWithStyle(){const e=this._selectionControls,[t,n]=this._skeleton.getLocation();return e.map(o=>{const s=o.getValue();return s.rangeWithCoord.sheetId=n,s.rangeWithCoord.unitId=t,s})}getSelectionControls(){return this._selectionControls}_addSelectionControlByModelData(e){var l;const t=this._skeleton,n=(l=e.style)!=null?l:bt(this._selectionTheme),o=this._scene;return e.style=n,this.newSelectionControl(o,t,e)}_clearAllSelectionControls(){const e=this._selectionControls;for(const t of e)t.dispose();this._selectionControls.length=0}_getFreeze(){var t;return(t=this._sheetSkeletonManagerService.getCurrentParam())==null?void 0:t.skeleton.getWorksheetConfig().freeze}_getViewportByCell(e,t){if(e===void 0||t===void 0)return null;const n=this._getFreeze();if(!n||n.startRow<=0&&n.startColumn<=0)return this._scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN);if(e>n.startRow&&t>n.startColumn)return this._scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN);if(e<=n.startRow&&t<=n.startColumn)return this._scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP);if(e<=n.startRow&&t>n.startColumn)return this._scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP);if(e>n.startRow&&t<=n.startColumn)return this._scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT)}getActiveRange(){const e=this.getSelectionControls(),t=e&&e[e.length-1].model;return t&&{startRow:t.startRow,startColumn:t.startColumn,endRow:t.endRow,endColumn:t.endColumn}}setActiveSelectionIndex(e){this._activeControlIndex=e}resetActiveSelectionIndex(){this._activeControlIndex=-1}getActiveSelectionControl(){const e=this.getSelectionControls();if(e)return this._activeControlIndex<0?e[e.length-1]:e[this._activeControlIndex]}endSelection(){var e;this._clearUpdatingListeners(),this._selectionMoveEnd$.next(this.getSelectionDataWithStyle()),(e=this._escapeShortcutDisposable)==null||e.dispose(),this._escapeShortcutDisposable=null}_reset(){var e;this._clearAllSelectionControls(),(e=this._downObserver)==null||e.unsubscribe(),this._downObserver=null}_setupPointerMoveListener(e,t,n,o=w.ScrollTimerType.ALL,s,l){var f,p;let c=0,d=0,u=s,h=l;this._scrollTimer=w.ScrollTimer.create(this._scene,o),this._scrollTimer.startScroll((f=e==null?void 0:e.left)!=null?f:0,(p=e==null?void 0:e.top)!=null?p:0,e);const g=this._scene,S=g.getActiveViewportByCoord(w.Vector2.FromArray([s,l]));this._scenePointerMoveSub=g.onPointerMove$.subscribeEvent(C=>{var H,W,k,U,B,F,L,j,z;const{offsetX:v,offsetY:b}=C,{x:R,y:T}=g.getCoordRelativeToViewport(w.Vector2.FromArray([v,b]));this._movingHandler(R,T,t,n);let M=R,y=T;const P=this.getActiveSelectionControl(),x=this._getFreeze(),A=P==null?void 0:P.model,N=(H=g.getActiveViewportByCoord(w.Vector2.FromArray([v,b])))!=null?H:this._getViewportByCell(A==null?void 0:A.endRow,A==null?void 0:A.endColumn);if((()=>{if(!S||!N||!e)return!1;const q=[w.SHEET_VIEWPORT_KEY.VIEW_MAIN,w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP,w.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP,w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT];return q.includes(S.viewportKey)&&q.includes(N.viewportKey)})()){if(!S||!N||!e)return!1;const q=u<e.left&&R>e.left||u>e.left&&R<e.left,te=h<e.top&&T>e.top||h>e.top&&T<e.top;q&&(c+=1),te&&(d+=1);const Q=S.viewportKey,ie=N.viewportKey;if(Q===w.SHEET_VIEWPORT_KEY.VIEW_ROW_TOP)b<e.top&&((W=A==null?void 0:A.endRow)!=null?W:0)<((k=x==null?void 0:x.startRow)!=null?k:0)?y=e.top:te&&d%2===1&&e.scrollToViewportPos({viewportScrollY:0});else if(Q===w.SHEET_VIEWPORT_KEY.VIEW_COLUMN_LEFT)v<e.left&&((U=A==null?void 0:A.endColumn)!=null?U:0)<((B=x==null?void 0:x.startColumn)!=null?B:0)?M=e.left:q&&c%2===1&&e.scrollToViewportPos({viewportScrollX:0});else if(Q===ie){let se=!1,X=!1;Q===w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP?(se=!0,X=!0):Q===w.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP?X=!0:Q===w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT&&(se=!0),((F=A==null?void 0:A.endRow)!=null?F:0)>((L=x==null?void 0:x.startRow)!=null?L:0)&&(X=!1),((j=A==null?void 0:A.endColumn)!=null?j:0)>((z=x==null?void 0:x.startColumn)!=null?z:0)&&(se=!1),se&&(M=e.left),X&&(y=e.top)}else{const se={x:S.scrollX,y:S.scrollY},X={x:N.scrollX,y:N.scrollY},Y=se.x!==X.x&&q&&c%2===1,G=se.y!==X.y&&te&&d%2===1;(Y||G)&&(e.scrollToBarPos({x:Y?se.x:void 0,y:G?se.y:void 0}),Y||(M=e.left),G||(y=e.top)),(Q===w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP&&ie===w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT||ie===w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP&&Q===w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT)&&(M=e.left),(Q===w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP&&ie===w.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP||ie===w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP&&Q===w.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP)&&(y=e.top)}u=R,h=T}this._scrollTimer.scrolling(M,y,()=>{this._movingHandler(R,T,t,n)})})}attachSelectionWithCoord(e){return Me(e,this._skeleton)}attachPrimaryWithCoord(e){return Jn(this._skeleton,e)}getSelectionCellByPosition(e,t){return this.getCellWithCoordByOffset(e,t)}getCellWithCoordByOffset(e,t,n){const o=this._scene,s=n!=null?n:this._skeleton,l=o.getViewportScrollXY(o.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN)),{scaleX:c,scaleY:d}=o.getAncestorScale();return s.getCellWithCoordByOffset(e,t,c,d,l)}_movingHandler(e,t,n,o){var M,y,P,x,A;const s=this._skeleton,l=this._scene,[c,d]=s.getLocation(),u={startRow:(M=n==null?void 0:n.model.startRow)!=null?M:-1,endRow:(y=n==null?void 0:n.model.endRow)!=null?y:-1,startColumn:(P=n==null?void 0:n.model.startColumn)!=null?P:-1,endColumn:(x=n==null?void 0:n.model.endColumn)!=null?x:-1,unitId:c,sheetId:d},h=l.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN),g=(A=this._getViewportByCell(u.endRow,u.endColumn))!=null?A:h,S=l.getScrollXYInfoByViewport(w.Vector2.FromArray([this._startViewportPosX,this._startViewportPosY]),g),{scaleX:f,scaleY:p}=l.getAncestorScale();o===a.RANGE_TYPE.ROW?e=Number.POSITIVE_INFINITY:o===a.RANGE_TYPE.COLUMN&&(t=Number.POSITIVE_INFINITY);const C=this._skeleton.getCellByOffset(e,t,f,p,S);if(!C)return;let v={startRow:Math.min(C.startRow,this._startRangeWhenPointerDown.startRow),startColumn:Math.min(C.startColumn,this._startRangeWhenPointerDown.startColumn),endRow:Math.max(C.endRow,this._startRangeWhenPointerDown.endRow),endColumn:Math.max(C.endColumn,this._startRangeWhenPointerDown.endColumn)};this._shouldDetectMergedCells&&(v=s.expandRangeByMerge(v));const R=Me({range:v,primary:void 0,style:null},s);R.rangeWithCoord.unitId=c,R.rangeWithCoord.sheetId=d,R.rangeWithCoord.rangeType=o;const T=u.startRow!==v.startRow||u.startColumn!==v.startColumn||u.endRow!==v.endRow||u.endColumn!==v.endColumn;n!=null&&T&&(n.updateRangeBySelectionWithCoord(R),this._selectionMoving$.next(this.getSelectionDataWithStyle()))}_clearUpdatingListeners(){var t,n,o,s,l;this._scene.enableObjectsEvent(),(t=this._scenePointerMoveSub)==null||t.unsubscribe(),this._scenePointerMoveSub=null,(n=this._scenePointerUpSub)==null||n.unsubscribe(),this._scenePointerUpSub=null,(o=this._cancelDownSubscription)==null||o.unsubscribe(),this._cancelDownSubscription=null,(s=this._cancelUpSubscription)==null||s.unsubscribe(),this._cancelUpSubscription=null,(l=this._scrollTimer)==null||l.dispose()}_addEndingListeners(){var n,o,s;const e=this._scene,t=(n=e.getEngine())==null?void 0:n.activeScene;!t||t===e||((o=this._cancelDownSubscription)==null||o.unsubscribe(),this._cancelDownSubscription=null,(s=this._cancelUpSubscription)==null||s.unsubscribe(),this._cancelUpSubscription=null,this._cancelDownSubscription=t.onPointerDown$.subscribeEvent(()=>this._clearUpdatingListeners()),this._cancelUpSubscription=t.onPointerUp$.subscribeEvent(()=>this._clearUpdatingListeners()))}_getSelectionWithCoordByOffset(e,t,n,o,s){const l=this._skeleton;if(l==null)return null;if(this._shouldDetectMergedCells){const v=l==null?void 0:l.getCellWithCoordByOffset(e,t,n,o,s,{firstMatch:!0});if(!v)return;const b=a.convertCellToRange(v);return{primaryWithCoord:v,rangeWithCoord:b}}const{row:c,column:d}=l.getCellIndexByOffset(e,t,n,o,s),u=l.getNoMergeCellWithCoordByIndex(c,d),{startX:h,startY:g,endX:S,endY:f}=u,p={startY:g,endY:f,startX:h,endX:S,startRow:c,endRow:c,startColumn:d,endColumn:d};return{primaryWithCoord:{mergeInfo:p,actualRow:c,actualColumn:d,isMerged:!1,isMergedMainCell:!1,startY:g,endY:f,startX:h,endX:S},rangeWithCoord:p}}_checkClearPreviousControls(e){this.getSelectionControls().length!==0&&(!e.ctrlKey&&!e.shiftKey&&!this._remainLastEnabled||this._singleSelectionEnabled&&!e.shiftKey)&&this._clearAllSelectionControls()}_makeSelectionByTwoCells(e,t,n,o,s){const{actualRow:l,actualColumn:c,mergeInfo:d}=e;this._startRangeWhenPointerDown={...e.mergeInfo};const u=Math.min(l,t.startRow,d.startRow),h=Math.max(l,t.endRow,d.endRow),g=Math.min(c,t.startColumn,d.startColumn),S=Math.max(c,t.endColumn,d.endColumn),p={range:n.expandRangeByMerge({startRow:u,startColumn:g,endRow:h,endColumn:S}),primary:null,style:null},C=Me(p,n);s.updateRangeBySelectionWithCoord(C)}isSelectionEnabled(){return this.contextService.getContextValue(m.SELECTIONS_ENABLED)}isSelectionDisabled(){return this.contextService.getContextValue(m.SELECTIONS_ENABLED)===!1}inRefSelectionMode(){return this.contextService.getContextValue(m.REF_SELECTIONS_ENABLED)}}function Ss(r){return{range:{startRow:0,startColumn:0,endRow:r.getRowCount()-1,endColumn:r.getColumnCount()-1,rangeType:a.RANGE_TYPE.ALL},primary:Oi(r).primary,style:null}}function Oi(r){return Qa(r,{startRow:0,startColumn:0,endRow:0,endColumn:0})}function Qa(r,i){const e=r.worksheet.getCellInfoInMergeData(i.startRow,i.startColumn),t=r.worksheet.getCellInfoInMergeData(i.endRow,i.endColumn);return{range:{startRow:e.startRow,startColumn:e.startColumn,endRow:t.endRow,endColumn:t.endColumn,rangeType:a.RANGE_TYPE.NORMAL},primary:{actualRow:e.startRow,actualColumn:e.startColumn,startRow:e.startRow,startColumn:e.startColumn,endRow:e.endRow,endColumn:e.endColumn,isMerged:e.isMerged,isMergedMainCell:e.isMergedMainCell},style:null}}var Md=Object.defineProperty,Pd=Object.getOwnPropertyDescriptor,Od=(r,i,e,t)=>{for(var n=t>1?void 0:t?Pd(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&Md(i,e,n),n},Zs=(r,i)=>(e,t)=>i(e,t,r);exports.SheetSkeletonManagerService=class extends a.Disposable{constructor(e,t,n){super();E(this,"_sheetId","");E(this,"_sheetSkeletonParamStore",new Map);E(this,"_currentSkeleton$",new O.BehaviorSubject(null));E(this,"currentSkeleton$",this._currentSkeleton$.asObservable());E(this,"_currentSkeletonBefore$",new O.BehaviorSubject(null));E(this,"currentSkeletonBefore$",this._currentSkeletonBefore$.asObservable());this._context=e,this._injector=t,this._sheetSkService=n,this.disposeWithMe(()=>{this._currentSkeletonBefore$.complete(),this._currentSkeleton$.complete(),this._sheetSkeletonParamStore.forEach(o=>this.disposeSkeleton(o.sheetId)),this._sheetSkeletonParamStore.clear()}),this._initRemoveSheet()}_initRemoveSheet(){this.disposeWithMe(this._context.unit.sheetDisposed$.subscribe(e=>{this.disposeSkeleton(e.getSheetId())}))}getCurrentSkeleton(){var e;return(e=this.getCurrentParam())==null?void 0:e.skeleton}getCurrent(){return this.getCurrentParam()}getCurrentParam(){return this._getSkeletonParam(this._sheetId)}getSkeleton(e){return this._getSkeleton(e)}getSkeletonParam(e){return this._getSkeletonParam(e)}getWorksheetSkeleton(e){return this.getSkeletonParam(e)}getUnitSkeleton(e,t){const n=this._getSkeletonParam(t);return n!=null&&(n.unitId=e),n}setCurrent(e){this._setCurrent(e.sheetId)}setSkeletonParam(e,t){this._sheetSkService.setSkeleton(t.unitId,e,t.skeleton),this._sheetSkeletonParamStore.set(e,t)}_setCurrent(e){this._sheetId=e;const t=this._getSkeletonParam(e),n=this._context.unitId;if(t!=null)this.reCalculate(t);else{const l=this._context.unit.getSheetBySheetId(e);if(l==null)return;const c=this._context.scene,d=this._buildSkeleton(l,c);this.setSkeletonParam(e,{unitId:n,sheetId:e,skeleton:d,dirty:!1})}const o=this._getSkeletonParam(e);this._currentSkeletonBefore$.next(o),this._currentSkeleton$.next(o)}reCalculate(e){e||(e=this.getCurrentParam()),e!=null&&(e.dirty&&(e.skeleton.makeDirty(!0),e.dirty=!1),e.skeleton.calculate())}makeDirty(e,t=!0){const n=this._getSkeletonParam(e.sheetId);n!=null&&(n.dirty=t)}getOrCreateSkeleton(e){return this.ensureSkeleton(e.sheetId)}ensureSkeleton(e){const t=this._getSkeletonParam(e);if(t)return t.skeleton;const o=this._context.unit.getSheetBySheetId(e);if(!o)return;const s=this._buildSkeleton(o);return this.setSkeletonParam(e,{unitId:this._context.unitId,sheetId:e,skeleton:s,dirty:!1}),s}disposeSkeleton(e){const t=this.getSkeletonParam(e);t&&(t.skeleton.dispose(),this._sheetSkeletonParamStore.delete(e),this._sheetSkService.deleteSkeleton(t.unitId,e))}attachRangeWithCoord(e){const t=this.getCurrentSkeleton();return t?bn(t,e):null}_getSkeletonParam(e){return this._sheetSkeletonParamStore.get(e)}_getSkeleton(e){const t=this._getSkeletonParam(e);return t?t.skeleton:null}_buildSkeleton(e,t){return this._injector.createInstance(w.SpreadsheetSkeleton,e,this._context.unit.getStyles())}setColumnHeaderSize(e,t,n){var u;if(!e)return;const o=(u=this.getWorksheetSkeleton(t))==null?void 0:u.skeleton;if(!o)return;o.columnHeaderHeight=n,e.scene.getViewports()[0].top=n,e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_COLUMN_RIGHT).setViewportSize({height:n}),e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_COLUMN_LEFT).setViewportSize({height:n}),e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_ROW_BOTTOM).setViewportSize({top:n}),e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_ROW_TOP).setViewportSize({top:n}),e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_LEFT_TOP).setViewportSize({height:n});const s=e==null?void 0:e.with(m.SheetsSelectionsService),l=e==null?void 0:e.with(Ce),c=s.getCurrentSelections();l.resetSelectionsByModelData(c);const d=this.getUnitSkeleton(e.unitId,t);d&&(d.commandId=Pi.id,this._currentSkeleton$.next(d))}setRowHeaderSize(e,t,n){var f;const o=(f=this.getWorksheetSkeleton(t))==null?void 0:f.skeleton;if(!e||!o)return;o.rowHeaderWidth=n;const s=e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_LEFT_TOP).width||46,l=n-s,c=e.scene.getViewports()[0].left;e.scene.getViewports()[0].left=c+l,e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_ROW_BOTTOM).setViewportSize({width:n}),e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_ROW_TOP).setViewportSize({width:n}),e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_COLUMN_LEFT).setViewportSize({left:n});const d=e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_COLUMN_RIGHT).left||0;e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_COLUMN_RIGHT).setViewportSize({left:d+l}),e.scene.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_LEFT_TOP).setViewportSize({width:n});const u=e==null?void 0:e.with(m.SheetsSelectionsService),h=e==null?void 0:e.with(Ce),g=u.getCurrentSelections();h.resetSelectionsByModelData(g);const S=this.getCurrent();S&&(S.commandId=Mi.id,this._currentSkeleton$.next(S))}};exports.SheetSkeletonManagerService=Od([Zs(1,a.Inject(a.Injector)),Zs(2,a.Inject(m.SheetSkeletonService))],exports.SheetSkeletonManagerService);function ht(){const r=_.useDependency(a.IUniverInstanceService),i=_.useObservable(()=>r.getCurrentTypeOfUnit$(a.UniverInstanceType.UNIVER_SHEET),void 0,void 0,[]);return i!=null?i:null}function xd(r){return _.useObservable(()=>{var e;return(e=r==null?void 0:r.activeSheet$)!=null?e:O.of(null)},void 0,void 0,[r])}function Ad(){const r=_.useDependency(a.IUniverInstanceService);return _.useObservable(()=>O.merge([r.getTypeOfUnitAdded$(a.UniverInstanceType.UNIVER_SHEET),r.getTypeOfUnitDisposed$(a.UniverInstanceType.UNIVER_SHEET)]).pipe(O.map(()=>r.getAllUnitsForType(a.UniverInstanceType.UNIVER_SHEET)),O.startWith(r.getAllUnitsForType(a.UniverInstanceType.UNIVER_SHEET))),[],void 0,[r])}const Nd="SheetUI";var fs=(r=>(r.FILTER_PANEL_EMBED_POINT="filter-panel-embed-point",r.SHEETS_FOOTER="sheets-footer",r.FORMULA_AUX="formula-aux",r))(fs||{}),Dd=Object.defineProperty,Hd=Object.getOwnPropertyDescriptor,kd=(r,i,e,t)=>{for(var n=t>1?void 0:t?Hd(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&Dd(i,e,n),n},kr=(r,i)=>(e,t)=>i(e,t,r);const ps=["ff","fs","tr","tb"];exports.AutoHeightController=class extends a.Disposable{constructor(i,e,t,n){super(),this._renderManagerService=i,this._sheetInterceptorService=e,this._selectionManagerService=t,this._univerInstanceService=n,this._initialize()}getUndoRedoParamsOfAutoHeight(i){const{_univerInstanceService:e}=this,t=e.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET),n=t.getActiveSheet(),o=t.getUnitId(),s=n.getSheetId(),l=this._renderManagerService.getRenderById(o).with(exports.SheetSkeletonManagerService);if(!s||!l.getCurrentParam())return{redos:[],undos:[]};const{skeleton:c}=l.getCurrentParam(),d=c.calculateAutoHeightInRange(i),u={subUnitId:s,unitId:o,rowsAutoHeightInfo:d},h=m.SetWorksheetRowAutoHeightMutationFactory(u,n);return{undos:[{id:m.SetWorksheetRowAutoHeightMutation.id,params:h}],redos:[{id:m.SetWorksheetRowAutoHeightMutation.id,params:u}]}}_initialize(){const{_sheetInterceptorService:i,_selectionManagerService:e}=this;this.disposeWithMe(i.interceptCommand({getMutations:t=>{if(t.id===m.SetRangeValuesCommand.id){const n=t.params;return this.getUndoRedoParamsOfAutoHeight(n.range)}return{redos:[],undos:[]}}})),this.disposeWithMe(i.interceptCommand({getMutations:t=>t.id!==m.SetWorksheetRowIsAutoHeightCommand.id?{redos:[],undos:[]}:this.getUndoRedoParamsOfAutoHeight(t.params.ranges)})),this.disposeWithMe(i.interceptCommand({getMutations:t=>{var o,s;if(t.id!==m.SetStyleCommand.id)return{redos:[],undos:[]};if(!ps.includes((o=t.params)==null?void 0:o.style.type))return{redos:[],undos:[]};const n=(s=e.getCurrentSelections())==null?void 0:s.map(l=>l.range);return n!=null&&n.length?this.getUndoRedoParamsOfAutoHeight(n):{redos:[],undos:[]}}})),this.disposeWithMe(i.interceptAfterCommand({getMutations:t=>{if(t.id===m.MoveRangeCommand.id){const n=t.params;return this.getUndoRedoParamsOfAutoHeight([n.fromRange,n.toRange])}if(t.id===m.ReorderRangeCommand.id){const n=t.params;return this.getUndoRedoParamsOfAutoHeight([n.range])}return{redos:[],undos:[]}}}))}};exports.AutoHeightController=kd([kr(0,w.IRenderManagerService),kr(1,a.Inject(m.SheetInterceptorService)),kr(2,a.Inject(m.SheetsSelectionsService)),kr(3,a.Inject(a.IUniverInstanceService))],exports.AutoHeightController);function Sn(r,i,e,t){const n=i.get(a.IUniverInstanceService),o=e?n.getUnit(e,a.UniverInstanceType.UNIVER_SHEET):n.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET),s=t?o==null?void 0:o.getSheetBySheetId(t):o==null?void 0:o.getActiveSheet();if(!s)return null;const{startRow:l,endRow:c,startColumn:d,endColumn:u}=r,h=[],g=[];for(let S=l;S<=c;S++)s.getRowFiltered(S)||h.push(S);for(let S=d;S<=u;S++)g.push(S);return{rows:h,cols:g}}function Rt(r){const{rows:i,cols:e}=r;return{startRow:i[0],endRow:i[i.length-1],startColumn:e[0],endColumn:e[e.length-1]}}function Kt(r){let i=[],e=[];const t=[];return r.forEach(n=>{i=i.concat(n.rows),e=e.concat(n.cols)}),i=Array.from(new Set(i)).sort((n,o)=>n-o),e=Array.from(new Set(e)).sort((n,o)=>n-o),r.forEach(n=>{t.push({startRow:i.findIndex(o=>o===n.rows[0]),endRow:i.findIndex(o=>o===n.rows[n.rows.length-1]),startColumn:e.findIndex(o=>o===n.cols[0]),endColumn:e.findIndex(o=>o===n.cols[n.cols.length-1])})}),{ranges:t,mapFunc:(n,o)=>({row:i[n],col:e[o]})}}function Vd(r){const i=new a.ObjectMatrix;return r.forEach(e=>{const{rows:t,cols:n}=e;t.forEach(o=>{n.forEach(s=>{i.setValue(o,s,{v:null,p:null,f:null,si:null,custom:null})})})}),i.getData()}const Lo={零:0,一:1,二:2,三:3,四:4,五:5,六:6,七:7,八:8,九:9},$r=["零","一","二","三","四","五","六","七","八","九"],Ks=["","万","亿","万亿","亿亿"],Wd=["","十","百","千"],Ja={十:{value:10,secUnit:!1},百:{value:100,secUnit:!1},千:{value:1e3,secUnit:!1},万:{value:1e4,secUnit:!0},亿:{value:1e8,secUnit:!0}};function ct(r){if(!r)return 0;let i=0,e=0,t=0,n=!1;const o=r.split("");for(let s=0;s<o.length;s++){const l=Lo[o[s]];if(typeof l<"u")t=l,s===o.length-1&&(e+=t);else{const c=Ja[o[s]],d=c.value;n=c.secUnit,n?(e=(e+t)*d,i+=e,e=0):e+=t*d,t=0}}return i+e}function Ud(r){let i="",e="",t=0,n=!0;for(;r>0;){const o=r%10;o===0?n||(n=!0,e=$r[o]+e):(n=!1,i=$r[o],i+=Wd[t],e=i+e),t++,r=Math.floor(r/10)}return e}function Ld(r){let i=0,e="",t="",n=!1;if(r===0)return $r[0];for(;r>0;){const o=r%1e4;n&&(t=$r[0]+t),e=Ud(o),e+=o!==0?Ks[i]:Ks[0],t=e+t,n=o<1e3&&o>0,r=Math.floor(r/1e4),i++}return t}function Bd(r){if(!r)return!1;let i=!0;if(r)if(r.length===1)r==="日"||r in Lo?i=!0:i=!1;else{const e=r.split("");for(let t=0;t<e.length;t++)if(!(e[t]in Lo||e[t]in Ja)){i=!1;break}}return i}function Vr(r){if(!r)return{isExtendNumber:!1};const i=/0|([1-9]+[0-9]*)/g,e=i.test(r);if(e){const t=r.match(i);if(t&&t.length>0){const n=t[t.length-1],o=r.lastIndexOf(n),s=r.substr(0,o),l=r.substr(o+n.length);return{isExtendNumber:!0,matchTxt:Number(n),beforeTxt:s,afterTxt:l}}return{isExtendNumber:!1}}return{isExtendNumber:e}}function Fd(r){let i;return r.length===2&&(r==="周一"||r==="周二"||r==="周三"||r==="周四"||r==="周五"||r==="周六"||r==="周日")?i=!0:i=!1,i}function jd(r){let i;return r.length===3&&(r==="星期一"||r==="星期二"||r==="星期三"||r==="星期四"||r==="星期五"||r==="星期六"||r==="星期日")?i=!0:i=!1,i}function zd(r,i){let e=0;for(let t=0;t<r.length&&r[t]<i;t++)e++;return e}function cr(r){let i=!0;const e=r[1]-r[0];for(let t=1;t<r.length;t++)if(r[t]-r[t-1]!==e){i=!1;break}return i}function Xd(r,i,e){const t=[],n=Math.floor(i/r),o=i%r;let s=0;if(n>0){for(let l=0;l<n;l++)for(let c=0;c<e.length;c++)t[e[c]+l*r]=s,s++;for(let l=0;l<e.length&&e[l]<o;l++)t[e[l]+r*n]=s,s++}else for(let l=0;l<e.length&&e[l]<o;l++)t[e[l]]=s,s++;return t}function Ft(r,i){const e=[];for(let t=1;t<=i;t++){const n=(t-1)%r.length,o=a.Tools.deepClone(r[n]);jt(o),e.push({v:null,s:null,p:null,f:null,si:null,t:null,...o})}return e}function Yd(r,i){var t;const e=[];for(let n=1;n<=i;n++){const o=(n-1)%r.length,s={s:(t=r[o])==null?void 0:t.s};e.push(s)}return e}function Gd(r){let i=!0;const e=r[1]/r[0];for(let t=1;t<r.length;t++)if(r[t]/r[t-1]!==e){i=!1;break}return i}function Zd(r){const i=[];for(let e=1;e<=r;e++)i.push(e);return i}function ei(r,i,e){var o,s,l,c;const t=[],n=[];for(let d=0;d<r.length;d++)n.push(Number((o=r[d])==null?void 0:o.v));if(r.length>2&&Gd(n))for(let d=1;d<=i;d++){const u=(d-1)%r.length,h=a.Tools.deepClone(r[u]);jt(h);const g=Number((s=r[r.length-1])==null?void 0:s.v)*(Number((l=r[1])==null?void 0:l.v)/Number((c=r[0])==null?void 0:c.v))**d;h&&($s(h)&&(h.v=g),t.push(h))}else{const d=Zd(r.length);for(let u=1;u<=i;u++){const h=(u-1)%r.length,g=a.Tools.deepClone(r[h]);jt(g);const S=e===a.Direction.DOWN||e===a.Direction.RIGHT,f=Kd(r.length+u,n,d,S);g&&($s(g)&&(g.v=f),t.push(g))}}return t}function Kd(r,i,e,t=!0){function n(h){let g=0;for(let S=0;S<h.length;S++)g+=h[S];return g/h.length}const o=n(e),s=n(i);let l=0,c=0;for(let h=0;h<e.length;h++)l+=(e[h]-o)*(i[h]-s),c+=(e[h]-o)*(e[h]-o);let d;c===0?d=t?1:-1:d=l/c;const u=s-d*o;return Math.round((u+d*r)*1e5)/1e5}function qs(r,i,e){var o;const t=[],n=/0|([1-9]+[0-9]*)/g;for(let s=1;s<=i;s++){const l=(s-1)%r.length,c=a.Tools.deepClone(r[l]);jt(c);const d=`${(o=r[r.length-1])==null?void 0:o.v}`,u=d==null?void 0:d.match(n),h=u==null?void 0:u[u.length-1],g=Math.abs(Number(h)+e*s);if(!d||!h)continue;const S=d.lastIndexOf(h),f=d.substr(0,S)+g.toString()+d.substr(S+h.length);c&&(c.v=f,t.push(c))}return t}function Rn(r,i,e,t=0){var l,c;const n=[["日","一","二","三","四","五","六"],["周日","周一","周二","周三","周四","周五","周六"],["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]];if(t>=n.length)return[];const o=n[t],s=[];for(let d=1;d<=i;d++){const u=(d-1)%r.length,h=a.Tools.deepClone(r[u]);jt(h);let g=0;if(((l=r[r.length-1])==null?void 0:l.v)===o[0])g=7+e*d;else{const f=`${(c=r[r.length-1])==null?void 0:c.v}`;if(f){const p=f.substr(f.length-1,1);g=ct(p)+e*d}}g<0&&(g=Math.ceil(Math.abs(g)/7)*7+g);const S=g%7;h&&(h.v=o[S],s.push(h))}return s}function Qs(r,i,e){var n;const t=[];for(let o=1;o<=i;o++){const s=(o-1)%r.length,l=a.Tools.deepClone(r[s]);jt(l);const c=`${(n=r[r.length-1])==null?void 0:n.v}`,d=ct(c)+e*o;let u;d<=0?u="零":u=Ld(d),l&&(l.v=u,t.push(l))}return t}const Kn={enWeek1:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],enWeek2:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],enMonth1:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],enMonth2:["January","February","March","April","May","June","July","August","September","October","November","December"],chnMonth1:["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],chnMonth2:["正月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","腊月"],chHour1:["子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"],chHour2:["子时","丑时","寅时","卯时","辰时","巳时","午时","未时","申时","酉时","戌时","亥时"],chYear1:["甲","乙","丙","丁","戊","己","庚","辛","壬","癸"],chSeason1:["春","夏","秋","冬"],chSeason2:["春季","夏季","秋季","冬季"]};function qd(r){let i=!1;return Object.keys(Kn).forEach(e=>{Kn[e].includes(r)&&(i=!0)}),i}function zi(r){let i="";const e=[];return Object.keys(Kn).forEach(t=>{Kn[t].includes(r)&&(i=t,e.push(...Kn[t]))}),{name:i,series:e}}function Js(r,i,e,t){var s;const n=t.length,o=[];for(let l=1;l<=i;l++){const c=(l-1)%r.length,d=a.Tools.deepClone(r[c]);jt(d);const u=`${(s=r[r.length-1])==null?void 0:s.v}`;let h=t.indexOf(u)+e*l;h<0&&(h+=Math.abs(e)*n);const g=h%n;d&&(d.v=t[g],o.push(d))}return o}function Qd(r,i){const e=[];let t;if(i.startRow<r.startRow)t=a.Direction.UP;else if(i.endRow>r.endRow)t=a.Direction.DOWN;else if(i.startColumn<r.startColumn)t=a.Direction.LEFT;else if(i.endColumn>r.endColumn)t=a.Direction.RIGHT;else return[];if(t===a.Direction.DOWN||t===a.Direction.UP){const n=r.endRow-r.startRow+1,o=i.endRow-i.startRow+1,s=Math.floor(o/n),l=o%n,c={startRow:0,startColumn:0,endRow:r.endRow-r.startRow,endColumn:r.endColumn-r.startColumn};if(t===a.Direction.DOWN){for(let d=0;d<s;d++)e.push({repeatStartCell:{row:r.startRow+(d+1)*n,col:r.startColumn},relativeRange:c});l>0&&e.push({repeatStartCell:{row:r.startRow+(s+1)*n,col:r.startColumn},relativeRange:{startRow:0,startColumn:0,endRow:l-1,endColumn:r.endColumn-r.startColumn}})}else{for(let d=0;d<s;d++)e.push({repeatStartCell:{row:r.startRow-(d+1)*n,col:r.startColumn},relativeRange:c});l>0&&e.push({repeatStartCell:{row:r.startRow-(s+1)*n,col:r.startColumn},relativeRange:{startRow:n-l,endRow:n-1,startColumn:0,endColumn:r.endColumn-r.startColumn}})}}if(t===a.Direction.RIGHT||t===a.Direction.LEFT){const n=r.endColumn-r.startColumn+1,o=i.endColumn-i.startColumn+1,s=Math.floor(o/n),l=o%n,c={startRow:0,startColumn:0,endRow:r.endRow-r.startRow,endColumn:r.endColumn-r.startColumn};if(t===a.Direction.RIGHT){for(let d=0;d<s;d++)e.push({repeatStartCell:{row:r.startRow,col:r.startColumn+(d+1)*n},relativeRange:c});l>0&&e.push({repeatStartCell:{row:r.startRow,col:r.startColumn+(s+1)*n},relativeRange:{startRow:0,startColumn:0,endRow:r.endRow-r.startRow,endColumn:l-1}})}else{for(let d=0;d<s;d++)e.push({repeatStartCell:{row:r.startRow,col:r.startColumn-(d+1)*n},relativeRange:c});l>0&&e.push({repeatStartCell:{row:r.startRow,col:r.startColumn-(s+1)*n},relativeRange:{startRow:0,startColumn:n-l,endRow:r.endRow-r.startRow,endColumn:n-1}})}}return e}function $s(r){return!(a.isFormulaString(r.f)||a.isFormulaId(r.si)||r.t===a.CellValueType.BOOLEAN)}function jt(r){r&&"custom"in r&&delete r.custom}var mn=(r=>(r.Append="APPEND",r.Default="DEFAULT",r.Only="ONLY",r))(mn||{}),Ie=(r=>(r.NUMBER="number",r.DATE="date",r.EXTEND_NUMBER="extendNumber",r.CHN_NUMBER="chnNumber",r.CHN_WEEK2="chnWeek2",r.CHN_WEEK3="chnWeek3",r.LOOP_SERIES="loopSeries",r.FORMULA="formula",r.OTHER="other",r))(Ie||{}),le=(r=>(r.COPY="COPY",r.SERIES="SERIES",r.ONLY_FORMAT="ONLY_FORMAT",r.NO_FORMAT="NO_FORMAT",r))(le||{});const Jd={type:Ie.DATE,priority:1100,match:(r,i)=>{var e;if(r!=null&&r.f||r!=null&&r.si)return!1;if((typeof(r==null?void 0:r.v)=="number"||(r==null?void 0:r.t)===a.CellValueType.NUMBER)&&r.s){if(typeof r.s=="string"){const n=i.get(a.IUniverInstanceService).getFocusedUnit().getStyles().get(r.s),o=(e=n==null?void 0:n.n)==null?void 0:e.pattern;if(o)return a.numfmt.getInfo(o).isDate}else if(r.s.n&&a.numfmt.getInfo(r.s.n.pattern).isDate)return!0}return!1},isContinue:(r,i)=>r.type===Ie.DATE,applyFunctions:{[le.SERIES]:(r,i,e)=>{const{data:t}=r;return e===a.Direction.LEFT||e===a.Direction.UP?(t.reverse(),ei(t,i,e).reverse()):ei(t,i,e)}}},$d={type:Ie.NUMBER,priority:1e3,match:r=>typeof(r==null?void 0:r.v)=="number"||(r==null?void 0:r.t)===a.CellValueType.NUMBER,isContinue:(r,i)=>r.type===Ie.NUMBER,applyFunctions:{[le.SERIES]:(r,i,e)=>{const{data:t}=r;return e===a.Direction.LEFT||e===a.Direction.UP?(t.reverse(),ei(t,i,e).reverse()):ei(t,i,e)}}},$a={type:Ie.OTHER,priority:0,match:()=>!0,isContinue:(r,i)=>r.type===Ie.OTHER},eu={type:Ie.EXTEND_NUMBER,priority:900,match:r=>Vr(`${r==null?void 0:r.v}`||"").isExtendNumber,isContinue:(r,i)=>{var e;if(r.type===Ie.EXTEND_NUMBER){const{beforeTxt:t,afterTxt:n}=Vr(`${(e=r.cellData)==null?void 0:e.v}`||""),{beforeTxt:o,afterTxt:s}=Vr(`${i==null?void 0:i.v}`||"");if(t===o&&n===s)return!0}return!1},applyFunctions:{[le.SERIES]:(r,i,e)=>{var l;const{data:t}=r,n=e===a.Direction.UP||e===a.Direction.LEFT;let o;if(t.length===1)return o=n?-1:1,Ue(qs(t,i,o),n);const s=[];for(let c=0;c<t.length;c++){const d=`${(l=t[c])==null?void 0:l.v}`;d&&s.push(Number(Vr(d).matchTxt))}return n&&(t.reverse(),s.reverse()),cr(s)?(o=s[1]-s[0],Ue(qs(t,i,o),n)):Ft(t,i)}}},tu={type:Ie.CHN_NUMBER,priority:830,match:r=>!!Bd(`${r==null?void 0:r.v}`||""),isContinue:(r,i)=>r.type===Ie.CHN_NUMBER,applyFunctions:{[le.SERIES]:(r,i,e)=>{var c,d,u;const{data:t}=r,n=e===a.Direction.LEFT||e===a.Direction.UP;if(t.length===1){const h=`${(c=t[0])==null?void 0:c.v}`;let g;return n?g=-1:g=1,h&&(h==="日"||ct(h)<7)?Ue(Rn(t,i,g),n):Ue(Qs(t,i,g),n)}let o=!1;for(let h=0;h<t.length;h++)if(((d=t[h])==null?void 0:d.v)==="日"){o=!0;break}const s=[];let l=0;for(let h=0;h<t.length;h++){const g=`${(u=t[h])==null?void 0:u.v}`;g==="日"?h===0?s.push(0):(l++,s.push(l*7)):o&&ct(g)>0&&ct(g)<7?s.push(ct(g)+l*7):s.push(ct(g))}if(n&&(t.reverse(),s.reverse()),cr(s)){if(o||s[s.length-1]<6&&s[0]>0||s[0]<6&&s[s.length-1]>0){const g=s[1]-s[0];return Ue(Rn(t,i,g),n)}const h=s[1]-s[0];return Ue(Qs(t,i,h),n)}return Ft(t,i)}}},nu={type:Ie.CHN_WEEK2,priority:820,match:r=>!!Fd(`${r==null?void 0:r.v}`||""),isContinue:(r,i)=>r.type===Ie.CHN_WEEK2,applyFunctions:{[le.SERIES]:(r,i,e)=>{var l;const{data:t}=r,n=e===a.Direction.LEFT||e===a.Direction.UP;if(t.length===1){let c;return n?c=-1:c=1,Ue(Rn(t,i,c,1),n)}const o=[];let s=0;for(let c=0;c<t.length;c++){const d=`${(l=t[c])==null?void 0:l.v}`,u=d==null?void 0:d.substr(d.length-1,1);d==="周日"?c===0?o.push(0):(s++,o.push(s*7)):o.push(ct(u)+s*7)}if(n&&(t.reverse(),o.reverse()),cr(o)){const c=o[1]-o[0];return Ue(Rn(t,i,c,1),n)}return Ft(t,i)}}},ru={type:Ie.CHN_WEEK3,priority:810,match:r=>jd(`${r==null?void 0:r.v}`||""),isContinue:(r,i)=>r.type===Ie.CHN_WEEK3,applyFunctions:{[le.SERIES]:(r,i,e)=>{var l;const{data:t}=r,n=e===a.Direction.LEFT||e===a.Direction.UP;if(t.length===1){let c;return n?c=-1:c=1,Ue(Rn(t,i,c,2),n)}const o=[];let s=0;for(let c=0;c<t.length;c++){const d=`${(l=t[c])==null?void 0:l.v}`;if(d){const u=d.substr(d.length-1,1);d==="星期日"?c===0?o.push(0):(s++,o.push(s*7)):o.push(ct(u)+s*7)}}if(n&&(t.reverse(),o.reverse()),cr(o)){const c=o[1]-o[0];return Ue(Rn(t,i,c,2),n)}return Ft(t,i)}}},iu={type:Ie.LOOP_SERIES,priority:800,match:r=>qd(`${r==null?void 0:r.v}`||""),isContinue:(r,i)=>{var e;return r.type===Ie.LOOP_SERIES?zi(`${(e=r.cellData)==null?void 0:e.v}`||"").name===zi(`${i==null?void 0:i.v}`||"").name:!1},applyFunctions:{[le.SERIES]:(r,i,e)=>{var c,d;const{data:t}=r,n=e===a.Direction.LEFT||e===a.Direction.UP,{series:o}=zi(`${(c=t[0])==null?void 0:c.v}`||"");if(t.length===1){let u;return n?u=-1:u=1,Ue(Js(t,i,u,o),n)}const s=[];let l=0;for(let u=0;u<t.length;u++){const h=`${(d=t[u])==null?void 0:d.v}`;h&&(h===o[0]?u===0?s.push(0):(l++,s.push(l*o.length)):s.push(o.indexOf(h)+l*7))}if(n&&(t.reverse(),s.reverse()),cr(s)){const u=s[1]-s[0];return Ue(Js(t,i,u,o),n)}return Ft(t,i)}}};function Ue(r,i){return i?r.reverse():r}var ou=Object.defineProperty,su=Object.getOwnPropertyDescriptor,au=(r,i,e,t)=>{for(var n=t>1?void 0:t?su(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&ou(i,e,n),n},on=(r,i)=>(e,t)=>i(e,t,r);exports.AutoFillService=class extends a.Disposable{constructor(e,t,n,o,s,l){super();E(this,"_rules",[]);E(this,"_hooks",[]);E(this,"_applyType$",new O.BehaviorSubject(le.SERIES));E(this,"_isFillingStyle",!0);E(this,"_prevUndos",[]);E(this,"_autoFillLocation$",new O.BehaviorSubject(null));E(this,"autoFillLocation$",this._autoFillLocation$.asObservable());E(this,"_showMenu$",new O.BehaviorSubject(!1));E(this,"showMenu$",this._showMenu$.asObservable());E(this,"_direction",a.Direction.DOWN);E(this,"applyType$",this._applyType$.asObservable());E(this,"_menu$",new O.BehaviorSubject([{label:"autoFill.copy",value:le.COPY,disable:!1},{label:"autoFill.series",value:le.SERIES,disable:!1},{label:"autoFill.formatOnly",value:le.ONLY_FORMAT,disable:!1},{label:"autoFill.noFormat",value:le.NO_FORMAT,disable:!1}]));E(this,"menu$",this._menu$.asObservable());this._commandService=e,this._undoRedoService=t,this._renderManagerService=n,this._univerInstanceService=o,this._selectionManagerService=s,this._injector=l,this._init()}_init(){this._rules=[Jd,$d,eu,tu,nu,ru,iu,$a].sort((e,t)=>t.priority-e.priority),this._isFillingStyle=!0}_getOneByPriority(e){return e.length<=0?[]:[e.reduce((n,o)=>(o.priority||0)>(n.priority||0)?o:n,e[0])]}_initPrevUndo(){this._prevUndos=[]}async triggerAutoFill(e,t,n,o){var S;if(n.startColumn===o.startColumn&&n.startRow===o.startRow&&n.endColumn===o.endColumn&&n.endRow===o.endRow)return!1;if(o.endColumn<n.endColumn&&o.endColumn>n.startColumn)return this._commandService.executeCommand(En.id,{clearRange:{startRow:o.startRow,endRow:o.endRow,startColumn:o.endColumn+1,endColumn:n.endColumn},selectionRange:o});if(o.endRow<n.endRow&&o.endRow>n.startRow)return this._commandService.executeCommand(En.id,{clearRange:{startRow:o.endRow+1,endRow:n.endRow,startColumn:o.startColumn,endColumn:o.endColumn},selectionRange:o});const s={startRow:o.startRow,endRow:o.endRow,startColumn:o.startColumn,endColumn:o.endColumn};let l=null;if(o.startRow<n.startRow)l=a.Direction.UP,s.endRow=n.startRow-1;else if(o.endRow>n.endRow)l=a.Direction.DOWN,s.startRow=n.endRow+1;else if(o.startColumn<n.startColumn)l=a.Direction.LEFT,s.endColumn=n.startColumn-1;else if(o.endColumn>n.endColumn)l=a.Direction.RIGHT,s.startColumn=n.endColumn+1;else return!1;this.direction=l;const c=this._injector.invoke(f=>Sn(n,f)),d=this._injector.invoke(f=>Sn(s,f));if(!c||!d)return!1;this.autoFillLocation={source:c,target:d,unitId:e,subUnitId:t};const u=[];this.getActiveHooks().forEach(f=>{var C;const p=(C=f==null?void 0:f.onBeforeFillData)==null?void 0:C.call(f,{source:c,target:d,unitId:e,subUnitId:t},l);p&&u.unshift(p)}),this._initPrevUndo();for(let f=0;f<u.length;f++){const p=this.menu.find(C=>C.value===u[f]);if(p&&!p.disable)return this.fillData(u[f])}const g=(S=this.menu.find(f=>f.disable===!1))==null?void 0:S.value;return g?this.fillData(g):!1}addHook(e){if(this._hooks.find(t=>t.id===e.id))throw new Error(`Add hook failed, hook id '${e.id}' already exist!`);return e.priority===void 0&&(e.priority=0),e.type===void 0&&(e.type=mn.Append),this._hooks.push(e),a.toDisposable(()=>{const t=this._hooks.findIndex(n=>n===e);t>-1&&this._hooks.splice(t,1)})}registerRule(e){if(this._rules.find(n=>n.type===e.type))throw new Error(`Registry rule failed, type '${e.type}' already exist!`);const t=this._rules.findIndex(n=>n.priority<e.priority);this._rules.splice(t===-1?this._rules.length:t,0,e)}getRules(){return this._rules}getAllHooks(){return this._hooks}getActiveHooks(){const{source:e,target:t,unitId:n,subUnitId:o}=this.autoFillLocation||{};if(!e||!t||!n||!o)return[];const s=this._hooks.filter(u=>{var h;return!((h=u.disable)!=null&&h.call(u,{source:e,target:t,unitId:n,subUnitId:o},this._direction,this.applyType))}),l=s.filter(u=>u.type===mn.Only);if(l.length>0)return this._getOneByPriority(l);const c=this._getOneByPriority(s.filter(u=>u.type===mn.Default)),d=s.filter(u=>u.type===mn.Append)||[];return[...c,...d]}get applyType(){return this._applyType$.getValue()}set applyType(e){this._applyType$.next(e)}get menu(){return this._menu$.getValue()}get direction(){return this._direction}set direction(e){this._direction=e}isFillingStyle(){return this._isFillingStyle}setFillingStyle(e){this._isFillingStyle=e}get autoFillLocation(){return this._autoFillLocation$.getValue()}set autoFillLocation(e){this._autoFillLocation$.next(e)}setDisableApplyType(e,t){this._menu$.next(this._menu$.getValue().map(n=>n.value===e?{...n,disable:t}:n))}setShowMenu(e){this._showMenu$.next(e)}fillData(e){var v,b,R;this.applyType=e;const{source:t,target:n,unitId:o=this._univerInstanceService.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET).getUnitId(),subUnitId:s=(v=this._univerInstanceService.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET).getActiveSheet())==null?void 0:v.getSheetId()}=this.autoFillLocation||{};if(!t||!n||!o||!s)return!1;const l=this.direction;this._prevUndos.length>0&&this._prevUndos.forEach(T=>{this._commandService.syncExecuteCommand(T.id,T.params)}),this._prevUndos=[];const c=a.Rectangle.union(Rt(t),Rt(n)),d=this.getActiveHooks(),u=this._univerInstanceService.getUnit(o,a.UniverInstanceType.UNIVER_SHEET);this._commandService.syncExecuteCommand(m.SetSelectionsOperation.id,{selections:[{primary:{...(R=(b=this._selectionManagerService.getCurrentLastSelection())==null?void 0:b.primary)!=null?R:c},range:{...c,rangeType:a.RANGE_TYPE.NORMAL}}],unitId:o,subUnitId:s});const h=[],g=[];d.forEach(T=>{var P;const{undos:M,redos:y}=((P=T.onFillData)==null?void 0:P.call(T,{source:t,target:n,unitId:o,subUnitId:s},l,e))||{};M&&h.push(...M),y&&g.push(...y)});const S=g.every(T=>this._commandService.syncExecuteCommand(T.id,T.params)),f=[];e!==le.NO_FORMAT&&g.forEach(T=>{if(T.id===m.SetRangeValuesMutation.id){const{cellValue:M}=T.params;new a.ObjectMatrix(M).forValue((P,x,A)=>{const N=Object.keys(u.getStyles().get(A==null?void 0:A.s)||{});(A!=null&&A.p||N.length&&ps.some(D=>N.includes(D)))&&f.push({startRow:P,endRow:P,startColumn:x,endColumn:x})})}});const p=this._getAutoHeightUndoRedos(o,s,f);return p.redos.every(T=>this._commandService.syncExecuteCommand(T.id,T.params))&&(h.push(...p.undos),g.push(...p.redos)),S&&(this._prevUndos=h,this._undoRedoService.pushUndoRedo({unitID:o,undoMutations:h,redoMutations:g})),d.forEach(T=>{var M;(M=T.onAfterFillData)==null||M.call(T,{source:t,target:n,unitId:o,subUnitId:s},l,e)}),this.setShowMenu(!0),!0}_getAutoHeightUndoRedos(e,t,n){var h,g;const o=(h=this._renderManagerService.getRenderById(e))==null?void 0:h.with(exports.SheetSkeletonManagerService),s=(g=o==null?void 0:o.getCurrentParam())==null?void 0:g.skeleton;if(!s)return{redos:[],undos:[]};const l=s.calculateAutoHeightInRange(n),c={subUnitId:t,unitId:e,rowsAutoHeightInfo:l},d=s.worksheet,u=m.SetWorksheetRowAutoHeightMutationFactory(c,d);return{undos:[{id:m.SetWorksheetRowAutoHeightMutation.id,params:u}],redos:[{id:m.SetWorksheetRowAutoHeightMutation.id,params:c}]}}};exports.AutoFillService=au([on(0,a.ICommandService),on(1,a.IUndoRedoService),on(2,w.IRenderManagerService),on(3,a.Inject(a.IUniverInstanceService)),on(4,a.Inject(m.SheetsSelectionsService)),on(5,a.Inject(a.Injector))],exports.AutoFillService);const Et=a.createIdentifier("univer.auto-fill-service"),vt={type:a.CommandType.COMMAND,id:"sheet.command.auto-fill",handler:async(r,i)=>{const e=r.get(Et),t=r.get(a.IUniverInstanceService),{sourceRange:n,targetRange:o}=i,s=m.getSheetCommandTarget(t);if(!s)return!1;const{subUnitId:l,unitId:c}=s;return e.triggerAutoFill(c,l,n,o)}},En={id:"sheet.command.auto-clear-content",type:a.CommandType.COMMAND,handler:async(r,i)=>{const e=r.get(a.IUniverInstanceService),t=r.get(a.ICommandService),n=r.get(a.IUndoRedoService),o=r.get(m.SheetInterceptorService),s=m.getSheetCommandTarget(e);if(!s)return!1;const{unitId:l,subUnitId:c}=s,{clearRange:d,selectionRange:u}=i,h={subUnitId:c,unitId:l,cellValue:m.generateNullCellValue([d])},g=m.SetRangeValuesUndoMutationFactory(r,h),{startColumn:S,startRow:f}=u,p={selections:[{primary:{startColumn:S,startRow:f,endColumn:S,endRow:f,actualRow:f,actualColumn:S,isMerged:!1,isMergedMainCell:!1},range:{...u}}],unitId:l,subUnitId:c};if(t.executeCommand(m.SetSelectionsOperation.id,p),t.syncExecuteCommand(m.SetRangeValuesMutation.id,h)){const v=o.afterCommandExecute({id:m.SetRangeValuesMutation.id,params:h});return a.sequenceExecute(v.redos,t),n.pushUndoRedo({unitID:l,undoMutations:[{id:m.SetRangeValuesMutation.id,params:g},...v.undos],redoMutations:[{id:m.SetRangeValuesMutation.id,params:h},...v.redos]}),!0}return!1}},dr={id:"sheet.command.refill",type:a.CommandType.COMMAND,handler:async(r,i)=>r.get(Et).fillData(i.type)};var lu=Object.defineProperty,cu=Object.getOwnPropertyDescriptor,du=(r,i,e,t)=>{for(var n=t>1?void 0:t?cu(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&lu(i,e,n),n},Mt=(r,i)=>(e,t)=>i(e,t,r);exports.EditorBridgeService=class extends a.Disposable{constructor(e,t,n,o,s,l,c){super();E(this,"_editorUnitId",a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY);E(this,"_editorIsDirty",!1);E(this,"_isDisabled",!1);E(this,"_visible",{visible:!1,eventType:w.DeviceInputEventType.Dblclick,unitId:""});E(this,"_currentEditCell",null);E(this,"_currentEditCellState",null);E(this,"_currentEditCellLayout",null);E(this,"_currentEditCellState$",new O.BehaviorSubject(null));E(this,"currentEditCellState$",this._currentEditCellState$.asObservable());E(this,"_currentEditCellLayout$",new O.BehaviorSubject(null));E(this,"currentEditCellLayout$",this._currentEditCellLayout$.asObservable());E(this,"currentEditCell$",this._currentEditCellState$.pipe(O.switchMap(e=>this._currentEditCellLayout$.pipe(O.map(t=>e&&t?{...e,...t}:null)))));E(this,"_visible$",new O.BehaviorSubject(this._visible));E(this,"visible$",this._visible$.asObservable());E(this,"_afterVisible$",new O.BehaviorSubject(this._visible));E(this,"afterVisible$",this._afterVisible$.asObservable());E(this,"_forceKeepVisible$",new O.BehaviorSubject(!1));E(this,"forceKeepVisible$",this._forceKeepVisible$.asObservable());this._sheetInterceptorService=e,this._renderManagerService=t,this._themeService=n,this._univerInstanceService=o,this._editorService=s,this._refSelectionsService=l,this._contextService=c,this.disposeWithMe(a.toDisposable(()=>{this._currentEditCellState$.complete(),this._currentEditCell=null})),this._univerInstanceService.getTypeOfUnitDisposed$(a.UniverInstanceType.UNIVER_SHEET).subscribe(d=>{var u;d.getUnitId()===((u=this._currentEditCellState)==null?void 0:u.unitId)&&this._clearCurrentEditCellState()})}refreshEditCellState(){const e=this.getLatestEditCellState();if(!e){this._currentEditCellState=null,this._currentEditCellLayout=null,this._currentEditCellState$.next(null),this._currentEditCellLayout$.next(null);return}const{position:t,scaleX:n,scaleY:o,canvasOffset:s,...l}=e;this._currentEditCellState=l,this._currentEditCellLayout={position:t,scaleX:n,scaleY:o,canvasOffset:s},this._currentEditCellState$.next(this._currentEditCellState),this._currentEditCellLayout$.next(this._currentEditCellLayout)}refreshEditCellPosition(e){var y;const t=this._currentEditCell;if(t==null)return;const n=this._renderManagerService.getCurrentTypeOfRenderer(a.UniverInstanceType.UNIVER_SHEET);if(!n)return;const o=(y=n.with(exports.SheetSkeletonManagerService).getSkeletonParam(t.sheetId))==null?void 0:y.skeleton,s=n.with(Ce);if(!o||!this._currentEditCellState)return;const{primary:l,unitId:c,sheetId:d,scene:u,engine:h}=t,g=Jn(o,l);if(g==null)return;const S=a.convertCellToRange(g),f=$.getCanvasOffsetByEngine(h);let{startX:p,startY:C,endX:v,endY:b}=S;const{scaleX:R,scaleY:T}=u.getAncestorScale(),M=u.getViewportScrollXY(s.getViewPort());p=w.convertTransformToOffsetX(p,R,M),C=w.convertTransformToOffsetY(C,T,M),v=w.convertTransformToOffsetX(v,R,M),b=w.convertTransformToOffsetY(b,T,M),e&&this._currentEditCellLayout&&(v=v-p+this._currentEditCellLayout.position.startX,b=b-C+this._currentEditCellLayout.position.startY,p=this._currentEditCellLayout.position.startX,C=this._currentEditCellLayout.position.startY),this._currentEditCellLayout={position:{startX:p,startY:C,endX:v,endY:b},canvasOffset:f,scaleX:R,scaleY:T},this._currentEditCellLayout$.next(this._currentEditCellLayout)}setEditCell(e){this._currentEditCell=e,this._editorService.getFocusEditor()||(this._editorService.focus(a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY),this._contextService.setContextValue(a.EDITOR_ACTIVATED,!1),this._contextService.setContextValue(a.FOCUSING_EDITOR_STANDALONE,!1));const t=this.getLatestEditCellState();if(!t){this._currentEditCellState=t,this._currentEditCellLayout=t,this._currentEditCellState$.next(t),this._currentEditCellLayout$.next(t);return}const{position:n,canvasOffset:o,scaleX:s,scaleY:l,...c}=t;this._currentEditCellState=c,this._currentEditCellLayout={position:n,canvasOffset:o,scaleX:s,scaleY:l},this._currentEditCellState$.next(this._currentEditCellState),this._currentEditCellLayout$.next(this._currentEditCellLayout)}_clearCurrentEditCellState(){this._currentEditCellState=null,this._currentEditCellState$.next(null),this._currentEditCellLayout=null,this._currentEditCellLayout$.next(null)}getEditCellState(){return this._currentEditCellState==null||this._currentEditCellLayout==null?null:{...this._currentEditCellState,...this._currentEditCellLayout}}getEditCellLayout(){return this._currentEditCellLayout}getEditLocation(){return this._currentEditCellState}updateEditLocation(e,t){this._currentEditCellState&&(this._currentEditCellState={...this._currentEditCellState,row:e,column:t})}getLatestEditCellState(){var W,k,U;const e=this._currentEditCell;if(e==null)return;const t=this._renderManagerService.getCurrentTypeOfRenderer(a.UniverInstanceType.UNIVER_SHEET);if(!t)return;const n=t.with(exports.SheetSkeletonManagerService).getCurrentSkeleton(),o=t.with(Ce);if(!n)return;const{primary:s,unitId:l,sheetId:c,scene:d,engine:u}=e,{startRow:h,startColumn:g}=s,S=Jn(n,s);if(S==null)return;const f=a.convertCellToRange(S),p=$.getCanvasOffsetByEngine(u);let{startX:C,startY:v,endX:b,endY:R}=f;const{scaleX:T,scaleY:M}=d.getAncestorScale(),y=d.getViewportScrollXY(o.getViewPort());C=w.convertTransformToOffsetX(C,T,y),v=w.convertTransformToOffsetY(v,M,y),b=w.convertTransformToOffsetX(b,T,y),R=w.convertTransformToOffsetY(R,M,y);const P=this._univerInstanceService.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET),x=P.getActiveSheet();if(!x)return;const A={workbook:P,worksheet:x,unitId:P.getUnitId(),subUnitId:x.getSheetId(),row:h,col:g,origin:x.getCellRaw(h,g)};let N;const D=this._sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(m.BEFORE_CELL_EDIT)(x.getCell(h,g),A);N=D&&n.getCellDocumentModelWithFormula(D);const H=(W=N==null?void 0:N.documentModel)==null?void 0:W.documentStyle.renderConfig;if(H!=null&&(H.cellValueType=a.CellValueType.STRING),!N||N.documentModel==null){const B=n.getBlankCellDocumentModel(D);if(N!=null){const{verticalAlign:F,horizontalAlign:L,wrapStrategy:j,textRotation:z,fill:q}=N,{centerAngle:te,vertexAngle:Q}=w.convertTextRotation(z);B.documentModel.documentStyle.renderConfig={...H,verticalAlign:F,horizontalAlign:L,wrapStrategy:j,background:{rgb:q},centerAngle:te,vertexAngle:Q}}N=B}if(N.fill=N.fill||"#fff",(k=N.documentModel)==null||k.setZoomRatio(Math.max(T,M)),(D==null?void 0:D.isInArrayFormulaRange)===!0){const B=(U=N.documentModel)==null?void 0:U.getBody();B&&(B.textRuns=[{st:0,ed:B.dataStream.length-2,ts:{cl:{rgb:this._themeService.getCurrentTheme().textColorSecondary}}}])}return{position:{startX:C,startY:v,endX:b,endY:R},scaleX:T,scaleY:M,canvasOffset:p,row:h,column:g,unitId:l,sheetId:c,documentLayoutObject:N,editorUnitId:this._editorUnitId,isInArrayFormulaRange:D==null?void 0:D.isInArrayFormulaRange}}getCurrentEditorId(){return this._editorUnitId}changeVisible(e){this._visible=e,e.visible&&(this._editorIsDirty=!1),this._visible$.next(this._visible),this._afterVisible$.next(this._visible)}isVisible(){return this._visible}enableForceKeepVisible(){this._forceKeepVisible$.next(!0)}disableForceKeepVisible(){this._forceKeepVisible$.next(!1)}isForceKeepVisible(){return this._forceKeepVisible$.getValue()}changeEditorDirty(e){this._editorIsDirty=e}getEditorDirty(){return this._editorIsDirty}};exports.EditorBridgeService=du([Mt(0,a.Inject(m.SheetInterceptorService)),Mt(1,w.IRenderManagerService),Mt(2,a.Inject(a.ThemeService)),Mt(3,a.IUniverInstanceService),Mt(4,$.IEditorService),Mt(5,m.IRefSelectionsService),Mt(6,a.IContextService)],exports.EditorBridgeService);const Se=a.createIdentifier("univer.sheet-editor-bridge.service"),ve={id:"sheet.operation.set-cell-edit-visible",type:a.CommandType.OPERATION,handler:(r,i)=>i?(r.get(Se).changeVisible(i),!0):!1},ur={id:"sheet.operation.set-cell-edit-visible-f2",type:a.CommandType.OPERATION,handler:(r,i)=>{const e=r.get(a.ICommandService),n=r.get(a.IUniverInstanceService).getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET);return n?(e.syncExecuteCommand(ve.id,{...i,unitId:n.getUnitId()}),!0):!1}},zt={id:"sheet.operation.set-cell-edit-visible-arrow",type:a.CommandType.OPERATION,handler:()=>!0},Nn={id:"sheet.command.change-zoom-ratio",type:a.CommandType.COMMAND,handler:(r,i)=>{if(!i)return!1;const e=r.get(a.IUniverInstanceService),t=m.getSheetCommandTarget(e);if(!t)return!1;const{worksheet:n,subUnitId:o,unitId:s}=t,{delta:l,reset:c}=i;let d=c?100:Math.round((n.getConfig().zoomRatio+l)*100);d=Math.max(ko[0],d),d=Math.min(ko[1],d);const u=d/100,g=r.get(Se).isVisible();return(g.unitId===s||g.unitId===a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY)&&g.visible?!1:r.get(a.ICommandService).executeCommand(Le.id,{unitId:s,subUnitId:o,zoomRatio:u})}},Dn={id:"sheet.command.set-zoom-ratio",type:a.CommandType.COMMAND,handler:(r,i)=>{if(!i)return!1;const{unitId:e,subUnitId:t,zoomRatio:n}=i,s=r.get(Se).isVisible();return(s.unitId===e||s.unitId===a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY)&&s.visible?!1:r.get(a.ICommandService).executeCommand(Le.id,{unitId:e,subUnitId:t,zoomRatio:n})}};function uu(r){return typeof r.isMainScene>"u"}function Ge(r,i){const e=r instanceof a.Workbook?r:r.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET);if(!e)return null;const t=e.getUnitId();let n,o,s,l;if(uu(i)){const g=i.getRenderById(t);if(g==null)return null;n=g.components,n=g.components,o=g.mainComponent,s=g.scene,l=g.engine}else n=i.components,o=i.mainComponent,s=i.scene,l=i.engine;if(!n||!o)return null;const c=o,d=n.get(me.ROW),u=n.get(me.COLUMN),h=n.get(me.LEFT_TOP);return{spreadsheet:c,spreadsheetRowHeader:d,spreadsheetColumnHeader:u,spreadsheetLeftTopPlaceholder:h,scene:s,engine:l}}function Bo(r,i,e,t){const{startX:n,startY:o,endX:s,endY:l}=t.getCellWithCoordByIndex(r,i);return{startX:n,startY:o,endX:s,endY:l}}function qe(r,i,e,t,n,o){const s=e.getCoordRelativeToViewport(w.Vector2.FromArray([r,i])),{x:l,y:c}=s,d=e.getScrollXYInfoByViewport(s,n),{scaleX:u,scaleY:h}=e.getAncestorScale(),g=t.getCellIndexByOffset(l,c,u,h,d,{closeFirst:o}),{row:S,column:f}=g,p=t.getNoMergeCellWithCoordByIndex(S,f),{startX:C,startY:v,endX:b,endY:R}=p;return{startX:C,startY:v,endX:b,endY:R,row:S,column:f}}function qn(r,i,e,t){const n=e.getCoordRelativeToViewport(w.Vector2.FromArray([r,i])),o=e.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN),s=e.getScrollXYInfoByViewport(n,o),{scaleX:l,scaleY:c}=e.getAncestorScale(),{x:d,y:u}=s,h=r/l+d,g=i/c+u;return{x:h,y:g}}var hu=Object.defineProperty,mu=Object.getOwnPropertyDescriptor,gu=(r,i,e,t)=>{for(var n=t>1?void 0:t?mu(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&hu(i,e,n),n},Wr=(r,i)=>(e,t)=>i(e,t,r);let $n=class extends a.Disposable{constructor(i,e,t,n,o){super();E(this,"_zoom");this._context=i,this._sheetSkeletonManagerService=e,this._commandService=t,this._contextService=n,this._editorBridgeService=o,this._initSkeletonListener(),this._initZoomEventListener()}updateZoom(i,e){const t=this._context.unit.getSheetBySheetId(i);return t?(t.getConfig().zoomRatio=e,t===this._context.unit.getActiveSheet()&&this._updateViewZoom(e),!0):!1}_initZoomEventListener(){const i=this._getSheetObject().scene;this.disposeWithMe(i.onMouseWheel$.subscribeEvent(e=>{if(!e.ctrlKey||!this._contextService.getContextValue(a.FOCUSING_SHEET))return;if(this._editorBridgeService){const d=this._editorBridgeService.isVisible();if((d.unitId===this._context.unitId||d.unitId===a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY)&&d.visible)return}const t=Math.abs(e.deltaX);let n=t<40?.2:t<80?.4:.2;n*=e.deltaY>0?-1:1,i.scaleX<1&&(n/=2);const o=this._context.unit,s=o.getActiveSheet();if(!s)return;const l=s.getZoomRatio();let c=+Number.parseFloat(`${l+n}`).toFixed(1);c=c>=4?4:c<=.1?.1:c,this._commandService.executeCommand(Dn.id,{zoomRatio:Math.round(c*10)/10,unitId:o.getUnitId(),subUnitId:s.getSheetId()}),e.preventDefault()}))}_initSkeletonListener(){this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(i=>{if(i==null)return;const t=this._context.unit.getActiveSheet();if(!t)return;const n=t.getZoomRatio()||1;this._zoom!==n&&this._updateViewZoom(n),this._zoom=n}))}_updateViewZoom(i){const e=this._getSheetObject();e==null||e.scene.scale(i,i),e==null||e.spreadsheet.makeForceDirty()}_getSheetObject(){return Ge(this._context.unit,this._context)}};$n=gu([Wr(1,a.Inject(exports.SheetSkeletonManagerService)),Wr(2,a.ICommandService),Wr(3,a.IContextService),Wr(4,a.Optional(Se))],$n);const Le={id:"sheet.operation.set-zoom-ratio",type:a.CommandType.OPERATION,handler:(r,i)=>{const t=r.get(w.IRenderManagerService).getRenderById(i.unitId);return t?t.with($n).updateZoom(i.subUnitId,i.zoomRatio):!1}};var Su=Object.defineProperty,fu=Object.getOwnPropertyDescriptor,pu=(r,i,e,t)=>{for(var n=t>1?void 0:t?fu(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&Su(i,e,n),n},Xi=(r,i)=>(e,t)=>i(e,t,r);const ea="univer-sheet-main-canvas";exports.SheetsRenderService=class extends a.RxDisposable{constructor(e,t,n){super();E(this,"_skeletonChangeMutations",new Set);this._contextService=e,this._instanceSrv=t,this._renderManagerService=n,Promise.resolve().then(()=>this._init())}registerSkeletonChangingMutations(e){return this._skeletonChangeMutations.has(e)?a.toDisposable(()=>{}):(this._skeletonChangeMutations.add(e),a.toDisposable(()=>this._skeletonChangeMutations.delete(e)))}checkMutationShouldTriggerRerender(e){return this._skeletonChangeMutations.has(e)}_init(){this._initWorkbookListener(),this._initContextListener()}_initWorkbookListener(){this._instanceSrv.getTypeOfUnitAdded$(a.UniverInstanceType.UNIVER_SHEET).pipe(O.takeUntil(this.dispose$)).subscribe(e=>this._createRenderer(e)),this._instanceSrv.getAllUnitsForType(a.UniverInstanceType.UNIVER_SHEET).forEach(e=>this._createRenderer(e)),this._instanceSrv.getTypeOfUnitDisposed$(a.UniverInstanceType.UNIVER_SHEET).pipe(O.takeUntil(this.dispose$)).subscribe(e=>this._disposeRenderer(e)),this.disposeWithMe(this._instanceSrv.getCurrentTypeOfUnit$(a.UniverInstanceType.UNIVER_SHEET).subscribe(e=>{e&&this._renderManagerService.setCurrent(e.getUnitId())}))}_createRenderer(e){const t=e.getUnitId();this._renderManagerService.created$.subscribe(n=>{n.unitId===t&&(n.engine.getCanvas().setId(`${ea}_${t}`),n.engine.getCanvas().getContext().setId(`${ea}_${t}`))}),this._renderManagerService.createRender(t)}_disposeRenderer(e){const t=e.getUnitId();this._renderManagerService.removeRender(t)}_initContextListener(){this.disposeWithMe(this._contextService.subscribeContextValue$(w.RENDER_RAW_FORMULA_KEY).pipe(O.distinctUntilChanged()).subscribe(()=>{this._renderManagerService.getRenderAll().forEach(e=>{e.mainComponent instanceof w.Spreadsheet&&e.mainComponent.makeForceDirty(!0)})}))}};exports.SheetsRenderService=pu([Xi(0,a.IContextService),Xi(1,a.IUniverInstanceService),Xi(2,w.IRenderManagerService)],exports.SheetsRenderService);var Cu=Object.defineProperty,vu=Object.getOwnPropertyDescriptor,_u=(r,i,e,t)=>{for(var n=t>1?void 0:t?vu(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&Cu(i,e,n),n},Pt=(r,i)=>(e,t)=>i(e,t,r);exports.AutoFillController=class extends a.Disposable{constructor(e,t,n,o,s,l,c){super();E(this,"_beforeApplyData",[]);E(this,"_currentLocation",null);E(this,"_copyData",[]);E(this,"_defaultHook");this._univerInstanceService=e,this._commandService=t,this._autoFillService=n,this._editorBridgeService=o,this._renderManagerService=s,this._injector=l,this._sheetsRenderService=c,this._defaultHook={id:"default",type:mn.Default,priority:0,onBeforeFillData:(d,u)=>this._presetAndCacheData(d,u),onFillData:(d,u,h)=>this._fillData(d,u,h)},this._init()}_init(){this._initDefaultHook(),this._initSelectionControlFillChanged(),this._initQuitListener(),this._initSkeletonChange()}_initSkeletonChange(){this.disposeWithMe(this._sheetsRenderService.registerSkeletonChangingMutations(vt.id))}_initDefaultHook(){this.disposeWithMe(this._autoFillService.addHook(this._defaultHook))}_initQuitListener(){const e=[ve.id,En.id,m.SetRangeValuesCommand.id,Le.id,m.SetWorksheetActiveOperation.id,m.MoveRangeMutation.id,m.RemoveRowMutation.id,m.RemoveColMutation.id,m.InsertRowMutation.id,m.InsertColMutation.id,m.MoveRowsMutation.id,m.MoveColsMutation.id,m.SetWorksheetColWidthMutation.id,m.SetWorksheetRowHeightMutation.id,m.SetRangeValuesMutation.id];this.disposeWithMe(this._commandService.onCommandExecuted((t,n)=>{var c,d;const o=n==null?void 0:n.fromCollab,s=n==null?void 0:n.fromSync,l=n==null?void 0:n.fromFormula;e.includes(t.id)&&!o&&!s&&!l&&t.params.trigger!==vt.id&&t.params.trigger!==dr.id&&this._quit(),t.id===m.RemoveSheetMutation.id&&t.params.unitId===((c=this._currentLocation)==null?void 0:c.unitId)&&t.params.subUnitId===((d=this._currentLocation)==null?void 0:d.subUnitId)&&this._quit()})),this.disposeWithMe(this._univerInstanceService.unitDisposed$.subscribe(t=>{var n;t.getUnitId()===((n=this._currentLocation)==null?void 0:n.unitId)&&this._quit()}))}_quit(){this._currentLocation=null,this._beforeApplyData=[],this._copyData=[],this._autoFillService.setShowMenu(!1)}_initSelectionControlFillChanged(){const e=new a.DisposableCollection,t=()=>{e.dispose();const n=this._renderManagerService.getCurrentTypeOfRenderer(a.UniverInstanceType.UNIVER_SHEET);if(!n)return;n.with(Ce).getSelectionControls().forEach(l=>{e.add(l.selectionFilled$.subscribe(c=>{if(c==null||c.startColumn===-1||c.startRow===-1||c.endColumn===-1||c.endRow===-1)return;const d={startColumn:l.model.startColumn,endColumn:l.model.endColumn,startRow:l.model.startRow,endRow:l.model.endRow},u={startColumn:c.startColumn,endColumn:c.endColumn,startRow:c.startRow,endRow:c.endRow};this._commandService.executeCommand(vt.id,{sourceRange:d,targetRange:u})})),e.add(l.fillControl.onDblclick$.subscribeEvent(()=>{const c={startColumn:l.model.startColumn,endColumn:l.model.endColumn,startRow:l.model.startRow,endRow:l.model.endRow};this._handleDbClickFill(c)})),e.add(l.fillControl.onPointerDown$.subscribeEvent(()=>{this._editorBridgeService.isVisible().visible&&this._commandService.syncExecuteCommand(ve.id,{visible:!1,eventType:w.DeviceInputEventType.PointerDown,unitId:n.unitId})}))})};t(),this.disposeWithMe(this._commandService.onCommandExecuted(n=>{n.id===m.SetSelectionsOperation.id&&t()})),this.disposeWithMe(this._univerInstanceService.getCurrentTypeOfUnit$(a.UniverInstanceType.UNIVER_SHEET).subscribe(()=>t()))}_handleDbClickFill(e){const t=this._detectFillRange(e);t.endRow<=e.endRow||this._commandService.executeCommand(vt.id,{sourceRange:e,targetRange:t})}_detectFillRange(e){var g,S,f,p,C,v;const{startRow:t,endRow:n,startColumn:o,endColumn:s}=e,l=(g=this._univerInstanceService.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET))==null?void 0:g.getActiveSheet();if(!l)return e;const c=l.getCellMatrix(),d=l.getMaxRows()-1,u=l.getMaxColumns()-1;let h=n;if(o>0&&((S=c.getValue(t,o-1))==null?void 0:S.v)!=null){let b=t;for(;((f=c.getValue(b,o-1))==null?void 0:f.v)!=null&&b<d;)b+=1;h=b-1}else if(s<u&&((p=c.getValue(n,s+1))==null?void 0:p.v)!=null){let b=t;for(;((C=c.getValue(b,s+1))==null?void 0:C.v)!=null&&b<d;)b+=1;h=b-1}for(let b=n+1;b<=h;b++)for(let R=o;R<=s;R++)if(((v=c.getValue(b,R))==null?void 0:v.v)!=null){h=b-1;break}return{startColumn:o,endColumn:s,startRow:t,endRow:h}}_getApplyData(e,t,n,o,s,l=!0){const c=[],d=Math.floor(n/t),u=n%t,h=this._autoFillService.getRules();if(!l&&s===le.ONLY_FORMAT)return console.error("ERROR: only format can not be applied when hasStyle is false"),[];const g={};h.forEach(S=>{g[S.type]=[]}),h.forEach(S=>{const{type:f,applyFunctions:p={}}=S,C=e[f];C&&C.forEach(v=>{const b=zd(v.index,u),R=v.index.length*d+b,T=this._applyFunctions(v,R,o,s,p,e),M=Xd(t,n,v.index);g[f].push({data:T,index:M})})});for(let S=0;S<n;S++)h.forEach(f=>{const{type:p}=f,C=g[p];for(let v=0;v<C.length;v++)S in C[v].index&&c.push(C[v].data[C[v].index[S]])});return c}_applyFunctions(e,t,n,o,s,l){const{data:c}=e,d=n===a.Direction.UP||n===a.Direction.LEFT;if(o===le.COPY){const u=s==null?void 0:s[le.COPY];return u?u(e,t,n,l):(d&&c.reverse(),Ft(c,t))}if(o===le.SERIES){const u=s==null?void 0:s[le.SERIES];return u?u(e,t,n,l):(d&&c.reverse(),s!=null&&s[le.COPY]?s[le.COPY](e,t,n,l):Ft(c,t))}if(o===le.ONLY_FORMAT){const u=s==null?void 0:s[le.ONLY_FORMAT];return u?u(e,t,n,l):Yd(c,t)}}_getCopyData(e,t){const n=this._univerInstanceService.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET).getActiveSheet();if(!n)throw new Error("No active sheet found");const o=n.getCellMatrix(),s=this._autoFillService.getRules(),l=[],c=t===a.Direction.DOWN||t===a.Direction.UP;let d,u;return c?(d=e.cols,u=e.rows):(d=e.rows,u=e.cols),d.forEach(h=>{const g=this._getEmptyCopyDataPiece(),S={type:void 0,cellData:void 0};u.forEach(f=>{let p;c?p=o.getValue(f,h):p=o.getValue(h,f);const{type:C,isContinue:v}=s.find(b=>b.match(p,this._injector))||$a;if(v(S,p)){const b=g[C],R=b[b.length-1];R.data.push(p),R.index.push(f-u[0])}else{const b=g[C];b?b.push({data:[p],index:[f-u[0]]}):g[C]=[{data:[p],index:[f-u[0]]}]}S.type=C,S.cellData=p}),l.push(g)}),l}_getEmptyCopyDataPiece(){const e={};return this._autoFillService.getRules().forEach(t=>{e[t.type]=[]}),e}_getMergeApplyData(e,t,n,o){const s=this._univerInstanceService.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET).getActiveSheet();if(!s)throw new Error("No active sheet found");const l=[];for(let c=e.startRow;c<=e.endRow;c++)for(let d=e.startColumn;d<=e.endColumn;d++){const{isMergedMainCell:u,startRow:h,startColumn:g,endRow:S,endColumn:f}=s.getCellInfoInMergeData(c,d);if(u){if(n===a.Direction.DOWN){let p=h+o,C=S+o;for(;C<=t.endRow;)l.push({startRow:p,startColumn:g,endRow:C,endColumn:f}),p+=o,C+=o}else if(n===a.Direction.UP){let p=h-o,C=S-o;for(;p>=t.startRow;)l.push({startRow:p,startColumn:g,endRow:C,endColumn:f}),p-=o,C-=o}else if(n===a.Direction.RIGHT){let p=g+o,C=f+o;for(;C<=t.endColumn;)l.push({startRow:h,startColumn:p,endRow:S,endColumn:C}),p+=o,C+=o}else if(n===a.Direction.LEFT){let p=g-o,C=f-o;for(;p>=t.startColumn;)l.push({startRow:h,startColumn:p,endRow:S,endColumn:C}),p-=o,C-=o}}}return l}_presetAndCacheData(e,t){const{source:n,target:o}=e,s=this._univerInstanceService.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET).getActiveSheet();if(!s)throw new Error("No active sheet found");const l=s.getCellMatrix(),c=[];return o.rows.forEach(d=>{const u=[];o.cols.forEach(h=>{u.push(a.Tools.deepClone(l.getValue(d,h)))}),c.push(u)}),this._beforeApplyData=c,this._copyData=this._getCopyData(n,t),this._currentLocation=e,this._shouldDisableSeries(this._copyData)?(this._autoFillService.setDisableApplyType(le.SERIES,!0),le.COPY):(this._autoFillService.setDisableApplyType(le.SERIES,!1),this._getPreferredApplyType(this._copyData))}_fillData(e,t,n){var W,k;const o=[],s=[];let l=!0;n===le.NO_FORMAT&&(l=!1,n=le.SERIES);const{source:c,target:d,unitId:u,subUnitId:h}=e;if(!c||!d||t==null)return{undos:o,redos:s};const g=Rt(c),S=Rt(d),{cols:f,rows:p}=d,{cols:C,rows:v}=c,b=this._copyData;let R;t===a.Direction.DOWN||t===a.Direction.UP?R=v.length:R=C.length;const T=[];if(t===a.Direction.DOWN||t===a.Direction.UP){const U=p.length,B=[];f.forEach((F,L)=>{const j=b[L],z=this._getApplyData(j,R,U,t,n,l);B.push(z)});for(let F=0;F<B[0].length;F++){const L=[];for(let j=0;j<B.length;j++)L.push({s:null,...B[j][F]});T.push(L)}}else{const U=f.length;p.forEach((B,F)=>{const L=b[F],j=this._getApplyData(L,R,U,t,n,l),z=[];for(let q=0;q<j.length;q++)z.push({s:null,...j[q]});T.push(z)})}let M=[];const y=this._univerInstanceService.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET).getStyles();if(l?(M=this._getMergeApplyData(g,S,t,R),T.forEach(U=>{U.forEach(B=>{B&&y&&y&&(B.s=y.getStyleByCell(B))})})):T.forEach((U,B)=>{U.forEach((F,L)=>{F&&y&&(F.s=y.getStyleByCell(this._beforeApplyData[B][L])||null)})}),n===le.ONLY_FORMAT&&T.forEach((U,B)=>{U.forEach((F,L)=>{if(F){const j=this._beforeApplyData[B][L]||{};F.f=j.f,F.si=j.si,F.t=j.t,F.v=j.v}})}),l){const U=[],B=(k=(W=this._univerInstanceService.getUniverSheetInstance(u))==null?void 0:W.getSheetBySheetId(h))==null?void 0:k.getMergeData();B&&B.forEach(j=>{a.Rectangle.intersects(j,S)&&U.push(j)});const F={unitId:u,subUnitId:h,ranges:U},L=this._injector.invoke(m.RemoveMergeUndoMutationFactory,F);U.length&&(s.push({id:m.RemoveWorksheetMergeMutation.id,params:F}),o.unshift({id:m.AddWorksheetMergeMutation.id,params:L}))}const P={subUnitId:h,unitId:u,cellValue:Vd([d])},x=this._injector.invoke(m.SetRangeValuesUndoMutationFactory,P);s.push({id:m.SetRangeValuesMutation.id,params:P}),o.unshift({id:m.SetRangeValuesMutation.id,params:x});const A=new a.ObjectMatrix;p.forEach((U,B)=>{f.forEach((F,L)=>{T[B][L]&&A.setValue(U,F,T[B][L])})});const N=A.getMatrix(),D={subUnitId:h,unitId:u,cellValue:N},H=this._injector.invoke(m.SetRangeValuesUndoMutationFactory,D);if(this._autoFillService.getActiveHooks().forEach(U=>{var B;(B=U.onBeforeSubmit)==null||B.call(U,e,t,n,N)}),o.unshift({id:m.SetRangeValuesMutation.id,params:H}),s.push({id:m.SetRangeValuesMutation.id,params:D}),M!=null&&M.length){const U=m.getAddMergeMutationRangeByType(M),B={unitId:u,subUnitId:h,ranges:U},F=this._injector.invoke(m.AddMergeUndoMutationFactory,B);o.unshift({id:m.RemoveWorksheetMergeMutation.id,params:F}),s.push({id:m.AddWorksheetMergeMutation.id,params:B})}return{undos:o,redos:s}}_shouldDisableSeries(e){return e.every(t=>Object.keys(t).every(n=>{var o;return((o=t[n])==null?void 0:o.length)===0||[Ie.OTHER,Ie.FORMULA].includes(n)}))}_getPreferredApplyType(e){return e.every(n=>Object.keys(n).every(o=>{var s,l;return((s=n[o])==null?void 0:s.length)===0||((l=n[o])==null?void 0:l.length)===1&&n[o][0].data.length===1&&Ie.NUMBER===o}))?le.COPY:le.SERIES}};exports.AutoFillController=_u([Pt(0,a.IUniverInstanceService),Pt(1,a.ICommandService),Pt(2,Et),Pt(3,Se),Pt(4,w.IRenderManagerService),Pt(5,a.Inject(a.Injector)),Pt(6,a.Inject(exports.SheetsRenderService))],exports.AutoFillController);var Iu=Object.defineProperty,bu=Object.getOwnPropertyDescriptor,Ru=(r,i,e,t)=>{for(var n=t>1?void 0:t?bu(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&Iu(i,e,n),n},Yi=(r,i)=>(e,t)=>i(e,t,r);exports.CellCustomRenderController=class extends a.Disposable{constructor(e,t,n,o){super();E(this,"_enterActiveRender");this._context=e,this._sheetSkeletonManagerService=t,this._renderManagerService=n,this._permissionService=o,this._initEventBinding()}_initEventBinding(){const e=new a.DisposableCollection,t=this._context.unit,n=o=>{if(e.dispose(),!o)return;const s=this._context.unitId,{skeleton:l}=o,c=this._renderManagerService.getRenderById(s);if(c&&c.mainComponent){const d=c.mainComponent,u=S=>{const{offsetX:f,offsetY:p}=S,C=c.scene,v=t.getActiveSheet();if(!v)return;const b=C.getActiveViewportByCoord(w.Vector2.FromArray([f,p]));if(!b)return;const{scaleX:R,scaleY:T}=C.getAncestorScale(),M={x:b.viewportScrollX,y:b.viewportScrollY},y=l.getCellIndexByOffset(f,p,R,T,M),P=l.worksheet.getMergedCell(y.row,y.column),x={actualRow:P?P.startRow:y.row,actualCol:P?P.startColumn:y.column,mergeCell:P,row:y.row,col:y.column};if(!x||!l)return;const A=v.getCell(x.actualRow,x.actualCol);if(!A)return;const N=A.customRender;if(!(N!=null&&N.length))return;const D=x.actualRow,H=x.actualCol,W=N.sort(a.sortRules),k=v.getSheetId(),U={data:A,style:l.getStyles().getStyleByCell(A),primaryWithCoord:l.getCellWithCoordByIndex(x.actualRow,x.actualCol),unitId:s,subUnitId:k,row:D,col:H,workbook:t,worksheet:v},B={x:M.x+f/R,y:M.y+p/T},F=W.find(L=>{var j;return(j=L.isHit)==null?void 0:j.call(L,B,U)});if(F)return[F,U]},h=d.onPointerDown$.subscribeEvent(S=>{var p,C,v,b;const f=u(S);if(f){const[R,T]=f,{row:M,col:y,worksheet:P,unitId:x,subUnitId:A}=T;if(!((p=this._permissionService.composePermission([new m.WorkbookEditablePermission(x).id,new m.WorksheetEditPermission(x,A).id]))==null?void 0:p.every(H=>H.value)))return!1;const D=(v=(C=P.getCell(M,y))==null?void 0:C.selectionProtection)==null?void 0:v[0];if((D==null?void 0:D[m.UnitAction.Edit])===!1)return!1;(b=R.onPointerDown)==null||b.call(R,T,S)}}),g=a.fromEventSubject(d.onPointerMove$).pipe(O.throttleTime(30)).subscribe(S=>{var p,C,v,b,R,T;const f=u(S);if(f){const[M,y]=f;this._enterActiveRender?this._enterActiveRender.render!==M&&((C=(p=this._enterActiveRender.render).onPointerLeave)==null||C.call(p,this._enterActiveRender.cellContext,S),this._enterActiveRender={render:M,cellContext:y},(v=M.onPointerEnter)==null||v.call(M,y,S)):(this._enterActiveRender={render:M,cellContext:y},(b=M.onPointerEnter)==null||b.call(M,y,S))}else this._enterActiveRender&&((T=(R=this._enterActiveRender.render).onPointerLeave)==null||T.call(R,this._enterActiveRender.cellContext,S),this._enterActiveRender=null)});h&&e.add(h),g&&e.add(g)}};this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(n)),n(this._sheetSkeletonManagerService.getCurrentParam()),this.disposeWithMe(e)}};exports.CellCustomRenderController=Ru([Yi(1,a.Inject(exports.SheetSkeletonManagerService)),Yi(2,w.IRenderManagerService),Yi(3,a.IPermissionService)],exports.CellCustomRenderController);class Cs extends a.Disposable{constructor(){super(...arguments);E(this,"rangesStack",[]);E(this,"selectedRangeWorksheet","")}}var Eu=Object.defineProperty,Tu=Object.getOwnPropertyDescriptor,wu=(r,i,e,t)=>{for(var n=t>1?void 0:t?Tu(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&Eu(i,e,n),n},ta=(r,i)=>(e,t)=>i(e,t,r);let Tn=class{constructor(r,i){E(this,"_current",null);E(this,"_shortcutParam",[]);this._univerInstanceService=r,this._localeService=i}dispose(){this._shortcutParam=[]}getCurrentBySearch(r){return this._getCurrentBySearch(r)}getCurrent(){return this._getCurrentBySearch(this._current)}addOrUpdate(r){const i=this._getCurrentBySearch({unitId:r.unitId,sheetId:r.sheetId,keycode:r.keycode});if(i!=null){const e=this._shortcutParam.indexOf(i);this._shortcutParam.splice(e,1)}return this._shortcutParam.push(r),i}remove(r){if(r==null)return;const i=this._getCurrentBySearch(r);if(i==null)return;const e=this._shortcutParam.indexOf(i);return this._shortcutParam.splice(e,1)[0]}_getCurrentBySearch(r){return r==null?void 0:this._shortcutParam.find(e=>e.unitId===r.unitId&&e.sheetId===r.sheetId&&e.keycode===r.keycode)}};Tn=wu([ta(0,a.IUniverInstanceService),ta(1,a.Inject(a.LocaleService))],Tn);var xi=(r=>(r.MergeAll="mergeAll",r.MergeVertical="mergeVertical",r.MergeHorizontal="mergeHorizontal",r))(xi||{});function _t(r,i,e,t,n=!0,o=1,s=!0){let l={...r};t==null&&(t={startRow:0,endRow:e.getRowCount()-1,startColumn:0,endColumn:e.getColumnCount()-1});let c;switch(i){case a.Direction.UP:for(c=r.startRow-o;c>-1&&!e.getRowVisible(c);)c-=1;c>=t.startRow?(l.startRow=c,l.endRow=c):s&&(l.startRow=t.endRow,l.endRow=t.endRow,n&&(l=_t(l,a.Direction.LEFT,e,t,!1)));break;case a.Direction.DOWN:for(c=r.endRow+o;c<e.getRowCount()&&!e.getRowVisible(c);)c+=1;c<=t.endRow?(l.startRow=c,l.endRow=c):s&&(l.startRow=t.startRow,l.endRow=t.startRow,n&&(l=_t(l,a.Direction.RIGHT,e,t,!1)));break;case a.Direction.LEFT:for(c=r.startColumn-o;c>-1&&!e.getColVisible(c);)c-=1;c>=t.startColumn?(l.startColumn=c,l.endColumn=c):s&&(l.startColumn=t.endColumn,l.endColumn=t.endColumn,n&&(l=_t(l,a.Direction.UP,e,t,!1)));break;case a.Direction.RIGHT:for(c=r.endColumn+o;c<e.getColumnCount()&&!e.getColVisible(c);)c+=1;c<=t.endColumn?(l.startColumn=c,l.endColumn=c):s&&(l.startColumn=t.startColumn,l.endColumn=t.startColumn,n&&(l=_t(l,a.Direction.DOWN,e,t,!1)));break}return l}function vs(r,i,e){const t={...r},{startRow:n,startColumn:o,endRow:s,endColumn:l}=er(r,i,e);let c=Ln(e,n,o,s,l).hasValue,d=!0,u=!0;for(;u;){if(a.Direction.UP===i){let h=t.startRow-1;for(;h>-1&&!e.getRowVisible(h);)h-=1;if(h===-1){u=!1;break}const{hasValue:g,matrix:S}=Ln(e,h,t.startColumn,h,t.endColumn);if(c&&!g&&!d){u=!1;break}else{if(S.getLength()!==0){let f=h;S.forValue(p=>{f=Math.min(p,f)}),t.startRow=f}else t.startRow=h;if(t.endRow=t.startRow,!c&&g){u=!1;break}c=g,d=!1}}if(a.Direction.DOWN===i){let h=t.endRow+1;for(;h<e.getRowCount()&&!e.getRowVisible(h);)h+=1;if(h===e.getRowCount()){u=!1;break}const{hasValue:g,matrix:S}=Ln(e,h,t.startColumn,h,t.endColumn);if(c&&!g&&!d){u=!1;break}else{if(S.getLength()!==0){let f=h;S.forValue((p,C,v)=>{f=Math.max(p+(v.rowSpan||1)-1,f)}),t.endRow=f}else t.endRow=h;if(t.startRow=t.endRow,!c&&g){u=!1;break}c=g,d=!1}}if(a.Direction.LEFT===i){let h=t.startColumn-1;for(;h>-1&&!e.getColVisible(h);)h-=1;if(h===-1){u=!1;break}const{hasValue:g,matrix:S}=Ln(e,t.startRow,h,t.endRow,h);if(c&&!g&&!d){u=!1;break}else{if(S.getLength()!==0){let f=h;S.forValue((p,C)=>{f=Math.min(C,f)}),t.startColumn=f}else t.startColumn=h;if(t.endColumn=t.startColumn,!c&&g){u=!1;break}c=g,d=!1}}if(a.Direction.RIGHT===i){let h=t.endColumn+1;for(;h<e.getColumnCount()&&!e.getColVisible(h);)h+=1;if(h===e.getColumnCount()){u=!1;break}const{hasValue:g,matrix:S}=Ln(e,t.startRow,h,t.endRow,h);if(c&&!g&&!d){u=!1;break}else{if(S.getLength()!==0){let f=h;S.forValue((p,C,v)=>{f=Math.max(C+(v.colSpan||1)-1,f)}),t.endColumn=f}else t.endColumn=h;if(t.startColumn=t.endColumn,!c&&g){u=!1;break}c=g,d=!1}}}return m.alignToMergedCellsBorders(t,e,!0)}function el(r,i,e){return _t(r,i,e,void 0,!1,1,!1)}function yu(r,i,e){const t=vs(r,i,e);return m.alignToMergedCellsBorders(a.Rectangle.union(t,r),e,!0)}function Mu(r,i,e){const t=el(r,i,e),n={startRow:Math.min(r.startRow,t.startRow),startColumn:Math.min(r.startColumn,t.startColumn),endRow:Math.max(r.endRow,t.endRow),endColumn:Math.max(r.endColumn,t.endColumn)};return m.alignToMergedCellsBorders(a.Rectangle.union(r,n),e)}function Pu(r,i,e,t){const n=a.getReverseDirection(e),o=vs(er(r,n,t),e,t);return e===a.Direction.UP&&o.startRow<=r.startRow?m.alignToMergedCellsBorders({...i,startColumn:r.startColumn,endColumn:r.endColumn},t,!0):e===a.Direction.DOWN&&o.endRow>=r.endRow?m.alignToMergedCellsBorders({...i,startColumn:r.startColumn,endColumn:r.endColumn},t,!0):e===a.Direction.LEFT&&o.startColumn<=r.startColumn?m.alignToMergedCellsBorders({...i,startRow:r.startRow,endRow:r.endRow},t,!0):e===a.Direction.RIGHT&&o.endColumn>=r.endColumn?m.alignToMergedCellsBorders({...i,startRow:r.startRow,endRow:r.endRow},t,!0):a.Rectangle.union(a.Rectangle.clone(i),o)}function Ou(r,i,e){const t=a.getReverseDirection(i),n=er(r,t,e),o=er(r,i,e),s=el(n,i,e);return m.alignToMergedCellsBorders(a.Rectangle.union(o,s),e,!1)}function na(r){return{startRow:0,startColumn:0,endRow:r.getRowCount()-1,endColumn:r.getColumnCount()-1,rangeType:a.RANGE_TYPE.ALL}}function er(r,i,e){let t;switch(i){case a.Direction.UP:t={startRow:r.startRow,startColumn:r.startColumn,endRow:r.startRow,endColumn:r.endColumn,rangeType:a.RANGE_TYPE.NORMAL};break;case a.Direction.DOWN:t={startRow:r.endRow,startColumn:r.startColumn,endRow:r.endRow,endColumn:r.endColumn,rangeType:a.RANGE_TYPE.NORMAL};break;case a.Direction.LEFT:t={startRow:r.startRow,startColumn:r.startColumn,endRow:r.endRow,endColumn:r.startColumn,rangeType:a.RANGE_TYPE.NORMAL};break;case a.Direction.RIGHT:t={startRow:r.startRow,startColumn:r.endColumn,endRow:r.endRow,endColumn:r.endColumn,rangeType:a.RANGE_TYPE.NORMAL};break;default:throw new Error("Invalid direction")}return m.alignToMergedCellsBorders(t,e,!1)}function Ln(r,i,e,t,n){let o=!1;const s=r.getMatrixWithMergedCells(i,e,t,n).forValue((l,c,d)=>{if(Au(d))return o=!0,!1});return{hasValue:o,matrix:s}}function Fo(r,i,e){const t=a.Rectangle.clone(r);if(i==null)return t;switch(e){case a.Direction.UP:case a.Direction.DOWN:t.startColumn=t.endColumn=i.actualColumn;break;case a.Direction.LEFT:case a.Direction.RIGHT:t.startRow=t.endRow=i.actualRow;break}return t}function xu(r,i,e){var l,c,d,u;const{primary:t,range:n}=r,o=a.Rectangle.clone(n);switch(i){case a.Direction.UP:case a.Direction.DOWN:o.startRow=(l=t==null?void 0:t.startRow)!=null?l:n.startRow,o.endRow=(c=t==null?void 0:t.endRow)!=null?c:n.startRow;break;case a.Direction.LEFT:case a.Direction.RIGHT:o.startColumn=(d=t==null?void 0:t.startColumn)!=null?d:n.startColumn,o.endColumn=(u=t==null?void 0:t.endColumn)!=null?u:n.startColumn;break}const s=er(o,i,e);switch(i){case a.Direction.DOWN:return n.startRow<s.startRow;case a.Direction.UP:return n.endRow>s.endRow;case a.Direction.LEFT:return s.endColumn<n.endColumn;case a.Direction.RIGHT:return s.startColumn>n.startColumn}}function tl(r,i){let e=r[0].startRow,t=r[0].endRow;r.forEach(o=>{const{startRow:s,endRow:l}=o;e=Math.min(e,s),t=Math.max(t,l)});const n=new Array(t-e+1).fill(!1);return i.forEach(o=>{const{startRow:s,endRow:l}=o;for(let c=Math.max(s,e);c<=Math.min(l,t);c++)n[c-e]=!0}),r.every(o=>{const{startRow:s,endRow:l}=o;for(let c=s;c<=l;c++)if(!n[c-e])return!1;return!0})}function nl(r,i){let e=r[0].startColumn,t=r[0].endColumn;r.forEach(o=>{const{startColumn:s,endColumn:l}=o;e=Math.min(e,s),t=Math.max(t,l)});const n=new Array(t-e+1).fill(!1);return i.forEach(o=>{const{startColumn:s,endColumn:l}=o;for(let c=Math.max(s,e);c<=Math.min(l,t);c++)n[c-e]=!0}),r.every(o=>{const{startColumn:s,endColumn:l}=o;for(let c=s;c<=l;c++)if(!n[c-e])return!1;return!0})}function _s(r,i){return i?r==="mergeAll"?i.filter(e=>!(e.startRow===e.endRow&&e.startColumn===e.endColumn)):r==="mergeVertical"?i.filter(e=>e.startRow!==e.endRow):r==="mergeHorizontal"?i.filter(e=>e.startColumn!==e.endColumn):i:null}function Au(r){return r.v!==void 0&&r.v!==null&&r.v!==""||r.p!==void 0}var Je=(r=>(r[r.moveStopeOne=0]="moveStopeOne",r[r.moveGap=1]="moveGap",r[r.moveStepPage=2]="moveStepPage",r[r.moveStepEnd=3]="moveStepEnd",r))(Je||{});const xe={id:"sheet.command.move-selection",type:a.CommandType.COMMAND,handler:(r,i)=>{if(!i)return!1;const e=m.getSheetCommandTarget(r.get(a.IUniverInstanceService));if(!e)return!1;const{workbook:t,worksheet:n}=e,o=m.getSelectionsService(r,i.fromCurrentSelection).getCurrentLastSelection();if(!o)return!1;const{direction:s,jumpOver:l,extra:c}=i,{range:d,primary:u}=o,h=Fo(d,u,s),g=l===1?vs(h,s,n):_t(h,s,n),S=m.getCellAtRowCol(g.startRow,g.startColumn,n);if(a.Rectangle.equals(S,h))return!1;const f=t.getUnitId();r.get(Tn).remove({unitId:f,sheetId:n.getSheetId(),keycode:_.KeyCode.TAB});const p=[{range:a.Rectangle.clone(S),primary:{startRow:S.startRow,startColumn:S.startColumn,endRow:S.endRow,endColumn:S.endColumn,actualRow:g.startRow,actualColumn:g.startColumn,isMerged:S.isMerged,isMergedMainCell:S.startRow===g.startRow&&S.startColumn===g.startColumn}}];return r.get(a.ICommandService).executeCommand(m.SetSelectionsOperation.id,{unitId:t.getUnitId(),subUnitId:n.getSheetId(),selections:p,type:m.SelectionMoveType.MOVE_END,extra:c,reveal:!0})}},$e={id:"sheet.command.move-selection-enter-tab",type:a.CommandType.COMMAND,handler:(r,i)=>{var U;if(!i)return!1;const e=m.getSheetCommandTarget(r.get(a.IUniverInstanceService));if(!e)return!1;const{workbook:t,worksheet:n}=e,o=m.getSelectionsService(r),{direction:s,keycode:l}=i,c=s===a.Direction.LEFT||s===a.Direction.UP,d=o.getCurrentSelections().concat(),u=d.findIndex(B=>B.primary),h=d[u];if(!h)return!1;const g=c?-1:1,S=u+g!==d.length?d[u+g]:d[0],f=d.findIndex(B=>B===S),p=t.getUnitId(),C=n.getSheetId();if(!h)return!1;const{range:v}=h,b=h.primary;let R=Fo(v,b,s);const T=r.get(Tn),M=T.getCurrentBySearch({unitId:p,sheetId:C,keycode:_.KeyCode.TAB});let y;const{startRow:P,endRow:x,startColumn:A,endColumn:N}=v,D=c?b.startRow===P&&b.startColumn===A:b.endRow===x&&b.endColumn===N;if(a.Rectangle.equals(v,b)){if(l===_.KeyCode.TAB)M==null&&T.addOrUpdate({unitId:p,sheetId:C,keycode:_.KeyCode.TAB,selection:h});else{const L=M==null?void 0:M.selection;if(L!=null){const{range:j,primary:z}=L;R=Fo(j,z,s)}T.remove({unitId:p,sheetId:C,keycode:_.KeyCode.TAB})}const B=_t(R,s,n),F=m.getCellAtRowCol(B.startRow,B.startColumn,n);if(a.Rectangle.equals(F,R))return!1;y={range:a.Rectangle.clone(F),primary:{startRow:F.startRow,startColumn:F.startColumn,endRow:F.endRow,endColumn:F.endColumn,actualRow:B.startRow,actualColumn:B.startColumn,isMerged:F.isMerged,isMergedMainCell:F.startRow===B.startRow&&F.startColumn===B.startColumn}}}else{T.remove({unitId:p,sheetId:C,keycode:_.KeyCode.TAB});const B=a.Tools.deepClone(b),F=D?S.range:_t({startRow:B.startRow,startColumn:B.startColumn,endRow:B.endRow,endColumn:B.endColumn},s,n,{startRow:P,endRow:x,startColumn:A,endColumn:N}),L=D&&c,j=L?m.getCellAtRowCol(F.endRow,F.endColumn,n):m.getCellAtRowCol(F.startRow,F.startColumn,n);y={range:D?S.range:a.Rectangle.clone(v),primary:{startRow:j.startRow,startColumn:j.startColumn,endRow:j.endRow,endColumn:j.endColumn,actualRow:L?j.startRow:F.startRow,actualColumn:L?j.startColumn:F.startColumn,isMerged:j.isMerged,isMergedMainCell:j.startRow===F.startRow&&j.startColumn===F.startColumn},style:D?S.style:h.style}}D?(d[u].primary=null,d[f]=y,o.setSelections(p,C,[],m.SelectionMoveType.MOVE_END)):d[u]=y;const H=r.get(a.ICommandService).executeCommand(m.SetSelectionsOperation.id,{unitId:p,subUnitId:C,type:m.SelectionMoveType.MOVE_END,selections:d,reveal:!0,extra:i.extra}),k=(U=r.get(w.IRenderManagerService).getRenderById(p))==null?void 0:U.with(Ce);return k==null||k.refreshSelectionMoveEnd(),H}},He={id:"sheet.command.expand-selection",type:a.CommandType.COMMAND,handler:(r,i)=>{if(!i)return!1;const e=m.getSheetCommandTarget(r.get(a.IUniverInstanceService));if(!e)return!1;const{worksheet:t,unitId:n,subUnitId:o}=e,s=m.getSelectionsService(r).getCurrentLastSelection();if(!s)return!1;const{range:l,primary:c}=s,{jumpOver:d,direction:u,extra:h}=i,S=xu(s,u,t)?d===1?Pu(l,{...a.Rectangle.clone(c),rangeType:a.RANGE_TYPE.NORMAL},u,t):Ou(l,u,t):d===1?yu(l,u,t):Mu(l,u,t);return S.rangeType=s.range.rangeType,a.Rectangle.equals(S,l)?!1:r.get(a.ICommandService).syncExecuteCommand(m.SetSelectionsOperation.id,{unitId:n,subUnitId:o,type:m.SelectionMoveType.ONLY_SET,selections:[{range:S,primary:c}],extra:h})}},Ai={id:"sheet.command.select-all",type:a.CommandType.COMMAND,handler:async(r,i={expandToGapFirst:!0,loop:!1})=>{const e=m.getSheetCommandTarget(r.get(a.IUniverInstanceService));if(!e)return!1;const t=m.getSelectionsService(r).getCurrentLastSelection();if(!t)return!1;const n=r.get(Cs),{worksheet:o,unitId:s,subUnitId:l}=e,c=`${s}|${l}`;c!==n.selectedRangeWorksheet&&(n.rangesStack=[],n.selectedRangeWorksheet=c);const d=o.getMaxRows(),u=o.getMaxColumns(),{expandToGapFirst:h,loop:g}=i,{range:S,primary:f}=t,p=S.endColumn===u-1&&S.endRow===d-1&&S.startRow===0&&S.startColumn===0;n.rangesStack.some(v=>a.Rectangle.equals(v,S))||(n.rangesStack=[],n.rangesStack.push(S));let C;if(p)if(g){if(n.rangesStack.findIndex(b=>a.Rectangle.equals(b,S))!==n.rangesStack.length-1)return!1;C=n.rangesStack[0]}else return!1;else h?(C=m.expandToContinuousRange(S,{left:!0,right:!0,up:!0,down:!0},o),a.Rectangle.equals(C,S)&&(C=na(o))):C=na(o);return n.rangesStack.some(v=>a.Rectangle.equals(v,C))||n.rangesStack.push(C),r.get(a.ICommandService).executeCommand(m.SetSelectionsOperation.id,{unitId:s,subUnitId:l,reveal:!0,selections:[{range:C,primary:f}]})}};var Nu=Object.defineProperty,Du=Object.getOwnPropertyDescriptor,Hu=(r,i,e,t)=>{for(var n=t>1?void 0:t?Du(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&Nu(i,e,n),n},ku=(r,i)=>(e,t)=>i(e,t,r);exports.SheetScrollManagerService=class{constructor(i,e){E(this,"_scrollStateMap",new Map);E(this,"_rawScrollInfo$",new O.BehaviorSubject(null));E(this,"rawScrollInfo$",this._rawScrollInfo$.asObservable());E(this,"validViewportScrollInfo$",new O.BehaviorSubject(null));E(this,"_searchParamForScroll",null);this._context=i,this._sheetSkeletonManagerService=e}dispose(){this._rawScrollInfo$.complete()}calcViewportScrollFromRowColOffset(i){if(!i)return{viewportScrollX:0,viewportScrollY:0};let{sheetViewStartColumn:e,sheetViewStartRow:t,offsetX:n,offsetY:o}=i;t=t||0,o=o||0;const s=this._sheetSkeletonManagerService.getCurrentSkeleton(),l=(s==null?void 0:s.rowHeightAccumulation[t-1])||0,d=((s==null?void 0:s.columnWidthAccumulation[e-1])||0)+n,u=l+o;return{viewportScrollX:d,viewportScrollY:u}}setSearchParam(i){this._searchParamForScroll=i}getScrollStateByParam(i){return this._getCurrentScroll(i)}getCurrentScrollState(){return this._getCurrentScroll(this._searchParamForScroll)}setValidScrollState(i){this._setScrollState(i)}emitRawScrollParam(i){this._emitRawScroll(i)}setValidScrollStateToCurrSheet(i){if(this._searchParamForScroll==null)return;this._setScrollState({...this._searchParamForScroll,...i});const e=this._searchParamForScroll.sheetId,t=this._sheetSkeletonManagerService.getSkeleton(e);t&&t.setScroll(i.viewportScrollX,i.viewportScrollY)}clear(){this._searchParamForScroll!=null&&this._clearByParamAndNotify(this._searchParamForScroll)}_setScrollState(i){const{unitId:e,sheetId:t,sheetViewStartColumn:n,sheetViewStartRow:o,offsetX:s,offsetY:l}=i;this._scrollStateMap.has(e)||this._scrollStateMap.set(e,new Map);const c=this._scrollStateMap.get(e),d={sheetViewStartRow:o,sheetViewStartColumn:n,offsetX:s,offsetY:l};c.set(t,d)}_clearByParamAndNotify(i){this._setScrollState({...i,sheetViewStartRow:0,sheetViewStartColumn:0,offsetX:0,offsetY:0}),this._emitRawScroll({sheetViewStartRow:0,sheetViewStartColumn:0,offsetX:0,offsetY:0})}_getCurrentScroll(i){var s;const e={sheetViewStartRow:0,sheetViewStartColumn:0,offsetX:0,offsetY:0};if(i==null)return e;const{unitId:t,sheetId:n}=i;return((s=this._scrollStateMap.get(t))==null?void 0:s.get(n))||e}_emitRawScroll(i){this._rawScrollInfo$.next(i)}};exports.SheetScrollManagerService=Hu([ku(1,a.Inject(exports.SheetSkeletonManagerService))],exports.SheetScrollManagerService);const je={id:"sheet.operation.set-scroll",type:a.CommandType.OPERATION,handler:(r,i)=>{if(!i)return!1;const{unitId:e,sheetId:t,offsetX:n,offsetY:o,sheetViewStartColumn:s,sheetViewStartRow:l}=i;return r.get(w.IRenderManagerService).getRenderById(e).with(exports.SheetScrollManagerService).emitRawScrollParam({unitId:e,sheetId:t,offsetX:n,offsetY:o,sheetViewStartRow:l,sheetViewStartColumn:s}),!0}},wn={id:"sheet.command.set-scroll-relative",type:a.CommandType.COMMAND,handler:async(r,i)=>{const e=r.get(a.ICommandService),t=r.get(a.IUniverInstanceService),n=r.get(w.IRenderManagerService),o=m.getSheetCommandTarget(t);if(!o)return!1;const{unitId:s,subUnitId:l}=o,d=n.getRenderById(s).with(exports.SheetScrollManagerService).getCurrentScrollState(),{offsetX:u=0,offsetY:h=0}=i||{},{sheetViewStartRow:g=0,sheetViewStartColumn:S=0,offsetX:f=0,offsetY:p=0}=d||{};return e.executeCommand(je.id,{unitId:s,sheetId:l,sheetViewStartRow:g,sheetViewStartColumn:S,offsetX:f+u,offsetY:p+h})}},ze={id:"sheet.command.scroll-view",type:a.CommandType.COMMAND,handler:(r,i)=>{if(!i)return!1;const e=r.get(a.IUniverInstanceService),t=r.get(w.IRenderManagerService),n=m.getSheetCommandTarget(e);if(!n)return!1;const{workbook:o,worksheet:s,unitId:l}=n,d=t.getRenderById(l).with(exports.SheetScrollManagerService).getCurrentScrollState();if(!s)return!1;const{sheetViewStartRow:u,sheetViewStartColumn:h,offsetX:g,offsetY:S}=i,{sheetViewStartColumn:f,sheetViewStartRow:p,offsetX:C,offsetY:v}=d||{},{xSplit:b,ySplit:R}=n.worksheet.getConfig().freeze;return r.get(a.ICommandService).syncExecuteCommand(je.id,{unitId:o.getUnitId(),sheetId:s.getSheetId(),sheetViewStartRow:u!=null?u:p!=null?p:0+R,sheetViewStartColumn:h!=null?h:f!=null?f:0+b,offsetX:g!=null?g:C,offsetY:S!=null?S:v})}},Is={id:"sheet.command.scroll-to-cell",type:a.CommandType.COMMAND,handler:(r,i)=>{const e=r.get(a.IUniverInstanceService);return r.get(w.IRenderManagerService).getRenderById(e.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET).getUnitId()).with(exports.SheetsScrollRenderController).scrollToRange(i.range,i.forceTop,i.forceLeft)}},Vu={id:"sheet.command.scroll-view-reset",type:a.CommandType.COMMAND,handler:async r=>{const i=r.get(a.IUniverInstanceService),e=m.getSheetCommandTarget(i);if(!e)return!1;const{subUnitId:t,unitId:n}=e;return r.get(a.ICommandService).executeCommand(je.id,{unitId:n,sheetId:t,sheetViewStartRow:0,sheetViewStartColumn:0})}};var Wu=Object.defineProperty,Uu=Object.getOwnPropertyDescriptor,Lu=(r,i,e,t)=>{for(var n=t>1?void 0:t?Uu(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&Wu(i,e,n),n},sn=(r,i)=>(e,t)=>i(e,t,r);xe.id,$e.id;const Bu=3;exports.SheetsScrollRenderController=class extends a.Disposable{constructor(i,e,t,n,o,s,l){super(),this._context=i,this._injector=e,this._sheetSkeletonManagerService=t,this._contextService=n,this._commandService=o,this._renderManagerService=s,this._scrollManagerService=l,this._initCommandListener(),this._wheelEventListener(),this._scrollBarEventListener(),this._initSkeletonListener()}_wheelEventListener(){const{scene:i}=this._context;if(!i)return;const e=i.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN);e&&this.disposeWithMe(i.onMouseWheel$.subscribeEvent((t,n)=>{if(t.ctrlKey||!this._contextService.getContextValue(a.FOCUSING_SHEET))return;let o=0,s=0;o=t.deltaX,t.shiftKey?o=(t.deltaY||t.deltaX)*Bu:s=t.deltaY,this._commandService.executeCommand(wn.id,{offsetX:o,offsetY:s}),this._context.scene.makeDirty(!0);const l=e.limitedScroll(e.scrollX+o,e.scrollY+s);l&&!l.isLimitedX&&!l.isLimitedY&&(t.preventDefault(),i.getParent().classType===w.RENDER_CLASS_TYPE.SCENE_VIEWER&&n.stopPropagation()),e.isWheelPreventDefaultX&&e.isWheelPreventDefaultY&&t.preventDefault()}))}_scrollBarEventListener(){const{scene:i}=this._context;if(i==null)return;const e=i.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN);e&&(this.disposeWithMe(a.toDisposable(this._scrollManagerService.rawScrollInfo$.subscribe(t=>{var S;const n=(S=this._sheetSkeletonManagerService.getCurrentParam())==null?void 0:S.skeleton;if(!n)return;if(t==null){e.scrollToViewportPos({viewportScrollX:0,viewportScrollY:0});return}const{sheetViewStartRow:o,sheetViewStartColumn:s,offsetX:l,offsetY:c}=t,{startX:d,startY:u}=n.getCellWithCoordByIndex(o,s,!1),h=d+l,g=u+c;e.scrollToViewportPos({viewportScrollX:h,viewportScrollY:g})}))),this.disposeWithMe(e.onScrollAfter$.subscribeEvent(t=>{var p;const n=(p=this._sheetSkeletonManagerService.getCurrentParam())==null?void 0:p.skeleton;if(n==null||t.isTrigger===!1)return;const o=this._getSheetObject();if(n==null||o==null)return;const{viewportScrollX:s,viewportScrollY:l,scrollX:c,scrollY:d}=t,{row:u,column:h,rowOffset:g,columnOffset:S}=n.getOffsetRelativeToRowCol(s,l),f={sheetViewStartRow:u,sheetViewStartColumn:h,offsetX:S,offsetY:g,viewportScrollX:s,viewportScrollY:l,scrollX:c,scrollY:d};this._scrollManagerService.setValidScrollStateToCurrSheet(f),this._scrollManagerService.validViewportScrollInfo$.next({...f,viewportScrollX:s,viewportScrollY:l,scrollX:c,scrollY:d})})),this.disposeWithMe(e.onScrollByBar$.subscribeEvent(t=>{var g;const n=(g=this._sheetSkeletonManagerService.getCurrentParam())==null?void 0:g.skeleton;if(n==null||t.isTrigger===!1)return;const o=this._getSheetObject();if(n==null||o==null)return;const{viewportScrollX:s=0,viewportScrollY:l=0}=t;this._getFreeze();const{row:c,column:d,rowOffset:u,columnOffset:h}=n.getOffsetRelativeToRowCol(s,l);this._commandService.executeCommand(ze.id,{sheetViewStartRow:c,sheetViewStartColumn:d,offsetX:h,offsetY:u})})))}_initSkeletonListener(){this.disposeWithMe(a.toDisposable(this._sheetSkeletonManagerService.currentSkeletonBefore$.subscribe(i=>{if(i==null)return;const e={unitId:i.unitId,sheetId:i.sheetId};if(this._scrollManagerService.setSearchParam(e),!this._getSheetObject())return;const n=this._scrollManagerService.getScrollStateByParam(e),{viewportScrollX:o,viewportScrollY:s}=this._scrollManagerService.calcViewportScrollFromRowColOffset(n);n&&this._updateViewportScroll(o,s)})))}_updateViewportScroll(i=0,e=0){const t=this._getSheetObject();if(!t)return;const n=t.scene,o=n.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN),s=n.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_COLUMN_RIGHT),l=n.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_ROW_BOTTOM),c=n.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT),d=n.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP);o&&(o.viewportScrollX=i,o.viewportScrollY=e),l&&(l.viewportScrollY=e),s&&(s.viewportScrollX=i),c&&(c.viewportScrollY=e),d&&(d.viewportScrollX=i)}scrollToRange(i,e,t){let{endRow:n,endColumn:o,startColumn:s,startRow:l}=i;const c=this._getViewportBounding();if(i.rangeType===a.RANGE_TYPE.ROW?(s=0,o=0):i.rangeType===a.RANGE_TYPE.COLUMN&&(l=0,n=0),c&&!e&&!t){const d=c.startRow>n?l:n,u=c.startColumn>o?s:o;return this._scrollToCell(d,u)}else return this._scrollToCell(l,s,e,t)}scrollToCell(i,e){const t=this._context.unit.getActiveSheet(),{ySplit:n,xSplit:o}=t.getFreeze();return this._commandService.syncExecuteCommand(ze.id,{sheetViewStartRow:i-n,sheetViewStartColumn:e-o,offsetX:0,offsetY:0})}_initCommandListener(){this.disposeWithMe(this._commandService.onCommandExecuted(i=>{switch(i.id){case m.SetSelectionsOperation.id:{const e=i.params;e.unitId===this._context.unitId&&e.reveal&&this._scrollToSelection()}break;case m.ScrollToCellOperation.id:{const e=i.params;if(e.unitId===this._context.unitId){const t=e.range;this.scrollToRange(t)}}break;case He.id:{const e=i.params;this._scrollToSelectionForExpand(e)}break}}))}_scrollToSelectionForExpand(i){setTimeout(()=>{const e=this._getSelectionsService().getCurrentLastSelection();if(e==null)return;const{startRow:t,startColumn:n,endRow:o,endColumn:s}=e.range,l=this._getViewportBounding();if(l==null)return;const{startRow:c,startColumn:d,endRow:u,endColumn:h}=l;let g=0,S=0;t>c?g=o:o<u?g=t:g=c,n>d?S=s:s<h?S=n:S=d,i.direction===a.Direction.DOWN?g=o:i.direction===a.Direction.UP?g=t:i.direction===a.Direction.RIGHT?S=s:i.direction===a.Direction.LEFT&&(S=n),this._scrollToCell(g,S)},0)}_getFreeze(){var e;const i=(e=this._sheetSkeletonManagerService.getCurrentParam())==null?void 0:e.skeleton.getWorksheetConfig();if(i!=null)return i.freeze}_updateSceneSize(i){var h;if(i==null)return;const{unitId:e}=this._context,{skeleton:t}=i,n=(h=this._renderManagerService.getRenderById(e))==null?void 0:h.scene;if(t==null||n==null)return;const{rowTotalHeight:o,columnTotalWidth:s,rowHeaderWidthAndMarginLeft:l,columnHeaderHeightAndMarginTop:c}=t;this._context.unit.getActiveSheet()&&(n==null||n.transformByState({width:l+s,height:c+o}))}_getSheetObject(){return Ge(this._context.unit,this._context)}_scrollToSelectionByDirection(i){const e=this._getViewportBounding();if(e==null)return!1;const{startRow:t,startColumn:n,endRow:o,endColumn:s}=e;let l=0,c=0;const{startRow:d,startColumn:u,endRow:h,endColumn:g}=i;d>=t&&(l=h),h<=o&&(l=d),u>=n&&(c=g),g<=s&&(c=u),this._scrollToCell(l,c)}_scrollToSelection(i=!0){var d;const e=this._getSelectionsService().getCurrentLastSelection();if(!e)return;const{startRow:t,startColumn:n,actualRow:o,actualColumn:s}=(d=e.primary)!=null?d:e.range,l=i&&o!=null?o:t,c=i&&s!=null?s:n;this._scrollToCell(l,c)}_getSelectionsService(){return m.getSelectionsService(this._injector)}_getViewportBounding(){var o,s;const i=(o=this._getSheetObject())==null?void 0:o.scene;if(i==null)return;const e=i.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN);if(e==null)return;const t=(s=this._sheetSkeletonManagerService.getCurrentParam())==null?void 0:s.skeleton;if(t==null)return;const n=e.calcViewportInfo();return t.getRangeByViewBound(n.viewBound)}_scrollToCell(i,e,t=!1,n=!1){var N,D,H;const{rowHeightAccumulation:o,columnWidthAccumulation:s}=(N=this._sheetSkeletonManagerService.getCurrentSkeleton())!=null?N:{};if(o==null||s==null)return!1;const l=(D=this._getSheetObject())==null?void 0:D.scene;if(l==null)return!1;const c=l.getViewport(w.SHEET_VIEWPORT_KEY.VIEW_MAIN);if(c==null)return!1;const d=(H=this._sheetSkeletonManagerService.getCurrentParam())==null?void 0:H.skeleton;if(d==null)return!1;const u=this._context.unit.getActiveSheet();if(!u)return!1;i=a.Tools.clamp(i,0,o.length-1),e=a.Tools.clamp(e,0,s.length-1);const{startColumn:h,startRow:g,ySplit:S,xSplit:f}=u.getFreeze(),p=this._getViewportBounding();if(p==null)return!1;const{startRow:C,startColumn:v,endRow:b,endColumn:R}=p;let T,M;if(i>=g&&e>=h-S&&(i<=C&&(T=i,t=!0),i>=b)){const W=o[i]-c.height;for(let k=C;k<=i&&(T=k+1,!(o[k]>=W));k++);}if(e>=h&&i>=g-S&&(e<=v&&(M=e,n=!0),e>=R)){const W=s[e]-c.width;for(let k=v;k<=e&&(M=k+1,!(s[k]>=W));k++);}if(T===void 0&&M===void 0)return!1;let{offsetX:y,offsetY:P,sheetViewStartRow:x,sheetViewStartColumn:A}=this._scrollManagerService.getCurrentScrollState()||{};if(T=T?Math.min(T,i):x+S,M=M?Math.min(M,e):A+f,n){y=0,M=e;const W=d.getHiddenColumnsInRange({startColumn:M-f,endColumn:M});M=M-W.length}if(t){P=0,T=i;const W=d.getHiddenRowsInRange({startRow:T-S,endRow:T});T=T-W.length}return this._commandService.syncExecuteCommand(ze.id,{sheetViewStartRow:Math.max(0,T-S),sheetViewStartColumn:Math.max(0,M-f),offsetX:y,offsetY:P})}};exports.SheetsScrollRenderController=Lu([sn(1,a.Inject(a.Injector)),sn(2,a.Inject(exports.SheetSkeletonManagerService)),sn(3,a.IContextService),sn(4,a.ICommandService),sn(5,w.IRenderManagerService),sn(6,a.Inject(exports.SheetScrollManagerService))],exports.SheetsScrollRenderController);const bs={id:"sheet.operation.scroll-to-range",type:a.CommandType.OPERATION,handler:(r,i)=>{if(!i)return!1;const e=r.get(a.IUniverInstanceService);return r.get(w.IRenderManagerService).getRenderById(e.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET).getUnitId()).with(exports.SheetsScrollRenderController).scrollToRange(i.range,i.forceTop,i.forceLeft)}};class rl{constructor(){E(this,"_state",null);E(this,"_rect",null);E(this,"_state$",new O.BehaviorSubject(null));E(this,"state$",this._state$.asObservable());E(this,"_rect$",new O.BehaviorSubject(null));E(this,"rect$",this._rect$.asObservable());E(this,"_focus",!1);E(this,"_focus$",new O.BehaviorSubject(this._focus));E(this,"focus$",this._focus$.asObservable())}dispose(){this._state$.complete(),this._state=null,this._rect$.complete(),this._rect=null}setState(i){this._state=i,this._refresh(i)}getRect(){return this._rect}setRect(i){this._rect=i,this._rect$.next(i)}getState(){return this._state}setFocus(i=!1){this._focus=i,this._focus$.next(i)}_refresh(i){this._state$.next(i)}}const hr=a.createIdentifier("univer.sheet-cell-editor-manager.service"),Fu="36",ju="univer-sheet-container",il={sheetFooterBarHeight:Fu,sheetContainer:ju};var zu=Object.defineProperty,Xu=Object.getOwnPropertyDescriptor,Yu=(r,i,e,t)=>{for(var n=t>1?void 0:t?Xu(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&zu(i,e,n),n},an=(r,i)=>(e,t)=>i(e,t,r);const ra=5,ia=2;exports.SheetCellEditorResizeService=class extends a.Disposable{constructor(i,e,t,n,o,s,l){super(),this._context=i,this._layoutService=e,this._cellEditorManagerService=t,this._editorBridgeService=n,this._renderManagerService=o,this._sheetSkeletonManagerService=s,this._univerInstanceService=l}fitTextSize(i){var A,N,D,H;const e=this._editorBridgeService.getEditCellState();if(!e)return;const{position:t,documentLayoutObject:n,canvasOffset:o,scaleX:s,scaleY:l}=e,{startX:c,startY:d,endX:u,endY:h}=t,g=this._univerInstanceService.getUnit(a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.UniverInstanceType.UNIVER_DOC);if(g==null)return;const S=this._getEditorSkeleton();if(!S)return;const f=this._predictingSize(t,o,S,n,s,l);if(!f)return;let{actualWidth:p,actualHeight:C}=f;const{verticalAlign:v,horizontalAlign:b,paddingData:R,fill:T}=n;p=p+((A=R.l)!=null?A:0)+((N=R.r)!=null?N:0),C=C+((D=R.t)!=null?D:0)+((H=R.b)!=null?H:0);let M=u-c,y=h-d;M<p&&(M=Math.ceil(p)),y<C&&(y=Math.ceil(C));let P=0;v===a.VerticalAlign.MIDDLE?P=(y-C)/2/l:v===a.VerticalAlign.TOP?P=R.t||0:P=(y-C)/l;let x=0;b===a.HorizontalAlign.CENTER?x=(M-p)/2/s:b===a.HorizontalAlign.RIGHT?x=(M-p)/s:x=R.l||0,P=P<(R.t||0)?R.t||0:P,x=x<(R.l||0)?R.l||0:x,g.updateDocumentDataMargin({t:P,l:x}),S.calculate(),this._editAreaProcessing(M,y,t,o,T,s,l,b,i)}_predictingSize(i,e,t,n,o=1,s=1){const{startX:l,endX:c}=i,{textRotation:d,wrapStrategy:u,paddingData:h}=n,g=this._univerInstanceService.getUnit(a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.UniverInstanceType.UNIVER_DOC),{vertexAngle:S}=w.convertTextRotation(d);if(u===a.WrapStrategy.WRAP&&S===0){g==null||g.updateDocumentDataPageSize(c-l),g==null||g.updateDocumentDataMargin({l:h.l,t:h.t}),t.calculate();const{actualWidth:v,actualHeight:b}=t.getActualSize();return{actualWidth:v*o,actualHeight:b*s}}const f=this._getEditorMaxSize(i,e,n.horizontalAlign);if(!f)return;g==null||g.updateDocumentDataPageSize(f.width/o),t.calculate();const p=t.getActualSize();let C=c-l;return C<p.actualWidth*o+ra*o&&(C=p.actualWidth*o+ra*o),g==null||g.updateDocumentDataPageSize(C/o),g==null||g.updateDocumentRenderConfig({horizontalAlign:a.HorizontalAlign.UNSPECIFIED,cellValueType:void 0}),{actualWidth:p.actualWidth*o,actualHeight:p.actualHeight*s}}_getEditorMaxSize(i,e,t){if(this._getEditorObject()==null)return;function o(b){return Number.parseInt(b.replace("px",""))}const l=this._context.engine.getCanvasElement(),c=l.getBoundingClientRect(),d=o(l.style.width),{width:u}=c,h=u/d,{startX:g,startY:S,endX:f}=i,p=this._context.engine.width,C=document.body.clientHeight-S-Number.parseFloat(il.sheetFooterBarHeight)-e.top-ia*2;let v=u-g;if(t===a.HorizontalAlign.CENTER){const b=p-f,R=g;v=f-g+Math.min(R,b)*2}else t===a.HorizontalAlign.RIGHT&&(v=f);return{height:C,width:v-ia,scaleAdjust:h}}_editAreaProcessing(i,e,t,n,o,s=1,l=1,c,d){var k;const u=this._getEditorObject();if(u==null)return;const g=this._context.engine.getCanvasElement();let{startX:S,startY:f}=t;const{document:p,scene:C,engine:v}=u,b=C.getViewport($.VIEWPORT_KEY.VIEW_MAIN),R=this._getEditorMaxSize(t,n,c),{height:T,width:M,scaleAdjust:y}=R;let P=e,x=b==null?void 0:b.getScrollBar();P>T?(x==null?b&&new w.ScrollBar(b,{enableHorizontal:!1,barSize:8}):b==null||b.resetCanvasSizeAndUpdateScroll(),b==null||b.scrollToViewportPos({viewportScrollY:P-T}),P=T):(x=null,(k=b==null?void 0:b.getScrollBar())==null||k.dispose()),i+=(x==null?void 0:x.barSize)||0,i>M&&(i=M),this._addBackground(C,i/s,e/l,o);const{scaleX:A,scaleY:N}=C.getPrecisionScale();C.transformByState({width:i*y/s,height:e*y/l,scaleX:s*y,scaleY:l*y}),p.resize(i*y/s,e*y/l),setTimeout(()=>{v.resizeBySize(w.fixLineWidthByScale(i,A),w.fixLineWidthByScale(P,N)),d==null||d()},0);const D=this._layoutService.getContentElement().getBoundingClientRect(),H=g.getBoundingClientRect();S=S*y+(H.left-D.left),f=f*y+(H.top-D.top);const W=t.endX-t.startX;c===a.HorizontalAlign.RIGHT?S+=(W-i)*y:c===a.HorizontalAlign.CENTER&&(S+=(W-i*y)/2),this._cellEditorManagerService.setState({startX:S,startY:f,endX:i*y+S,endY:P*y+f,show:!0})}_addBackground(i,e,t,n){const o="_backgroundRectHelperColor_",s=i.getObject(o);s==null&&n==null||(s==null?i.addObjects([new w.Rect(o,{width:e,height:t,fill:n,evented:!1})],$.DOCS_COMPONENT_MAIN_LAYER_INDEX):n==null?s.dispose():(s.setProps({fill:n}),s.transformByState({width:e,height:t})))}resizeCellEditor(i){var y;const e=this._cellEditorManagerService.getState();if(!e||!this._editorBridgeService.isVisible().visible)return;this._editorBridgeService.refreshEditCellPosition(!0);const t=this._editorBridgeService.getEditCellState();if(!t)return;const n=(y=this._sheetSkeletonManagerService.getSkeletonParam(t.sheetId))==null?void 0:y.skeleton;if(!n)return;const{row:o,column:s,scaleX:l,scaleY:c,position:d,canvasOffset:u,documentLayoutObject:h}=t,{horizontalAlign:g}=h,S=this._getEditorMaxSize(d,u,g);if(!S)return;const{height:f,width:p,scaleAdjust:C}=S,v=n.getCellWithCoordByIndex(o,s),b=Math.min((v.mergeInfo.endY-v.mergeInfo.startY)*c,f)*C,R=Math.min((v.mergeInfo.endX-v.mergeInfo.startX)*l,p)*C,T=e.endY-e.startY,M=e.endX-e.startX;if(T!==b||M!==R){if(this._editorBridgeService.refreshEditCellPosition(!0),!this._getEditorSkeleton())return;this.fitTextSize(i)}}_getEditorObject(){return wi(this._editorBridgeService.getCurrentEditorId(),this._renderManagerService)}_getEditorSkeleton(){var i;return(i=this._renderManagerService.getRenderById(a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY))==null?void 0:i.with(Re.DocSkeletonManagerService).getSkeleton()}};exports.SheetCellEditorResizeService=Yu([an(1,_.ILayoutService),an(2,hr),an(3,Se),an(4,w.IRenderManagerService),an(5,a.Inject(exports.SheetSkeletonManagerService)),an(6,a.IUniverInstanceService)],exports.SheetCellEditorResizeService);function Rs(r){return r.getContextValue(a.FOCUSING_SHEET)}function ce(r){return r.getContextValue(a.FOCUSING_SHEET)&&r.getContextValue(a.FOCUSING_UNIVER_EDITOR)&&!r.getContextValue(a.EDITOR_ACTIVATED)&&!r.getContextValue(a.FOCUSING_COMMON_DRAWINGS)}function ol(r){return r.getContextValue(a.FOCUSING_SHEET)&&r.getContextValue(a.FOCUSING_UNIVER_EDITOR)&&!r.getContextValue(a.EDITOR_ACTIVATED)&&!r.getContextValue(a.FOCUSING_FX_BAR_EDITOR)&&!r.getContextValue(a.FOCUSING_COMMON_DRAWINGS)}function Gu(r){return r.getContextValue(a.FOCUSING_SHEET)&&r.getContextValue(a.FOCUSING_UNIVER_EDITOR)&&r.getContextValue(a.EDITOR_ACTIVATED)}function Zu(r){return r.getContextValue(a.FOCUSING_UNIVER_EDITOR)&&r.getContextValue(a.EDITOR_ACTIVATED)}function sl(r){return r.getContextValue(a.FOCUSING_FX_BAR_EDITOR)&&r.getContextValue(a.FOCUSING_UNIVER_EDITOR)}function Ku(r){return r.getContextValue(a.FOCUSING_SHEET)&&r.getContextValue(a.EDITOR_ACTIVATED)&&r.getContextValue(a.FOCUSING_EDITOR_INPUT_FORMULA)&&r.getContextValue(a.FOCUSING_UNIVER_EDITOR)}function tr(r){return r.getContextValue(a.FOCUSING_SHEET)&&r.getContextValue(a.FOCUSING_UNIVER_EDITOR)&&r.getContextValue(a.EDITOR_ACTIVATED)&&!r.getContextValue(a.FOCUSING_EDITOR_INPUT_FORMULA)&&!r.getContextValue(a.FOCUSING_EDITOR_STANDALONE)}const al=[_.KeyCode.ARROW_DOWN,_.KeyCode.ARROW_UP,_.KeyCode.ARROW_LEFT,_.KeyCode.ARROW_RIGHT],qu=[_.KeyCode.ENTER,_.KeyCode.TAB,...al];function ll(){const r=[];for(const i of al)r.push({id:zt.id,binding:i,preconditions:e=>tr(e),staticParameters:{visible:!1,eventType:w.DeviceInputEventType.Keyboard,keycode:i,isShift:!1}}),r.push({id:zt.id,binding:i|_.MetaKeys.SHIFT,preconditions:e=>tr(e),staticParameters:{visible:!1,eventType:w.DeviceInputEventType.Keyboard,keycode:i,isShift:!0}});return r}const cl={id:ur.id,binding:_.KeyCode.F2,description:"shortcut.sheet.start-editing",group:"4_sheet-edit",preconditions:ce,staticParameters:{visible:!0,eventType:w.DeviceInputEventType.Keyboard,keycode:_.KeyCode.F2}},dl={id:ve.id,binding:_.KeyCode.ENTER,description:"shortcut.sheet.toggle-editing",group:"4_sheet-edit",preconditions:r=>tr(r),staticParameters:{visible:!1,eventType:w.DeviceInputEventType.Keyboard,keycode:_.KeyCode.ENTER}},ul={id:ve.id,binding:_.KeyCode.TAB,preconditions:r=>tr(r),staticParameters:{visible:!1,eventType:w.DeviceInputEventType.Keyboard,keycode:_.KeyCode.TAB}},hl={id:ve.id,binding:_.KeyCode.ESC,description:"shortcut.sheet.abort-editing",group:"4_sheet-edit",preconditions:r=>tr(r),staticParameters:{visible:!1,eventType:w.DeviceInputEventType.Keyboard,keycode:_.KeyCode.ESC}},ml={id:$.BreakLineCommand.id,description:"shortcut.sheet.break-line",group:"4_sheet-edit",preconditions:r=>Gu(r),binding:_.KeyCode.ENTER|_.MetaKeys.ALT},gl={id:$.DeleteLeftCommand.id,preconditions:r=>Zu(r)||sl(r),binding:_.KeyCode.BACKSPACE},Sl={id:ve.id,description:"shortcut.sheet.delete-and-start-editing",group:"4_sheet-edit",preconditions:r=>ce(r)&&!sl(r),binding:_.KeyCode.BACKSPACE,staticParameters:{visible:!0,eventType:w.DeviceInputEventType.Keyboard,keycode:_.KeyCode.BACKSPACE}};function Qu(r){return r.startsWith("'")}function Ju(r){return r.slice(1)}const $u={"0":"0","1":"1","2":"2","3":"3","4":"4","5":"5","6":"6","7":"7","8":"8","9":"9",A:"A",B:"B",C:"C",D:"D",E:"E",F:"F",G:"G",H:"H",I:"I",J:"J",K:"K",L:"L",M:"M",N:"N",O:"O",P:"P",Q:"Q",R:"R",S:"S",T:"T",U:"U",V:"V",W:"W",X:"X",Y:"Y",Z:"Z",a:"a",b:"b",c:"c",d:"d",e:"e",f:"f",g:"g",h:"h",i:"i",j:"j",k:"k",l:"l",m:"m",n:"n",o:"o",p:"p",q:"q",r:"r",s:"s",t:"t",u:"u",v:"v",w:"w",x:"x",y:"y",z:"z","%":"%","-":"-",".":".",":":":","/":"/","+":"+","@":"@","&":"&","*":"*","#":"#","=":"=",">":">","<":"<","$":"$",""":'"',"'":"'","(":"(",")":")"," ":" ",",":",","!":"!","?":"?",";":";","[":"[","]":"]","{":"{","}":"}"},jo={true:"TRUE",false:"FALSE"};function eh(r){return[a.LocaleType.ZH_CN,a.LocaleType.ZH_TW].includes(r)}function th(r,i,e,t){let n=r;if(eh(e)){if(r.startsWith("'")||r.startsWith("'"))return`'${r.slice(1)}`;n=nh(n)}if(n.startsWith("="))return rh(r,n,i,t);const o=n.toLowerCase();return jo[o]?jo[o]:a.numfmt.parseValue(n)==null?r:n}function nh(r){const i=/['"].*?['"]/g,e=[],t=r.split(i);r.replace(i,o=>(e.push(o),""));let n=t.join("").split("").map(sh).join("");return e.forEach((o,s)=>{n=n.slice(0,s*2)+o+n.slice((s+1)*2)}),n}function rh(r,i,e,t){const n=e.sequenceNodesBuilder(i);if(!n)return r;let o=i,s=0;return n.forEach((l,c)=>{if(typeof l=="object"){const d=l.token;if(jo[d.toLowerCase()]){const u=l.startIndex+s+1,h=l.endIndex+s+2;o=ln(d.toLocaleUpperCase(),o,u,h)}else if(l.nodeType===K.sequenceNodeType.FUNCTION&&ih(d,t,n,c)||l.nodeType===K.sequenceNodeType.REFERENCE){const u=d.indexOf("!");if(u>-1){const h=d.substring(u+1),g=l.startIndex+s+(u+1)+1,S=l.endIndex+s+2;o=ln(h.toLocaleUpperCase(),o,g,S)}else{const h=l.startIndex+s+1,g=l.endIndex+s+2;o=ln(d.toLocaleUpperCase(),o,h,g)}}else if(d.startsWith('"')&&d.endsWith('"')||d.startsWith("'")&&d.endsWith("'")){const u=l.startIndex+s+2,h=l.endIndex+s+1;o=ln(r.slice(u,h),o,u,h)}else if(l.nodeType!==K.sequenceNodeType.ARRAY){const u=a.numfmt.parseValue(d);if(u==null){const h=l.startIndex+s+1,g=l.endIndex+s+2;o=ln(r.slice(h,g),o,h,g)}else if(typeof u.v=="number"&&(u.z===void 0||!a.numfmt.isDate(u.z))){const h=`${u.v}`,g=l.startIndex+s+1,S=l.endIndex+s+2,{processedString:f,offset:p}=ah(d,h);o=ln(f,o,g,S),s+=p}}}}),o}function ih(r,i,e,t){const n=i.getDescriptions();return e[t+1]!==K.matchToken.OPEN_BRACKET?!1:n.get(oh(r).toLocaleUpperCase())!==void 0}function oh(r){const i=/^@+/;return r.replace(i,"")}function sh(r){return $u[r]||r}function ln(r,i,e,t){return i.substring(0,e)+r+i.substring(t)}function ah(r,i){var s,l;const e=((s=r.match(/^\s*/))==null?void 0:s[0])||"",t=((l=r.match(/\s*$/))==null?void 0:l[0])||"",n=e+i+t,o=n.length-r.length;return{processedString:n,offset:o}}const fl="range_selector",pl=r=>r.includes(fl);var lh=Object.defineProperty,ch=Object.getOwnPropertyDescriptor,dh=(r,i,e,t)=>{for(var n=t>1?void 0:t?ch(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&lh(i,e,n),n},ye=(r,i)=>(e,t)=>i(e,t,r);const oa=-1e3;exports.EditingRenderController=class extends a.Disposable{constructor(e,t,n,o,s,l,c,d,u,h,g,S,f,p,C,v){super();E(this,"_cursorChange",0);E(this,"_isUnitEditing",!1);E(this,"_workbookSelections");E(this,"_d");E(this,"_cursorTimeout");this._context=e,this._undoRedoService=n,this._contextService=o,this._renderManagerService=s,this._editorBridgeService=l,this._cellEditorManagerService=c,this._lexerTreeBuilder=d,this._functionService=u,this._textSelectionManagerService=h,this._commandService=g,this._localService=S,this._editorService=f,this._sheetCellEditorResizeService=p,this._univerInstanceService=C,this._sheetInterceptorService=v,this._workbookSelections=t.getWorkbookSelections(this._context.unitId),this.disposeWithMe(this._univerInstanceService.getCurrentTypeOfUnit$(a.UniverInstanceType.UNIVER_SHEET).subscribe(b=>{(b==null?void 0:b.getUnitId())===this._context.unitId?this._d=this._init():(this._disposeCurrent(),this._isUnitEditing&&(this._handleEditorInvisible({visible:!1,eventType:w.DeviceInputEventType.Keyboard,keycode:_.KeyCode.ESC,unitId:this._context.unitId}),this._isUnitEditing=!1))})),this._initEditorVisibilityListener()}dispose(){super.dispose(),this._disposeCurrent()}_disposeCurrent(){var e;(e=this._d)==null||e.dispose(),this._d=null}_init(){const e=new a.DisposableCollection;return this._subscribeToCurrentCell(e),this._initialCursorSync(e),this._listenEditorFocus(e),this._commandExecutedListener(e),this._initSkeletonListener(e),e.add(()=>{clearTimeout(this._cursorTimeout)}),this._cursorTimeout=setTimeout(()=>{this._cursorStateListener(e)},1e3),e}_initEditorVisibilityListener(){this.disposeWithMe(this._editorBridgeService.visible$.pipe(O.distinctUntilChanged((e,t)=>e.visible===t.visible)).subscribe(e=>{(e.unitId===a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY||e.unitId===a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY||e.unitId===this._context.unitId)&&e.visible?(this._isUnitEditing=!0,this._handleEditorVisible(e)):this._isUnitEditing&&(this._handleEditorInvisible(e),this._isUnitEditing=!1)}))}_listenEditorFocus(e){const t=this._getEditorObject();t&&e.add(t.document.onPointerDown$.subscribeEvent(()=>{if(this._isUnitEditing&&this._editorBridgeService.isVisible()){const n=this._editorBridgeService.getEditCellState(),o=this._editorBridgeService.getCurrentEditorId();if(!n||!o)return;this._sheetCellEditorResizeService.fitTextSize()}}))}_initialCursorSync(e){e.add(this._cellEditorManagerService.focus$.pipe(O.filter(t=>!!t)).subscribe(()=>{var n;const t=(n=this._renderManagerService.getCurrentTypeOfRenderer(a.UniverInstanceType.UNIVER_DOC))==null?void 0:n.with($.DocSelectionRenderService);t&&t.sync()}))}_initSkeletonListener(e){const t=new Set(m.COMMAND_LISTENER_SKELETON_CHANGE);t.delete(m.SetWorksheetActiveOperation.id),e.add(this._commandService.onCommandExecuted(n=>{if(t.has(n.id)){switch(n.id){case m.MoveRowsMutation.id:case m.MoveColsMutation.id:case m.RemoveColMutation.id:case m.RemoveRowMutation.id:case m.InsertColMutation.id:case m.InsertRowMutation.id:{const o=this._editorBridgeService.getEditLocation();if(!o)break;const s={startRow:o.row,startColumn:o.column,endRow:o.row,endColumn:o.column},l=m.adjustRangeOnMutation(s,n);if(!l)break;const c=l.startRow,d=l.startColumn;this._editorBridgeService.updateEditLocation(c,d);break}}this._sheetCellEditorResizeService.resizeCellEditor(()=>{this._textSelectionManagerService.refreshSelection({unitId:a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,subUnitId:a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY})})}}))}_subscribeToCurrentCell(e){e.add(this._editorBridgeService.currentEditCellState$.subscribe(t=>{var b,R;if(t==null||this._editorBridgeService.isForceKeepVisible())return;const n=this._editorBridgeService.getEditCellState();if(n==null)return;const{position:o,documentLayoutObject:s,scaleX:l,editorUnitId:c}=n;if(this._contextService.getContextValue(a.FOCUSING_EDITOR_STANDALONE))return;const d=this._getDocumentDataModel();if(d==null)return;const{startX:u,endX:h}=o,{textRotation:g,wrapStrategy:S,documentModel:f}=s,{vertexAngle:p}=w.convertTextRotation(g);S===a.WrapStrategy.WRAP&&p===0&&d.updateDocumentDataPageSize((h-u)/l),this._commandService.syncExecuteCommand($.ReplaceSnapshotCommand.id,{unitId:c,snapshot:f.getSnapshot()}),this._contextService.setContextValue(a.FOCUSING_EDITOR_BUT_HIDDEN,!0),this._textSelectionManagerService.replaceDocRanges([{startOffset:0,endOffset:0}],{unitId:a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,subUnitId:a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY});const C=(b=this._renderManagerService.getRenderById(a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY))==null?void 0:b.with($.DocSelectionRenderService),v=(R=this._renderManagerService.getRenderById(a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY))==null?void 0:R.with($.DocSelectionRenderService);(C!=null&&C.canFocusing||v!=null&&v.canFocusing)&&(this._univerInstanceService.setCurrentUnitForType(a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY),C==null||C.activate(oa,oa,!0))}))}_commandExecutedListener(e){e.add(this._commandService.onCommandExecuted(n=>{if(n.id===Re.RichTextEditingMutation.id){const o=n.params,{unitId:s}=o;if(!this._isCurrentSheetFocused()||pl(s)||(this._editorBridgeService.changeEditorDirty(!0),!this._editorBridgeService.isVisible().visible))return;s===a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY&&this._sheetCellEditorResizeService.fitTextSize()}}));const t=[zt.id];e.add(this._commandService.onCommandExecuted(n=>{if(t.includes(n.id)){const o=n.params,{keycode:s,isShift:l}=o;if(s!=null&&(this._cursorChange===2||this._contextService.getContextValue(a.FOCUSING_FX_BAR_EDITOR))){this._moveInEditor(s,l);return}this._commandService.syncExecuteCommand(ve.id,o)}n.id===ur.id&&(this._cursorChange=2)}))}_handleEditorVisible(e){var S,f,p;const{eventType:t,keycode:n}=e;this._cursorChange=t===w.DeviceInputEventType.PointerDown||t===w.DeviceInputEventType.Dblclick?2:1;const o=this._editorBridgeService.getEditLocation();if(o==null)return;this._commandService.syncExecuteCommand(bs.id,{range:{startRow:o.row,startColumn:o.column,endRow:o.row,endColumn:o.column}}),this._editorBridgeService.refreshEditCellPosition(!1);const{unitId:s,isInArrayFormulaRange:l=!1}=o,c=this._getEditorObject();if(c==null)return;const{document:d,scene:u}=c;this._contextService.setContextValue(a.EDITOR_ACTIVATED,!0);const h=this._getDocumentDataModel(),g=this._getEditorSkeleton(a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY);if(!(!g||!h)){if(this._sheetCellEditorResizeService.fitTextSize(()=>{const C=u.getViewport($.VIEWPORT_KEY.VIEW_MAIN);C==null||C.scrollToViewportPos({viewportScrollX:Number.POSITIVE_INFINITY,viewportScrollY:Number.POSITIVE_INFINITY})}),t===w.DeviceInputEventType.Keyboard&&n===_.KeyCode.F2){d.makeDirty(),this._textSelectionManagerService.replaceDocRanges([{startOffset:0,endOffset:0}]);const C=((f=(S=h.getBody())==null?void 0:S.dataStream.length)!=null?f:2)-2;this._textSelectionManagerService.replaceDocRanges([{startOffset:C,endOffset:C}],{unitId:a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,subUnitId:a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY})}else if(t===w.DeviceInputEventType.Keyboard||t===w.DeviceInputEventType.Dblclick&&l)this._emptyDocumentDataModel(h.getSnapshot().documentStyle,!!l),d.makeDirty(),(n===_.KeyCode.BACKSPACE||t===w.DeviceInputEventType.Dblclick)&&(g.calculate(),this._editorBridgeService.changeEditorDirty(!0)),this._textSelectionManagerService.replaceDocRanges([{startOffset:0,endOffset:0}],{unitId:a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,subUnitId:a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY});else if(t===w.DeviceInputEventType.Dblclick){if(this._contextService.getContextValue(a.FOCUSING_EDITOR_INPUT_FORMULA))return;const C=h.getBody().dataStream.length-2||0;this._textSelectionManagerService.replaceDocRanges([{startOffset:C,endOffset:C}])}(p=this._renderManagerService.getRenderById(s))==null||p.scene.resetCursor()}}async _handleEditorInvisible(e){const t=this._editorBridgeService.getEditCellState(),n=this._univerInstanceService.getUnit(a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY),o=a.Tools.deepClone(n==null?void 0:n.getSnapshot());let{keycode:s}=e;if(this._cursorChange=0,this._exitInput(e),t==null)return;this._editorBridgeService.getEditorDirty()===!1&&(s=_.KeyCode.ESC);const d=this._context.unit.getActiveSheet(),u=this._context.unitId,h=d.getSheetId(),{unitId:g,sheetId:S}=t;if(u===g&&S!==h&&await this._commandService.executeCommand(m.SetWorksheetActivateCommand.id,{subUnitId:S,unitId:g}),s===_.KeyCode.ESC){this._editorBridgeService.isForceKeepVisible()&&this._editorBridgeService.disableForceKeepVisible();const f=this._workbookSelections.getCurrentSelections();f&&(this._contextService.setContextValue(m.REF_SELECTIONS_ENABLED,!1),this._commandService.syncExecuteCommand(m.SetSelectionsOperation.id,{unitId:this._context.unit.getUnitId(),subUnitId:S,selections:f}));return}o&&await this._submitCellData(o),this._moveCursor(s)}_getEditorObject(){return wi(this._editorBridgeService.getCurrentEditorId(),this._renderManagerService)}submitCellData(e){return this._submitCellData(e.getSnapshot())}async _submitCellData(e){const t=this._editorBridgeService.getEditCellState();if(t==null)return;const{unitId:n,sheetId:o,row:s,column:l}=t,c=this._context.unit;let d=c.getActiveSheet();if(!c.getSheetBySheetId(o))return;d=c.getActiveSheet();const h=uh({...d.getCellRaw(s,l)||{}},e,this._lexerTreeBuilder,this._localService,this._functionService,c.getStyles());if(!h)return;const g=await this._sheetInterceptorService.onWriteCell(c,d,s,l,h);g!==d.getCellRaw(s,l)&&this._commandService.executeCommand(m.SetRangeValuesCommand.id,{subUnitId:o,unitId:n,range:{startRow:s,startColumn:l,endRow:s,endColumn:l},value:g})}_exitInput(e){var o;this._contextService.setContextValue(a.FOCUSING_EDITOR_INPUT_FORMULA,!1),this._contextService.setContextValue(a.EDITOR_ACTIVATED,!1),this._contextService.setContextValue(a.FOCUSING_EDITOR_BUT_HIDDEN,!1),this._contextService.setContextValue(a.FOCUSING_FX_BAR_EDITOR,!1),this._cellEditorManagerService.setState({show:e.visible});const t=this._getEditorObject();(o=t==null?void 0:t.scene.getViewport($.VIEWPORT_KEY.VIEW_MAIN))==null||o.scrollToViewportPos({viewportScrollX:0,viewportScrollY:0});const n=this._editorBridgeService.getCurrentEditorId();n==null||!this._editorService.isSheetEditor(n)||(this._undoRedoService.clearUndoRedo(n),this._undoRedoService.clearUndoRedo(a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY))}_moveCursor(e){if(e==null||!qu.includes(e))return;let t=a.Direction.LEFT;switch(e){case _.KeyCode.ENTER:t=a.Direction.DOWN;break;case _.KeyCode.TAB:t=a.Direction.RIGHT;break;case _.KeyCode.ARROW_DOWN:t=a.Direction.DOWN;break;case _.KeyCode.ARROW_UP:t=a.Direction.UP;break;case _.KeyCode.ARROW_LEFT:t=a.Direction.LEFT;break;case _.KeyCode.ARROW_RIGHT:t=a.Direction.RIGHT;break}e===_.KeyCode.ENTER||e===_.KeyCode.TAB?this._commandService.executeCommand($e.id,{keycode:e,direction:t}):this._commandService.executeCommand(xe.id,{direction:t})}_cursorStateListener(e){const t=this._getEditorObject();if(!(t!=null&&t.document))return;const{document:n}=t;e.add(a.toDisposable(n.onPointerDown$.subscribeEvent(()=>{this._cursorChange===1&&(this._cursorChange=2)})))}_moveInEditor(e,t){let n=a.Direction.LEFT;e===_.KeyCode.ARROW_DOWN?n=a.Direction.DOWN:e===_.KeyCode.ARROW_UP?n=a.Direction.UP:e===_.KeyCode.ARROW_RIGHT&&(n=a.Direction.RIGHT),t?this._commandService.executeCommand($.MoveSelectionOperation.id,{direction:n}):this._commandService.executeCommand($.MoveCursorOperation.id,{direction:n})}_getDocumentDataModel(){return this._univerInstanceService.getUnit(a.DOCS_NORMAL_EDITOR_UNIT_ID_KEY,a.UniverInstanceType.UNIVER_DOC)}_isCurrentSheetFocused(){var e;return((e=this._univerInstanceService.getFocusedUnit())==null?void 0:e.getUnitId())===this._context.unitId}_getEditorSkeleton(e){var t;return(t=this._renderManagerService.getRenderById(e))==null?void 0:t.with(Re.DocSkeletonManagerService).getSkeleton()}_getEditorViewModel(e){var t;return(t=this._renderManagerService.getRenderById(e))==null?void 0:t.with(Re.DocSkeletonManagerService).getViewModel()}_emptyDocumentDataModel(e,t){const n=(l,c)=>{const d=a.Tools.deepClone(l.getSnapshot()),u=this._getEditorViewModel(l.getUnitId());u!=null&&(hh(d.body,t),c&&(d.documentStyle=e),d.drawings={},d.drawingsOrder=[],l.reset(d),u.reset(l))},o=this._getDocumentDataModel();o&&n(o,!0);const s=this._univerInstanceService.getUnit(a.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,a.UniverInstanceType.UNIVER_DOC);s&&n(s)}};exports.EditingRenderController=dh([ye(1,a.Inject(m.SheetsSelectionsService)),ye(2,a.IUndoRedoService),ye(3,a.IContextService),ye(4,w.IRenderManagerService),ye(5,Se),ye(6,hr),ye(7,a.Inject(K.LexerTreeBuilder)),ye(8,K.IFunctionService),ye(9,a.Inject(Re.DocSelectionManagerService)),ye(10,a.ICommandService),ye(11,a.Inject(a.LocaleService)),ye(12,$.IEditorService),ye(13,a.Inject(exports.SheetCellEditorResizeService)),ye(14,a.IUniverInstanceService),ye(15,a.Inject(m.SheetInterceptorService))],exports.EditingRenderController);function uh(r,i,e,t,n,o){var h,g,S;if((i==null?void 0:i.body)==null)return null;const{body:s}=i;r.t=void 0;const l=s.dataStream;let d=l.substring(l.length-2,l.length)===a.DEFAULT_EMPTY_DOCUMENT_VALUE?l.substring(0,l.length-2):l;const u=t.getCurrentLocale();if(d=th(d,e,u,n),(h=i.drawingsOrder)!=null&&h.length)r.v="",r.f=null,r.si=null,r.p=i,r.t=a.CellValueType.STRING;else if(r.s&&bd.isTextFormat((S=(g=o==null?void 0:o.get(r.s))==null?void 0:g.n)==null?void 0:S.pattern)){r.v=d,r.f=null,r.si=null,r.p=null,r.t=a.CellValueType.STRING,zo(s)&&s.dataStream!==`\r
2
2
  `&&(r.p=i);const f=sa(i);f&&(r.s=f)}else if(a.isFormulaString(d)){if(r.f===d)return null;const f=e.checkIfAddBracket(d);for(let p=0;p<f;p++)d+=K.matchToken.CLOSE_BRACKET;r.f=d,r.si=null,r.v=null,r.p=null}else if(Qu(d)){const f=Ju(d);r.v=f,r.f=null,r.si=null,r.p=null,r.t=a.CellValueType.FORCE_STRING}else if(zo(s))s.dataStream===`\r
3
3
  `?(r.v="",r.f=null,r.si=null,r.p=null):(r.p=i,r.v=null,r.f=null,r.si=null);else{if(d===""&&r.v==null&&r.p==null)return null;r.v=d,r.f=null,r.si=null,r.p=null;const f=sa(i);f&&(r.s=f)}return r}function zo(r){const{textRuns:i=[],paragraphs:e=[],customRanges:t,customBlocks:n=[]}=r,o=r.dataStream.replace(/(\r\n)+$/,""),s=["va"];return i.filter(l=>l.st<o.length).some(l=>{var d;return!!(l.ts&&Object.keys(l.ts).some(u=>s.includes(u)))||Object.keys((d=l.ts)!=null?d:{}).length&&l.ed-l.st<o.length})||e.some(l=>l.bullet)||e.length>=2||!!(t!=null&&t.length)||n.length>0}function sa(r){const{body:i}=r;if(!i)return null;const{textRuns:e=[]}=i;let t={};const n=i.dataStream.replace(`\r
4
4
  `,"");return e.forEach(o=>{const{st:s,ed:l,ts:c}=o;l-s>=n.length&&(t={...t,...c})}),Object.keys(t).length?t:null}function hh(r,i=!1){r.dataStream=a.DEFAULT_EMPTY_DOCUMENT_VALUE,r.textRuns!=null&&(r.textRuns.length===1&&!i?(r.textRuns[0].st=0,r.textRuns[0].ed=1):r.textRuns=void 0),r.paragraphs!=null&&(r.paragraphs=[{startIndex:0}]),r.sectionBreaks!=null&&(r.sectionBreaks=void 0),r.tables!=null&&(r.tables=void 0),r.customRanges!=null&&(r.customRanges=void 0),r.customBlocks!=null&&(r.customBlocks=void 0)}var mh=Object.defineProperty,gh=Object.getOwnPropertyDescriptor,Sh=(r,i,e,t)=>{for(var n=t>1?void 0:t?gh(i,e):i,o=r.length-1,s;o>=0;o--)(s=r[o])&&(n=(t?s(i,e,n):s(n))||n);return t&&n&&mh(i,e,n),n},Gi=(r,i)=>(e,t)=>i(e,t,r);const aa=ms.MARK_SELECTION,qt=a.createIdentifier("univer.mark-selection-service");exports.MarkSelectionService=class extends a.Disposable{constructor(e,t,n){super();E(this,"_shapeMap",new Map);this._currentService=e,this._renderManagerService=t,this._themeService=n}addShape(e,t=[],n=aa){var d;const o=this._currentService.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET),s=(d=o.getActiveSheet())==null?void 0:d.getSheetId();if(!s)return null;const l=a.Tools.generateRandomId(),c={selection:e,subUnitId:s,unitId:o.getUnitId(),zIndex:n,control:null,exits:t};return this._shapeMap.set(l,c),this.refreshShapes(),l}addShapeWithNoFresh(e,t=[],n=aa){var c;const o=this._currentService.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET),s=(c=o.getActiveSheet())==null?void 0:c.getSheetId();if(!s)return null;const l=a.Tools.generateRandomId();return this._shapeMap.set(l,{selection:e,subUnitId:s,unitId:o.getUnitId(),zIndex:n,control:null,exits:t}),l}refreshShapes(){var o;const e=this._currentService.getCurrentUnitForType(a.UniverInstanceType.UNIVER_SHEET);if(!e)return;const t=e.getUnitId(),n=(o=e.getActiveSheet())==null?void 0:o.getSheetId();this._shapeMap.forEach(s=>{var R;const{unitId:l,subUnitId:c,selection:d,control:u,zIndex:h}=s;if(u==null||u.dispose(),l!==t||c!==n)return;const g=this._renderManagerService.getRenderById(l);if(!g)return;const S=(R=this._renderManagerService.withCurrentTypeOfUnit(a.UniverInstanceType.UNIVER_SHEET,exports.SheetSkeletonManagerService))==null?void 0:R.getCurrentSkeleton();if(!S)return;const{scene:f}=g,{rowHeaderWidth:p,columnHeaderHeight:C}=S,v=new lr(f,h,this._themeService,{enableAutoFill:!1,highlightHeader:!1,rowHeaderWidth:p,columnHeaderHeight:C}),b=Me(d,S);v.updateRangeBySelectionWithCoord(b),s.control=v})}getShapeMap(){return this._shapeMap}removeShape(e){const t=this._shapeMap.get(e);if(!t)return;const{control:n}=t;n==null||n.dispose(),this._shapeMap.delete(e)}removeAllShapes(){for(const e of this._shapeMap.values()){const{control:t}=e;t==null||t.dispose()}this._shapeMap.clear()}};exports.MarkSelectionService=Sh([Gi(0,a.IUniverInstanceService),Gi(1,w.IRenderManagerService),Gi(2,a.Inject(a.ThemeService))],exports.MarkSelectionService);function Cl(r){return{strokeWidth:1.5,stroke:r.getCurrentTheme().primaryColor,fill:"rgba(178, 178, 178, 0.10)",widgets:{},strokeDash:8}}const fh=10,ph=6;function Ch(){return a.Tools.generateRandomId(ph)}function vh(r){const i=r.match(/data-copy-id="([^\s]+)"/);return i&&i[1]?i[1]:null}class _h{constructor(){E(this,"_cache",new a.LRUMap(fh))}set(i,e){this._cache.set(i,e)}get(i){return this._cache.get(i)}del(i){this._cache.delete(i)}clear(){this._cache.clear()}clearWithUnitId(i){this._cache.forEach((e,t)=>{e.unitId===i&&this._cache.delete(t)})}}function ti(r,i){const e=i!=null?i:r.style,t={},n=r.tagName.toLowerCase();switch(n){case"b":case"em":case"strong":{t.bl=a.BooleanNumber.TRUE;break}case"s":{t.st={s:a.BooleanNumber.TRUE};break}case"u":{t.ul={s:a.BooleanNumber.TRUE};break}case"i":{t.it=a.BooleanNumber.TRUE;break}case"sub":case"sup":{t.va=n==="sup"?a.BaselineOffset.SUPERSCRIPT:a.BaselineOffset.SUBSCRIPT;break}}return Ih(e,t),t}function Ih(r,i){if(r instanceof CSSStyleDeclaration)for(let e=0;e<r.length;e++){const t=r[e],n=r.getPropertyValue(t);la(t,n,i)}else for(const e in r){const t=r[e];la(e,t,i)}}function la(r,i,e){switch(r){case"font-family":e.ff=i;break;case"font-size":{const t=Number.parseInt(i);Number.isNaN(t)||(i.endsWith("pt")?e.fs=t:i.endsWith("px")&&(e.fs=t*.75));break}case"font-style":i==="italic"&&(e.it=a.BooleanNumber.TRUE);break;case"font-weight":{(Number(i)>400||i==="bold")&&(e.bl=a.BooleanNumber.TRUE);break}case"text-decoration":{/underline/.test(i)?e.ul={s:a.BooleanNumber.TRUE}:/overline/.test(i)?e.ol={s:a.BooleanNumber.TRUE}:/line-through/.test(i)&&(e.st={s:a.BooleanNumber.TRUE});break}case"color":{try{const t=new a.ColorKit(i);t.isValid&&(e.cl={rgb:t.toRgbString()})}catch{}break}case"background-color":{const t=new a.ColorKit(i),n=t.isValid?t.toRgbString():"";n!==_.DEFAULT_BACKGROUND_COLOR_RGB&&n!==_.DEFAULT_BACKGROUND_COLOR_RGBA&&(e.bg={rgb:n});break}}}function bh(r){const i=document.createElement("body");return i.innerHTML=r,i}function vl(r){const i=r.style,e={};for(let t=0;t<i.length;t++){const n=i[t],o=i.getPropertyValue(n);switch(n){case"margin-top":{const s=Number.parseInt(o);e.spaceAbove={v:/pt/.test(o)?w.ptToPixel(s):s};break}case"margin-bottom":{const s=Number.parseInt(o);e.spaceBelow={v:/pt/.test(o)?w.ptToPixel(s):s};break}case"line-height":{const s=Number.parseFloat(o);e.lineSpacing=s;break}}}return Object.getOwnPropertyNames(e).length?e:null}function Zi(r,i){const e=[];for(let t=0,n=r.length;t<n;t++)r[t]===a.DataStreamTreeTokenType.PARAGRAPH&&e.push({startIndex:t});return e}function ca(r,i,e){var s,l,c;const t=i.length,o=((s=e==null?void 0:e.length)!=null?s:0)===1&&e[0].st===0&&e[0].ed===t;return r.p?o&&((c=(l=r.p.body)==null?void 0:l.textRuns)!=null&&c.length)?(r.p.body.textRuns=[],{...r,s:e[0].ts}):r:o?{...r,s:e[0].ts}:r}const da=["color","background","font-size","text-align","vertical-align","font-weight","font-style","font-family","text-decoration","white-space","word-wrap","border-left","border-right","border-top","border-bottom","--data-rotate"];function ua(r,i){const e=r.tagName.toLowerCase();return typeof i=="string"?e===i:Array.isArray(i)?i.some(t=>t===e):i(r)}const Ti=class Ti{constructor(i){E(this,"_styleMap",new Map);E(this,"_styleCache",new Map);E(this,"_styleRules",[]);E(this,"_afterProcessRules",[]);E(this,"_dom",null);E(this,"_getCurrentSkeleton");this._getCurrentSkeleton=i.getCurrentSkeleton}static use(i){if(this._pluginList.includes(i))throw new Error(`Univer paste plugin ${i.name} already added`);this._pluginList.push(i)}convert(i){const e=Ti._pluginList.find(p=>p.checkPasteType(i));e&&(this._styleRules=[...e.stylesRules],this._afterProcessRules=[...e.afterProcessRules]);const t=new a.ObjectMatrix;this._dom=bh(i);const n=this._dom.querySelector("style");if(n){const p=document.createElement("div"),C=p.attachShadow({mode:"open"});document.body.appendChild(p),C.appendChild(n);for(const v of n.sheet.cssRules){const b=v,R=b.selectorText,T=b.style;this._styleMap.set(R,T)}n.remove(),p.remove()}const o={dataStream:"",textRuns:[]},s=[],l=[],c=i.match(/<table\b[^>]*>([\s\S]*?)<\/table>/gi),d=[];this.process(null,this._dom.childNodes,o,d);const{paragraphs:u,dataStream:h,textRuns:g,payloads:S,customRanges:f}=o;if(u){const p=u.map(C=>C.startIndex+1);p.unshift(0);for(let C=0;C<p.length;C++){let v;if(C===p.length-1){if(v=`${h.substring(p[C])}\r