import { ExternalActionId as e } from "../../types/external-action.types.mjs";
import { networkRegistry as t } from "../../constants/chains.constants.mjs";
import "../../constants/protocol.constants.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 "../../constants/index.mjs";
import { UserKeys as i } from "../crypto-keys/keys.mjs";
import { getCurrentTimeInSeconds as a } from "../../functions/utils/time.utils.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 { constructAdminData as u } from "../../functions/pre-transaction/constructAdminData.mjs";
import { ensureAmountChanges as d } from "../../functions/pre-transaction/ensureAmountChanges.mjs";
import { getFeeStructure as f } from "../../functions/pre-transaction/getFeeStructure.mjs";
import { buildCommitmentValidationData as p } from "../../functions/pre-transaction/buildCommitmentValidationData.mjs";
import "../../functions/pre-transaction/index.mjs";
import { calculateModifiedFeeStructure as m } from "../../functions/utils/fees.utils.mjs";
import { constructSolanaZkProof as h } from "../../functions/snarkjs/constructSolanaZkProof.mjs";
import { solanaTransactCallRelayer as g } from "../../functions/web3/functionCalls/transactCallRelayer.mjs";
import { ethers as _ } from "ethers";
//#region libs/shared/common/src/data-structures/Hinkal/hinkalSolanaWithdraw.ts
var v = async (e, t, n, r) => {
	let i = [...await s(e, t, n, r, 6)][0], o = a().toString(), { outputUtxos: l } = c(e.userKeys, i, r[0], o);
	return {
		inputUtxos: i,
		outputUtxos: l
	};
}, y = async (a, s, c, y, b, x, S) => {
	let C = o(s), w = s.map((e) => e.erc20TokenAddress), T = s[0];
	if (!T) throw Error("Solana Withdraw: No Token Found");
	let { originalDeployer: E } = t[C].contractData;
	if (!E) throw Error("missing data");
	let { userKeys: D, merkleTreeHinkalByChain: O } = a, k = x ?? await f(C, b, w, e.Transact), A = await m(C, T, -c[0], k);
	c[0] -= A.flatFee;
	let [j, { inputUtxos: M, outputUtxos: N }] = await Promise.all([a.getRandomRelay(C, !0), v(a, C, w, c)]);
	if (!j) throw Error(n.RELAYER_NOT_AVAILABLE);
	if (w.length > 1) throw Error("Solana Withdraw: Only one mint address is supported");
	let P = i.findCorrectRandomization(r(31), a.userKeys.getShieldedPrivateKey()), F = {
		tokenNumber: w.length,
		nullifierAmount: M.length,
		outputAmount: N.length
	}, I = l([N])[0][0], L = Array.from(_.getBytes(I));
	d([M], [N], c);
	let [{ proofAArr: R, proofBArr: z, proofCArr: B, publicInputsArr: V }, H] = await Promise.all([h(a.generateProofRemotely, O[C], D, [w[0]], [M], [N], P, A.flatFee, A.variableRate, y, j, F, [L], C), p(C, D, w, [M])]), U = {
		recipient: y,
		mint: w[0] === "11111111111111111111111111111111" ? void 0 : w[0]
	};
	return g({
		chainId: C,
		relayAddress: j,
		functionName: "transact",
		args: {
			proofAArr: R,
			proofBArr: z,
			proofCArr: B,
			publicInputsArr: V,
			encryptedOutputs: [L],
			relayerFee: A.flatFee.toString(),
			dimensions: F
		},
		accounts: U,
		commitmentValidationData: H,
		adminData: u(S, C, w, c, await a.getEthereumAddress())
	});
};
//#endregion
export { y as hinkalSolanaWithdraw };
