import { isSolanaLike as e, isTronLike as t } from "../../../constants/chains.constants.mjs";
import { caseInsensitiveEqual as n } from "../../utils/caseInsensitive.utils.mjs";
import { hashEthereumAddress as r } from "../../utils/addresses.mjs";
import { getScheduledTransactionsNullifierIndexes as i } from "../../../API/scheduled-transactions-calls.mjs";
import { Utxo as a } from "../../../data-structures/utxo/Utxo.mjs";
import { checkAddressNotUpdated as o } from "../../utils/upToDateState.mjs";
import { getHinkalCache as s, resetCache as c, setHinkalCache as l } from "../../utils/cacheFunctions.mjs";
import { encodeTokenWithId as u } from "../../utils/encodeTokenWithId.mjs";
import { formatMintAddress as d } from "../../utils/solanaMint.utils.mjs";
import { getInputUtxosEnclave as f } from "./getInputUtxosEnclave.mjs";
import { addressToHexFormat as p } from "../../utils/tron.utils.mjs";
import { Mutex as m } from "async-mutex";
//#region libs/shared/common/src/functions/web3/events/getInputUtxoAndBalance.ts
var h = new m(), g = async (e, t, n, r, i, a, s, c, u) => h.runExclusive(async () => {
	u += 1;
	let d = a.slice(u), { lastOutput: f, additionalEncryptedOutputs: p } = await e.utxoUtils.batchProcess(d, c, t, n), m = [...s, ...p];
	return await o(e, r), l({
		lastOutput: f,
		encryptedOutputs: m
	}, e, n, i), await e.utxoUtils.buildBatchProcess(m, t, n);
}), _ = async (e, t, n, r) => {
	let { utxos: i, encryptedOutputs: a, lastOutput: o } = await f(e, n, t);
	return l({
		encryptedOutputs: a,
		lastOutput: o
	}, e, n, r), i;
}, v = async (e, t, n, r, i, a, o, s, c) => {
	try {
		return await _(e, t, n, i);
	} catch {
		return g(e, t, n, r, i, a, o, s, c);
	}
}, y = (e) => {
	if (Array.isArray(e)) return e;
	if (e !== void 0) return [e];
}, b = async (e, t, n) => await e.utxoUtils.batchFilterUtxosWithNullifier(t, n), x = (e, t, n, r, i, o, s, c) => {
	for (e.sort((e, t) => e.amount <= t.amount ? 1 : -1); e.length < t || e.length > t && e.length < 6;) if (e.push(new a({
		amount: 0n,
		erc20TokenAddress: o,
		mintAddress: c,
		nullifyingKey: r,
		spendingPublicKey: i,
		isNewStyle: !0
	})), n) for (; e.length > 6;) e.splice(e.length - 1);
}, S = async ({ hinkal: e, chainId: t, passedShieldedPublicKey: n = void 0, ethAddress: r = void 0, resetCacheBefore: i = !1, allowRemoteDecryption: a = !1, useBlockedUtxos: l = !1 }) => {
	let u = r ?? await e.getEthereumAddress(), d = n ?? e.userKeys.getShieldedPublicKey(), { userKeys: f } = e, p = e.encryptedOutputsByChain[t], m = e.nullifiersByChain[t], h = Array.from(p);
	i && c(e, t, d);
	let { encryptedOutputs: _, lastOutput: y } = s(e, t, d);
	await o(e, u);
	let x = y !== "", S = h.findIndex((e) => e.value === y), C = S > -1, w = !x && S === -1, T = e.areMerkleTreeUpdatesDisabled(), E = !T && (C || !a && w), D = !T && a && w, O = !E && !D, k = [];
	E ? k = await g(e, f, t, u, d, h, _, y, S) : D ? k = await v(e, f, t, u, d, h, _, y, S) : O && (k = await e.utxoUtils.buildBatchProcess(_, f, t)), await o(e, u);
	let A = await b(e, k, m);
	return { inputUtxos: l ? A.filter((e) => e.isBlocked) : A.filter((e) => !e.isBlocked) };
}, C = async ({ hinkal: e, chainId: n, passedShieldedPublicKey: a = void 0, ethAddress: o = void 0, resetCacheBefore: s = !1, allowRemoteDecryption: c = !1 }) => {
	let l = o ?? await e.getEthereumAddressByChain(n), u = r(t(n) ? p(l) : l), { inputUtxos: d } = await S({
		hinkal: e,
		chainId: n,
		passedShieldedPublicKey: a,
		ethAddress: l,
		resetCacheBefore: s,
		allowRemoteDecryption: c,
		useBlockedUtxos: !0
	});
	if (!d.length) return { inputUtxos: [] };
	let { indexes: f } = await i({
		hashedEthereumAddress: u,
		nullifiers: d.map((e) => e.getNullifier())
	});
	return { inputUtxos: f.map((e) => d[e]).filter((e) => !!e) };
}, w = async ({ hinkal: t, tokenWithId: r, minInput: i = 2, sliceIfMore6: a = !0, chainId: o, passedShieldedPrivateKey: s = void 0, passedShieldedPublicKey: c = void 0, ethAddress: l = void 0, resetCacheBefore: f = !1, ensuredTokensWithId: p, allowRemoteDecryption: m = !1, useBlockedUtxos: h = !1 }) => {
	let g = s ?? t.userKeys.getShieldedPrivateKey(), _ = t.userKeys.getSpendingKeyPair().pubSpendingBJJPoint, { inputUtxos: v } = h ? await C({
		hinkal: t,
		chainId: o,
		ethAddress: l,
		passedShieldedPrivateKey: s,
		resetCacheBefore: f,
		allowRemoteDecryption: m
	}) : await S({
		hinkal: t,
		tokenWithId: r,
		minInput: i,
		sliceIfMore6: a,
		chainId: o,
		passedShieldedPrivateKey: s,
		passedShieldedPublicKey: c,
		ethAddress: l,
		resetCacheBefore: f,
		allowRemoteDecryption: m
	}), b = y(r), w = /* @__PURE__ */ new Map();
	v.forEach((e) => {
		let t = e.getTokenAddress(o);
		if (b && !b.some((e) => n(e.erc20TokenAddress, t))) return;
		let r = u(o, {
			erc20TokenAddress: t,
			tokenId: 0
		}), i = w.get(r) ?? [];
		w.set(r, [...i, e]);
	}), p?.forEach((e) => {
		let t = u(o, {
			erc20TokenAddress: e.erc20TokenAddress,
			tokenId: 0
		});
		w.set(t, w.get(t) ?? []);
	});
	for (let [t, n] of w) {
		let r = t.split("-")[0], s = Number(t.split("-")[1]), c = e(o), l = c ? r : void 0;
		x(n, i, a, g, _, c ? d(r).compressedAddress.toLowerCase() : r.toLowerCase(), s, l);
	}
	return w;
};
//#endregion
export { v as attemptGetInputUtxosRemotely, S as getInputUtxoAndBalance, C as getInputUtxoAndBalanceOfStuckUtxos, w as getInputUtxoAndBalancePerToken, _ as getInputUtxosRemotely };
