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{RuntimeModel as r,ConsoleModel as o}from"../sdk/sdk.js";import{Context as c}from"../ui/ui.js";import{Workspace as a}from"../workspace/workspace.js";import{FilteredListWidget as u}from"../quick_open/quick_open.js";const l={scriptSnippet:"Script snippet #{PH1}",linkedTo:"Linked to {PH1}"},g=s.registerUIStrings("snippets/ScriptSnippetFileSystem.js",l),d=s.getLocalizedString.bind(void 0,g);function m(e){return escape(e)}function S(e){return unescape(e)}class h 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=>m(e.name)))}async createFile(e,t){const n=this._lastSnippetIdentifierSetting.get()+1;this._lastSnippetIdentifierSetting.set(n);const s=d(l.scriptSnippet,{PH1:n}),i=this._snippetsSetting.get();return i.push({name:s,content:""}),this._snippetsSetting.set(i),m(s)}async deleteFile(e){const t=S(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=S(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=S(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=S(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:///"+m(e.name)))}mimeFromPath(e){return"text/javascript"}contentType(e){return t.resourceTypes.Script}tooltipForURL(e){return d(l.linkedTo,{PH1:S(e.substring(this.path().length))})}supportsAutomapping(){return!0}}async function f(e){if(!e.url().startsWith("snippet://"))return;const t=c.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 p=e.url(),a=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 a&&a.exceptionDetails)return void o.ConsoleModel.instance().addMessage(o.ConsoleMessage.fromException(s,a.exceptionDetails,void 0,void 0,p));if(!("object"in a)||!a.object)return;const u=t.debuggerModel.scriptsForSourceURL(p);if(u.length<1)return;const l=u[u.length-1].scriptId;o.ConsoleModel.instance().addMessage(new o.ConsoleMessage(s,o.MessageSource.JS,o.MessageLevel.Info,"",o.MessageType.Result,p,void 0,void 0,[a.object],void 0,void 0,t.id,l))}function _(){const e=a.WorkspaceImpl.instance().projectsForType(a.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 y=Object.freeze({__proto__:null,UIStrings:l,SnippetFileSystem:h,evaluateScriptSnippet:f,isSnippetsUISourceCode:function(e){return e.url().startsWith("snippet://")},isSnippetsProject:function(e){return e.type()===a.projectTypes.FileSystem&&"snippets"===p.FileSystemWorkspaceBinding.fileSystemType(e)},findSnippetsProject:_,Snippet:undefined});const j={noSnippetsFound:"No snippets found."},F=s.registerUIStrings("snippets/SnippetsQuickOpen.js",j),k=s.getLocalizedString.bind(void 0,F);class v extends u.Provider{constructor(){super(),this._snippets=[]}selectItem(e,t){null!==e&&f(this._snippets[e])}notFoundText(e){return k(j.noSnippetsFound)}attach(){this._snippets=_().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 C=Object.freeze({__proto__:null,UIStrings:j,SnippetsQuickOpen:v});export{y as ScriptSnippetFileSystem,C as SnippetsQuickOpen};
