var B = Object.defineProperty;
var P = (o, t, n) => t in o ? B(o, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : o[t] = n;
var d = (o, t, n) => P(o, typeof t != "symbol" ? t + "" : t, n);
import { b1 as f, af as l, b3 as y, bE as E, ae as S, W as N, c7 as R, a5 as I, j as p, bG as x, bH as D } from "./address-BkeBsCZD.mjs";
import { C as L, P as O } from "./chain-uXEmLNMk.mjs";
class G extends L {
  async getTokenAccount(t, n) {
    const { getAssociatedTokenAddress: e } = await import("./index-UZaBhUoD.mjs"), i = new f(n).unwrap(), s = new f(t).unwrap(), r = await e(i, s);
    return {
      chain: this.chain,
      address: new f(r.toString())
    };
  }
}
const W = new l("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), F = new l("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), z = new l("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
new l("So11111111111111111111111111111111111111112");
new l("9pan9bMn5HatX4EJdBwg9VgCa7Uz5HL8N1m5D3NdXejP");
const c = class c extends O {
  constructor(t, n) {
    super(t, n ?? E(t, c._platform));
  }
  getRpc(t, n = {
    commitment: "confirmed",
    disableRetryOnRateLimit: !0
  }) {
    if (t in this.config)
      return new S(this.config[t].rpc, n);
    throw new Error("No configuration available for chain: " + t);
  }
  getChain(t, n) {
    if (t in this.config)
      return new G(t, this, n);
    throw new Error("No configuration available for chain: " + t);
  }
  static nativeTokenId(t, n) {
    if (!c.isSupportedChain(n))
      throw new Error(`invalid chain: ${n}`);
    return N.chainAddress(n, R);
  }
  static isNativeTokenId(t, n, e) {
    return !this.isSupportedChain(n) || e.chain !== n ? !1 : this.nativeTokenId(t, n) == e;
  }
  static isSupportedChain(t) {
    return I(t) === c._platform;
  }
  static async getDecimals(t, n, e) {
    if (p(e))
      return x(c._platform);
    let i = await n.getParsedAccountInfo(new f(e).unwrap());
    if (!i || !i.value)
      throw new Error("could not fetch token details");
    const { decimals: s } = i.value.data.parsed.info;
    return s;
  }
  static async getBalance(t, n, e, i) {
    const s = new l(e);
    if (p(i))
      return BigInt(await n.getBalance(s));
    const r = await n.getTokenAccountsByOwner(s, {
      mint: new f(i).unwrap()
    }), h = r.value.length > 0 ? r.value[0].pubkey : s, a = await n.getTokenAccountBalance(h);
    return BigInt(a.value.amount);
  }
  static async getBalances(t, n, e, i) {
    let s;
    i.includes("native") && (s = BigInt(await n.getBalance(new l(e))));
    const r = (await Promise.all([W, F].map((a) => new l(a)).map((a) => n.getParsedTokenAccountsByOwner(new l(e), { programId: a })))).reduce((a, u) => a.concat(u.value), []);
    return i.map((a) => {
      var w, k, T, b;
      if (p(a))
        return { native: s };
      const u = new f(a).toString(), m = (b = (T = (k = (w = r.find((g) => {
        var A, C;
        return ((C = (A = g == null ? void 0 : g.account.data.parsed) == null ? void 0 : A.info) == null ? void 0 : C.mint) === a.toString();
      })) == null ? void 0 : w.account.data.parsed) == null ? void 0 : k.info) == null ? void 0 : T.tokenAmount) == null ? void 0 : b.amount;
      return m ? { [u]: BigInt(m) } : { [u]: null };
    }).reduce((a, u) => Object.assign(a, u), {});
  }
  static async sendWait(t, n, e, i) {
    const s = await Promise.all(e.map((a) => this.sendTxWithRetry(n, a, i))), r = s.map((a) => a.signature), h = s.filter((a) => a.response.value.err).map((a) => a.response.value.err);
    if (h.length > 0)
      throw new Error(`Failed to confirm transaction: ${h}`);
    return r;
  }
  static async sendTxWithRetry(t, n, e = {}, i = 5e3) {
    const s = e.preflightCommitment ?? t.commitment, r = await t.sendRawTransaction(n, {
      ...e,
      skipPreflight: !1,
      // The first send should not skip preflight to catch any errors
      maxRetries: 0,
      preflightCommitment: s
    }), { blockhash: h, lastValidBlockHeight: a } = await t.getLatestBlockhash(), u = t.confirmTransaction({
      signature: r,
      blockhash: h,
      lastValidBlockHeight: a
    }, s);
    let m = null;
    for (; !m && (m = await Promise.race([
      u,
      new Promise((w) => setTimeout(() => {
        w(null);
      }, i))
    ]), !m); )
      await t.sendRawTransaction(n, {
        ...e,
        skipPreflight: !0,
        maxRetries: 0,
        preflightCommitment: s
      });
    return { signature: r, 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 = D(c._platform, t);
    if (!n)
      throw new Error(`No matching genesis hash to determine network and chain: ${t}`);
    const [e, i] = n;
    return [e, i];
  }
  static async chainFromRpc(t) {
    try {
      const n = await t.getGenesisHash();
      return c.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;
    }
  }
};
d(c, "_platform", y);
let v = c;
class K {
  constructor(t, n, e, i, s = !1) {
    d(this, "transaction");
    d(this, "network");
    d(this, "chain");
    d(this, "description");
    d(this, "parallelizable");
    this.transaction = t, this.network = n, this.chain = e, this.description = i, this.parallelizable = s;
  }
}
function M(o) {
  return o.signatures !== void 0 && o.message !== void 0;
}
export {
  z as A,
  K as S,
  W as T,
  v as a,
  G as b,
  F as c,
  M as i
};
