import { e as h, u as f, W as d } from "./chunk-KJH4KKG6-Brt4fyqz.mjs";
import { bY as p, bZ as w, b_ as g, W as m, d as C, i as l, b$ as _, c0 as y } from "./platform-Cil_UtAQ.mjs";
import { _ as b, d as c } from "./address-rVHWlcKQ.mjs";
class T extends p {
}
class o extends w {
  static _platform = b;
  constructor(t, a) {
    super(t, a ?? g(t, o._platform));
  }
  getRpc(t) {
    if (t in this.config) {
      const a = this.network === "Mainnet" ? h.MAINNET : h.TESTNET, n = new f({ fullnode: this.config[t].rpc, network: a });
      return new d(n);
    }
    throw new Error("No configuration available for chain: " + t);
  }
  getChain(t, a) {
    if (t in this.config)
      return new T(t, this);
    throw new Error("No configuration available for chain: " + t);
  }
  static nativeTokenId(t, a) {
    if (!this.isSupportedChain(a))
      throw new Error(`invalid chain: ${a}`);
    return m.tokenId(a, c);
  }
  static isNativeTokenId(t, a, n) {
    return !this.isSupportedChain(a) || n.chain !== a ? !1 : this.nativeTokenId(t, a) == n;
  }
  static isSupportedChain(t) {
    return C(t) === o._platform;
  }
  static async getDecimals(t, a, n, e) {
    if (l(e) || e === c)
      return _(o._platform);
    const s = e.toString();
    return (await n.getFungibleAssetMetadataByAssetType({ assetType: s })).decimals;
  }
  static async getBalance(t, a, n, e, s) {
    const r = l(s) ? c : s.toString();
    try {
      return (await n.getCurrentFungibleAssetBalances({
        options: {
          where: {
            owner_address: { _eq: e },
            asset_type: { _eq: r }
          }
        }
      }))[0]?.amount ?? null;
    } catch (i) {
      if (i.status === 404)
        return null;
      throw i;
    }
  }
  static async getBalances(t, a, n, e) {
    try {
      const s = await n.getCurrentFungibleAssetBalances({
        options: {
          where: {
            owner_address: { _eq: e }
          }
        }
      });
      let r = {};
      for (const i of s)
        if (i.asset_type) {
          let u = i.asset_type === "0x1::aptos_coin::AptosCoin" ? "native" : i.asset_type;
          r[u] = i.amount;
        }
      return r;
    } catch (s) {
      if (s.status === 404)
        return {};
      throw s;
    }
  }
  static async sendWait(t, a, n) {
    const e = [];
    for (const s of n) {
      const r = await a.transaction.submit.simple(s.transaction), i = await a.waitForTransaction({
        transactionHash: r.hash
      });
      e.push(i.hash);
    }
    return e;
  }
  static async getLatestBlock(t) {
    const a = await t.getLedgerInfo();
    return Number(a.block_height);
  }
  static async getLatestFinalizedBlock(t) {
    const a = await t.getLedgerInfo();
    return Number(a.block_height);
  }
  static chainFromChainId(t) {
    const a = y(o._platform, BigInt(t));
    if (!a)
      throw new Error(`No matching chainId to determine network and chain: ${t}`);
    const [n, e] = a;
    return [n, e];
  }
  static async chainFromRpc(t) {
    const a = await t.getLedgerInfo();
    return this.chainFromChainId(a.chain_id.toString());
  }
}
export {
  o as A,
  T as a
};
