import { networkRegistry as e } from "../../constants/chains.constants.mjs";
import "../../constants/protocol.constants.mjs";
import { ExternalActionId as t } from "../../types/external-action.types.mjs";
import "../../types/index.mjs";
import { transactionErrorCodes as n } from "../../error-handling/error-codes.constants.mjs";
import { randomBigInt as r } from "../../functions/web3/etherFunctions.mjs";
import { getCurrentTimeInSeconds as i } from "../../functions/utils/time.utils.mjs";
import { Utxo as a } from "../utxo/Utxo.mjs";
import { validateAndGetChainId as o } from "../../functions/utils/token-check.utils.mjs";
import { addPaddingToUtxos as s } from "../../functions/web3/events/getShieldedBalance.mjs";
import { outputUtxoProcessing as c } from "../../functions/pre-transaction/outputUtxoProcessing.mjs";
import { calcEncryptedOutputs as l } from "../../functions/snarkjs/common.snarkjs.mjs";
import { calculateTotalFee as u } from "../../functions/utils/fees.utils.mjs";
import { constructAdminData as d } from "../../functions/pre-transaction/constructAdminData.mjs";
import { ensureAmountChanges as f } from "../../functions/pre-transaction/ensureAmountChanges.mjs";
import { getFeeStructure as p } from "../../functions/pre-transaction/getFeeStructure.mjs";
import { shouldPatchAccessTokenMerkleTree as m } from "../../functions/pre-transaction/shouldPatchAccessTokenMerkleTree.mjs";
import { buildCommitmentValidationData as h } from "../../functions/pre-transaction/buildCommitmentValidationData.mjs";
import "../../functions/pre-transaction/index.mjs";
import "../../constants/index.mjs";
import { constructSolanaZkProof as g } from "../../functions/snarkjs/constructSolanaZkProof.mjs";
import { solanaTransactCallRelayer as _ } from "../../functions/web3/functionCalls/transactCallRelayer.mjs";
import { ethers as v } from "ethers";
//#region libs/shared/common/src/data-structures/Hinkal/hinkalSolanaWithdraw.ts
var y = async (e, t, n, r) => {
	let a = [...await s(e, t, n, r, 6)][0], o = i().toString(), { outputUtxos: l } = c(e.userKeys, a, r[0], o);
	return {
		inputUtxos: a,
		outputUtxos: l
	};
}, b = async (i, s, c, b, x, S, C) => {
	let w = o(s), T = s.map((e) => e.erc20TokenAddress), { originalDeployer: E, hinkalIdl: D } = e[w].contractData;
	if (!E || !D) throw Error("missing data");
	let { userKeys: O, merkleTreeAccessTokenByChain: k, merkleTreeHinkalByChain: A } = i, j = S ?? await p(w, x, T, t.Transact), M = u(-c[0], j), N = {
		feeToken: j.feeToken,
		flatFee: M,
		variableRate: 0n
	};
	c[0] -= N.flatFee;
	let [{ patchAccessTokenMerkleTree: P, kycRequired: F, hasAccessToken: I }, L, R, { inputUtxos: z, outputUtxos: B }] = await Promise.all([
		m(i, w, T, c),
		i.getEthereumAddressByChain(w),
		i.getRandomRelay(w, !0),
		y(i, w, T, c)
	]);
	if (!R) throw Error(n.RELAYER_NOT_AVAILABLE);
	if (T.length > 1) throw Error("Solana Withdraw: Only one mint address is supported");
	let V = a.findCorrectRandomization(r(31), i.userKeys.getShieldedPrivateKey()), H = {
		tokenNumber: T.length,
		nullifierAmount: z.length,
		outputAmount: B.length
	}, U = i.getSolanaProgram(D), W = l([B])[0][0], G = Array.from(v.getBytes(W));
	f([z], [B], c);
	let [{ proofAArr: K, proofBArr: q, proofCArr: J, publicInputsArr: Y }, X] = await Promise.all([g(i.generateProofRemotely, E, A[w], k[w], O, U, [T[0]], [z], [B], V, N.flatFee, N.variableRate, b, R, H, [G], w, P), h(w, O, T, [z])]), Z = {
		recipient: b,
		mint: T[0] === "11111111111111111111111111111111" ? void 0 : T[0]
	}, Q = {
		proofAArr: K,
		proofBArr: q,
		proofCArr: J,
		publicInputsArr: Y,
		encryptedOutputs: [G],
		relayerFee: N.flatFee.toString(),
		dimensions: H
	}, $ = d(C, w, T, c, await i.getEthereumAddress());
	return _({
		chainId: w,
		relayAddress: R,
		functionName: "transact",
		args: Q,
		accounts: Z,
		commitmentValidationData: X,
		solanaAccessTokenData: !I && F ? {
			ethereumAddress: L,
			accessKey: O.getAccessKey()
		} : void 0,
		adminData: $
	});
};
//#endregion
export { b as hinkalSolanaWithdraw };
