import{InspectorFrontendHost as e}from"../host/host.js";import{Runtime as s}from"../root/root.js";class t{createRemoteService(e){if(!this._remoteConnection){const e=s.Runtime.queryParam("service-backend");if(!e)return console.error("No endpoint address specified"),Promise.resolve(null);this._remoteConnection=new o(new r(e))}return this._remoteConnection._createService(e)}createAppService(t,i){let r=t+".js";const c=s.Runtime.queryParam("remoteBase"),a=s.Runtime.queryParam("debugFrontend"),h=e.isUnderTest(),_=[];c&&_.push("remoteBase="+c),a&&_.push("debugFrontend="+a),h&&_.push("isUnderTest=true"),_.length&&(r+="?"+_.join("&"));const d=new Worker(r,{type:"module"});return new o(new n(d))._createService(i)}}class o{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 i(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){s.error&&console.error("Service error: "+s.error);const e=this._callbacks.get(s.id);return void(e&&(e(s.error?null:s.result),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 i{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 r{constructor(e){this._url=e,this._socket}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=o.bind(this),s.onclose=i.bind(this),s.onopen=t.bind(this)}catch(s){e(!1)}function t(){this._socket=s,e(!0)}function o(e){this._messageHandler&&this._messageHandler(e.data)}function i(){this._socket||e(!1),this._socketClosed(!!this._socket)}}.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&&this._closeHandler()}}class n{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&&this._messageHandler(e.data)}.bind(this)}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 c=Object.freeze({__proto__:null,ServiceManager:t,Connection:o,Service:i,RemoteServicePort:r,WorkerServicePort:n});const a=new t;export{c as ServiceManager,a as serviceManager};
