import{Settings as e,ResourceType as t,Console as n,UIString as s}from"../common/common.js";import{PlatformFileSystem as i,FileSystemWorkspaceBinding as o,IsolatedFileSystemManager as p}from"../persistence/persistence.js";import{RuntimeModel as r,ConsoleModel as c}from"../sdk/sdk.js";import{Context as a}from"../ui/ui.js";import{Workspace as l}from"../workspace/workspace.js";import{FilteredListWidget as u}from"../quick_open/quick_open.js";function g(e){return encodeURIComponent(e)}function m(e){return decodeURIComponent(e)}class d extends i.PlatformFileSystem{constructor(){super("snippet://","snippets"),this._lastSnippetIdentifierSetting=e.Settings.instance().createSetting("scriptSnippets_lastIdentifier",0),this._snippetsSetting=e.Settings.instance().createSetting("scriptSnippets",[])}initialFilePaths(){return this._snippetsSetting.get().map(e=>g(e.name))}async createFile(e,t){const n=this._lastSnippetIdentifierSetting.get()+1;this._lastSnippetIdentifierSetting.set(n);const s=ls`Script snippet #${n}`,i=this._snippetsSetting.get();return i.push({name:s,content:""}),this._snippetsSetting.set(i),g(s)}async deleteFile(e){const t=m(e.substring(1)),n=this._snippetsSetting.get(),s=n.filter(e=>e.name!==t);return n.length!==s.length&&(this._snippetsSetting.set(s),!0)}async requestFileContent(e){const t=m(e.substring(1)),n=this._snippetsSetting.get().find(e=>e.name===t);return{content:n?n.content:null,isEncoded:!1}}async setFileContent(e,t,n){const s=m(e.substring(1)),i=this._snippetsSetting.get(),o=i.find(e=>e.name===s);return!!o&&(o.content=t,this._snippetsSetting.set(i),!0)}renameFile(e,t,n){const s=m(e.substring(1)),i=this._snippetsSetting.get(),o=i.find(e=>e.name===s);t=t.trim(),o&&0!==t.length&&!i.find(e=>e.name===t)?(o.name=t,this._snippetsSetting.set(i),n(!0,t)):n(!1)}async searchInPath(e,t){const n=new RegExp(e.escapeForRegExp(),"i");return this._snippetsSetting.get().filter(e=>e.content.match(n)).map(e=>"snippet:///"+g(e.name))}mimeFromPath(e){return"text/javascript"}contentType(e){return t.resourceTypes.Script}tooltipForURL(e){return ls`Linked to ${m(e.substring(this.path().length))}`}supportsAutomapping(){return!0}}async function S(e){if(!e.url().startsWith("snippet://"))return;const t=a.Context.instance().flavor(r.ExecutionContext);if(!t)return;const s=t.runtimeModel;await e.requestContent(),e.commitWorkingCopy();const i=e.workingCopy();n.Console.instance().show();const o=e.url(),p=await t.evaluate({expression:`${i}\n//# sourceURL=${o}`,objectGroup:"console",silent:!1,includeCommandLineAPI:!0,returnByValue:!1,generatePreview:!0,replMode:!0},!1,!0);if(p.exceptionDetails)return void c.ConsoleModel.instance().addMessage(c.ConsoleMessage.fromException(s,p.exceptionDetails,void 0,void 0,o));if(!p.object)return;const l=t.debuggerModel.scriptsForSourceURL(o);if(l.length<1)return;const u=l[l.length-1].scriptId;c.ConsoleModel.instance().addMessage(new c.ConsoleMessage(s,c.MessageSource.JS,c.MessageLevel.Info,"",c.MessageType.Result,o,void 0,void 0,[p.object],void 0,void 0,t.id,u))}p.IsolatedFileSystemManager.instance().addPlatformFileSystem("snippet://",new d);const h=l.WorkspaceImpl.instance().projectsForType(l.projectTypes.FileSystem).find(e=>"snippets"===o.FileSystemWorkspaceBinding.fileSystemType(e));var f=Object.freeze({__proto__:null,evaluateScriptSnippet:S,isSnippetsUISourceCode:function(e){return e.url().startsWith("snippet://")},isSnippetsProject:function(e){return e.type()===l.projectTypes.FileSystem&&"snippets"===o.FileSystemWorkspaceBinding.fileSystemType(e)},project:h});class _ extends u.Provider{constructor(){super(),this._snippets=[]}selectItem(e,t){null!==e&&S(this._snippets[e])}notFoundText(e){return s.UIString("No snippets found.")}attach(){this._snippets=Snippets.project.uiSourceCodes()}detach(){this._snippets=[]}itemCount(){return this._snippets.length}itemKeyAt(e){return this._snippets[e].name()}renderItem(e,t,n,s){n.textContent=unescape(this._snippets[e].name()),n.classList.add("monospace"),u.FilteredListWidget.highlightRanges(n,t,!0)}}var y=Object.freeze({__proto__:null,SnippetsQuickOpen:_});const F=h;export{f as ScriptSnippetFileSystem,y as SnippetsQuickOpen,F as project};
