import{InspectorFrontendHost as e}from"../host/host.js";class s{createRemoteService(e){if(!this._remoteConnection){const e=Root.Runtime.queryParam("service-backend");if(!e)return console.error("No endpoint address specified"),Promise.resolve(null);this._remoteConnection=new t(new i(e))}return this._remoteConnection._createService(e)}createAppService(s,o){let i=s+".js";const n=Root.Runtime.queryParam("remoteBase"),c=Root.Runtime.queryParam("debugFrontend"),a=e.isUnderTest(),h=[];n&&h.push("remoteBase="+n),c&&h.push("debugFrontend="+c),a&&h.push("isUnderTest=true"),h.length&&(i+="?"+h.join("&"));const _=new Worker(i,{type:"module"});return new t(new r(_))._createService(o)}}class t{constructor(e){this._port=e,this._port.setHandlers(this._onMessage.bind(this),this._connectionClosed.bind(this)),this._lastId=1,this._callbacks=new Map,this._services=new Map}_createService(e){return this._sendCommand(e+".create").then(s=>{if(!s)return console.error("Could not initialize service: "+e),null;const t=new o(this,e,s.id);return this._services.set(e+":"+s.id,t),t})}_serviceDisposed(e){this._services.delete(e._serviceName+":"+e._objectId),this._services.size||this._port.close()}_sendCommand(e,s){const t=this._lastId++,o=JSON.stringify({id:t,method:e,params:s||{}});return this._port.send(o).then(e=>e?new Promise(e=>this._callbacks.set(t,e)):Promise.resolve(null))}_onMessage(e){let s;try{s=JSON.parse(e)}catch(e){return void console.error(e)}if(s.id)return s.error&&console.error("Service error: "+s.error),this._callbacks.get(s.id)(s.error?null:s.result),void this._callbacks.delete(s.id);const t=s.method.split("."),o=t[0],i=t[1],r=this._services.get(o+":"+s.params.id);r?r._dispatchNotification(i,s.params):console.error("Unable to lookup stub for "+o+":"+s.params.id)}_connectionClosed(){for(const e of this._callbacks.values())e(null);this._callbacks.clear();for(const e of this._services.values())e._dispatchNotification("disposed");this._services.clear()}}class o{constructor(e,s,t){this._connection=e,this._serviceName=s,this._objectId=t,this._notificationHandlers=new Map}dispose(){const e={id:this._objectId};return this._connection._sendCommand(this._serviceName+".dispose",e).then(()=>{this._connection._serviceDisposed(this)})}on(e,s){this._notificationHandlers.set(e,s)}send(e,s){return(s=s||{}).id=this._objectId,this._connection._sendCommand(this._serviceName+"."+e,s)}_dispatchNotification(e,s){const t=this._notificationHandlers.get(e);t?t(s):console.error("Could not report notification '"+e+"' on '"+this._objectId+"'")}}class i{constructor(e){this._url=e}setHandlers(e,s){this._messageHandler=e,this._closeHandler=s}_open(){return this._connectionPromise||(this._connectionPromise=new Promise(function(e){let s;try{s=new WebSocket(this._url),s.onmessage=function(e){this._messageHandler(e.data)}.bind(this),s.onclose=function(){this._socket||e(!1);this._socketClosed(!!this._socket)}.bind(this),s.onopen=function(){this._socket=s,e(!0)}.bind(this)}catch(s){e(!1)}}.bind(this))),this._connectionPromise}send(e){return this._open().then(()=>!!this._socket&&(this._socket.send(e),!0))}close(){return this._open().then(()=>(this._socket&&(this._socket.close(),this._socketClosed(!0)),!0))}_socketClosed(e){this._socket=null,delete this._connectionPromise,e&&this._closeHandler()}}class r{constructor(e){let s;this._worker=e,this._workerPromise=new Promise(e=>{s=e}),this._worker.onmessage=function(e){if("workerReady"===e.data)return void s(!0);this._messageHandler(e.data)}.bind(this),this._worker.onclose=this._closeHandler}setHandlers(e,s){this._messageHandler=e,this._closeHandler=s}send(e){return this._workerPromise.then(()=>{try{return this._worker.postMessage(e),!0}catch(e){return!1}})}close(){return this._workerPromise.then(()=>(this._worker&&this._worker.terminate(),!1))}}var n=Object.freeze({__proto__:null,ServiceManager:s,Connection:t,Service:o,RemoteServicePort:i,WorkerServicePort:r});const c=new s;export{n as ServiceManager,c as serviceManager};
