var b = Object.defineProperty;
var C = (r, t, n) => t in r ? b(r, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : r[t] = n;
var w = (r, t, n) => C(r, typeof t != "symbol" ? t + "" : t, n);
import { aM as d, P as u, bM as E, aO as y, bN as N, H as I, W as R, ch as S, n as x, m as B, bP as D, bQ as L } from "./address-QpeLv2yn.mjs";
import { C as _ } from "./chain-B7MIR9_r.mjs";
class M extends _ {
  async getTokenAccount(t, n) {
    const { getAssociatedTokenAddress: a } = await import("./index-DQp_O8iB.mjs"), e = new d(n).unwrap(), s = new d(t).unwrap(), i = await a(e, s);
    return {
      chain: this.chain,
      address: new d(i.toString())
    };
  }
}
const P = new u("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), v = new u("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), G = new u("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
new u("So11111111111111111111111111111111111111112");
new u("9pan9bMn5HatX4EJdBwg9VgCa7Uz5HL8N1m5D3NdXejP");
const l = class l extends E {
  constructor(t, n) {
    super(t, n ?? N(t, l._platform));
  }
  getRpc(t, n = {
    commitment: "confirmed",
    disableRetryOnRateLimit: !0
  }) {
    if (t in this.config)
      return new I(this.config[t].rpc, n);
    throw new Error("No configuration available for chain: " + t);
  }
  getChain(t, n) {
    if (t in this.config)
      return new M(t, this, n);
    throw new Error("No configuration available for chain: " + t);
  }
  static nativeTokenId(t, n) {
    if (!l.isSupportedChain(n))
      throw new Error(`invalid chain: ${n}`);
    return R.chainAddress(n, S);
  }
  static isNativeTokenId(t, n, a) {
    return !this.isSupportedChain(n) || a.chain !== n ? !1 : this.nativeTokenId(t, n) == a;
  }
  static isSupportedChain(t) {
    return x(t) === l._platform;
  }
  static async getDecimals(t, n, a, e) {
    if (B(e))
      return D(l._platform);
    let s = await a.getParsedAccountInfo(new d(e).unwrap());
    if (!s || !s.value)
      throw new Error("could not fetch token details");
    const { decimals: i } = s.value.data.parsed.info;
    return i;
  }
  static async getBalance(t, n, a, e, s) {
    const i = new u(e);
    if (B(s))
      return BigInt(await a.getBalance(i));
    const c = await a.getTokenAccountsByOwner(i, {
      mint: new d(s).unwrap()
    }), o = c.value.length > 0 ? c.value[0].pubkey : i, h = await a.getTokenAccountBalance(o);
    return BigInt(h.value.amount);
  }
  static async getBalances(t, n, a, e) {
    const s = BigInt(await a.getBalance(new u(e))), i = (await Promise.all([P, v].map((o) => new u(o)).map((o) => a.getParsedTokenAccountsByOwner(new u(e), {
      programId: o
    })))).reduce((o, h) => o.concat(h.value), []), c = { native: s };
    return i.forEach((o) => {
      var f, g, p, k, T;
      const h = (g = (f = o.account.data.parsed) == null ? void 0 : f.info) == null ? void 0 : g.mint, m = (T = (k = (p = o.account.data.parsed) == null ? void 0 : p.info) == null ? void 0 : k.tokenAmount) == null ? void 0 : T.amount;
      h && m && (c[h] = BigInt(m));
    }), c;
  }
  static async sendWait(t, n, a, e) {
    const s = await Promise.all(a.map((o) => this.sendTxWithRetry(n, o, e))), i = s.map((o) => o.signature), c = s.filter((o) => o.response.value.err).map((o) => o.response.value.err);
    if (c.length > 0)
      throw new Error(`Failed to confirm transaction: ${c}`);
    return i;
  }
  static async sendTxWithRetry(t, n, a = {}, e = 5e3) {
    const s = a.preflightCommitment ?? t.commitment, i = await t.sendRawTransaction(n, {
      ...a,
      skipPreflight: !1,
      // The first send should not skip preflight to catch any errors
      maxRetries: 0,
      preflightCommitment: s
    }), { blockhash: c, lastValidBlockHeight: o } = await t.getLatestBlockhash(), h = t.confirmTransaction({
      signature: i,
      blockhash: c,
      lastValidBlockHeight: o
    }, s);
    let m = null;
    for (; !m && (m = await Promise.race([
      h,
      new Promise((f) => setTimeout(() => {
        f(null);
      }, e))
    ]), !m); )
      await t.sendRawTransaction(n, {
        ...a,
        skipPreflight: !0,
        maxRetries: 0,
        preflightCommitment: s
      });
    return { signature: i, response: m };
  }
  static async latestBlock(t, n) {
    return t.getLatestBlockhash(n ?? t.commitment);
  }
  static async getLatestBlock(t) {
    return await t.getSlot();
  }
  static async getLatestFinalizedBlock(t) {
    const { lastValidBlockHeight: n } = await this.latestBlock(t, "finalized");
    return n;
  }
  static chainFromChainId(t) {
    const n = L(l._platform, t);
    if (!n)
      throw new Error(`No matching genesis hash to determine network and chain: ${t}`);
    const [a, e] = n;
    return [a, e];
  }
  static async chainFromRpc(t) {
    try {
      const n = await t.getGenesisHash();
      return l.chainFromChainId(n);
    } catch (n) {
      if (t.rpcEndpoint.includes("http://127") || t.rpcEndpoint.includes("http://localhost") || t.rpcEndpoint === "http://solana-devnet:8899")
        return ["Devnet", "Solana"];
      throw n;
    }
  }
  static async getTokenProgramId(t, n) {
    const a = await t.getAccountInfo(n);
    if (!a)
      throw new Error(`Mint account not found: ${n.toBase58()}`);
    const e = a.owner;
    if (!e)
      throw new Error(`Mint account has no owner: ${n.toBase58()}`);
    if (e.equals(P) || e.equals(v))
      return e;
    throw new Error(`Mint account has unsupported token program: ${n.toBase58()} (${e.toBase58()})`);
  }
};
w(l, "_platform", y);
let A = l;
class H {
  constructor(t, n, a, e, s = !1) {
    w(this, "transaction");
    w(this, "network");
    w(this, "chain");
    w(this, "description");
    w(this, "parallelizable");
    this.transaction = t, this.network = n, this.chain = a, this.description = e, this.parallelizable = s;
  }
}
function V(r) {
  return r.signatures !== void 0 && r.message !== void 0;
}
export {
  G as A,
  H as S,
  P as T,
  A as a,
  M as b,
  v as c,
  V as i
};
