var g = Object.defineProperty;
var p = (r, t, o) => t in r ? g(r, t, { enumerable: !0, configurable: !0, writable: !0, value: o }) : r[t] = o;
var i = (r, t, o) => p(r, typeof t != "symbol" ? t + "" : t, o);
import { m as l, o as d, ci as u, t as B, cj as f, ck as k } from "./address-QpeLv2yn.mjs";
class P {
  constructor(t, o, e) {
    i(this, "network");
    i(this, "chain");
    i(this, "config");
    i(this, "platform");
    i(this, "rpc");
    // Cached Protocol clients
    i(this, "protocols", /* @__PURE__ */ new Map());
    /**
     * Check to see if the Wormhole Core protocol is supported by this chain
     * @returns a boolean indicating if this chain supports the Wormhole Core protocol
     */
    i(this, "supportsWormholeCore", () => this.supportsProtocol("WormholeCore"));
    /**
     * Get the Wormhole Core protocol client for this chain
     * @returns the Wormhole Core protocol client for this chain
     */
    i(this, "getWormholeCore", () => this.getProtocol("WormholeCore"));
    /**
     * Check to see if the Token Bridge protocol is supported by this chain
     * @returns a boolean indicating if this chain supports the Token Bridge protocol
     */
    i(this, "supportsTokenBridge", () => this.supportsProtocol("TokenBridge"));
    /**
     * Get the Token Bridge protocol client for this chain
     * @returns the Token Bridge protocol client for this chain
     */
    i(this, "getTokenBridge", () => this.getProtocol("TokenBridge"));
    /**
     * Check to see if the Automatic Token Bridge protocol is supported by this chain
     * @returns  a boolean indicating if this chain supports the Automatic Token Bridge protocol
     */
    i(this, "supportsAutomaticTokenBridge", () => this.supportsProtocol("AutomaticTokenBridge"));
    /**
     * Get the Automatic Token Bridge protocol client for this chain
     * @returns the Automatic Token Bridge protocol client for this chain
     */
    i(this, "getAutomaticTokenBridge", () => this.getProtocol("AutomaticTokenBridge"));
    /**
     * Check to see if the Executor Token Bridge protocol is supported by this chain
     * @returns  a boolean indicating if this chain supports the Executor Token Bridge protocol
     */
    i(this, "supportsExecutorTokenBridge", () => this.supportsProtocol("ExecutorTokenBridge"));
    /**
     * Get the Executor Token Bridge protocol client for this chain
     * @returns the Executor Token Bridge protocol client for this chain
     */
    i(this, "getExecutorTokenBridge", () => this.getProtocol("ExecutorTokenBridge"));
    /**
     * Check to see if the Circle Bridge protocol is supported by this chain
     * @returns a boolean indicating if this chain supports the Circle Bridge protocol
     */
    i(this, "supportsCircleBridge", () => this.supportsProtocol("CircleBridge"));
    /**
     * Get the Circle Bridge protocol client for this chain
     * @returns the Circle Bridge protocol client for this chain
     */
    i(this, "getCircleBridge", () => this.getProtocol("CircleBridge"));
    /**
     * Check to see if the Automatic Circle Bridge protocol is supported by this chain
     * @returns a boolean indicating if this chain supports the Automatic Circle Bridge protocol
     */
    i(this, "supportsAutomaticCircleBridge", () => this.supportsProtocol("AutomaticCircleBridge"));
    /**
     * Get the Automatic Circle Bridge protocol client for this chain
     * @returns the Automatic Circle Bridge protocol client for this chain
     */
    i(this, "getAutomaticCircleBridge", () => this.getProtocol("AutomaticCircleBridge"));
    /**
     * Check to see if the IBC Bridge protocol is supported by this chain
     * @returns a boolean indicating if this chain supports the IBC Bridge protocol
     */
    i(this, "supportsIbcBridge", () => this.supportsProtocol("IbcBridge"));
    /**
     * Get the IBC Bridge protocol client for this chain
     * @returns the IBC Bridge protocol client for this chain
     */
    i(this, "getIbcBridge", () => this.getProtocol("IbcBridge"));
    /**
     * Check to see if the Portico Bridge protocol is supported by this chain
     * @returns a boolean indicating if this chain supports the Portico Bridge protocol
     */
    i(this, "supportsPorticoBridge", () => this.supportsProtocol("PorticoBridge"));
    /**
     * Get the Portico Bridge protocol client for this chain
     * @returns the Portico Bridge protocol client for this chain
     */
    i(this, "getPorticoBridge", () => this.getProtocol("PorticoBridge"));
    /**
     * Check to see if the TBTC Bridge protocol is supported by this chain
     * @returns a boolean indicating if this chain supports the TBTC Bridge protocol
     */
    i(this, "supportsTBTCBridge", () => this.supportsProtocol("TBTCBridge"));
    /**
     * Get the TBTC Bridge protocol client for this chain
     * @returns the TBTC Bridge protocol client for this chain
     */
    i(this, "getTBTCBridge", () => this.getProtocol("TBTCBridge"));
    this.config = o.config[t], this.platform = o, this.chain = this.config.key, this.network = this.config.network, this.rpc = e;
  }
  /**
   * Get an RPC connection for this chain, uses the configuration passed in
   * the initial constructor
   *
   * @returns the RPC connection for this chain
   */
  getRpc() {
    return this.rpc = this.rpc ? this.rpc : this.platform.getRpc(this.chain), this.rpc;
  }
  /**
   *  Get the number of decimals for a token
   *
   *  @param token the token to get the decimals for
   *  @returns the number of decimals for the token
   */
  async getDecimals(t) {
    if (l(t))
      return this.config.nativeTokenDecimals;
    if (this.config.tokenMap) {
      const o = d({ chain: this.chain, address: t }), e = u(this.network, this.chain, o);
      if (e)
        return e.decimals;
    }
    return this.platform.utils().getDecimals(this.network, this.chain, await this.getRpc(), t);
  }
  /**
   * Get the balance of a token for a given address
   *
   * @param walletAddr the address to get the balance for
   * @param token the token to get the balance for
   * @returns the balance of the token for the address
   *
   */
  async getBalance(t, o) {
    return this.platform.utils().getBalance(this.network, this.chain, await this.getRpc(), t, o);
  }
  /**
   * Get the latest block number seen by the chain according to the RPC
   *
   * @returns the latest block number
   */
  async getLatestBlock() {
    return this.platform.utils().getLatestBlock(await this.getRpc());
  }
  /**
   * Get the latest _finalized_ block number seen by the chain according to the RPC
   *
   * @returns the latest finalized block number
   */
  async getLatestFinalizedBlock() {
    return this.platform.utils().getLatestFinalizedBlock(await this.getRpc());
  }
  /**
   * Parse the Wormhole Core messages from a transaction
   *
   * @param txid the transaction to parse
   * @returns the Wormhole Core messages emitted by the transaction
   */
  async parseTransaction(t) {
    return this.platform.parseWormholeMessages(this.chain, await this.getRpc(), t);
  }
  /**
   *  Send a transaction and wait for it to be confirmed
   *
   * @param stxns the signed transaction to send
   * @returns the transaction hashes of the sent transactions
   */
  async sendWait(t) {
    return this.platform.utils().sendWait(this.chain, await this.getRpc(), t);
  }
  /**
   * Get the token data from the local cache if available
   * @param symbol the symbol of the token to get
   * @returns the token data if available
   */
  getToken(t) {
    if (this.config.tokenMap && t in this.config.tokenMap)
      return this.config.tokenMap[t];
  }
  /**
   * Get the token id of the wrapped token for the native gas token
   *
   * @returns the wrapped token for the native gas token
   */
  async getNativeWrappedTokenId() {
    if (this.config.wrappedNative) {
      const { address: o } = this.config.wrappedNative;
      return { chain: this.chain, address: B(this.chain, o) };
    }
    const t = await this.getTokenBridge();
    return { chain: this.chain, address: await t.getWrappedNative() };
  }
  /**
   * Get the token account for a given address and token
   *
   * @remarks
   * This is really only useful in the context of Solana but in order
   * to provide a consistent interface, we provide it here.
   *
   * @param address the address to get the token account for
   * @param token the token to get the token account for
   * @returns the token account for the address and token
   */
  async getTokenAccount(t, o) {
    return { chain: this.chain, address: t };
  }
  /**
   * Check to see if a given protocol is supported by this chain
   * by checking if it is registered in the platform and the configuration
   * is available and correct
   *
   * @param protocolName the name of the Protocol to check for support
   * @returns a boolean indicating if this protocol is supported
   */
  supportsProtocol(t) {
    return f(this.chain, t);
  }
  /**
   * Construct a protocol client for the given protocol
   *
   * Note: If no contracts are passed, we assume the default contracts should be used
   * and that the protocol client is cacheable
   *
   * @param protocolName The name of the protocol to construct a client for
   * @returns An instance of the protocol client that implements the protocol interface for the chain
   */
  async getProtocol(t, o, e) {
    if (!o && this.protocols.has(t))
      return this.protocols.get(t);
    const a = o ? { ...this.config.contracts, ...o } : this.config.contracts, c = e ?? await this.getRpc(), n = this.platform.getProtocolInitializer(t);
    let s;
    if (e) {
      if (o)
        throw new Error("Custom contracts are currently not supported with custom rpc connection. Add the contracts to the base config.");
      s = await this.platform.getProtocol(t, c);
    } else if (k(n)) {
      const h = await n.getVersion(c, a);
      s = new n(this.network, this.chain, c, a, h);
    } else
      s = new n(this.network, this.chain, c, a);
    return o || this.protocols.set(t, s), s;
  }
}
export {
  P as C
};
