import*as Bindings from"../bindings/bindings.js";import*as Common from"../common/common.js";import*as Persistence from"../persistence/persistence.js";import*as Platform from"../platform/platform.js";import*as TextUtils from"../text_utils/text_utils.js";import*as Workspace from"../workspace/workspace.js";export class SourcesSearchScope{constructor(){this._searchId=0,this._searchResultCandidates=[],this._searchResultCallback=null,this._searchFinishedCallback=null,this._searchConfig=null}static _filesComparator(e,t){if(e.isDirty()&&!t.isDirty())return-1;if(!e.isDirty()&&t.isDirty())return 1;const s=e.project().type()===Workspace.Workspace.projectTypes.FileSystem&&!Persistence.Persistence.PersistenceImpl.instance().binding(e);if(s!==(t.project().type()===Workspace.Workspace.projectTypes.FileSystem&&!Persistence.Persistence.PersistenceImpl.instance().binding(t)))return s?1:-1;const r=e.url(),i=t.url();return r&&!i?-1:!r&&i?1:Platform.StringUtilities.naturalOrderComparator(e.fullDisplayName(),t.fullDisplayName())}performIndexing(e){this.stopSearch();const t=this._projects(),s=new Common.Progress.CompositeProgress(e);for(let r=0;r<t.length;++r){const e=t[r],i=s.createSubProgress(e.uiSourceCodes().length);e.indexContent(i)}}_projects(){const e=Common.Settings.Settings.instance().moduleSetting("searchInAnonymousAndContentScripts").get();return Workspace.Workspace.WorkspaceImpl.instance().projects().filter((t=>t.type()!==Workspace.Workspace.projectTypes.Service&&(!(!e&&t.isServiceProject()&&t.type()!==Workspace.Workspace.projectTypes.Formatter)&&!(!e&&t.type()===Workspace.Workspace.projectTypes.ContentScripts))))}performSearch(e,t,s,r){this.stopSearch(),this._searchResultCandidates=[],this._searchResultCallback=s,this._searchFinishedCallback=r,this._searchConfig=e;const i=[],o=new Common.Progress.CompositeProgress(t),n=o.createSubProgress(),a=new Common.Progress.CompositeProgress(o.createSubProgress());for(const c of this._projects()){const t=c.uiSourceCodes().length,s=a.createSubProgress(t),r=this._projectFilesMatchingFileQuery(c,e),o=c.findFilesMatchingSearchRequest(e,r,s).then(this._processMatchingFilesForProject.bind(this,this._searchId,c,e,r));i.push(o)}Promise.all(i).then(this._processMatchingFiles.bind(this,this._searchId,n,this._searchFinishedCallback.bind(this,!0)))}_projectFilesMatchingFileQuery(e,t,s){const r=[],i=e.uiSourceCodes();for(let o=0;o<i.length;++o){const e=i[o];if(!e.contentType().isTextType())continue;const n=Persistence.Persistence.PersistenceImpl.instance().binding(e);n&&n.network===e||(s&&!e.isDirty()||t.filePathMatchesFileQuery(e.fullDisplayName())&&r.push(e.url()))}return r.sort(Platform.StringUtilities.naturalOrderComparator),r}_processMatchingFilesForProject(e,t,s,r,i){if(e!==this._searchId&&this._searchFinishedCallback)return void this._searchFinishedCallback(!1);i.sort(Platform.StringUtilities.naturalOrderComparator),i=Platform.ArrayUtilities.intersectOrdered(i,r,Platform.StringUtilities.naturalOrderComparator);const o=this._projectFilesMatchingFileQuery(t,s,!0);i=Platform.ArrayUtilities.mergeOrdered(i,o,Platform.StringUtilities.naturalOrderComparator);const n=[];for(const a of i){const e=t.uiSourceCodeForURL(a);if(!e)continue;const s=Bindings.DefaultScriptMapping.DefaultScriptMapping.scriptForUISourceCode(e);s&&!s.isAnonymousScript()||n.push(e)}n.sort(SourcesSearchScope._filesComparator),this._searchResultCandidates=Platform.ArrayUtilities.mergeOrdered(this._searchResultCandidates,n,SourcesSearchScope._filesComparator)}_processMatchingFiles(e,t,s){if(e!==this._searchId&&this._searchFinishedCallback)return void this._searchFinishedCallback(!1);const r=this._searchResultCandidates;if(!r.length)return t.done(),void s();t.setTotalWork(r.length);let i=0;let o=0;for(let l=0;l<20&&l<r.length;++l)a.call(this);function n(e){e.isDirty()?c.call(this,e,e.workingCopy()):e.requestContent().then((t=>{c.call(this,e,t.content||"")}))}function a(){if(i>=r.length)return o?void 0:(t.done(),void s());++o;const e=r[i++];setTimeout(n.bind(this,e),0)}function c(e,s){function r(e,t){return e.lineNumber-t.lineNumber}t.worked(1);let i=[];const n=this._searchConfig,c=n.queries();if(null!==s)for(let t=0;t<c.length;++t){const e=TextUtils.TextUtils.performSearchInContent(s,c[t],!n.ignoreCase(),n.isRegex());i=Platform.ArrayUtilities.mergeOrdered(i,e,r)}if(i&&this._searchResultCallback){const t=new FileBasedSearchResult(e,i);this._searchResultCallback(t)}--o,a.call(this)}}stopSearch(){++this._searchId}}export class FileBasedSearchResult{constructor(e,t){this._uiSourceCode=e,this._searchMatches=t}label(){return this._uiSourceCode.displayName()}description(){return this._uiSourceCode.fullDisplayName()}matchesCount(){return this._searchMatches.length}matchLineContent(e){return this._searchMatches[e].lineContent}matchRevealable(e){const t=this._searchMatches[e];return this._uiSourceCode.uiLocation(t.lineNumber,void 0)}matchLabel(e){return this._searchMatches[e].lineNumber+1}}