var l=Object.defineProperty;var u=(r,t,i)=>t in r?l(r,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):r[t]=i;var o=(r,t,i)=>u(r,typeof t!="symbol"?t+"":t,i);import{cg as d,m as f,ar as B,ch as m,ci as w,cj as P}from"../main.js";function k(r){return"getVersion"in r}const s={};function R(r,t,i){t in s||(s[t]={});const e=s[t];if(r in e)throw new Error(`Protocol ${r} for protocol ${t} has already registered`);s[t][r]=i}function T(r,t){const i=d(r)?f.get(r):r;return t in s?i in s[t]:!1}function g(r,t){if(t in s){const i=s[t];if(i&&r in i){const e=i[r];if(e)return e}}throw new Error(`No protocols registered for ${r}:${t}. This may be because the platform specific protocol implementation is not registered (by installing and importing it) or no implementation exists for this platform`)}const C=(r,t,i,e)=>g(r,t).fromRpc(i,e);class W{constructor(t,i){o(this,"network");o(this,"config");this.network=t,this.config=i}utils(){return this.constructor}getProtocol(t,i){return C(this.utils()._platform,t,i,this.config)}getProtocolInitializer(t){return g(this.utils()._platform,t)}async parseWormholeMessages(t,i,e){return(await this.getProtocol("WormholeCore",i)).parseTransaction(e)}}class I{constructor(t,i,e){o(this,"network");o(this,"chain");o(this,"config");o(this,"platform");o(this,"rpc");o(this,"protocols",new Map);o(this,"supportsWormholeCore",()=>this.supportsProtocol("WormholeCore"));o(this,"getWormholeCore",()=>this.getProtocol("WormholeCore"));o(this,"supportsTokenBridge",()=>this.supportsProtocol("TokenBridge"));o(this,"getTokenBridge",()=>this.getProtocol("TokenBridge"));o(this,"supportsAutomaticTokenBridge",()=>this.supportsProtocol("AutomaticTokenBridge"));o(this,"getAutomaticTokenBridge",()=>this.getProtocol("AutomaticTokenBridge"));o(this,"supportsCircleBridge",()=>this.supportsProtocol("CircleBridge"));o(this,"getCircleBridge",()=>this.getProtocol("CircleBridge"));o(this,"supportsAutomaticCircleBridge",()=>this.supportsProtocol("AutomaticCircleBridge"));o(this,"getAutomaticCircleBridge",()=>this.getProtocol("AutomaticCircleBridge"));o(this,"supportsIbcBridge",()=>this.supportsProtocol("IbcBridge"));o(this,"getIbcBridge",()=>this.getProtocol("IbcBridge"));o(this,"supportsPorticoBridge",()=>this.supportsProtocol("PorticoBridge"));o(this,"getPorticoBridge",()=>this.getProtocol("PorticoBridge"));o(this,"supportsTBTCBridge",()=>this.supportsProtocol("TBTCBridge"));o(this,"getTBTCBridge",()=>this.getProtocol("TBTCBridge"));this.config=i.config[t],this.platform=i,this.chain=this.config.key,this.network=this.config.network,this.rpc=e}getRpc(){return this.rpc=this.rpc?this.rpc:this.platform.getRpc(this.chain),this.rpc}async getDecimals(t){if(B(t))return this.config.nativeTokenDecimals;if(this.config.tokenMap){const i=m({chain:this.chain,address:t}),e=w(this.network,this.chain,i);if(e)return e.decimals}return this.platform.utils().getDecimals(this.chain,await this.getRpc(),t)}async getBalance(t,i){return this.platform.utils().getBalance(this.chain,await this.getRpc(),t,i)}async getLatestBlock(){return this.platform.utils().getLatestBlock(await this.getRpc())}async getLatestFinalizedBlock(){return this.platform.utils().getLatestFinalizedBlock(await this.getRpc())}async parseTransaction(t){return this.platform.parseWormholeMessages(this.chain,await this.getRpc(),t)}async sendWait(t){return this.platform.utils().sendWait(this.chain,await this.getRpc(),t)}getToken(t){if(this.config.tokenMap&&t in this.config.tokenMap)return this.config.tokenMap[t]}async getNativeWrappedTokenId(){if(this.config.wrappedNative){const{address:i}=this.config.wrappedNative;return{chain:this.chain,address:P(this.chain,i)}}const t=await this.getTokenBridge();return{chain:this.chain,address:await t.getWrappedNative()}}async getTokenAccount(t,i){return{chain:this.chain,address:t}}supportsProtocol(t){return T(this.chain,t)}async getProtocol(t,i,e){if(!i&&this.protocols.has(t))return this.protocols.get(t);const n=i?{...this.config.contracts,...i}:this.config.contracts,a=e??await this.getRpc(),h=this.platform.getProtocolInitializer(t);let c;if(e){if(i)throw new Error("Custom contracts are currently not supported with custom rpc connection. Add the contracts to the base config.");c=await this.platform.getProtocol(t,a)}else if(k(h)){const p=await h.getVersion(a,n);c=new h(this.network,this.chain,a,n,p)}else c=new h(this.network,this.chain,a,n);return i||this.protocols.set(t,c),c}}export{I as C,W as P,R as r};
