import{Page}from"./Page.js";import{WebWorker}from"./WebWorker.js";export class Target{constructor(t,e,r,i,s){this._targetInfo=t,this._browserContext=e,this._targetId=t.targetId,this._sessionFactory=r,this._ignoreHTTPSErrors=i,this._defaultViewport=s,this._pagePromise=null,this._workerPromise=null,this._initializedPromise=new Promise((t=>this._initializedCallback=t)).then((async t=>{if(!t)return!1;const e=this.opener();if(!e||!e._pagePromise||"page"!==this.type())return!0;const r=await e._pagePromise;if(!r.listenerCount("popup"))return!0;const i=await this.page();return r.emit("popup",i),!0})),this._isClosedPromise=new Promise((t=>this._closedCallback=t)),this._isInitialized="page"!==this._targetInfo.type||""!==this._targetInfo.url,this._isInitialized&&this._initializedCallback(!0)}createCDPSession(){return this._sessionFactory()}async page(){return"page"!==this._targetInfo.type&&"background_page"!==this._targetInfo.type&&"webview"!==this._targetInfo.type||this._pagePromise||(this._pagePromise=this._sessionFactory().then((t=>Page.create(t,this,this._ignoreHTTPSErrors,this._defaultViewport)))),this._pagePromise}async worker(){return"service_worker"!==this._targetInfo.type&&"shared_worker"!==this._targetInfo.type?null:(this._workerPromise||(this._workerPromise=this._sessionFactory().then((t=>new WebWorker(t,this._targetInfo.url,(()=>{}),(()=>{}))))),this._workerPromise)}url(){return this._targetInfo.url}type(){const t=this._targetInfo.type;return"page"===t||"background_page"===t||"service_worker"===t||"shared_worker"===t||"browser"===t||"webview"===t?t:"other"}browser(){return this._browserContext.browser()}browserContext(){return this._browserContext}opener(){const{openerId:t}=this._targetInfo;return t?this.browser()._targets.get(t):null}_targetInfoChanged(t){if(this._targetInfo=t,!this._isInitialized&&("page"!==this._targetInfo.type||""!==this._targetInfo.url))return this._isInitialized=!0,void this._initializedCallback(!0)}}