/**
 * Minified by jsDelivr using Terser v5.48.0.
 * Original file: /npm/@univerjs/sheets-find-replace@1.5.2/lib/es/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
import{ColorKit,CommandType,DependentOn,Disposable,EDITOR_ACTIVATED,ICommandService,IConfigService,IContextService,IUndoRedoService,IUniverInstanceService,Inject,Injector,ObjectMatrix,Plugin,Rectangle,ThemeService,Tools,UniverInstanceType,fromCallback,groupBy,merge,regexp,replaceInDocumentBody,rotate}from"@univerjs/core";import{SelectRangeCommand,SetRangeValuesCommand,SetSelectionsOperation,SetWorksheetActivateCommand,SetWorksheetActiveOperation,SheetsSelectionsService,UniverSheetsPlugin}from"@univerjs/sheets";import{IRenderManagerService,RENDER_RAW_FORMULA_KEY,Rect,Shape,UniverRenderEnginePlugin,convertTransformToOffsetY}from"@univerjs/engine-render";import{CloseFindDialogOperation,FindBy,FindDirection,FindModel,FindScope,IFindReplaceService,UniverFindReplaceMobileUIPlugin,UniverFindReplacePlugin}from"@univerjs/find-replace";import{ScrollToCellCommand,SetScrollRelativeCommand,SheetSkeletonManagerService,UniverSheetsMobileUIPlugin,UniverSheetsUIPlugin,getCoordByCell,getSheetObject,getViewportByCell}from"@univerjs/sheets-ui";import{Subject,debounceTime,filter,merge as merge$1,skip,throttleTime}from"rxjs";const SheetReplaceCommand={id:"sheet.command.replace",type:CommandType.COMMAND,handler:async(e,t)=>{const i=e.get(IUndoRedoService),n=e.get(ICommandService),{unitId:r,replacements:o}=t,s=i.__tempBatchingUndoRedo(r),a=await Promise.all(o.map(e=>n.executeCommand(SetRangeValuesCommand.id,{unitId:r,subUnitId:e.subUnitId,value:e.value})));return s.dispose(),getReplaceAllResult(a,o)}};function getReplaceAllResult(e,t){let i=0,n=0;return e.forEach((e,r)=>{const o=t[r].count;e?i+=o:n+=o}),{success:i,failure:n}}function _typeof(e){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},_typeof(e)}function toPrimitive(e,t){if("object"!=_typeof(e)||!e)return e;var i;if("undefined"!=typeof Symbol&&void 0!==(i=e[Symbol.toPrimitive])){var n=i.call(e,t||"default");if("object"!=_typeof(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}function toPropertyKey(e){var t=toPrimitive(e,"string");return"symbol"==_typeof(t)?t:t+""}function _defineProperty(e,t,i){return(t=toPropertyKey(t))in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}var SheetFindReplaceHighlightShape=class extends Shape{constructor(e,t){super(e,t),_defineProperty(this,"_activated",!1),_defineProperty(this,"_inHiddenRange",!1),_defineProperty(this,"_color",void 0),t&&this.setShapeProps(t)}setShapeProps(e){this._activated=!!e.activated,void 0!==e.inHiddenRange&&(this._inHiddenRange=e.inHiddenRange),void 0!==e.color&&(this._color=e.color),this.transformByState({width:e.width,height:e.height})}_draw(e){const t=this._activated,i=`rgba(${this._color.r}, ${this._color.g}, ${this._color.b}, 0.35)`,n=`rgb(${this._color.r}, ${this._color.g}, ${this._color.b})`;Rect.drawWith(e,{width:this.width,height:this.height,fill:i,stroke:t?n:void 0,strokeWidth:t?2:0,evented:!1})}};function isSamePosition(e,t){return e.startRow===t.startRow&&e.startColumn===t.startColumn}function isBehindPositionWithRowPriority(e,t){return e.startRow<t.startRow||e.startRow===t.startRow&&e.startColumn<=t.startColumn}function isBehindPositionWithColumnPriority(e,t){return e.startColumn<t.startColumn||e.startColumn===t.startColumn&&e.startRow<=t.startRow}function isBeforePositionWithRowPriority(e,t){return e.startRow>t.startRow||e.startRow===t.startRow&&e.startColumn>=t.startColumn}function isBeforePositionWithColumnPriority(e,t){return e.startColumn>t.startColumn||e.startColumn===t.startColumn&&e.startRow>=t.startRow}function isSelectionSingleCell(e,t){const{range:i}=e,{startRow:n,startColumn:r}=i,o=t.getMergedCell(n,r);return o?Rectangle.equals(i,o):i.endRow===i.startRow&&i.endColumn===i.startColumn}function __decorateParam(e,t){return function(i,n){t(i,n,e)}}function __decorate(e,t,i,n){var r,o=arguments.length,s=o<3?t:null===n?n=Object.getOwnPropertyDescriptor(t,i):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,i,n);else for(var a=e.length-1;a>=0;a--)(r=e[a])&&(s=(o<3?r(s):o>3?r(t,i,s):r(t,i))||s);return o>3&&s&&Object.defineProperty(t,i,s),s}let SheetsFindReplaceController=class extends Disposable{constructor(e,t,i,n){super(),this._injector=e,this._contextService=t,this._findReplaceService=i,this._commandService=n,_defineProperty(this,"_provider",void 0),this._init(),this._initCommands()}dispose(){super.dispose(),this._findReplaceService.terminate(),this._provider.dispose()}_init(){const e=this._injector.createInstance(SheetsFindReplaceProvider);this._provider=e,this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(e)),this.disposeWithMe(this._contextService.subscribeContextValue$(EDITOR_ACTIVATED).pipe(filter(e=>!!e)).subscribe(()=>{this._commandService.executeCommand(CloseFindDialogOperation.id).catch(()=>{})}))}_initCommands(){[SheetReplaceCommand].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e)))}};SheetsFindReplaceController=__decorate([__decorateParam(0,Inject(Injector)),__decorateParam(1,IContextService),__decorateParam(2,IFindReplaceService),__decorateParam(3,ICommandService)],SheetsFindReplaceController);const SHEETS_FIND_REPLACE_PROVIDER_NAME="sheets-find-replace-provider",FIND_REPLACE_Z_INDEX=1e4;let SheetFindModel=class extends FindModel{get _matchesCount(){return this._matches.length}get unitId(){return this._workbook.getUnitId()}get matchesCount(){return this._matchesCount}get matchesPosition(){return this._matchesPosition}get currentMatch(){return this._matchesPosition>0?this._matches[this._matchesPosition-1]:null}constructor(e,t,i,n,r,o,s,a){super(),this._workbook=e,this._sheetSkeletonManagerService=t,this._univerInstanceService=i,this._renderManagerService=n,this._commandService=r,this._contextService=o,this._themeService=s,_defineProperty(this,"_matchesUpdate$",new Subject),_defineProperty(this,"matchesUpdate$",this._matchesUpdate$.asObservable()),_defineProperty(this,"_activelyChangingMatch$",new Subject),_defineProperty(this,"activelyChangingMatch$",this._activelyChangingMatch$.asObservable()),_defineProperty(this,"_matchesByWorksheet",new Map),_defineProperty(this,"_matches",[]),_defineProperty(this,"_matchesPosition",0),_defineProperty(this,"_activeHighlightIndex",-1),_defineProperty(this,"_highlightShapes",[]),_defineProperty(this,"_currentHighlightShape",null),_defineProperty(this,"_query",null),_defineProperty(this,"_workbookSelections",void 0),this._workbookSelections=a.getWorkbookSelections(this.unitId)}dispose(){super.dispose(),this._disposeHighlights(),this._toggleDisplayRawFormula(!1)}getMatches(){return this._matches}start(e){switch(this._query=e,e.findBy===FindBy.FORMULA?this._toggleDisplayRawFormula(!0):this._toggleDisplayRawFormula(!1),e.findScope){case FindScope.UNIT:this.findInWorkbook(e);break;case FindScope.SUBUNIT:default:this.findInActiveWorksheet(e)}}focusSelection(){const e=this.currentMatch;e&&this._commandService.executeCommand(SelectRangeCommand.id,{unitId:e.unitId,subUnit:e.range.subUnitId,range:e.range.range})}_toggleDisplayRawFormula(e){this._contextService.setContextValue(RENDER_RAW_FORMULA_KEY,e)}findInWorkbook(e){const t=this._workbook.getUnitId();let i,n=!0;const r=()=>{const r=this._workbook.getSheets().filter(e=>!e.isSheetHidden()).map(i=>{const n=this._findInWorksheet(i,e,t),r=i.getSheetId(),{results:o}=n;return o.length?this._matchesByWorksheet.set(r,n.results):this._matchesByWorksheet.delete(r),n});this._matches=r.map(e=>e.results).flat(),this._updateFindHighlight(),n?(i={results:this._matches},n=!1):this._matchesUpdate$.next(this._matches)};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>{this._updateFindHighlight(),this._updateCurrentHighlightShape(this._activeHighlightIndex)})),this.disposeWithMe(fromCallback(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(filter(([e,t])=>e.id===SetWorksheetActiveOperation.id&&!(null==t?void 0:t.fromFindReplace))).subscribe(()=>{const e=this._workbook.getActiveSheet();if(!e)return;const t=e.getSheetId();this._matchesByWorksheet.has(t)&&this._findNextMatchOnActiveSheetChange(e)})),this.disposeWithMe(fromCallback(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(filter(([e])=>e.type===CommandType.MUTATION&&e.params.unitId===this._workbook.getUnitId()),throttleTime(600,void 0,{leading:!1,trailing:!0})).subscribe(()=>r())),r(),i}_findNextMatchOnActiveSheetChange(e){let t,i,n=0;const r=this._matchesByWorksheet.get(e.getSheetId()),o=this._workbookSelections.getCurrentSelections();(null==o?void 0:o.length)?([t,n]=this._findNextMatchByRange(r,o[0].range),i=r.findIndex(e=>e===t)):(t=r[0],i=0,n=this._matches.findIndex(e=>e===t)),this._matchesPosition=n+1,this._activelyChangingMatch$.next(t),this._activeHighlightIndex=i,this._updateFindHighlight(),this._updateCurrentHighlightShape(i)}findInActiveWorksheet(e){const t=this._workbook.getUnitId(),i=()=>{const e=this._workbook.getActiveSheet();if(!e)return!1;const t=this._workbookSelections.getCurrentSelections();return(null==t?void 0:t.some(t=>!isSelectionSingleCell(t,e)))??!1};let n,r=!0,o=!1;const s=()=>{const s=this._workbook.getActiveSheet();if(!s)return{results:[]};const a=this.currentMatch;o=i();const c=this._workbookSelections.getCurrentSelections(),h=o?this._findInSelections(s,c,e,t):this._findInWorksheet(s,e,t);return this._matches=h.results,this._matchesPosition=this._tryRestoreLastMatchesPosition(a,this._matches),r?(n=h,r=!1):this._matchesUpdate$.next(this._matches),this._updateFindHighlight(),h};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>this._updateFindHighlight())),this.disposeWithMe(merge$1(fromCallback(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(filter(([e])=>{if(e.type===CommandType.MUTATION&&e.params.unitId===this._workbook.getUnitId())return!0;if(e.id===SetSelectionsOperation.id&&e.params.unitId===t){const e=i();return(!1!==e||!1!==o)&&(o=e,!0)}return!1})),this._workbook.activeSheet$.pipe(skip(1))).pipe(debounceTime(200)).subscribe(()=>s())),s(),n}_findInRange(e,t,i,n,r){const o=[],s=e.getSheetId(),a=(t.findDirection===FindDirection.COLUMN?e.iterateByColumn:e.iterateByRow).bind(e)(i);for(const i of a){const{row:a,col:c,colSpan:h,rowSpan:l,value:d}=i;if((null==r?void 0:r(a,c))||!d)continue;if(e.getRowFiltered(a))continue;const{hit:u,replaceable:_,isFormula:g}=hitCell(e,a,c,t,d);if(u){const e={provider:"sheets-find-replace-provider",unitId:n,replaceable:_,isFormula:g,range:{subUnitId:s,range:{startRow:a,startColumn:c,endColumn:c+(h??1)-1,endRow:a+(l??1)-1}}};o.push(e)}}return{results:o}}_findInSelections(e,t,i,n){const{findDirection:r}=i,o=r===FindDirection.ROW?isBehindPositionWithRowPriority:isBehindPositionWithColumnPriority,s=new Set;return{results:t.map(t=>this._findInRange(e,i,t.range,n,(e,t)=>{const i=`${e}-${t}`;return!!s.has(i)||(s.add(i),!1)}).results).flat().sort((e,t)=>o(e.range.range,t.range.range)?-1:1)}}_findInWorksheet(e,t,i){const n={startRow:0,startColumn:0,endRow:e.getRowCount()-1,endColumn:e.getColumnCount()-1};return this._findInRange(e,t,n,i)}_disposeHighlights(){var e;this._highlightShapes.forEach(e=>{var t;null===(t=e.getScene())||void 0===t||t.makeDirty(),e.dispose()}),this._highlightShapes=[],null===(e=this._currentHighlightShape)||void 0===e||e.dispose(),this._currentHighlightShape=null}_updateFindHighlight(){this._disposeHighlights();const e=this._sheetSkeletonManagerService.getCurrentSkeleton();if(!e)return;const t=this._workbook.getUnitId(),i=this._renderManagerService.getRenderUnitById(t);if(null==i)return;const{scene:n}=i,r=this._matches,o=this._themeService.getColorFromTheme("yellow.400"),s=new ColorKit(o).toRgb(),a=this._workbook.getActiveSheet();if(!a)return;const c=a.getSheetId(),h=r.filter(e=>e.range.subUnitId===c).map((t,i)=>{const{startColumn:r,startRow:o,endColumn:c,endRow:h}=t.range.range,l=getCoordByCell(o,r,n,e),d=getCoordByCell(h,c,n,e),{startX:u,startY:_}=l,{endX:g,endY:S}=d;let m=!0;for(let e=o;e<=h;e++)if(a.getRowRawVisible(e)){m=!1;break}let p=!0;for(let e=r;e<=c;e++)if(a.getColVisible(e)){p=!1;break}return new SheetFindReplaceHighlightShape(`find-highlight-${i}`,{left:u,top:_,color:s,width:p?2:g-u,height:m?2:S-_,evented:!1,inHiddenRange:m||p,zIndex:1e4})});n.addObjects(h),this._highlightShapes=h,n.makeDirty()}_updateCurrentHighlightShape(e){var t;if(null===(t=this._currentHighlightShape)||void 0===t||t.setShapeProps({activated:!1}),this._currentHighlightShape=null,void 0!==e){const t=this._highlightShapes[e];if(!t)return;this._currentHighlightShape=t,t.setShapeProps({activated:!0})}}_getSheetObject(){return getSheetObject(this._univerInstanceService,this._renderManagerService)}async _focusMatch(e){var t;const{subUnitId:i,range:n}=e.range;if(i!==(null===(t=this._workbook.getActiveSheet())||void 0===t?void 0:t.getSheetId())){const e=this._workbook.getUnitId();await this._commandService.executeCommand(SetWorksheetActivateCommand.id,{unitId:e,subUnitId:i},{fromFindReplace:!0})}this._commandService.executeCommand(ScrollToCellCommand.id,{range:n},{fromFindReplace:!0})}_tryRestoreLastMatchesPosition(e,t){if(!e)return 0;const{subUnitId:i}=e.range,{startColumn:n,startRow:r}=e.range.range,o=t.findIndex(e=>{if(i!==e.range.subUnitId)return!1;const{startColumn:t,startRow:o}=e.range.range;return t===n&&o===r});return o>-1?o+1:0}moveToNextMatch(e){if(!this._matches.length)return null;const t=(null==e?void 0:e.loop)??!1,i=(null==e?void 0:e.stayIfOnMatch)??!1,n=(null==e?void 0:e.noFocus)??!1,r=(null==e?void 0:e.ignoreSelection)??!1,o=this._findNextMatch(t,i,r);if(o){const[e,t]=o;return this._matchesPosition=t+1,this._query.findScope===FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(e.range.subUnitId).findIndex(t=>t===e):this._activeHighlightIndex=t,n||this._focusMatch(e),this._updateCurrentHighlightShape(this._activeHighlightIndex),e}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}moveToPreviousMatch(e){if(!this._matches.length)return null;const t=(null==e?void 0:e.loop)??!1,i=(null==e?void 0:e.stayIfOnMatch)??!1,n=(null==e?void 0:e.noFocus)??!1,r=(null==e?void 0:e.ignoreSelection)??!1,o=this._findPreviousMatch(t,i,r);if(o){const[e,t]=o;return this._matchesPosition=t+1,this._query.findScope===FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(e.range.subUnitId).findIndex(t=>t===e):this._activeHighlightIndex=t,n||this._focusMatch(e),this._updateCurrentHighlightShape(this._activeHighlightIndex),e}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}_findPreviousMatch(e=!1,t=!1,i=!1){var n;if(this.currentMatch){const i=this._matches.findIndex(e=>e===this.currentMatch);if(t)return[this.currentMatch,i];const n=i-1;if(!e&&n<0)return null;const r=this._matches.length,o=(n+r)%r;return[this._matches[o],o]}const r=this._workbookSelections.getCurrentLastSelection();if(i||!r){const e=this._matches.length-1;return[this._matches[e],e]}if(this._query.findScope!==FindScope.UNIT)return this._findPreviousMatchByRange(this._matches,r.range);const o=null===(n=this._workbook.getActiveSheet())||void 0===n?void 0:n.getSheetId();if(!o)return null;const s=this._findPreviousWorksheetThatHasAMatch(o,e);return s?this._findPreviousMatchByRange(this._matchesByWorksheet.get(s),r.range):null}_findNextMatch(e=!1,t=!1,i=!1){var n;if(this.currentMatch){const i=this._matches.findIndex(e=>e===this.currentMatch);if(t)return[this.currentMatch,i];const n=i+1,r=this._matches.length;if(!e&&n>=r)return null;const o=n%r;return[this._matches[o],o]}const r=this._workbookSelections.getCurrentLastSelection();if(i||!r)return[this._matches[0],0];if(this._query.findScope!==FindScope.UNIT)return this._findNextMatchByRange(this._matches,r.range,t);const o=null===(n=this._workbook.getActiveSheet())||void 0===n?void 0:n.getSheetId();if(!o)return null;const s=this._findNextWorksheetThatHasAMatch(o,e);return s?this._findNextMatchByRange(this._matchesByWorksheet.get(s),r.range):null}_findPreviousWorksheetThatHasAMatch(e,t=!1){const i=this._workbook.getSheetOrders(),n=i.findIndex(t=>t===e);return(t?rotate(i,n+1):i.slice(0,n+1)).findLast(e=>this._matchesByWorksheet.has(e))??null}_findNextWorksheetThatHasAMatch(e,t=!1){const i=this._workbook.getSheetOrders(),n=i.findIndex(t=>t===e);return(t?rotate(i,n):i.slice(n)).find(e=>this._matchesByWorksheet.has(e))??null}_findNextMatchByRange(e,t,i=!1){const n=this._query.findDirection===FindDirection.ROW;let r=e.findIndex(e=>{const r=e.range.range;if(!(n?isBehindPositionWithRowPriority(t,r):isBehindPositionWithColumnPriority(t,r)))return!1;const o=isSamePosition(t,r);return i||!o});-1===r&&(r=e.length-1);const o=e[r];return[o,this._matches.findIndex(e=>e===o)]}_findPreviousMatchByRange(e,t,i=!1){const n=this._query.findDirection===FindDirection.ROW;let r=this._matches.findLastIndex(e=>{const r=e.range.range;if(!(n?isBeforePositionWithRowPriority(t,r):isBeforePositionWithColumnPriority(t,r)))return!1;const o=isSamePosition(t,r);return i?o:!o});-1===r&&(r=0);const o=e[r];return[o,this._matches.findIndex(e=>e===o)]}async replace(e){if(0===this._matchesCount||!this.currentMatch||!this._query||!this.currentMatch.replaceable)return!1;const t=this.currentMatch.range,i=this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId),n=this._getReplacedCellData(this.currentMatch,i,this._query.findBy===FindBy.FORMULA,this._query.findString,e,this._query.caseSensitive?"g":"ig"),r={unitId:this.currentMatch.unitId,subUnitId:t.subUnitId,value:{[t.range.startRow]:{[t.range.startColumn]:n}}};return this._commandService.executeCommand(SetRangeValuesCommand.id,r)}async replaceAll(e){if(0===this._matchesCount||!this._query)return{success:0,failure:0};const t=this._workbook.getUnitId(),{findString:i,caseSensitive:n,findBy:r}=this._query,o=r===FindBy.FORMULA,s=n?"g":"ig",a=[];return groupBy(this._matches.filter(e=>e.replaceable),e=>e.range.subUnitId).forEach((t,n)=>{const r=new ObjectMatrix,c=this._workbook.getSheetBySheetId(n);t.forEach(t=>{const{startColumn:n,startRow:a}=t.range.range,h=this._getReplacedCellData(t,c,o,i,e,s);h&&r.setValue(a,n,h)}),a.push({count:t.length,subUnitId:n,value:r.getMatrix()})}),a?this._commandService.executeCommand(SheetReplaceCommand.id,{unitId:t,replacements:a}):{success:0,failure:0}}_getReplacedCellData(e,t,i,n,r,o){var s;const{startRow:a,startColumn:c}=e.range.range,h=t.getCellRaw(a,c);if(e.isFormula)return i?{f:h.f.replace(regexp.createLiteralRegExp(n,o),r),v:null}:null;if(null===(s=h.p)||void 0===s?void 0:s.body){const e=Tools.deepClone(h.p);return replaceInDocumentBody(e.body,n,r,this._query.caseSensitive),{p:e}}return{v:h.v.toString().replace(regexp.createLiteralRegExp(n,o),r)}}};SheetFindModel=__decorate([__decorateParam(2,IUniverInstanceService),__decorateParam(3,IRenderManagerService),__decorateParam(4,ICommandService),__decorateParam(5,IContextService),__decorateParam(6,Inject(ThemeService)),__decorateParam(7,Inject(SheetsSelectionsService))],SheetFindModel);let SheetsFindReplaceProvider=class extends Disposable{constructor(e,t,i){super(),this._univerInstanceService=e,this._renderManagerService=t,this._injector=i,_defineProperty(this,"capabilities",{caseSensitive:!0,matchesTheWholeWord:!1,matchesTheWholeCell:!0,findDirection:!0,findScope:!0,findBy:!0}),_defineProperty(this,"_findModelsByUnitId",new Map)}isSupported(e){return e.type===UniverInstanceType.UNIVER_SHEET}async find(e){this._terminate();const t=this._univerInstanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_SHEET);if(!t)return[];const i=this._preprocessQuery(e),n=this._renderManagerService.getRenderUnitById(t.getUnitId()).with(SheetSkeletonManagerService),r=this._injector.createInstance(SheetFindModel,t,n);return this._findModelsByUnitId.set(t.getUnitId(),r),r.start(i),[r]}terminate(){this._terminate()}_terminate(){this._findModelsByUnitId.forEach(e=>e.dispose()),this._findModelsByUnitId.clear()}_preprocessQuery(e){const t=e.caseSensitive?e.findString:e.findString.toLowerCase();return{...e,findString:t}}};SheetsFindReplaceProvider=__decorate([__decorateParam(0,IUniverInstanceService),__decorateParam(1,IRenderManagerService),__decorateParam(2,Inject(Injector))],SheetsFindReplaceProvider);const VALUE_PASSING_OBJECT={hit:!1,replaceable:!1,isFormula:!1,rawData:null};function hitCell(e,t,i,n,r){const{findBy:o}=n,s=o===FindBy.FORMULA,a=e.getCellRaw(t,i);return VALUE_PASSING_OBJECT.rawData=a,(null==a?void 0:a.f)?(VALUE_PASSING_OBJECT.isFormula=!0,s?matchCellData({v:a.f},n)?(VALUE_PASSING_OBJECT.hit=!0,VALUE_PASSING_OBJECT.replaceable=!0,VALUE_PASSING_OBJECT):(VALUE_PASSING_OBJECT.hit=!1,VALUE_PASSING_OBJECT.replaceable=!1,VALUE_PASSING_OBJECT):(VALUE_PASSING_OBJECT.replaceable=!1,matchCellData(r,n)?VALUE_PASSING_OBJECT.hit=!0:VALUE_PASSING_OBJECT.hit=!1,VALUE_PASSING_OBJECT)):(VALUE_PASSING_OBJECT.isFormula=!1,matchCellData(r,n)?a?(VALUE_PASSING_OBJECT.hit=!0,VALUE_PASSING_OBJECT.replaceable=!0):(VALUE_PASSING_OBJECT.hit=!0,VALUE_PASSING_OBJECT.replaceable=!1):(VALUE_PASSING_OBJECT.hit=!1,VALUE_PASSING_OBJECT.replaceable=!1),VALUE_PASSING_OBJECT)}function matchCellData(e,t){const i=extractPureValue(e);return!!i&&(t.matchesTheWholeCell?t.caseSensitive?i===t.findString:i.toLowerCase()===t.findString:t.caseSensitive?i.indexOf(t.findString)>-1:i.toLowerCase().indexOf(t.findString)>-1)}function extractPureValue(e){var t;const i=(null==e||null===(t=e.p)||void 0===t||null===(t=t.body)||void 0===t?void 0:t.dataStream)??(null==e?void 0:e.v);return"number"==typeof i?`${i}`:"boolean"==typeof i?i?"1":"0":i}var name="@univerjs/sheets-find-replace",version="1.5.2";const SHEETS_FIND_REPLACE_PLUGIN_CONFIG_KEY="sheets-find-replace.config",configSymbol=Symbol("sheets-find-replace.config"),defaultPluginConfig={},MOBILE_FIND_REPLACE_BAR_SELECTOR='[data-u-comp="mobile-find-replace-bar"]',MOBILE_FIND_MATCH_SAFE_PADDING=8;function getMobileFindMatchScrollOffset(e,t,i){return Math.max(0,e-t+i)}let SheetsFindReplaceMobileController=class extends Disposable{constructor(e,t,i){super(),this._commandService=e,this._univerInstanceService=t,this._renderManagerService=i,_defineProperty(this,"_pendingFrameId",null),this.disposeWithMe(this._commandService.onCommandExecuted((e,t)=>{var i;if(e.id!==ScrollToCellCommand.id||!0!==(null==t?void 0:t.fromFindReplace))return;const n=null===(i=e.params)||void 0===i?void 0:i.range;if(!n)return;const r=this._univerInstanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_SHEET),o=null==r?void 0:r.getActiveSheet();r&&o&&this._scheduleReveal(r.getUnitId(),o.getSheetId(),n.endRow,n.endColumn)}))}dispose(){null!==this._pendingFrameId&&(cancelAnimationFrame(this._pendingFrameId),this._pendingFrameId=null),super.dispose()}_scheduleReveal(e,t,i,n){null!==this._pendingFrameId&&cancelAnimationFrame(this._pendingFrameId),this._pendingFrameId=requestAnimationFrame(()=>{this._pendingFrameId=null,this._revealMatch(e,t,i,n)})}_revealMatch(e,t,i,n){const r=document.querySelector(MOBILE_FIND_REPLACE_BAR_SELECTOR);if(!r)return;const o=this._univerInstanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_SHEET),s=null==o?void 0:o.getActiveSheet();if(!o||!s||o.getUnitId()!==e||s.getSheetId()!==t)return;const a=this._renderManagerService.getRenderUnitById(o.getUnitId()),c=getSheetObject(o,this._renderManagerService),h=null==a?void 0:a.with(SheetSkeletonManagerService).getSkeleton(s.getSheetId());if(!c||!h)return;const{scene:l,engine:d}=c,u=getViewportByCell(i,n,l,s);if(!u)return;const{endY:_}=getCoordByCell(i,n,l,h),{scaleY:g}=l.getAncestorScale(),S=l.getViewportScrollXY(u),m=getMobileFindMatchScrollOffset(d.getCanvasElement().getBoundingClientRect().top+convertTransformToOffsetY(_,g,S),r.getBoundingClientRect().top,8);0!==m&&this._commandService.executeCommand(SetScrollRelativeCommand.id,{offsetY:m}).catch(()=>{})}};SheetsFindReplaceMobileController=__decorate([__decorateParam(0,ICommandService),__decorateParam(1,IUniverInstanceService),__decorateParam(2,IRenderManagerService)],SheetsFindReplaceMobileController);let UniverSheetsFindReplaceMobileUIPlugin=class extends Plugin{constructor(e=defaultPluginConfig,t,i){super(),this._config=e,this._injector=t,this._configService=i;const{...n}=merge({},defaultPluginConfig,this._config);this._configService.setConfig("sheets-find-replace.config",n)}onStarting(){[[SheetsFindReplaceController],[SheetsFindReplaceMobileController]].forEach(e=>this._injector.add(e))}onRendered(){this._injector.get(SheetsFindReplaceMobileController)}onSteady(){this._injector.get(SheetsFindReplaceController)}};_defineProperty(UniverSheetsFindReplaceMobileUIPlugin,"pluginName","SHEET_FIND_REPLACE_PLUGIN"),_defineProperty(UniverSheetsFindReplaceMobileUIPlugin,"packageName",name),_defineProperty(UniverSheetsFindReplaceMobileUIPlugin,"version",version),_defineProperty(UniverSheetsFindReplaceMobileUIPlugin,"type",UniverInstanceType.UNIVER_SHEET),UniverSheetsFindReplaceMobileUIPlugin=__decorate([DependentOn(UniverRenderEnginePlugin,UniverSheetsPlugin,UniverFindReplaceMobileUIPlugin,UniverSheetsMobileUIPlugin),__decorateParam(1,Inject(Injector)),__decorateParam(2,IConfigService)],UniverSheetsFindReplaceMobileUIPlugin);let UniverSheetsFindReplacePlugin=class extends Plugin{constructor(e=defaultPluginConfig,t,i){super(),this._config=e,this._injector=t,this._configService=i;const{...n}=merge({},defaultPluginConfig,this._config);this._configService.setConfig("sheets-find-replace.config",n)}onStarting(){[[SheetsFindReplaceController]].forEach(e=>this._injector.add(e))}onSteady(){this._injector.get(SheetsFindReplaceController)}};_defineProperty(UniverSheetsFindReplacePlugin,"pluginName","SHEET_FIND_REPLACE_PLUGIN"),_defineProperty(UniverSheetsFindReplacePlugin,"packageName",name),_defineProperty(UniverSheetsFindReplacePlugin,"version",version),_defineProperty(UniverSheetsFindReplacePlugin,"type",UniverInstanceType.UNIVER_SHEET),UniverSheetsFindReplacePlugin=__decorate([DependentOn(UniverRenderEnginePlugin,UniverSheetsPlugin,UniverFindReplacePlugin,UniverSheetsUIPlugin),__decorateParam(1,Inject(Injector)),__decorateParam(2,IConfigService)],UniverSheetsFindReplacePlugin);export{SheetReplaceCommand,SheetsFindReplaceController,UniverSheetsFindReplaceMobileUIPlugin,UniverSheetsFindReplacePlugin};
//# sourceMappingURL=/sm/4bb4f7585895d69bec4e57e39f2f740889d5702639ab34449e3100f0ac84795d.map