var w = Object.defineProperty;
var _ = (i, t, a) => t in i ? w(i, t, { enumerable: !0, configurable: !0, writable: !0, value: a }) : i[t] = a;
var h = (i, t, a) => _(i, typeof t != "symbol" ? t + "" : t, a);
import { ag as y, ah as b, ai as p, aj as S, ak as C, al as u, h as T, am as d, an as v, ao as f, ap as A, aq as N, aa as I } from "./index-TykGc-eA.mjs";
import { e as g, s as B, t as k, o as x, z as E } from "./chunk-IMTC3J2M-BdRzY3I0.mjs";
class m extends y {
}
const c = class c extends b {
  constructor(t, a) {
    super(t, a ?? S(t, c._platform));
  }
  getRpc(t) {
    if (t in this.config) {
      const a = this.network === "Mainnet" ? g.MAINNET : g.TESTNET, n = new B({ fullnode: this.config[t].rpc, network: a });
      return new k(n);
    }
    throw new Error("No configuration available for chain: " + t);
  }
  getChain(t, a) {
    if (t in this.config)
      return new m(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 C.tokenId(a, u);
  }
  static isNativeTokenId(t, a, n) {
    return !this.isSupportedChain(a) || n.chain !== a ? !1 : this.nativeTokenId(t, a) == n;
  }
  static isSupportedChain(t) {
    return T(t) === c._platform;
  }
  static async getDecimals(t, a, n) {
    if (d(n) || n === u)
      return v(c._platform);
    const s = n.toString();
    return (await a.getFungibleAssetMetadataByAssetType({ assetType: s })).decimals;
  }
  static async getBalance(t, a, n, s) {
    var e;
    const o = d(s) ? u : s.toString();
    try {
      return ((e = (await a.getCurrentFungibleAssetBalances({
        options: {
          where: {
            owner_address: { _eq: n },
            asset_type: { _eq: o }
          }
        }
      }))[0]) == null ? void 0 : e.amount) ?? null;
    } catch (r) {
      if (r.status === 404)
        return null;
      throw r;
    }
  }
  static async getBalances(t, a, n, s) {
    return (await Promise.all(s.map(async (e) => {
      const r = await this.getBalance(t, a, n, e);
      return { [d(e) ? "native" : new f(e).toString()]: r };
    }))).reduce((e, r) => Object.assign(e, r), {});
  }
  static async sendWait(t, a, n) {
    const s = [];
    for (const o of n) {
      const e = await a.transaction.submit.simple(o.transaction), r = await a.waitForTransaction({
        transactionHash: e.hash
      });
      s.push(r.hash);
    }
    return s;
  }
  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 = A(c._platform, BigInt(t));
    if (!a)
      throw new Error(`No matching chainId to determine network and chain: ${t}`);
    const [n, s] = a;
    return [n, s];
  }
  static async chainFromRpc(t) {
    const a = await t.getLedgerInfo();
    return this.chainFromChainId(a.chain_id.toString());
  }
};
h(c, "_platform", p);
let l = c;
async function F(i, t) {
  const [a, n] = await l.chainFromRpc(i), s = x.fromPrivateKey({
    // TODO: support secp256k1
    privateKey: new E(N.decode(t))
  });
  return new O(n, s, i);
}
class O {
  constructor(t, a, n, s) {
    h(this, "_chain");
    h(this, "_account");
    h(this, "_rpc");
    h(this, "_debug");
    this._chain = t, this._account = a, this._rpc = n, this._debug = s;
  }
  chain() {
    return this._chain;
  }
  address() {
    return this._account.accountAddress.toString();
  }
  async signAndSend(t) {
    const a = [];
    for (const n of t) {
      const { description: s, transaction: o } = n;
      this._debug && console.log(`Signing: ${s} for ${this.address()}`);
      const e = await this._rpc.transaction.build.simple({
        sender: this._account.accountAddress,
        data: o
      }), { hash: r } = await this._simSignSend(e);
      a.push(r);
    }
    return a;
  }
  async _simSignSend(t) {
    return await this._rpc.transaction.simulate.simple({
      signerPublicKey: this._account.publicKey,
      transaction: t
    }).then((a) => a.forEach((n) => {
      if (!n.success)
        throw new Error(`Transaction failed: ${n.vm_status}
${JSON.stringify(n, null, 2)}`);
    })), this._rpc.signAndSubmitTransaction({ signer: this._account, transaction: t }).then((a) => this._rpc.waitForTransaction({ transactionHash: a.hash }));
  }
}
const P = {
  Address: f,
  Platform: l,
  getSigner: F,
  protocols: {
    WormholeCore: () => import("./index-D_9dbHJA.mjs"),
    TokenBridge: () => import("./index-DXrLGiFi.mjs"),
    CircleBridge: () => import("./index-DkHuDrw3.mjs")
  },
  getChain: (i, t, a) => new m(t, new l(i, I(i, p, {
    [t]: a
  })))
}, M = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  default: P
}, Symbol.toStringTag, { value: "Module" }));
export {
  l as A,
  M as a
};
