import { isTronLike as e, networkRegistry as t } from "../../constants/chains.constants.mjs";
import { TRON_DEFAULT_FEE_LIMIT_SUN as n, zeroAddress as r } from "../../constants/protocol.constants.mjs";
import { zeroFeeStructure as i } from "../../types/hinkal.types.mjs";
import { isValidPrivateAddress as a } from "../../functions/utils/addresses.mjs";
import { transactionErrorCodes as o } from "../../error-handling/error-codes.constants.mjs";
import { emitTxPublicData as s } from "../../API/admin-calls.mjs";
import { getCurrentTimeInSeconds as c } from "../../functions/utils/time.utils.mjs";
import { Utxo as l } from "../utxo/Utxo.mjs";
import { addressToHexFormat as u, approveTronTokens as d, assertTronSufficientBalance as f, circomDataToArray as p, createTronCallData as m, evmHexToTronBase58Address as h, parseZkCalldata as g, reorderZkCallData as _, splitTronTokensByNative as v } from "../../functions/utils/tron.utils.mjs";
import { validateAndGetChainId as y } from "../../functions/utils/token-check.utils.mjs";
import { addPaddingToUtxos as b } from "../../functions/web3/events/getShieldedBalance.mjs";
import { outputUtxoProcessing as x } from "../../functions/pre-transaction/outputUtxoProcessing.mjs";
import { constructAdminData as S } from "../../functions/pre-transaction/constructAdminData.mjs";
import { simulateTronTransaction as C } from "../../functions/utils/tronSimulation.utils.mjs";
import { constructZkProof as w } from "../../functions/snarkjs/constructGeneralZkProof.mjs";
import { waitForTronErc20Approvals as T } from "../../functions/web3/functionCalls/waitForErc20Approvals.mjs";
import { transactCallDirect as E } from "../../functions/web3/functionCalls/transactCallDirect.mjs";
//#region libs/shared/common/src/data-structures/Hinkal/hinkalDeposit.ts
var D = async (e, t) => ({
	externalActionData: {
		externalActionId: 0n,
		externalAddress: u(await e.getEthereumAddressByChain(t)),
		externalActionMetadata: "0x00"
	},
	contractTransaction: void 0,
	contractToApprove: void 0
}), O = async (e, t, n, r) => {
	let i = [...await b(e, t, n, r)], a = [], o = c().toString();
	for (let t = 0; t < n.length; t += 1) {
		let { outputUtxos: n } = x(e.userKeys, i[t], r[t], o);
		a.push(n);
	}
	return {
		inputUtxosArray: i,
		outputUtxosArray: a
	};
}, k = async (e, r, i, a, o, s, c, l = !0, u = !1) => {
	let y = e.getTronWeb(), { hinkalAddress: b, hinkalABI: x } = t[r].contractData;
	if (!x) throw Error("Hinkal ABI not found");
	let S = h(b), w = await e.getEthereumAddressByChain(r), { totalTrxValue: E, tokensNeedingApproval: D } = v(i, a), { a: O, b: k, c: A } = g(o), j = await _(!0, o, c, s, !1);
	if (!j) throw Error("Tron proof signature not found");
	let M = [
		j.v,
		j.r,
		j.s
	], N = [
		c.tokenNumber,
		c.nullifierAmount,
		c.outputAmount
	], P = p(s), F = E > 0n ? Number(E) : 0;
	if (u) {
		let e = m(x, "transact", [
			M,
			O,
			k,
			A,
			N,
			P
		]), { transaction: t } = await y.transactionBuilder.triggerSmartContract(S, "", {
			feeLimit: n,
			callValue: F,
			input: e
		}, [], w);
		return t;
	}
	await d(y, w, S, D), D.size > 0 && await T(y, w, S, Array.from(D.entries()).map(([e, t]) => ({
		tokenAddress: e,
		requiredAmount: t
	}))), l && (await C(r, S, "transact", x, [
		M,
		O,
		k,
		A,
		N,
		P
	], w, E > 0n ? E : void 0), await f(y, r, S, w, m(x, "transact", [
		M,
		O,
		k,
		A,
		N,
		P
	]), E));
	let I = await y.contract(x, S).transact(M, O, k, A, N, P).send({
		feeLimit: n,
		callValue: F
	}), L = typeof I == "string" ? I : I?.txid ?? I?.transaction?.txID;
	if (!L) throw Error("Tron deposit failed (missing txid)");
	return L;
}, A = async (t, n, a, o = !0, s = !1) => {
	let c = y(n), l = e(c), u = n.map((e) => e.erc20TokenAddress), [{ externalActionData: d, contractTransaction: f, contractToApprove: p }, { inputUtxosArray: m, outputUtxosArray: h }] = await Promise.all([D(t, c), O(t, c, u, a)]), { zkCallData: g, circomData: _, dimData: v } = await w(t.merkleTreeHinkalByChain[c], m, h, t.userKeys, d.externalActionId, d.externalAddress, d.externalActionMetadata, t.generateProofRemotely, i, r, c);
	return l ? await k(t, c, n, a, g, _, v, o, s) : await E(t, c, a, n, g, _, v, p, f, o, s);
}, j = async (t, n, c, u, d = !0, f = !1, p) => {
	let m = y(n), h = e(m), g = n.map((e) => e.erc20TokenAddress), [_, v, b, , x] = u.split(",");
	if (!a(u)) throw Error(o.RECIPIENT_FORMAT_INCORRECT);
	let C = g.map((e) => [new l({
		amount: 0n,
		erc20TokenAddress: e,
		nullifyingKey: t.userKeys.getShieldedPrivateKey(),
		spendingPublicKey: t.userKeys.getSpendingKeyPair().pubSpendingBJJPoint,
		isNewStyle: !0
	}), new l({
		amount: 0n,
		erc20TokenAddress: e,
		nullifyingKey: t.userKeys.getShieldedPrivateKey(),
		spendingPublicKey: t.userKeys.getSpendingKeyPair().pubSpendingBJJPoint,
		isNewStyle: !0
	})]), T = g.map((e, t) => [new l({
		amount: c[t],
		erc20TokenAddress: e,
		H0: [BigInt(v), BigInt(b)],
		stealthAddress: _,
		encryptionKey: x,
		isNewStyle: !0
	})]), { externalActionData: O, contractTransaction: A, contractToApprove: j } = await D(t, m), { zkCallData: M, circomData: N, dimData: P } = await w(t.merkleTreeHinkalByChain[m], C, T, t.userKeys, O.externalActionId, O.externalAddress, O.externalActionMetadata, t.generateProofRemotely, i, r, m);
	if (h) return await k(t, m, n, c, M, N, P, d, f);
	let F = await E(t, m, c, n, M, N, P, j, A, d, f);
	return s(S(p, m, g, c, await t.getEthereumAddress())), F;
};
//#endregion
export { A as hinkalDeposit, j as hinkalDepositForOther };
