import { isExtension as e } from "../../../constants/vite.constants.mjs";
import { isSolanaLike as t } from "../../../constants/chains.constants.mjs";
import { DIRECTLY_SHOW_MESSAGE_ERROR_CODES as n } from "../../../error-handling/error-codes.constants.mjs";
import { getAmountWithPrecision as r } from "../etherFunctions.mjs";
import { getERC20Token as i, getErc20TokensForChain as a } from "../../utils/erc20tokenFunctions.mjs";
import { caseInsensitiveEqual as o } from "../../utils/caseInsensitive.utils.mjs";
import { Utxo as s } from "../../../data-structures/utxo/Utxo.mjs";
import { ErrorWithAmount as c } from "../../../error-handling/customErrors/ErrorWithAmount.mjs";
import "../../../error-handling/customErrors/index.mjs";
import { encodeTokenWithId as l } from "../../utils/encodeTokenWithId.mjs";
import { formatMintAddress as u } from "../../utils/solanaMint.utils.mjs";
import { getInputUtxoAndBalance as d, getInputUtxoAndBalanceOfStuckUtxos as f, getInputUtxoAndBalancePerToken as p } from "./getInputUtxoAndBalance.mjs";
import { privateTokensDB as m } from "../../../data-structures/TokenDBs/PrivateTokensDB.mjs";
import { getChainBalanceFetchingMutex as h } from "../../utils/mutexes.utils.mjs";
//#region libs/shared/common/src/functions/web3/events/getShieldedBalance.ts
var g = async (t, n, r, i, s, c = !1, l, u = !1, p = !1) => h(n).runExclusive(async () => {
	let { inputUtxos: h } = p ? await f({
		hinkal: t,
		chainId: n,
		ethAddress: s,
		passedShieldedPrivateKey: r,
		resetCacheBefore: c,
		allowRemoteDecryption: l
	}) : await d({
		hinkal: t,
		sliceIfMore6: !1,
		chainId: n,
		passedShieldedPrivateKey: r,
		passedShieldedPublicKey: i,
		ethAddress: s,
		resetCacheBefore: c,
		allowRemoteDecryption: l
	});
	u && e && await m.fetchAndUpdatePrivateTokens(h.map((e) => e.getTokenAddress(n)).filter((e) => e !== void 0), n, s);
	let g = (e ? await m.getPrivateTokens(n, s) : a(n)).map((e) => ({
		token: e,
		balance: h.reduce((t, r) => {
			let i = e.erc20TokenAddress;
			return o(i, r.getTokenAddress(n)) ? t + r.amount : t;
		}, 0n),
		timestamp: h.filter((t) => o(t.getTokenAddress(n), e.erc20TokenAddress))[0]?.timeStamp,
		nfts: h.filter((t) => o(t.getTokenAddress(n), e.erc20TokenAddress) && t.tokenId !== 0).map((e) => ({
			tokenId: e.tokenId,
			timestamp: e.timeStamp
		}))
	})), _ = /* @__PURE__ */ new Map();
	return g.forEach((e) => {
		_.set(e.token.erc20TokenAddress.toLowerCase(), e);
	}), _;
}), _ = async (e, a, o, d, f = 6, m = o.map(() => 0), h = !1, g = !1) => {
	let { userKeys: _ } = e, v = [], y = [];
	if (o.length === 0) return [];
	let b = await p({
		hinkal: e,
		sliceIfMore6: !1,
		ensuredTokensWithId: o.map((e, t) => ({
			erc20TokenAddress: o[t],
			tokenId: m[t]
		})),
		allowRemoteDecryption: e.generateProofRemotely,
		useBlockedUtxos: g,
		chainId: a
	}), x = 0;
	for (let e = 0; e < o.length; e += 1) if (h === !1) {
		let t = b.get(l(a, {
			erc20TokenAddress: o[e],
			tokenId: m[e]
		})) ?? [];
		if (!t) throw Error("Missing utxos for token");
		x = Math.max(t.length, x), y.push(t);
	} else y.push([]);
	return x === 2 ? y : (y.forEach((e, l) => {
		if (e.length > f) {
			let t = e.slice(0, f), s = t.reduce((e, t) => e + t.amount, 0n);
			if (d[l] < 0n && s < -d[l]) {
				let e = i(o[l], a), t = e.decimals === 18 ? 6 : 2, u = Number(r(s, e, t));
				throw new c(u, `${n.UTXO_LIMITATIONS}. Please try again with ${u} ${e.symbol}, including gas fees.`);
			}
			v.push(t);
		} else if (e.length <= f) {
			let n = [...e], r = f - e.length;
			for (; r > 0;) {
				--r;
				let e = t(a), i = e ? o[l] : void 0, c = e ? u(o[l]).compressedAddress : o[l];
				n.push(new s({
					amount: 0n,
					erc20TokenAddress: c,
					mintAddress: i,
					shieldedPrivateKey: _.getShieldedPrivateKey(),
					tokenId: m[l]
				}));
			}
			v.push(n);
		}
	}), v);
};
//#endregion
export { _ as addPaddingToUtxos, g as getShieldedBalance };
