import*as Common from"../common/common.js";import*as Host from"../host/host.js";import*as i18n from"../i18n/i18n.js";import*as Persistence from"../persistence/persistence.js";import*as Recorder from"../recorder/recorder.js";import*as SDK from"../sdk/sdk.js";import*as Snippets from"../snippets/snippets.js";import*as UI from"../ui/ui.js";import*as Workspace from"../workspace/workspace.js";import{NavigatorView}from"./NavigatorView.js";const UIStrings={syncChangesInDevtoolsWithThe:"Sync changes in DevTools with the local filesystem",learnMoreAboutWorkspaces:"Learn more about Workspaces",overridePageAssetsWithFilesFromA:"Override page assets with files from a local folder",learnMore:"Learn more",clearConfiguration:"Clear configuration",selectFolderForOverrides:"Select folder for overrides",contentScriptsServedByExtensions:"Content scripts served by extensions appear here",createAndSaveCodeSnippetsFor:"Create and save code snippets for later reuse",newSnippet:"New snippet",createNewSnippet:"Create new snippet",run:"Run",rename:"Rename\u2026",remove:"Remove",saveAs:"Save as...",recordAndReplayBrowser:"Record and replay browser interactions",addRecording:"Add recording"},str_=i18n.i18n.registerUIStrings("sources/SourcesNavigator.js",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);let networkNavigatorViewInstance,filesNavigatorViewInstance,overridesNavigatorViewInstance,contentScriptsNavigatorViewInstance,snippetsNavigatorViewInstance,recordingsNavigatorViewInstance,actionDelegateInstance;export class NetworkNavigatorView extends NavigatorView{constructor(){super(),SDK.SDKModel.TargetManager.instance().addEventListener(SDK.SDKModel.Events.InspectedURLChanged,this._inspectedURLChanged,this),Host.userMetrics.panelLoaded("sources","DevTools.Launch.Sources")}static instance(e={forceNew:null}){const{forceNew:t}=e;return networkNavigatorViewInstance&&!t||(networkNavigatorViewInstance=new NetworkNavigatorView),networkNavigatorViewInstance}acceptProject(e){return e.type()===Workspace.Workspace.projectTypes.Network}_inspectedURLChanged(e){const t=SDK.SDKModel.TargetManager.instance().mainTarget();if(e.data!==t)return;const n=t&&t.inspectedURL();if(n)for(const r of this.workspace().uiSourceCodes())this.acceptProject(r.project())&&r.url()===n&&this.revealUISourceCode(r,!0)}uiSourceCodeAdded(e){const t=SDK.SDKModel.TargetManager.instance().mainTarget(),n=t&&t.inspectedURL();n&&e.url()===n&&this.revealUISourceCode(e,!0)}}export class FilesNavigatorView extends NavigatorView{constructor(){super();const e=new UI.EmptyWidget.EmptyWidget("");this.setPlaceholder(e),e.appendParagraph().appendChild(UI.Fragment.html`
      <div>${i18nString(UIStrings.syncChangesInDevtoolsWithThe)}</div><br />
      ${UI.XLink.XLink.create("https://developers.google.com/web/tools/chrome-devtools/workspaces/",i18nString(UIStrings.learnMoreAboutWorkspaces))}
    `);const t=new UI.Toolbar.Toolbar("navigator-toolbar");t.appendItemsAtLocation("files-navigator-toolbar").then((()=>{t.empty()||this.contentElement.insertBefore(t.element,this.contentElement.firstChild)}))}static instance(){return filesNavigatorViewInstance||(filesNavigatorViewInstance=new FilesNavigatorView),filesNavigatorViewInstance}acceptProject(e){return e.type()===Workspace.Workspace.projectTypes.FileSystem&&"overrides"!==Persistence.FileSystemWorkspaceBinding.FileSystemWorkspaceBinding.fileSystemType(e)&&!Snippets.ScriptSnippetFileSystem.isSnippetsProject(e)&&!Recorder.RecordingFileSystem.isRecordingProject(e)}handleContextMenu(e){const t=new UI.ContextMenu.ContextMenu(e);t.defaultSection().appendAction("sources.add-folder-to-workspace",void 0,!0),t.show()}}export class OverridesNavigatorView extends NavigatorView{constructor(){super();const e=new UI.EmptyWidget.EmptyWidget("");this.setPlaceholder(e),e.appendParagraph().appendChild(UI.Fragment.html`
      <div>${i18nString(UIStrings.overridePageAssetsWithFilesFromA)}</div><br />
      ${UI.XLink.XLink.create("https://developers.google.com/web/updates/2018/01/devtools#overrides",i18nString(UIStrings.learnMore))}
    `),this._toolbar=new UI.Toolbar.Toolbar("navigator-toolbar"),this.contentElement.insertBefore(this._toolbar.element,this.contentElement.firstChild),Persistence.NetworkPersistenceManager.NetworkPersistenceManager.instance().addEventListener(Persistence.NetworkPersistenceManager.Events.ProjectChanged,this._updateProjectAndUI,this),this.workspace().addEventListener(Workspace.Workspace.Events.ProjectAdded,this._onProjectAddOrRemoved,this),this.workspace().addEventListener(Workspace.Workspace.Events.ProjectRemoved,this._onProjectAddOrRemoved,this),this._updateProjectAndUI()}static instance(e={forceNew:null}){const{forceNew:t}=e;return overridesNavigatorViewInstance&&!t||(overridesNavigatorViewInstance=new OverridesNavigatorView),overridesNavigatorViewInstance}_onProjectAddOrRemoved(e){const t=e.data;t&&t.type()===Workspace.Workspace.projectTypes.FileSystem&&"overrides"!==Persistence.FileSystemWorkspaceBinding.FileSystemWorkspaceBinding.fileSystemType(t)||this._updateUI()}_updateProjectAndUI(){this.reset();const e=Persistence.NetworkPersistenceManager.NetworkPersistenceManager.instance().project();e&&this.tryAddProject(e),this._updateUI()}_updateUI(){this._toolbar.removeToolbarItems();const e=Persistence.NetworkPersistenceManager.NetworkPersistenceManager.instance().project();if(e){const t=new UI.Toolbar.ToolbarSettingCheckbox(Common.Settings.Settings.instance().moduleSetting("persistenceNetworkOverridesEnabled"));this._toolbar.appendToolbarItem(t),this._toolbar.appendToolbarItem(new UI.Toolbar.ToolbarSeparator(!0));const n=new UI.Toolbar.ToolbarButton(i18nString(UIStrings.clearConfiguration),"largeicon-clear");return n.addEventListener(UI.Toolbar.ToolbarButton.Events.Click,(()=>{e.remove()})),void this._toolbar.appendToolbarItem(n)}const t=i18nString(UIStrings.selectFolderForOverrides),n=new UI.Toolbar.ToolbarButton(t,"largeicon-add",t);n.addEventListener(UI.Toolbar.ToolbarButton.Events.Click,(e=>{this._setupNewWorkspace()}),this),this._toolbar.appendToolbarItem(n)}async _setupNewWorkspace(){await Persistence.IsolatedFileSystemManager.IsolatedFileSystemManager.instance().addFileSystem("overrides")&&Common.Settings.Settings.instance().moduleSetting("persistenceNetworkOverridesEnabled").set(!0)}acceptProject(e){return e===Persistence.NetworkPersistenceManager.NetworkPersistenceManager.instance().project()}}export class ContentScriptsNavigatorView extends NavigatorView{constructor(){super();const e=new UI.EmptyWidget.EmptyWidget("");this.setPlaceholder(e),e.appendParagraph().appendChild(UI.Fragment.html`
      <div>${i18nString(UIStrings.contentScriptsServedByExtensions)}</div><br />
      ${UI.XLink.XLink.create("https://developer.chrome.com/extensions/content_scripts",i18nString(UIStrings.learnMore))}
    `)}static instance(e={forceNew:null}){const{forceNew:t}=e;return contentScriptsNavigatorViewInstance&&!t||(contentScriptsNavigatorViewInstance=new ContentScriptsNavigatorView),contentScriptsNavigatorViewInstance}acceptProject(e){return e.type()===Workspace.Workspace.projectTypes.ContentScripts}}export class SnippetsNavigatorView extends NavigatorView{constructor(){super();const e=new UI.EmptyWidget.EmptyWidget("");this.setPlaceholder(e),e.appendParagraph().appendChild(UI.Fragment.html`
      <div>${i18nString(UIStrings.createAndSaveCodeSnippetsFor)}</div><br />
      ${UI.XLink.XLink.create("https://developers.google.com/web/tools/chrome-devtools/javascript/snippets",i18nString(UIStrings.learnMore))}
    `);const t=new UI.Toolbar.Toolbar("navigator-toolbar"),n=new UI.Toolbar.ToolbarButton(i18nString(UIStrings.newSnippet),"largeicon-add",i18nString(UIStrings.newSnippet));n.addEventListener(UI.Toolbar.ToolbarButton.Events.Click,(e=>{this.create(Snippets.ScriptSnippetFileSystem.findSnippetsProject(),"")})),t.appendToolbarItem(n),this.contentElement.insertBefore(t.element,this.contentElement.firstChild)}static instance(){return snippetsNavigatorViewInstance||(snippetsNavigatorViewInstance=new SnippetsNavigatorView),snippetsNavigatorViewInstance}acceptProject(e){return Snippets.ScriptSnippetFileSystem.isSnippetsProject(e)}handleContextMenu(e){const t=new UI.ContextMenu.ContextMenu(e);t.headerSection().appendItem(i18nString(UIStrings.createNewSnippet),(()=>this.create(Snippets.ScriptSnippetFileSystem.findSnippetsProject(),""))),t.show()}handleFileContextMenu(e,t){const n=t.uiSourceCode(),r=new UI.ContextMenu.ContextMenu(e);r.headerSection().appendItem(i18nString(UIStrings.run),(()=>Snippets.ScriptSnippetFileSystem.evaluateScriptSnippet(n))),r.editSection().appendItem(i18nString(UIStrings.rename),(()=>this.rename(t,!1))),r.editSection().appendItem(i18nString(UIStrings.remove),(()=>n.project().deleteFile(n))),r.saveSection().appendItem(i18nString(UIStrings.saveAs),this._handleSaveAs.bind(this,n)),r.show()}async _handleSaveAs(e){e.commitWorkingCopy();const{content:t}=await e.requestContent();Workspace.FileManager.FileManager.instance().save(e.url(),t||"",!0),Workspace.FileManager.FileManager.instance().close(e.url())}}export class RecordingsNavigatorView extends NavigatorView{constructor(){super();const e=new UI.EmptyWidget.EmptyWidget("");this.setPlaceholder(e);e.appendParagraph().innerText=i18nString(UIStrings.recordAndReplayBrowser);const t=new UI.Toolbar.Toolbar("navigator-toolbar"),n=new UI.Toolbar.ToolbarButton(i18nString(UIStrings.addRecording),"largeicon-add",i18nString(UIStrings.addRecording));n.addEventListener(UI.Toolbar.ToolbarButton.Events.Click,(e=>{this.create(Recorder.RecordingFileSystem.findRecordingsProject(),'{"steps": []}')})),t.appendToolbarItem(n),this.contentElement.insertBefore(t.element,this.contentElement.firstChild)}static instance(){return recordingsNavigatorViewInstance||(recordingsNavigatorViewInstance=new RecordingsNavigatorView),recordingsNavigatorViewInstance}acceptProject(e){return Recorder.RecordingFileSystem.isRecordingProject(e)}handleContextMenu(e){const t=new UI.ContextMenu.ContextMenu(e);t.headerSection().appendItem(i18nString(UIStrings.addRecording),(()=>this.create(Recorder.RecordingFileSystem.findRecordingsProject(),""))),t.show()}handleFileContextMenu(e,t){const n=t.uiSourceCode(),r=new UI.ContextMenu.ContextMenu(e);r.editSection().appendItem(i18nString(UIStrings.rename),(()=>this.rename(t,!1))),r.editSection().appendItem(i18nString(UIStrings.remove),(()=>n.project().deleteFile(n))),r.saveSection().appendItem(i18nString(UIStrings.saveAs),this._handleSaveAs.bind(this,n)),r.show()}async _handleSaveAs(e){e.commitWorkingCopy();const{content:t}=await e.requestContent();Workspace.FileManager.FileManager.instance().save(e.url(),t||"",!0),Workspace.FileManager.FileManager.instance().close(e.url())}}export class ActionDelegate{static instance(e={forceNew:null}){const{forceNew:t}=e;return actionDelegateInstance&&!t||(actionDelegateInstance=new ActionDelegate),actionDelegateInstance}handleAction(e,t){switch(t){case"sources.create-snippet":return Snippets.ScriptSnippetFileSystem.findSnippetsProject().createFile("",null,"").then((e=>Common.Revealer.reveal(e))),!0;case"sources.add-folder-to-workspace":return Persistence.IsolatedFileSystemManager.IsolatedFileSystemManager.instance().addFileSystem(),!0}return!1}}