import*as Common from"../../core/common/common.js";import*as Host from"../../core/host/host.js";import*as i18n from"../../core/i18n/i18n.js";import*as Platform from"../../core/platform/platform.js";import*as SDK from"../../core/sdk/sdk.js";import*as Bindings from"../../models/bindings/bindings.js";import*as Persistence from"../../models/persistence/persistence.js";import*as Workspace from"../../models/workspace/workspace.js";import*as UI from"../../ui/legacy/legacy.js";import*as Snippets from"../snippets/snippets.js";import{NavigatorView}from"./NavigatorView.js";import sourcesNavigatorStyles from"./sourcesNavigator.css.js";const UIStrings={explainWorkspace:"Set up workspace to sync edits directly to the sources you develop",explainLocalOverrides:"Override network requests and web content locally to mock remote resources",learnMore:"Learn more",clearConfiguration:"Clear configuration",selectFolderForOverrides:"Select folder for overrides",explainContentScripts:"View content scripts served by extensions",explainSnippets:"Save the JavaScript code you run often to run it again anytime",newSnippet:"New snippet",createNewSnippet:"Create new snippet",run:"Run",rename:"Rename…",remove:"Remove",saveAs:"Save as..."},str_=i18n.i18n.registerUIStrings("panels/sources/SourcesNavigator.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);let networkNavigatorViewInstance,filesNavigatorViewInstance,overridesNavigatorViewInstance,contentScriptsNavigatorViewInstance,snippetsNavigatorViewInstance,actionDelegateInstance;export class NetworkNavigatorView extends NavigatorView{constructor(){super(!0),SDK.TargetManager.TargetManager.instance().addEventListener(SDK.TargetManager.Events.InspectedURLChanged,this.inspectedURLChanged,this),Host.userMetrics.panelLoaded("sources","DevTools.Launch.Sources"),SDK.TargetManager.TargetManager.instance().addScopeChangeListener(this.onScopeChange.bind(this))}wasShown(){this.registerCSSFiles([sourcesNavigatorStyles]),super.wasShown()}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&&SDK.TargetManager.TargetManager.instance().isInScope(Bindings.NetworkProject.NetworkProject.getTargetForProject(e))}onScopeChange(){for(const e of Workspace.Workspace.WorkspaceImpl.instance().projects())this.acceptProject(e)?this.tryAddProject(e):this.removeProject(e)}inspectedURLChanged(e){const t=SDK.TargetManager.TargetManager.instance().scopeTarget();if(e.data!==t)return;const n=t&&t.inspectedURL();if(n)for(const e of this.workspace().uiSourceCodes())this.acceptProject(e.project())&&e.url()===n&&this.revealUISourceCode(e,!0)}uiSourceCodeAdded(e){const t=SDK.TargetManager.TargetManager.instance().scopeTarget(),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.explainWorkspace)}</div><br />
  ${UI.XLink.XLink.create("https://goo.gle/devtools-workspace",i18nString(UIStrings.learnMore))}
  `);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}sourceSelected(e,t){Host.userMetrics.actionTaken(Host.UserMetrics.Action.FileSystemSourceSelected),super.sourceSelected(e,t)}acceptProject(e){return e.type()===Workspace.Workspace.projectTypes.FileSystem&&"overrides"!==Persistence.FileSystemWorkspaceBinding.FileSystemWorkspaceBinding.fileSystemType(e)&&!Snippets.ScriptSnippetFileSystem.isSnippetsProject(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{toolbar;constructor(){super();const e=new UI.EmptyWidget.EmptyWidget("");this.setPlaceholder(e),e.appendParagraph().appendChild(UI.Fragment.html`
  <div>${i18nString(UIStrings.explainLocalOverrides)}</div><br />
  ${UI.XLink.XLink.create("https://goo.gle/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),"clear");return n.addEventListener(UI.Toolbar.ToolbarButton.Events.Click,(()=>{Common.Settings.Settings.instance().moduleSetting("persistenceNetworkOverridesEnabled").set(!1),e.remove()})),void this.toolbar.appendToolbarItem(n)}const t=i18nString(UIStrings.selectFolderForOverrides),n=new UI.Toolbar.ToolbarButton(t,"plus",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)}sourceSelected(e,t){Host.userMetrics.actionTaken(Host.UserMetrics.Action.OverridesSourceSelected),super.sourceSelected(e,t)}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.explainContentScripts)}</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.explainSnippets)}</div><br />
  ${UI.XLink.XLink.create("https://goo.gle/devtools-snippets",i18nString(UIStrings.learnMore))}
  `);const t=new UI.Toolbar.Toolbar("navigator-toolbar"),n=new UI.Toolbar.ToolbarButton(i18nString(UIStrings.newSnippet),"plus",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(this.addJSExtension(e.url()),t||"",!0),Workspace.FileManager.FileManager.instance().close(e.url())}addJSExtension(e){return Common.ParsedURL.ParsedURL.concatenate(e,".js")}}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(Platform.DevToolsPath.EmptyEncodedPathString,null,"").then((e=>Common.Revealer.reveal(e))),!0;case"sources.add-folder-to-workspace":return Persistence.IsolatedFileSystemManager.IsolatedFileSystemManager.instance().addFileSystem(),!0}return!1}}