import{Settings as e,ResourceType as t,Console as n,UIString as s}from"../common/common.js";import{PlatformFileSystem as i,FileSystemWorkspaceBinding as p,IsolatedFileSystemManager as r}from"../persistence/persistence.js";import{RuntimeModel as o,ConsoleModel as c}from"../sdk/sdk.js";import{Context as a}from"../ui/ui.js";import{Workspace as u}from"../workspace/workspace.js";import{FilteredListWidget as l}from"../quick_open/quick_open.js";function g(e){return escape(e)}function m(e){return unescape(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 n?{content:n.content,isEncoded:!1}:{content:null,isEncoded:!1,error:`A snippet with name '${t}' was not found`}}async setFileContent(e,t,n){const s=m(e.substring(1)),i=this._snippetsSetting.get(),p=i.find(e=>e.name===s);return!!p&&(p.content=t,this._snippetsSetting.set(i),!0)}renameFile(e,t,n){const s=m(e.substring(1)),i=this._snippetsSetting.get(),p=i.find(e=>e.name===s);t=t.trim(),p&&0!==t.length&&!i.find(e=>e.name===t)?(p.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(o.ExecutionContext);if(!t)return;const s=t.runtimeModel;await e.requestContent(),e.commitWorkingCopy();const i=e.workingCopy();n.Console.instance().show();const p=e.url(),r=await t.evaluate({expression:`${i}\n//# sourceURL=${p}`,objectGroup:"console",silent:!1,includeCommandLineAPI:!0,returnByValue:!1,generatePreview:!0,replMode:!0},!1,!0);if("exceptionDetails"in r&&r.exceptionDetails)return void c.ConsoleModel.instance().addMessage(c.ConsoleMessage.fromException(s,r.exceptionDetails,void 0,void 0,p));if(!("object"in r)||!r.object)return;const u=t.debuggerModel.scriptsForSourceURL(p);if(u.length<1)return;const l=u[u.length-1].scriptId;c.ConsoleModel.instance().addMessage(new c.ConsoleMessage(s,c.MessageSource.JS,c.MessageLevel.Info,"",c.MessageType.Result,p,void 0,void 0,[r.object],void 0,void 0,t.id,l))}r.IsolatedFileSystemManager.instance().addPlatformFileSystem("snippet://",new d);const h=u.WorkspaceImpl.instance().projectsForType(u.projectTypes.FileSystem).find(e=>"snippets"===p.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()===u.projectTypes.FileSystem&&"snippets"===p.FileSystemWorkspaceBinding.fileSystemType(e)},project:h,Snippet:void 0});class _ extends l.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=h.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"),l.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};
