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 Workspace from"../workspace/workspace.js";const UIStrings={defaultRecordingName:"Recording #{nextId}",linkedToS:"Linked to {PH1}"},str_=i18n.i18n.registerUIStrings("recorder/RecordingFileSystem.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);function escapeRecordingName(e){return escape(e)}function unescapeRecordingName(e){return unescape(e)}export class RecordingFileSystem extends Persistence.PlatformFileSystem.PlatformFileSystem{constructor(){super("recording://","recordings"),this._lastRecordingIdentifierSetting=Common.Settings.Settings.instance().createSetting("recorder_lastIdentifier",0),this._recordingsSetting=Common.Settings.Settings.instance().createSetting("recorder_recordings",[])}initialFilePaths(){return this._recordingsSetting.get().map((e=>escapeRecordingName(e.name)))}async createFile(e,t){const n=this._lastRecordingIdentifierSetting.get()+1;this._lastRecordingIdentifierSetting.set(n);const r=i18nString(UIStrings.defaultRecordingName,{nextId:n}),i=this._recordingsSetting.get();return i.push({name:r,content:'{"steps": []}'}),this._recordingsSetting.set(i),escapeRecordingName(r)}async deleteFile(e){const t=unescapeRecordingName(e.substring(1)),n=this._recordingsSetting.get(),r=n.filter((e=>e.name!==t));return n.length!==r.length&&(this._recordingsSetting.set(r),!0)}async requestFileContent(e){const t=unescapeRecordingName(e.substring(1)),n=this._recordingsSetting.get().find((e=>e.name===t));return n?{content:n.content,isEncoded:!1}:{content:null,isEncoded:!1,error:`A recording with name '${t}' was not found`}}async setFileContent(e,t,n){const r=unescapeRecordingName(e.substring(1)),i=this._recordingsSetting.get(),s=i.find((e=>e.name===r));return!!s&&(s.content=t,this._recordingsSetting.set(i),!0)}renameFile(e,t,n){const r=unescapeRecordingName(e.substring(1)),i=this._recordingsSetting.get(),s=i.find((e=>e.name===r));t=t.trim(),s&&0!==t.length&&!i.find((e=>e.name===t))?(s.name=t,this._recordingsSetting.set(i),n(!0,t)):n(!1)}async searchInPath(e,t){const n=new RegExp(Platform.StringUtilities.escapeForRegExp(e),"i");return this._recordingsSetting.get().filter((e=>e.content.match(n))).map((e=>`recording:///${escapeRecordingName(e.name)}`))}mimeFromPath(e){return"text/javascript"}contentType(e){return Common.ResourceType.resourceTypes.Script}tooltipForURL(e){return i18nString(UIStrings.linkedToS,{PH1:unescapeRecordingName(e.substring(this.path().length))})}supportsAutomapping(){return!0}}export function isRecordingUISourceCode(e){return e.url().startsWith("recording://")}export function isRecordingProject(e){return e.type()===Workspace.Workspace.projectTypes.FileSystem&&"recordings"===Persistence.FileSystemWorkspaceBinding.FileSystemWorkspaceBinding.fileSystemType(e)}export function findRecordingsProject(){const e=Workspace.Workspace.WorkspaceImpl.instance().projectsForType(Workspace.Workspace.projectTypes.FileSystem).find((e=>"recordings"===Persistence.FileSystemWorkspaceBinding.FileSystemWorkspaceBinding.fileSystemType(e)));if(!e)throw new Error("Unable to find workspace project for the recordings file system.");return e}