import { isTronLike as e } from "../../constants/chains.constants.mjs";
import { zeroAddress as t } from "../../constants/protocol.constants.mjs";
import { ContractType as n } from "../../types/ethereum-network.types.mjs";
import { ExternalActionId as r } from "../../types/external-action.types.mjs";
import "../../types/index.mjs";
import { transactionErrorCodes as i } from "../../error-handling/error-codes.constants.mjs";
import { getCurrentTimeInSeconds as a } from "../../functions/utils/time.utils.mjs";
import { addressToHexFormat as o, reorderZkCallData as s } from "../../functions/utils/tron.utils.mjs";
import { validateAndGetChainId as c } from "../../functions/utils/token-check.utils.mjs";
import { addPaddingToUtxos as l } from "../../functions/web3/events/getShieldedBalance.mjs";
import { outputUtxoProcessing as u } from "../../functions/pre-transaction/outputUtxoProcessing.mjs";
import { calculateTotalFee as d } from "../../functions/utils/fees.utils.mjs";
import { modifyVolatileTokenAmountChanges as f } from "../../functions/pre-transaction/getVolatileTransferAmount.mjs";
import { constructAdminData as p } from "../../functions/pre-transaction/constructAdminData.mjs";
import { getFeeStructure as m } from "../../functions/pre-transaction/getFeeStructure.mjs";
import { getKycAndSignatureData as h } from "../../functions/pre-transaction/getKycAndSignatureData.mjs";
import { buildCommitmentValidationData as g } from "../../functions/pre-transaction/buildCommitmentValidationData.mjs";
import "../../functions/pre-transaction/index.mjs";
import { constructZkProof as _ } from "../../functions/snarkjs/constructGeneralZkProof.mjs";
import { transactCallDirect as v } from "../../functions/web3/functionCalls/transactCallDirect.mjs";
import { transactCallRelayer as y } from "../../functions/web3/functionCalls/transactCallRelayer.mjs";
import { mergeWithFeeStructure as b } from "../../functions/pre-transaction/merge-with-fee-structure.mjs";
//#region libs/shared/common/src/data-structures/Hinkal/hinkalWithdraw.ts
var x = async (e, t, n, r) => {
	let i = [...await l(e, t, n, r)], o = [], s = a().toString();
	for (let t = 0; t < n.length; t += 1) {
		let { outputUtxos: n } = u(e.userKeys, i[t], r[t], s);
		o.push(n);
	}
	return {
		inputUtxosArray: i,
		outputUtxosArray: o
	};
}, S = async (a, l, u, S, C, w, T, E) => {
	let D = c(l), O = e(D), k = o(await a.getEthereumAddressByChain(D)), A = l.map((e) => e.erc20TokenAddress), j = await f(D, l, u), M;
	if (!C) {
		let e = T ?? await m(D, w, A, r.Transact), t = d(-j[0], e);
		M = {
			feeToken: e.feeToken,
			flatFee: t,
			variableRate: 0n
		}, b(D, A, j, M);
	}
	let N = l[0];
	if (!N) throw Error("Deposit Action: No Token Found");
	let P = {
		externalActionId: 0n,
		externalAddress: o(S),
		externalActionMetadata: "0x00"
	}, [F, { patchAccessTokenMerkleTree: I, signatureData: L }, { inputUtxosArray: R, outputUtxosArray: z }] = await Promise.all([
		a.getRandomRelay(D, !0),
		h(a, D, A, j, k),
		x(a, D, A, j)
	]);
	if (!C && !F) throw Error(i.RELAYER_NOT_AVAILABLE);
	let B = `swapperM${j.length.toString()}x${R[0].length}x1`, [{ zkCallData: V, circomData: H, dimData: U }, W] = await Promise.all([_(a.merkleTreeHinkalByChain[D], a.merkleTreeAccessTokenByChain[D], R, z, a.userKeys, B, P.externalActionId, P.externalAddress, P.externalActionMetadata, a.generateProofRemotely, C ? t : F, D, void 0, void 0, void 0, I, void 0, M, a.getContractWithFetcher(D, n.HinkalHelperContract), L, C ? k : void 0), g(D, a.userKeys, A, R)]), G = await s(O, V, U, H);
	if (C) {
		if (!N) throw Error("Withdraw Action: No Token Found");
		return await v(a, D, j[0], N, V, H, U);
	}
	return await y(D, V, U, H, W, void 0, void 0, p(E, D, A, j, k), G);
};
//#endregion
export { S as hinkalWithdraw };
