import { networkRegistry as e } from "../../constants/chains.constants.mjs";
import { HINKAL_PRIVATE_SEND_VARIABLE_RATE as t, solanaNativeAddress as n } from "../../constants/protocol.constants.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 { randomBigInt as a } from "../../functions/web3/etherFunctions.mjs";
import { getCurrentTimeInSeconds as o } from "../../functions/utils/time.utils.mjs";
import { Utxo as s } from "../utxo/Utxo.mjs";
import { isValidPrivateAddress as c } from "../../functions/utils/addresses.mjs";
import { validateAndGetChainId as l } from "../../functions/utils/token-check.utils.mjs";
import { addPaddingToUtxos as u } from "../../functions/web3/events/getShieldedBalance.mjs";
import { outputUtxoProcessing as d } from "../../functions/pre-transaction/outputUtxoProcessing.mjs";
import { calcEncryptedOutputs as f } from "../../functions/snarkjs/common.snarkjs.mjs";
import { calculateTotalFee as p } from "../../functions/utils/fees.utils.mjs";
import { constructAdminData as m } from "../../functions/pre-transaction/constructAdminData.mjs";
import { ensureAmountChanges as h } from "../../functions/pre-transaction/ensureAmountChanges.mjs";
import { getFeeStructure as g } from "../../functions/pre-transaction/getFeeStructure.mjs";
import { buildCommitmentValidationData as _ } from "../../functions/pre-transaction/buildCommitmentValidationData.mjs";
import "../../functions/pre-transaction/index.mjs";
import { constructSolanaZkProof as v } from "../../functions/snarkjs/constructSolanaZkProof.mjs";
import { solanaTransactCallRelayer as y } from "../../functions/web3/functionCalls/transactCallRelayer.mjs";
import { ethers as b } from "ethers";
//#region libs/shared/common/src/data-structures/Hinkal/hinkalSolanaTransfer.ts
var x = async (e, t, n, r, i, a) => {
	let c = (await u(e, t, n, r, 6))[0], l = o().toString(), { outputUtxos: f } = d(e.userKeys, c, r[0], l), [p, m, h] = i.split(",");
	return {
		inputUtxos: c,
		senderOutputUtxos: f,
		recipientUtxo: new s({
			amount: a,
			erc20TokenAddress: c[0].erc20TokenAddress,
			mintAddress: c[0].mintAddress,
			timeStamp: l,
			randomization: BigInt(p),
			stealthAddress: m,
			encryptionKey: h
		})
	};
}, S = async (o, u, d, y, S, C, w) => {
	if (!c(y)) throw Error(i.RECIPIENT_FORMAT_INCORRECT);
	let T = l(u), E = [...d], { originalDeployer: D, hinkalIdl: O } = e[T].contractData;
	if (!D || !O) throw Error("missing data");
	let { userKeys: k, merkleTreeAccessTokenByChain: A, merkleTreeHinkalByChain: j } = o, M = u.map((e) => e.erc20TokenAddress);
	if (M.length > 1) throw Error("Solana Transfer: Only one mint address is supported");
	let N = C ?? await g(T, S, M, r.Transact, [], 5n);
	N.variableRate ||= t;
	let P = p(-E[0], N), F = -E[0];
	E[0] -= P;
	let [I, L, { inputUtxos: R, senderOutputUtxos: z, recipientUtxo: B }] = await Promise.all([
		o.getRandomRelay(T, !0),
		o.getEthereumAddressByChain(T),
		x(o, T, M, E, y, F)
	]);
	if (!I) throw Error(i.RELAYER_NOT_AVAILABLE);
	let V = z[0], H = s.findCorrectRandomization(a(31), o.userKeys.getShieldedPrivateKey()), U = {
		tokenNumber: M.length,
		nullifierAmount: R.length,
		outputAmount: 2
	}, W = o.getSolanaProgram(O), G = f([[V, B]])[0].map((e) => Array.from(b.getBytes(e)));
	h([R], [z], E);
	let [{ proofAArr: K, proofBArr: q, proofCArr: J, publicInputsArr: Y }, X] = await Promise.all([v(o.generateProofRemotely, D, j[T], A[T], k, W, [M[0]], [R], [[V, B]], H, P, 0n, n, I, U, G, T), _(T, k, M, [R])]), Z = {
		recipient: I,
		mint: M[0] === "11111111111111111111111111111111" ? void 0 : M[0]
	};
	return {
		chainId: T,
		relay: I,
		args: {
			proofAArr: K,
			proofBArr: q,
			proofCArr: J,
			publicInputsArr: Y,
			encryptedOutputs: G,
			relayerFee: P.toString(),
			dimensions: U
		},
		accounts: Z,
		adminData: m(w, T, M, E, L),
		commitmentValidationData: X,
		recipientUtxo: B
	};
}, C = async (e, t, n, r, i, a, o, s = !1) => {
	if (s) throw Error("onlyGasEstimate is not supported for Solana transfer");
	let c = await S(e, t, n, r, i, a, o);
	return y({
		chainId: c.chainId,
		relayAddress: c.relay,
		functionName: "transfer",
		args: c.args,
		accounts: c.accounts,
		adminData: c.adminData,
		commitmentValidationData: c.commitmentValidationData
	});
};
//#endregion
export { C as hinkalSolanaTransfer };
