import*as i18n from"../i18n/i18n.js";import*as Workspace from"../workspace/workspace.js";const UIStrings={unknownErrorLoadingFile:"Unknown error loading file"},str_=i18n.i18n.registerUIStrings("bindings/ContentProviderBasedProject.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class ContentProviderBasedProject extends Workspace.Workspace.ProjectStore{constructor(e,t,r,n,o){super(e,t,r,n),this._contentProviders=new Map,this._isServiceProject=o,this._uiSourceCodeToData=new WeakMap,e.addProject(this)}async requestFileContent(e){const t=this._contentProviders.get(e.url());try{const[e,r]=await Promise.all([t.requestContent(),t.contentEncoded()]);return{content:e.content,isEncoded:r,error:"error"in e&&e.error||""}}catch(r){return{content:null,isEncoded:!1,error:r?String(r):i18nString(UIStrings.unknownErrorLoadingFile)}}}isServiceProject(){return this._isServiceProject}async requestMetadata(e){const{metadata:t}=this._uiSourceCodeToData.get(e);return t}canSetFileContent(){return!1}async setFileContent(e,t,r){}fullDisplayName(e){let t=e.parentURL().replace(/^(?:https?|file)\:\/\//,"");try{t=decodeURI(t)}catch(r){}return t+"/"+e.displayName(!0)}mimeType(e){const{mimeType:t}=this._uiSourceCodeToData.get(e);return t}canRename(){return!1}rename(e,t,r){const n=e.url();this.performRename(n,t,function(t,o){if(t&&o){const t=n.split("/");t[t.length-1]=o;const r=t.join("/"),i=this._contentProviders.get(n);this._contentProviders.set(r,i),this._contentProviders.delete(n),this.renameUISourceCode(e,o)}r(t,o)}.bind(this))}excludeFolder(e){}canExcludeFolder(e){return!1}async createFile(e,t,r,n){return null}canCreateFile(){return!1}deleteFile(e){}remove(){}performRename(e,t,r){r(!1)}searchInFileContent(e,t,r,n){return this._contentProviders.get(e.url()).searchInContent(t,r,n)}async findFilesMatchingSearchRequest(e,t,r){const n=[];return r.setTotalWork(t.length),await Promise.all(t.map(async function(t){const o=this._contentProviders.get(t);let i=!0;for(const r of e.queries().slice()){if(!(await o.searchInContent(r,!e.ignoreCase(),e.isRegex())).length){i=!1;break}}i&&n.push(t);r.worked(1)}.bind(this))),r.done(),n}indexContent(e){Promise.resolve().then(e.done.bind(e))}addUISourceCodeWithProvider(e,t,r,n){this._contentProviders.set(e.url(),t),this._uiSourceCodeToData.set(e,{mimeType:n,metadata:r}),this.addUISourceCode(e)}addContentProvider(e,t,r){const n=this.createUISourceCode(e,t.contentType());return this.addUISourceCodeWithProvider(n,t,null,r),n}removeFile(e){this._contentProviders.delete(e),this.removeUISourceCode(e)}reset(){this._contentProviders.clear(),this.removeProject(),this.workspace().addProject(this)}dispose(){this._contentProviders.clear(),this.removeProject()}}