"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var u=require("events"),d=require("@walletconnect/safe-json"),r=require("@walletconnect/jsonrpc-utils");const p=()=>typeof WebSocket<"u"?WebSocket:typeof global<"u"&&typeof global.WebSocket<"u"?global.WebSocket:typeof window<"u"&&typeof window.WebSocket<"u"?window.WebSocket:typeof self<"u"&&typeof self.WebSocket<"u"?self.WebSocket:require("ws"),g=()=>typeof WebSocket<"u"||typeof global<"u"&&typeof global.WebSocket<"u"||typeof window<"u"&&typeof window.WebSocket<"u"||typeof self<"u"&&typeof self.WebSocket<"u",a=c=>c.split("?")[0],h=10,v=p();class f{constructor(e){if(this.url=e,this.events=new u.EventEmitter,this.registering=!1,!r.isWsUrl(e))throw new Error(`Provided URL is not compatible with WebSocket connection: ${e}`);this.url=e}get connected(){return typeof this.socket<"u"}get connecting(){return this.registering}on(e,t){this.events.on(e,t)}once(e,t){this.events.once(e,t)}off(e,t){this.events.off(e,t)}removeListener(e,t){this.events.removeListener(e,t)}async open(e=this.url){await this.register(e)}async close(){return new Promise((e,t)=>{if(typeof this.socket>"u"){t(new Error("Connection already closed"));return}this.socket.onclose=s=>{this.onClose(s),e()},this.socket.close()})}async send(e){typeof this.socket>"u"&&(this.socket=await this.register());try{this.socket.send(d.safeJsonStringify(e))}catch(t){this.onError(e.id,t)}}register(e=this.url){if(!r.isWsUrl(e))throw new Error(`Provided URL is not compatible with WebSocket connection: ${e}`);if(this.registering){const t=this.events.getMaxListeners();return(this.events.listenerCount("register_error")>=t||this.events.listenerCount("open")>=t)&&this.events.setMaxListeners(t+1),new Promise((s,o)=>{this.events.once("register_error",n=>{this.resetMaxListeners(),o(n)}),this.events.once("open",()=>{if(this.resetMaxListeners(),typeof this.socket>"u")return o(new Error("WebSocket connection is missing or invalid"));s(this.socket)})})}return this.url=e,this.registering=!0,new Promise((t,s)=>{const o=r.isReactNative()?void 0:{rejectUnauthorized:!r.isLocalhostUrl(e)},n=new v(e,[],o);g()?n.onerror=i=>{const l=i;s(this.emitError(l.error))}:n.on("error",i=>{s(this.emitError(i))}),n.onopen=()=>{this.onOpen(n),t(n)}})}onOpen(e){e.onmessage=t=>this.onPayload(t),e.onclose=t=>this.onClose(t),this.socket=e,this.registering=!1,this.events.emit("open")}onClose(e){this.socket=void 0,this.registering=!1,this.events.emit("close",e)}onPayload(e){if(typeof e.data>"u")return;const t=typeof e.data=="string"?d.safeJsonParse(e.data):e.data;this.events.emit("payload",t)}onError(e,t){const s=this.parseError(t),o=s.message||s.toString(),n=r.formatJsonRpcError(e,o);this.events.emit("payload",n)}parseError(e,t=this.url){return r.parseConnectionError(e,a(t),"WS")}resetMaxListeners(){this.events.getMaxListeners()>h&&this.events.setMaxListeners(h)}emitError(e){const t=this.parseError(new Error(e?.message||`WebSocket connection failed for host: ${a(this.url)}`));return this.events.emit("register_error",t),t}}exports.WsConnection=f,exports.default=f;
//# sourceMappingURL=index.cjs.js.map
