import*as Common from"../common/common.js";import*as i18n from"../i18n/i18n.js";import*as Persistence from"../persistence/persistence.js";import*as Platform from"../platform/platform.js";import*as SDK from"../sdk/sdk.js";import*as UI from"../ui/ui.js";import*as Workspace from"../workspace/workspace.js";const UIStrings={scriptSnippet:"Script snippet #{PH1}",linkedTo:"Linked to {PH1}"},str_=i18n.i18n.registerUIStrings("snippets/ScriptSnippetFileSystem.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);function escapeSnippetName(e){return escape(e)}function unescapeSnippetName(e){return unescape(e)}export class SnippetFileSystem extends Persistence.PlatformFileSystem.PlatformFileSystem{constructor(){super("snippet://","snippets"),this._lastSnippetIdentifierSetting=Common.Settings.Settings.instance().createSetting("scriptSnippets_lastIdentifier",0),this._snippetsSetting=Common.Settings.Settings.instance().createSetting("scriptSnippets",[])}initialFilePaths(){return this._snippetsSetting.get().map((e=>escapeSnippetName(e.name)))}async createFile(e,t){const n=this._lastSnippetIdentifierSetting.get()+1;this._lastSnippetIdentifierSetting.set(n);const s=i18nString(UIStrings.scriptSnippet,{PH1:n}),i=this._snippetsSetting.get();return i.push({name:s,content:""}),this._snippetsSetting.set(i),escapeSnippetName(s)}async deleteFile(e){const t=unescapeSnippetName(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=unescapeSnippetName(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=unescapeSnippetName(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=unescapeSnippetName(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(Platform.StringUtilities.escapeForRegExp(e),"i");return this._snippetsSetting.get().filter((e=>e.content.match(n))).map((e=>`snippet:///${escapeSnippetName(e.name)}`))}mimeFromPath(e){return"text/javascript"}contentType(e){return Common.ResourceType.resourceTypes.Script}tooltipForURL(e){return i18nString(UIStrings.linkedTo,{PH1:unescapeSnippetName(e.substring(this.path().length))})}supportsAutomapping(){return!0}}export async function evaluateScriptSnippet(e){if(!e.url().startsWith("snippet://"))return;const t=UI.Context.Context.instance().flavor(SDK.RuntimeModel.ExecutionContext);if(!t)return;const n=t.runtimeModel;await e.requestContent(),e.commitWorkingCopy();const s=e.workingCopy();Common.Console.Console.instance().show();const i=e.url(),o=await t.evaluate({expression:`${s}\n//# sourceURL=${i}`,objectGroup:"console",silent:!1,includeCommandLineAPI:!0,returnByValue:!1,generatePreview:!0,replMode:!0},!1,!0);if("exceptionDetails"in o&&o.exceptionDetails)return void SDK.ConsoleModel.ConsoleModel.instance().addMessage(SDK.ConsoleModel.ConsoleMessage.fromException(n,o.exceptionDetails,void 0,void 0,i));if(!("object"in o)||!o.object)return;const p=t.debuggerModel.scriptsForSourceURL(i);if(p.length<1)return;const r=p[p.length-1].scriptId;SDK.ConsoleModel.ConsoleModel.instance().addMessage(new SDK.ConsoleModel.ConsoleMessage(n,SDK.ConsoleModel.MessageSource.Javascript,SDK.ConsoleModel.MessageLevel.Info,"",SDK.ConsoleModel.MessageType.Result,i,void 0,void 0,[o.object],void 0,void 0,t.id,r))}export function isSnippetsUISourceCode(e){return e.url().startsWith("snippet://")}export function isSnippetsProject(e){return e.type()===Workspace.Workspace.projectTypes.FileSystem&&"snippets"===Persistence.FileSystemWorkspaceBinding.FileSystemWorkspaceBinding.fileSystemType(e)}export function findSnippetsProject(){const e=Workspace.Workspace.WorkspaceImpl.instance().projectsForType(Workspace.Workspace.projectTypes.FileSystem).find((e=>"snippets"===Persistence.FileSystemWorkspaceBinding.FileSystemWorkspaceBinding.fileSystemType(e)));if(!e)throw new Error("Unable to find workspace project for the snippets file system");return e}