import { isSolanaLike as e, isTronLike as t } from "../../../constants/chains.constants.mjs";
import { caseInsensitiveEqual as n } from "../../utils/caseInsensitive.utils.mjs";
import { Utxo as r } from "../../../data-structures/utxo/Utxo.mjs";
import { checkAddressNotUpdated as i } from "../../utils/upToDateState.mjs";
import { getHinkalCache as a, resetCache as o, setHinkalCache as s } from "../../utils/cacheFunctions.mjs";
import { encodeTokenWithId as c } from "../../utils/encodeTokenWithId.mjs";
import { formatMintAddress as l } from "../../utils/solanaMint.utils.mjs";
import { getInputUtxosEnclave as u } from "./getInputUtxosEnclave.mjs";
import { hashEthereumAddress as d } from "../../utils/addresses.mjs";
import { getScheduledTransactionsNullifierIndexes as f } from "../../../API/scheduled-transactions-calls.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, a, o, c, l, u) => h.runExclusive(async () => {
	u += 1;
	let d = o.slice(u), { lastOutput: f, additionalEncryptedOutputs: p } = await e.utxoUtils.batchProcess(d, l, t, n), m = [...c, ...p];
	return await i(e, r), s({
		lastOutput: f,
		encryptedOutputs: m
	}, e, n, a), await e.utxoUtils.buildBatchProcess(m, t, n);
}), _ = async (e, t, n, r) => {
	let { utxos: i, encryptedOutputs: a, lastOutput: o } = await u(e, n, t);
	return s({
		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, i, a, o, s) => {
	for (e.sort((e, t) => e.amount <= t.amount ? 1 : -1); e.length < t || e.length > t && e.length < 6;) if (e.push(new r({
		amount: 0n,
		erc20TokenAddress: a,
		mintAddress: s,
		shieldedPrivateKey: i,
		tokenId: o
	})), 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: s = !1, allowRemoteDecryption: c = !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);
	s && o(e, t, d);
	let { encryptedOutputs: _, lastOutput: y } = a(e, t, d);
	await i(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 || !c && w), D = !T && c && 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 i(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: r = void 0, ethAddress: i = void 0, resetCacheBefore: a = !1, allowRemoteDecryption: o = !1 }) => {
	let s = i ?? await e.getEthereumAddressByChain(n), c = d(t(n) ? p(s) : s), { inputUtxos: l } = await S({
		hinkal: e,
		chainId: n,
		passedShieldedPublicKey: r,
		ethAddress: s,
		resetCacheBefore: a,
		allowRemoteDecryption: o,
		useBlockedUtxos: !0
	});
	if (!l.length) return { inputUtxos: [] };
	let { indexes: u } = await f({
		hashedEthereumAddress: c,
		nullifiers: l.map((e) => e.getNullifier())
	});
	return { inputUtxos: u.map((e) => l[e]).filter((e) => !!e) };
}, w = async ({ hinkal: t, tokenWithId: r, minInput: i = 2, sliceIfMore6: a = !0, chainId: o, passedShieldedPrivateKey: s = void 0, passedShieldedPublicKey: u = void 0, ethAddress: d = void 0, resetCacheBefore: f = !1, ensuredTokensWithId: p, allowRemoteDecryption: m = !1, useBlockedUtxos: h = !1 }) => {
	let g = s ?? t.userKeys.getShieldedPrivateKey(), { inputUtxos: _ } = h ? await C({
		hinkal: t,
		chainId: o,
		ethAddress: d,
		passedShieldedPrivateKey: s,
		resetCacheBefore: f,
		allowRemoteDecryption: m
	}) : await S({
		hinkal: t,
		tokenWithId: r,
		minInput: i,
		sliceIfMore6: a,
		chainId: o,
		passedShieldedPrivateKey: s,
		passedShieldedPublicKey: u,
		ethAddress: d,
		resetCacheBefore: f,
		allowRemoteDecryption: m
	}), v = y(r), b = /* @__PURE__ */ new Map();
	_.forEach((e) => {
		let t = e.getTokenAddress(o);
		if (v && !v.some((r) => n(r.erc20TokenAddress, t) && r.tokenId === e.tokenId)) return;
		let r = c(o, {
			erc20TokenAddress: t,
			tokenId: e.tokenId
		}), i = b.get(r) ?? [];
		b.set(r, [...i, e]);
	}), p?.forEach((e) => {
		let t = c(o, {
			erc20TokenAddress: e.erc20TokenAddress,
			tokenId: e.tokenId
		});
		b.set(t, b.get(t) ?? []);
	});
	for (let [t, n] of b) {
		let r = t.split("-")[0], s = Number(t.split("-")[1]), c = e(o), u = c ? r : void 0;
		x(n, i, a, g, c ? l(r).compressedAddress.toLowerCase() : r.toLowerCase(), s, u);
	}
	return b;
};
//#endregion
export { v as attemptGetInputUtxosRemotely, S as getInputUtxoAndBalance, C as getInputUtxoAndBalanceOfStuckUtxos, w as getInputUtxoAndBalancePerToken, _ as getInputUtxosRemotely };
