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 { AdminTransactionType as i } from "../../types/admin.types.mjs";
import "../../types/index.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 { Utxo as l } from "../utxo/Utxo.mjs";
import { validateAndGetChainId as u } from "../../functions/utils/token-check.utils.mjs";
import { calcEncryptedOutputs as d } from "../../functions/snarkjs/common.snarkjs.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 { 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/hinkalSolanaClaimUtxo.ts
var v = async (v, y, b, x, S) => {
	let C = u([y]), w = y.erc20TokenAddress;
	if (b.getTokenAddress(C) !== w) throw Error("Off-chain UTXO token mismatch");
	if (!b.shieldedPrivateKey) throw Error("Claimable UTXO shieldedPrivateKey is missing");
	let { originalDeployer: T, hinkalIdl: E } = e[C].contractData;
	if (!T || !E) throw Error("missing data");
	let D = new s(void 0, b.shieldedPrivateKey), O = new l({
		amount: b.amount,
		erc20TokenAddress: b.erc20TokenAddress,
		mintAddress: b.mintAddress,
		timeStamp: b.timeStamp,
		tokenId: b.tokenId,
		randomization: b.randomization,
		shieldedPrivateKey: D.getShieldedPrivateKey()
	}), k = x ?? await p(C, w, [w], r.Transact, void 0, void 0, {
		mintTo: w,
		nullifierCount: 1
	});
	if (k.feeToken !== w) throw Error("Claim fee token mismatch: fee must be paid from claimed UTXO token");
	let A = k.variableRate > 0n ? k.variableRate : t;
	if (O.amount <= k.flatFee) throw Error(a.INSUFFICIENT_FUNDS_TO_TRANSACT);
	let j = O.amount - k.flatFee, M = j * A / 10000n, N = j - M;
	if (N <= 0n) throw Error(a.RECIPIENT_AMOUNT_INVALID);
	let P = k.flatFee + M, [F, I, L] = v.getRecipientInfo().split(",");
	if (!F || !I || !L) throw Error("Invalid recipient info");
	let R = c().toString(), z = new l({
		amount: 0n,
		erc20TokenAddress: O.erc20TokenAddress,
		mintAddress: O.mintAddress,
		tokenId: O.tokenId,
		shieldedPrivateKey: D.getShieldedPrivateKey()
	}), B = new l({
		amount: 0n,
		erc20TokenAddress: O.erc20TokenAddress,
		mintAddress: O.mintAddress,
		tokenId: O.tokenId,
		shieldedPrivateKey: D.getShieldedPrivateKey(),
		timeStamp: R
	}), V = new l({
		amount: N,
		erc20TokenAddress: O.erc20TokenAddress,
		mintAddress: O.mintAddress,
		tokenId: O.tokenId,
		timeStamp: R,
		randomization: BigInt(F),
		stealthAddress: I,
		encryptionKey: L
	}), H = [O, z], U = [B, V], W = await v.getRandomRelay(C, !0);
	if (!W) throw Error(a.RELAYER_NOT_AVAILABLE);
	let G = l.findCorrectRandomization(o(31), D.getShieldedPrivateKey()), K = {
		tokenNumber: 1,
		nullifierAmount: H.length,
		outputAmount: U.length
	}, q = d([U])[0].map((e) => Array.from(_.getBytes(e))), J = v.getSolanaProgram(E), [{ proofAArr: Y, proofBArr: X, proofCArr: Z, publicInputsArr: Q }, $] = await Promise.all([h(v.generateProofRemotely, T, v.merkleTreeHinkalByChain[C], v.merkleTreeAccessTokenByChain[C], D, J, [w], [H], [U], G, P, 0n, n, W, K, q, C), m(C, D, [w], [H])]), ee = {
		recipient: W,
		mint: w === "11111111111111111111111111111111" ? void 0 : w
	};
	return g({
		chainId: C,
		relayAddress: W,
		functionName: "transfer",
		args: {
			proofAArr: Y,
			proofBArr: X,
			proofCArr: Z,
			publicInputsArr: Q,
			encryptedOutputs: q,
			relayerFee: P.toString(),
			dimensions: K
		},
		accounts: ee,
		adminData: f(S ?? i.ClaimUtxo, C, [w], [-j], await v.getEthereumAddress()),
		commitmentValidationData: $
	});
};
//#endregion
export { v as hinkalSolanaClaimUtxo };
