import*as Common from"../common/common.js";import*as Host from"../host/host.js";import{ParallelConnection}from"./Connections.js";import{Capability,Events as SDKModelEvents,SDKModel,TargetManager,Type}from"./SDKModel.js";let _attachCallback,_lastAnonymousTargetId=0;export class ChildTargetManager extends SDKModel{constructor(t){super(t),this._targetManager=t.targetManager(),this._parentTarget=t,this._targetAgent=t.targetAgent(),this._targetInfos=new Map,this._childTargets=new Map,this._parallelConnections=new Map,this._parentTargetId=null,t.registerTargetDispatcher(this),this._targetAgent.invoke_setAutoAttach({autoAttach:!0,waitForDebuggerOnStart:!0,flatten:!0}),t.parentTarget()||Host.InspectorFrontendHost.isUnderTest()||(this._targetAgent.invoke_setDiscoverTargets({discover:!0}),this._targetAgent.invoke_setRemoteLocations({locations:[{host:"localhost",port:9229}]}))}static install(t){_attachCallback=t,SDKModel.register(ChildTargetManager,Capability.Target,!0)}childTargets(){return Array.from(this._childTargets.values())}suspendModel(){return this._targetAgent.invoke_setAutoAttach({autoAttach:!0,waitForDebuggerOnStart:!1,flatten:!0})}resumeModel(){return this._targetAgent.invoke_setAutoAttach({autoAttach:!0,waitForDebuggerOnStart:!0,flatten:!0})}dispose(){for(const t of this._childTargets.keys())this.detachedFromTarget({sessionId:t,targetId:void 0})}targetCreated({targetInfo:t}){this._targetInfos.set(t.targetId,t),this._fireAvailableTargetsChanged(),this.dispatchEventToListeners(Events.TargetCreated,t)}targetInfoChanged({targetInfo:t}){this._targetInfos.set(t.targetId,t);const e=this._childTargets.get(t.targetId);e&&e.updateTargetInfo(t),this._fireAvailableTargetsChanged(),this.dispatchEventToListeners(Events.TargetInfoChanged,t)}targetDestroyed({targetId:t}){this._targetInfos.delete(t),this._fireAvailableTargetsChanged(),this.dispatchEventToListeners(Events.TargetDestroyed,t)}targetCrashed({targetId:t,status:e,errorCode:a}){}_fireAvailableTargetsChanged(){TargetManager.instance().dispatchEventToListeners(SDKModelEvents.AvailableTargetsChanged,[...this._targetInfos.values()])}async _getParentTargetId(){return this._parentTargetId||(this._parentTargetId=(await this._parentTarget.targetAgent().invoke_getTargetInfo({})).targetInfo.targetId),this._parentTargetId}attachedToTarget({sessionId:t,targetInfo:e,waitingForDebugger:a}){if(this._parentTargetId===e.targetId)return;let r="";if("worker"===e.type&&e.title&&e.title!==e.url)r=e.title;else if("iframe"!==e.type){const t=Common.ParsedURL.ParsedURL.fromString(e.url);r=t?t.lastPathComponentWithFragment():"#"+ ++_lastAnonymousTargetId}let s=Type.Browser;"iframe"===e.type||"page"===e.type?s=Type.Frame:"worker"===e.type?s=Type.Worker:"service_worker"===e.type&&(s=Type.ServiceWorker);const n=this._targetManager.createTarget(e.targetId,r,s,this._parentTarget,t,void 0,void 0,e);this._childTargets.set(t,n),_attachCallback?_attachCallback({target:n,waitingForDebugger:a}).then((()=>{n.runtimeAgent().invoke_runIfWaitingForDebugger()})):n.runtimeAgent().invoke_runIfWaitingForDebugger()}detachedFromTarget({sessionId:t}){if(this._parallelConnections.has(t))this._parallelConnections.delete(t);else{const e=this._childTargets.get(t);e&&(e.dispose("target terminated"),this._childTargets.delete(t))}}receivedMessageFromTarget({}){}async createParallelConnection(t){const e=await this._getParentTargetId(),{connection:a,sessionId:r}=await this._createParallelConnectionAndSessionForTarget(this._parentTarget,e);return a.setOnMessage(t),this._parallelConnections.set(r,a),a}async _createParallelConnectionAndSessionForTarget(t,e){const a=t.targetAgent(),r=t.router(),s=(await a.invoke_attachToTarget({targetId:e,flatten:!0})).sessionId,n=new ParallelConnection(r.connection(),s);return r.registerSession(t,s,n),n.setOnDisconnect((()=>{r.unregisterSession(s),a.invoke_detachFromTarget({sessionId:s})})),{connection:n,sessionId:s}}targetInfos(){return Array.from(this._targetInfos.values())}}export const Events={TargetCreated:Symbol("TargetCreated"),TargetDestroyed:Symbol("TargetDestroyed"),TargetInfoChanged:Symbol("TargetInfoChanged")};