import { ExternalActionId as e } from "../../types/external-action.types.mjs";
import { networkRegistry as t } from "../../constants/chains.constants.mjs";
import { HINKAL_PRIVATE_SEND_VARIABLE_RATE as n, solanaNativeAddress as r } from "../../constants/protocol.constants.mjs";
import "../../types/index.mjs";
import { isValidPrivateAddress as i } from "../../functions/utils/addresses.mjs";
import { transactionErrorCodes as a } from "../../error-handling/error-codes.constants.mjs";
import { randomBigInt as o } from "../../functions/web3/etherFunctions.mjs";
import { UserKeys as s } from "../crypto-keys/keys.mjs";
import { getCurrentTimeInSeconds as c } from "../../functions/utils/time.utils.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 { constructAdminData as p } from "../../functions/pre-transaction/constructAdminData.mjs";
import { ensureAmountChanges as m } from "../../functions/pre-transaction/ensureAmountChanges.mjs";
import { getFeeStructure as h } from "../../functions/pre-transaction/getFeeStructure.mjs";
import { buildCommitmentValidationData as g } from "../../functions/pre-transaction/buildCommitmentValidationData.mjs";
import "../../functions/pre-transaction/index.mjs";
import { calculateTotalFee as _ } from "../../functions/utils/fees.utils.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 o = (await u(e, t, n, r, 6))[0], s = c().toString(), { outputUtxos: l } = d(e.userKeys, o, r[0], s, !0, i, a);
	return {
		inputUtxos: o,
		senderUtxo: l[0],
		recipientUtxo: l[1]
	};
}, S = async (c, u, d, y, S, C, w) => {
	if (!i(y)) throw Error(a.RECIPIENT_FORMAT_INCORRECT);
	let T = l(u), E = [...d], { originalDeployer: D } = t[T].contractData;
	if (!D) throw Error("missing data");
	let { userKeys: O, merkleTreeHinkalByChain: k } = c, A = u.map((e) => e.erc20TokenAddress);
	if (A.length > 1) throw Error("Solana Transfer: Only one mint address is supported");
	let j = C ?? await h(T, S, A, e.Transact, [], 5n);
	j.variableRate ||= n;
	let M = _(-E[0], j), N = -E[0];
	E[0] -= M;
	let [P, F, { inputUtxos: I, senderUtxo: L, recipientUtxo: R }] = await Promise.all([
		c.getRandomRelay(T, !0),
		c.getEthereumAddressByChain(T),
		x(c, T, A, E, y, N)
	]);
	if (!P) throw Error(a.RELAYER_NOT_AVAILABLE);
	let z = s.findCorrectRandomization(o(31), c.userKeys.getShieldedPrivateKey()), B = {
		tokenNumber: A.length,
		nullifierAmount: I.length,
		outputAmount: 2
	}, V = f([[L, R]])[0].map((e) => Array.from(b.getBytes(e)));
	m([I], [[L]], E);
	let [{ proofAArr: H, proofBArr: U, proofCArr: W, publicInputsArr: G }, K] = await Promise.all([v(c.generateProofRemotely, k[T], O, [A[0]], [I], [[L, R]], z, M, 0n, r, P, B, V, T), g(T, O, A, [I])]), q = {
		recipient: P,
		mint: A[0] === "11111111111111111111111111111111" ? void 0 : A[0]
	};
	return {
		chainId: T,
		relay: P,
		args: {
			proofAArr: H,
			proofBArr: U,
			proofCArr: W,
			publicInputsArr: G,
			encryptedOutputs: V,
			relayerFee: M.toString(),
			dimensions: B
		},
		accounts: q,
		adminData: p(w, T, A, E, F),
		commitmentValidationData: K,
		recipientUtxo: R
	};
}, 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 { S as buildSolanaTransferPlan, C as hinkalSolanaTransfer };
