import{Settings as e,ResourceType as t,Console as n}from"../common/common.js";import{i18n as s}from"../i18n/i18n.js";import{PlatformFileSystem as i,FileSystemWorkspaceBinding as p}from"../persistence/persistence.js";import{StringUtilities as r}from"../platform/platform.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";const g={scriptSnippet:"Script snippet #{PH1}",linkedTo:"Linked to {PH1}"},d=s.registerUIStrings("snippets/ScriptSnippetFileSystem.ts",g),m=s.getLocalizedString.bind(void 0,d);function S(e){return escape(e)}function h(e){return unescape(e)}class f 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=>S(e.name)))}async createFile(e,t){const n=this._lastSnippetIdentifierSetting.get()+1;this._lastSnippetIdentifierSetting.set(n);const s=m(g.scriptSnippet,{PH1:n}),i=this._snippetsSetting.get();return i.push({name:s,content:""}),this._snippetsSetting.set(i),S(s)}async deleteFile(e){const t=h(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=h(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=h(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=h(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(r.escapeForRegExp(e),"i");return this._snippetsSetting.get().filter((e=>e.content.match(n))).map((e=>"snippet:///"+S(e.name)))}mimeFromPath(e){return"text/javascript"}contentType(e){return t.resourceTypes.Script}tooltipForURL(e){return m(g.linkedTo,{PH1:h(e.substring(this.path().length))})}supportsAutomapping(){return!0}}async function _(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.Javascript,c.MessageLevel.Info,"",c.MessageType.Result,p,void 0,void 0,[r.object],void 0,void 0,t.id,l))}function y(){const e=u.WorkspaceImpl.instance().projectsForType(u.projectTypes.FileSystem).find((e=>"snippets"===p.FileSystemWorkspaceBinding.fileSystemType(e)));if(!e)throw new Error("Unable to find workspace project for the snippets file system");return e}var F=Object.freeze({__proto__:null,UIStrings:g,SnippetFileSystem:f,evaluateScriptSnippet:_,isSnippetsUISourceCode:function(e){return e.url().startsWith("snippet://")},isSnippetsProject:function(e){return e.type()===u.projectTypes.FileSystem&&"snippets"===p.FileSystemWorkspaceBinding.fileSystemType(e)},findSnippetsProject:y});const j={noSnippetsFound:"No snippets found."},v=s.registerUIStrings("snippets/SnippetsQuickOpen.ts",j),k=s.getLocalizedString.bind(void 0,v);class C extends l.Provider{constructor(){super(),this._snippets=[]}selectItem(e,t){null!==e&&_(this._snippets[e])}notFoundText(e){return k(j.noSnippetsFound)}attach(){this._snippets=y().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 x=Object.freeze({__proto__:null,UIStrings:j,SnippetsQuickOpen:C});export{F as ScriptSnippetFileSystem,x as SnippetsQuickOpen};
