import { isExtension as e } from "../../../constants/vite.constants.mjs";
import { isSolanaLike as t } from "../../../constants/chains.constants.mjs";
import { caseInsensitiveEqual as n } from "../../utils/caseInsensitive.utils.mjs";
import { DIRECTLY_SHOW_MESSAGE_ERROR_CODES as r } from "../../../error-handling/error-codes.constants.mjs";
import { getAmountWithPrecision as i } from "../etherFunctions.mjs";
import { getERC20Token as a, getErc20TokensForChain as o } from "../../utils/erc20tokenFunctions.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, r, i, a, s, c = !1, l, u = !1, p = !1) => h(r).runExclusive(async () => {
	let { inputUtxos: h } = p ? await f({
		hinkal: t,
		chainId: r,
		ethAddress: s,
		passedShieldedPrivateKey: i,
		resetCacheBefore: c,
		allowRemoteDecryption: l
	}) : await d({
		hinkal: t,
		sliceIfMore6: !1,
		chainId: r,
		passedShieldedPrivateKey: i,
		passedShieldedPublicKey: a,
		ethAddress: s,
		resetCacheBefore: c,
		allowRemoteDecryption: l
	});
	u && e && await m.fetchAndUpdatePrivateTokens(h.map((e) => e.getTokenAddress(r)).filter((e) => e !== void 0), r, s);
	let g = (e ? await m.getPrivateTokens(r, s) : o(r)).map((e) => ({
		token: e,
		balance: h.reduce((t, i) => {
			let a = e.erc20TokenAddress;
			return n(a, i.getTokenAddress(r)) ? t + i.amount : t;
		}, 0n),
		timestamp: h.filter((t) => n(t.getTokenAddress(r), e.erc20TokenAddress))[0]?.timeStamp,
		nfts: []
	})), _ = /* @__PURE__ */ new Map();
	return g.forEach((e) => {
		_.set(e.token.erc20TokenAddress.toLowerCase(), e);
	}), _;
}), _ = async (e, n, o, d, f = 6, m = o.map(() => 0), h = !1, g = !1) => {
	let { userKeys: _ } = e, v = _.getSpendingKeyPair().pubSpendingBJJPoint, y = [], b = [];
	if (o.length === 0) return [];
	let x = await p({
		hinkal: e,
		sliceIfMore6: !1,
		ensuredTokensWithId: o.map((e, t) => ({
			erc20TokenAddress: o[t],
			tokenId: m[t]
		})),
		allowRemoteDecryption: e.generateProofRemotely,
		useBlockedUtxos: g,
		chainId: n
	}), S = 0;
	for (let e = 0; e < o.length; e += 1) if (h === !1) {
		let t = x.get(l(n, {
			erc20TokenAddress: o[e],
			tokenId: m[e]
		})) ?? [];
		if (!t) throw Error("Missing utxos for token");
		S = Math.max(t.length, S), b.push(t);
	} else b.push([]);
	return S === 2 ? b : (b.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 = a(o[l], n), t = e.decimals === 18 ? 6 : 2, u = Number(i(s, e, t));
				throw new c(u, `${r.UTXO_LIMITATIONS}. Please try again with ${u} ${e.symbol}, including gas fees.`);
			}
			y.push(t);
		} else if (e.length <= f) {
			let r = [...e], i = f - e.length;
			for (; i > 0;) {
				--i;
				let e = t(n), a = e ? o[l] : void 0, c = e ? u(o[l]).compressedAddress : o[l];
				r.push(new s({
					amount: 0n,
					erc20TokenAddress: c,
					mintAddress: a,
					nullifyingKey: _.getShieldedPrivateKey(),
					spendingPublicKey: v,
					isNewStyle: !0
				}));
			}
			y.push(r);
		}
	}), y);
};
//#endregion
export { _ as addPaddingToUtxos, g as getShieldedBalance };
