import*as Common from'../common/common.js';import*as Components from'../components/components.js';import*as Host from'../host/host.js';import*as MobileThrottling from'../mobile_throttling/mobile_throttling.js';import*as SDK from'../sdk/sdk.js';import*as UI from'../ui/ui.js';export class InspectorMainImpl extends Common.ObjectWrapper.ObjectWrapper{async run(){let firstCall=true;await SDK.Connections.initMainConnection(async()=>{const type=Root.Runtime.queryParam('v8only')?SDK.SDKModel.Type.Node:SDK.SDKModel.Type.Frame;const waitForDebuggerInPage=type===SDK.SDKModel.Type.Frame&&Root.Runtime.queryParam('panel')==='sources';const target=self.SDK.targetManager.createTarget('main',Common.UIString.UIString('Main'),type,null,undefined,waitForDebuggerInPage);if(!firstCall){return;}
firstCall=false;if(waitForDebuggerInPage){const debuggerModel=target.model(SDK.DebuggerModel.DebuggerModel);if(!debuggerModel.isReadyToPause()){await debuggerModel.once(SDK.DebuggerModel.Events.DebuggerIsReadyToPause);}
debuggerModel.pause();}
target.runtimeAgent().runIfWaitingForDebugger();},Components.TargetDetachedDialog.TargetDetachedDialog.webSocketConnectionLost);new SourcesPanelIndicator();new BackendSettingsSync();new MobileThrottling.NetworkPanelIndicator.NetworkPanelIndicator();Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(Host.InspectorFrontendHostAPI.Events.ReloadInspectedPage,event=>{const hard=(event.data);SDK.ResourceTreeModel.ResourceTreeModel.reloadAllPages(hard);});}}
export class ReloadActionDelegate{handleAction(context,actionId){switch(actionId){case'inspector_main.reload':SDK.ResourceTreeModel.ResourceTreeModel.reloadAllPages(false);return true;case'inspector_main.hard-reload':SDK.ResourceTreeModel.ResourceTreeModel.reloadAllPages(true);return true;}
return false;}}
export class FocusDebuggeeActionDelegate{handleAction(context,actionId){self.SDK.targetManager.mainTarget().pageAgent().bringToFront();return true;}}
export class NodeIndicator{constructor(){const element=createElement('div');const shadowRoot=UI.Utils.createShadowRootWithCoreStyles(element,'inspector_main/nodeIcon.css');this._element=shadowRoot.createChild('div','node-icon');element.addEventListener('click',()=>Host.InspectorFrontendHost.InspectorFrontendHostInstance.openNodeFrontend(),false);this._button=new UI.Toolbar.ToolbarItem(element);this._button.setTitle(Common.UIString.UIString('Open dedicated DevTools for Node.js'));self.SDK.targetManager.addEventListener(SDK.SDKModel.Events.AvailableTargetsChanged,event=>this._update((event.data)));this._button.setVisible(false);this._update([]);}
_update(targetInfos){const hasNode=!!targetInfos.find(target=>target.type==='node'&&!target.attached);this._element.classList.toggle('inactive',!hasNode);if(hasNode){this._button.setVisible(true);}}
item(){return this._button;}}
export class SourcesPanelIndicator{constructor(){self.Common.settings.moduleSetting('javaScriptDisabled').addChangeListener(javaScriptDisabledChanged);javaScriptDisabledChanged();function javaScriptDisabledChanged(){let icon=null;const javaScriptDisabled=self.Common.settings.moduleSetting('javaScriptDisabled').get();if(javaScriptDisabled){icon=UI.Icon.Icon.create('smallicon-warning');icon.title=Common.UIString.UIString('JavaScript is disabled');}
self.UI.inspectorView.setPanelIcon('sources',icon);}}}
export class BackendSettingsSync{constructor(){this._autoAttachSetting=self.Common.settings.moduleSetting('autoAttachToCreatedPages');this._autoAttachSetting.addChangeListener(this._updateAutoAttach,this);this._updateAutoAttach();this._adBlockEnabledSetting=self.Common.settings.moduleSetting('network.adBlockingEnabled');this._adBlockEnabledSetting.addChangeListener(this._update,this);this._emulatePageFocusSetting=self.Common.settings.moduleSetting('emulatePageFocus');this._emulatePageFocusSetting.addChangeListener(this._update,this);self.SDK.targetManager.observeTargets(this);}
_updateTarget(target){if(target.type()!==SDK.SDKModel.Type.Frame||target.parentTarget()){return;}
target.pageAgent().setAdBlockingEnabled(this._adBlockEnabledSetting.get());target.emulationAgent().setFocusEmulationEnabled(this._emulatePageFocusSetting.get());}
_updateAutoAttach(){Host.InspectorFrontendHost.InspectorFrontendHostInstance.setOpenNewWindowForPopups(this._autoAttachSetting.get());}
_update(){for(const target of self.SDK.targetManager.targets()){this._updateTarget(target);}}
targetAdded(target){this._updateTarget(target);}
targetRemoved(target){}}
SDK.ChildTargetManager.ChildTargetManager.install();