import { ContractType as e } from "../../types/ethereum-network.types.mjs";
import { TRON_DEFAULT_FEE_LIMIT_SUN as t, zeroAddress as n } from "../../constants/protocol.constants.mjs";
import { caseInsensitiveEqual as r } from "../../functions/utils/caseInsensitive.utils.mjs";
import { ERC20ABI as i } from "../../externalABIs/index.mjs";
import { isSameERC20Token as a } from "../../functions/utils/token-check.utils.mjs";
import { waitForEthereumTransactionConfirmation as o } from "../../functions/utils/tx-confirmation.utils.mjs";
//#region libs/shared/common/src/data-structures/Hinkal/hinkalProoflessDeposit.utils.ts
var s = (e, t) => {
	let n = [], r = 0;
	for (let i = 0; i < e.length; i += 1) {
		let o = !1;
		for (let s = 0; s < r; s += 1) if (a(n[s].token, e[i])) {
			n[s].balance += t[i], o = !0;
			break;
		}
		o || (n[r] = {
			token: e[i],
			balance: t[i]
		}, r += 1);
	}
	return n;
}, c = async (t, i, a, s) => {
	for (let c = 0; c < s.length; c += 1) r(s[c].token.erc20TokenAddress, n) || await o(i, (await t.getContractWithSigner(i, e.ERC20Contract, s[c].token.erc20TokenAddress).approve(a, s[c].balance)).hash);
}, l = async (e, n, r, a) => {
	let o = Array.from(a.entries());
	for (let a = 0; a < o.length; a += 1) {
		let [s, c] = o[a], l = await e.contract(i, s).allowance(n, r).call({ from: n });
		if (BigInt(String(l)) < c) {
			let { transaction: i } = await e.transactionBuilder.triggerSmartContract(s, "approve(address,uint256)", { feeLimit: t }, [{
				type: "address",
				value: r
			}, {
				type: "uint256",
				value: c.toString()
			}], n), a = await e.trx.sign(i), o = await e.trx.sendRawTransaction(a);
			if (!o.result) {
				let e = o.code ?? "UNKNOWN", t = o.message ? Buffer.from(o.message, "hex").toString() : "no message";
				throw Error(`Tron token approval failed: ${e} - ${t}`);
			}
		}
	}
}, u = (e) => {
	let t = /* @__PURE__ */ new Set();
	e.forEach(({ extraRandomization: e, stealthAddress: n }) => {
		let r = `${e.toString()}:${n.toString()}`;
		if (t.has(r)) throw Error("Duplicate randomization and stealth address pair detected in stealthAddressStructures");
		t.add(r);
	});
};
//#endregion
export { s as aggregateAmountsForApproval, c as approveTokens, l as approveTronTokens, u as assertNoDuplicateStealthAddressStructures };
