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 { transactionErrorCodes as i } from "../../error-handling/error-codes.constants.mjs";
import { randomBigInt as a } from "../../functions/web3/etherFunctions.mjs";
import { UserKeys as o } from "../crypto-keys/keys.mjs";
import { getCurrentTimeInSeconds as s } from "../../functions/utils/time.utils.mjs";
import { Utxo as c } from "../utxo/Utxo.mjs";
import { validateAndGetChainId as l } from "../../functions/utils/token-check.utils.mjs";
import { calcEncryptedOutputs as u } from "../../functions/snarkjs/common.snarkjs.mjs";
import { getFeeStructure as d } from "../../functions/pre-transaction/getFeeStructure.mjs";
import { buildCommitmentValidationData as f } from "../../functions/pre-transaction/buildCommitmentValidationData.mjs";
import "../../functions/pre-transaction/index.mjs";
import { constructSolanaZkProof as p } from "../../functions/snarkjs/constructSolanaZkProof.mjs";
import { solanaTransactCallRelayer as m } from "../../functions/web3/functionCalls/transactCallRelayer.mjs";
import { ethers as h } from "ethers";
//#region libs/shared/common/src/data-structures/Hinkal/hinkalSolanaClaimUtxo.ts
var g = async (g, _, v, y, b) => {
	let x = l([_]), S = _.erc20TokenAddress;
	if (v.getTokenAddress(x) !== S) throw Error("Off-chain UTXO token mismatch");
	let C = b ? new o(b) : new o(v.nullifyingKey, v.nullifyingKey);
	if (!v.nullifyingKey && !b) throw Error("Claimable UTXO nullifyingKey is missing");
	let w = C.getShieldedPrivateKey();
	if (v.nullifyingKey && v.nullifyingKey !== w) throw Error("Claimable UTXO key mismatch");
	let { originalDeployer: T } = t[x].contractData;
	if (!T) throw Error("missing data");
	let E = c.createFrom(v, { nullifyingKey: w }), D = y ?? await d(x, S, [S], e.Transact, void 0, void 0, {
		mintTo: S,
		nullifierCount: 1
	});
	if (D.feeToken !== S) throw Error("Claim fee token mismatch: fee must be paid from claimed UTXO token");
	let O = D.variableRate > 0n ? D.variableRate : n;
	if (E.amount <= D.flatFee) throw Error(i.INSUFFICIENT_FUNDS_TO_TRANSACT);
	let k = E.amount - D.flatFee, A = k * O / 10000n, j = k - A;
	if (j <= 0n) throw Error(i.RECIPIENT_AMOUNT_INVALID);
	let M = D.flatFee + A, [N, P, F, , I] = g.getRecipientInfo().split(",");
	if (!P || !F || !N || !I) throw Error("Invalid recipient info");
	let L = s().toString(), R = new c({
		amount: 0n,
		erc20TokenAddress: E.erc20TokenAddress,
		mintAddress: E.mintAddress,
		nullifyingKey: C.getShieldedPrivateKey(),
		...E.isNewStyle ? {
			spendingPublicKey: C.getSpendingKeyPair().pubSpendingBJJPoint,
			isNewStyle: !0
		} : { isNewStyle: !1 }
	}), z = new c({
		amount: 0n,
		erc20TokenAddress: E.erc20TokenAddress,
		mintAddress: E.mintAddress,
		nullifyingKey: C.getShieldedPrivateKey(),
		timeStamp: L,
		spendingPublicKey: C.getSpendingKeyPair().pubSpendingBJJPoint,
		isNewStyle: !0
	}), B = new c({
		amount: j,
		erc20TokenAddress: E.erc20TokenAddress,
		mintAddress: E.mintAddress,
		timeStamp: L,
		H0: [BigInt(P), BigInt(F)],
		stealthAddress: N,
		encryptionKey: I,
		isNewStyle: !0
	}), V = [E, R], H = [z, B], U = await g.getRandomRelay(x, !0);
	if (!U) throw Error(i.RELAYER_NOT_AVAILABLE);
	let W = o.findCorrectRandomization(a(31), C.getShieldedPrivateKey()), G = {
		tokenNumber: 1,
		nullifierAmount: V.length,
		outputAmount: H.length
	}, K = u([H])[0].map((e) => Array.from(h.getBytes(e))), [{ proofAArr: q, proofBArr: J, proofCArr: Y, publicInputsArr: X }, Z] = await Promise.all([p(g.generateProofRemotely, g.merkleTreeHinkalByChain[x], C, [S], [V], [H], W, M, 0n, r, U, G, K, x), f(x, C, [S], [V])]), Q = {
		recipient: U,
		mint: S === "11111111111111111111111111111111" ? void 0 : S
	};
	return m({
		chainId: x,
		relayAddress: U,
		functionName: "transfer",
		args: {
			proofAArr: q,
			proofBArr: J,
			proofCArr: Y,
			publicInputsArr: X,
			encryptedOutputs: K,
			relayerFee: M.toString(),
			dimensions: G
		},
		accounts: Q,
		commitmentValidationData: Z
	});
};
//#endregion
export { g as hinkalSolanaClaimUtxo };
