"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var r=require("events"),s=require("@walletconnect/jsonrpc-utils");class i extends s.IJsonRpcProvider{constructor(e){super(e),this.events=new r.EventEmitter,this.hasRegisteredEventListeners=!1,this.connection=this.setConnection(e),this.connection.connected&&this.registerEventListeners()}async connect(e=this.connection){await this.open(e)}async disconnect(){await this.close()}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 request(e,t){return this.requestStrict(s.formatJsonRpcRequest(e.method,e.params||[],e.id||s.getBigIntRpcId().toString()),t)}async requestStrict(e,t){return new Promise(async(c,o)=>{if(!this.connection.connected)try{await this.open()}catch(n){o(n)}this.events.on(`${e.id}`,n=>{s.isJsonRpcError(n)?o(n.error):c(n.result)});try{await this.connection.send(e,t)}catch(n){o(n)}})}setConnection(e=this.connection){return e}onPayload(e){this.events.emit("payload",e),s.isJsonRpcResponse(e)?this.events.emit(`${e.id}`,e):this.events.emit("message",{type:e.method,data:e.params})}onClose(e){e&&e.code===3e3&&this.events.emit("error",new Error(`WebSocket connection closed abnormally with code: ${e.code} ${e.reason?`(${e.reason})`:""}`)),this.events.emit("disconnect")}async open(e=this.connection){this.connection===e&&this.connection.connected||(this.connection.connected&&this.close(),typeof e=="string"&&(await this.connection.open(e),e=this.connection),this.connection=this.setConnection(e),await this.connection.open(),this.registerEventListeners(),this.events.emit("connect"))}async close(){await this.connection.close()}registerEventListeners(){this.hasRegisteredEventListeners||(this.connection.on("payload",e=>this.onPayload(e)),this.connection.on("close",e=>this.onClose(e)),this.connection.on("error",e=>this.events.emit("error",e)),this.connection.on("register_error",e=>this.onClose()),this.hasRegisteredEventListeners=!0)}}exports.JsonRpcProvider=i,exports.default=i;
//# sourceMappingURL=index.cjs.js.map
