import { ExternalActionId as e } from "../../types/external-action.types.mjs";
import { isTronLike as t } from "../../constants/chains.constants.mjs";
import { zeroAddress as n } from "../../constants/protocol.constants.mjs";
import { zeroFeeStructure as r } from "../../types/hinkal.types.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 { modifyVolatileTokenAmountChanges as d } from "../../functions/pre-transaction/getVolatileTransferAmount.mjs";
import { constructAdminData as f } from "../../functions/pre-transaction/constructAdminData.mjs";
import { getFeeStructure as p } from "../../functions/pre-transaction/getFeeStructure.mjs";
import { buildCommitmentValidationData as m } from "../../functions/pre-transaction/buildCommitmentValidationData.mjs";
import "../../functions/pre-transaction/index.mjs";
import { calculateModifiedFeeStructure as h } from "../../functions/utils/fees.utils.mjs";
import { constructZkProof as g } from "../../functions/snarkjs/constructGeneralZkProof.mjs";
import { transactCallDirect as _ } from "../../functions/web3/functionCalls/transactCallDirect.mjs";
import { transactCallRelayer as v } from "../../functions/web3/functionCalls/transactCallRelayer.mjs";
import { transactCallDirectTron as y } from "../../functions/web3/functionCalls/transactCallDirectTron.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 = t(D), k = o(await a.getEthereumAddressByChain(D)), A = l.map((e) => e.erc20TokenAddress), j = await d(D, l, u), M = l[0];
	if (!M) throw Error("Deposit Action: No Token Found");
	let N;
	if (!C) {
		let t = T ?? await p(D, w, A, e.Transact);
		N = await h(D, M, -j[0], t), b(D, A, j, N);
	}
	let P = {
		externalActionId: 0n,
		externalAddress: o(S),
		externalActionMetadata: "0x00"
	}, [F, { inputUtxosArray: I, outputUtxosArray: L }] = await Promise.all([a.getRandomRelay(D, !0), x(a, D, A, j)]);
	if (!C && !F) throw Error(i.RELAYER_NOT_AVAILABLE);
	let [{ zkCallData: R, circomData: z, dimData: B }, V] = await Promise.all([g(a.merkleTreeHinkalByChain[D], I, L, a.userKeys, P.externalActionId, P.externalAddress, P.externalActionMetadata, a.generateProofRemotely, N ?? r, C ? n : F, D, void 0, void 0, C ? k : void 0), m(D, a.userKeys, A, I)]), H = O && C ? void 0 : await s(O, R, B, z);
	if (C) {
		if (!M) throw Error("Withdraw Action: No Token Found");
		return O ? await y(a, D, j[0], M, R, z, B) : await _(a, D, j[0], M, R, z, B);
	}
	return await v(D, R, B, z, V, void 0, void 0, f(E, D, A, j, k), H);
};
//#endregion
export { S as hinkalWithdraw };
