import*as Common from"../common/common.js";import*as Components from"../components/components.js";import*as Host from"../host/host.js";import*as i18n from"../i18n/i18n.js";import*as SDK from"../sdk/sdk.js";const UIStrings={main:"Main",nodejsS:"Node.js: {PH1}"},str_=i18n.i18n.registerUIStrings("node_main/NodeMain.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);let nodeMainImplInstance;export class NodeMainImpl extends Common.ObjectWrapper.ObjectWrapper{static instance(e={forceNew:null}){const{forceNew:t}=e;return nodeMainImplInstance&&!t||(nodeMainImplInstance=new NodeMainImpl),nodeMainImplInstance}async run(){Host.userMetrics.actionTaken(Host.UserMetrics.Action.ConnectToNodeJSFromFrontend),SDK.Connections.initMainConnection((async()=>{SDK.SDKModel.TargetManager.instance().createTarget("main",i18nString(UIStrings.main),SDK.SDKModel.Type.Browser,null).setInspectedURL("Node.js")}),Components.TargetDetachedDialog.TargetDetachedDialog.webSocketConnectionLost)}}Common.Runnable.registerEarlyInitializationRunnable(NodeMainImpl.instance);export class NodeChildTargetManager extends SDK.SDKModel.SDKModel{constructor(e){super(e),this._targetManager=e.targetManager(),this._parentTarget=e,this._targetAgent=e.targetAgent(),this._childTargets=new Map,this._childConnections=new Map,e.registerTargetDispatcher(this),this._targetAgent.invoke_setDiscoverTargets({discover:!0}),Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(Host.InspectorFrontendHostAPI.Events.DevicesDiscoveryConfigChanged,this._devicesDiscoveryConfigChanged,this),Host.InspectorFrontendHost.InspectorFrontendHostInstance.setDevicesUpdatesEnabled(!1),Host.InspectorFrontendHost.InspectorFrontendHostInstance.setDevicesUpdatesEnabled(!0)}_devicesDiscoveryConfigChanged(e){const t=e.data,n=[];for(const s of t.networkDiscoveryConfig){const e=s.split(":"),t=parseInt(e[1],10);e[0]&&t&&n.push({host:e[0],port:t})}this._targetAgent.invoke_setRemoteLocations({locations:n})}dispose(){Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.removeEventListener(Host.InspectorFrontendHostAPI.Events.DevicesDiscoveryConfigChanged,this._devicesDiscoveryConfigChanged,this);for(const e of this._childTargets.keys())this.detachedFromTarget({sessionId:e})}targetCreated({targetInfo:e}){"node"!==e.type||e.attached||this._targetAgent.invoke_attachToTarget({targetId:e.targetId,flatten:!1})}targetInfoChanged(e){}targetDestroyed(e){}attachedToTarget({sessionId:e,targetInfo:t}){const n=i18nString(UIStrings.nodejsS,{PH1:t.url}),s=new NodeConnection(this._targetAgent,e);this._childConnections.set(e,s);const o=this._targetManager.createTarget(t.targetId,n,SDK.SDKModel.Type.Node,this._parentTarget,void 0,void 0,s);this._childTargets.set(e,o),o.runtimeAgent().invoke_runIfWaitingForDebugger()}detachedFromTarget({sessionId:e}){const t=this._childTargets.get(e);t&&t.dispose("target terminated"),this._childTargets.delete(e),this._childConnections.delete(e)}receivedMessageFromTarget({sessionId:e,message:t}){const n=this._childConnections.get(e),s=n?n._onMessage:null;s&&s.call(null,t)}targetCrashed(e){}}export class NodeConnection{constructor(e,t){this._targetAgent=e,this._sessionId=t,this._onMessage=null,this._onDisconnect=null}setOnMessage(e){this._onMessage=e}setOnDisconnect(e){this._onDisconnect=e}sendRawMessage(e){this._targetAgent.invoke_sendMessageToTarget({message:e,sessionId:this._sessionId})}async disconnect(){this._onDisconnect&&this._onDisconnect.call(null,"force disconnect"),this._onDisconnect=null,this._onMessage=null,await this._targetAgent.invoke_detachFromTarget({sessionId:this._sessionId})}}SDK.SDKModel.SDKModel.register(NodeChildTargetManager,SDK.SDKModel.Capability.Target,!0);