import { isSolanaLike as e, networkRegistry as t } from "../../constants/chains.constants.mjs";
import { TokenType as n, zeroAddress as r } from "../../constants/protocol.constants.mjs";
import { httpClient as i } from "../../data-structures/http/HttpClient.mjs";
import "../../data-structures/http/index.mjs";
import { isValidSolanaPublicKey as a } from "./addresses.mjs";
import { ERC1155ABI as o, ERC721ABI as s } from "../../externalABIs/index.mjs";
import { getErc20TokensForChain as c, isNFTToken as l } from "./erc20tokenFunctions.mjs";
import { Logger as u } from "../../error-handling/logger.mjs";
import { createCustomSolanaConnection as d, createJsonRpcProvider as f } from "./create-provider.mjs";
import { ERC20ABI as p } from "../../externalABIs/swapAbi.mjs";
import { getFormattedIpfsUrl as m } from "./ipfs.mjs";
import { ethers as h } from "ethers";
import { Connection as g, PublicKey as _ } from "@solana/web3.js";
import { TOKEN_2022_PROGRAM_ID as v, TOKEN_PROGRAM_ID as y, getAssociatedTokenAddress as b } from "@solana/spl-token";
//#region libs/shared/common/src/functions/utils/publicBalance.utils.ts
var x = async (e, t, n) => {
	try {
		let i = n.erc20TokenAddress, a, o = new h.Contract(n.erc20TokenAddress, p, e);
		a = i === r ? await e.getBalance(t) : await o?.balanceOf(t);
		let s = a;
		return s <= 0n ? null : {
			token: n,
			balance: s
		};
	} catch {
		return null;
	}
}, S = async (e, t) => {
	let r = t.nftTokenType === n.ERC721, a = new h.Contract(t.erc20TokenAddress, r ? s : o, e), { tokenIds: c } = t;
	if (!c?.length) return null;
	let l = await Promise.all(c.map(async (e) => {
		try {
			let t = await a[r ? "tokenURI" : "uri"](e), n = t.startsWith("ipfs://") ? m(t, !1) : t, o = r ? n : n.replace("{id}", e);
			if (o) {
				let t = await i.get(o);
				return t?.image?.startsWith("ipfs://") && (t.image = m(t.image, !1)), {
					image: t.image,
					tokenId: Number(e),
					name: t.name
				};
			}
			return { tokenId: Number(e) };
		} catch {
			return { tokenId: Number(e) };
		}
	}));
	return {
		token: t,
		balance: c?.length ? BigInt(c.length) : 0n,
		nfts: l
	};
}, C = async (e, t, n) => {
	let r = n ?? c(e), i = f(e);
	return (await Promise.all(r.map(async (e) => l(e) ? S(i, e) : x(i, t, e)))).filter((e) => e !== null);
}, w = async (e, t, n) => {
	try {
		let i = f(e), a = new h.Contract(n, p, i), o;
		return o = n === r ? await i.getBalance(t) : await a?.balanceOf(t), o;
	} catch {
		return u.error(`Failed to fetch public balance for address ${t}`), null;
	}
}, T = async (n, r) => {
	try {
		if (e(n)) {
			if (!a(r)) return 0;
			let { fetchRpcUrl: e } = t[n];
			return e ? (await new g(e).getSignaturesForAddress(new _(r))).length : 0;
		}
		return a(r) ? 0 : await f(n).getTransactionCount(r);
	} catch (e) {
		return u.error(`Failed to fetch transaction count for address ${r} on chain ${n}`, e), 0;
	}
}, E = async (e, n, r) => {
	let i = d(t[e].fetchRpcUrl ?? "");
	return (await Promise.all(r.map(async (e) => {
		if (l(e)) return {
			token: e,
			balance: 1n,
			nfts: [{
				image: e.logoURI,
				tokenId: 0,
				name: e.symbol
			}]
		};
		if (e.erc20TokenAddress === "11111111111111111111111111111111") return {
			token: e,
			balance: BigInt(await i.getBalance(new _(n)))
		};
		let t = await b(new _(e.erc20TokenAddress), new _(n), !0, e.is2022Program ? v : y);
		if (!await i.getAccountInfo(t)) return {
			token: e,
			balance: 0n
		};
		let r = await i.getTokenAccountBalance(t);
		return {
			token: e,
			balance: BigInt(r.value.amount)
		};
	}))).filter((e) => e.balance > 0n);
};
//#endregion
export { x as getPublicBalance, w as getPublicBalanceByTokenAddress, C as getPublicBalancesOfTokens, T as getPublicTransactionCount, E as getSolanaPublicBalances };
