import { ExternalActionId as e } from "../../types/external-action.types.mjs";
import { isTronLike as t } from "../../constants/chains.constants.mjs";
import { HINKAL_PRIVATE_SEND_VARIABLE_RATE as n } from "../../constants/protocol.constants.mjs";
import { caseInsensitiveEqual as r } from "../../functions/utils/caseInsensitive.utils.mjs";
import { transactionErrorCodes as i } from "../../error-handling/error-codes.constants.mjs";
import { UserKeys as a } from "../crypto-keys/keys.mjs";
import { getCurrentTimeInSeconds as o } from "../../functions/utils/time.utils.mjs";
import { Utxo as s } from "../utxo/Utxo.mjs";
import { addressToHexFormat as c, reorderZkCallData as l } from "../../functions/utils/tron.utils.mjs";
import { validateAndGetChainId as u } from "../../functions/utils/token-check.utils.mjs";
import { outputUtxoProcessing as d } from "../../functions/pre-transaction/outputUtxoProcessing.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 { constructZkProof as m } from "../../functions/snarkjs/constructGeneralZkProof.mjs";
import { transactCallRelayer as h } from "../../functions/web3/functionCalls/transactCallRelayer.mjs";
//#region libs/shared/common/src/data-structures/Hinkal/hinkalClaimUtxo.ts
var g = async (g, _, v, y, b) => {
	let x = u([_]), S = t(x), C = _.erc20TokenAddress, w = c(await g.getEthereumAddressByChain(x));
	if (!r(C, v.erc20TokenAddress)) throw Error("Off-chain UTXO token mismatch");
	let T = b ? new a(b) : new a(v.nullifyingKey, v.nullifyingKey);
	if (!v.nullifyingKey && !b) throw Error("Claimable UTXO nullifyingKey is missing");
	let E = T.getShieldedPrivateKey();
	if (v.nullifyingKey && v.nullifyingKey !== E) throw Error("Claimable UTXO key mismatch");
	let D = g.getRecipientInfo(), O = s.createFrom(v, { nullifyingKey: E }), k = new s({
		amount: 0n,
		erc20TokenAddress: O.erc20TokenAddress,
		mintAddress: O.mintAddress,
		nullifyingKey: E,
		...O.isNewStyle ? {
			spendingPublicKey: T.getSpendingKeyPair().pubSpendingBJJPoint,
			isNewStyle: !0
		} : { isNewStyle: !1 }
	}), A = [C], j = y ?? await f(x, C, [C], e.Transact);
	if (!r(j.feeToken, C)) throw Error("Claim fee token mismatch: fee must be paid from claimed UTXO token");
	let M = j.variableRate > 0n ? j.variableRate : n;
	if (O.amount <= j.flatFee) throw Error(i.INSUFFICIENT_FUNDS_TO_TRANSACT);
	let N = O.amount - j.flatFee, P = N * M / 10000n, F = N - P;
	if (F <= 0n) throw Error(i.RECIPIENT_AMOUNT_INVALID);
	let I = {
		feeToken: j.feeToken,
		flatFee: j.flatFee + P,
		variableRate: 0n
	}, L = [-O.amount], R = await g.getRandomRelay(x, !0);
	if (!R) throw Error(i.RELAYER_NOT_AVAILABLE);
	let z = {
		externalActionId: 0n,
		externalAddress: R ?? w,
		externalActionMetadata: "0x00"
	}, B = [[O, k]], V = [], H = o().toString(), { outputUtxos: U } = d(T, B[0], L[0], H, !0, D, F);
	V.push(U);
	let [{ zkCallData: W, circomData: G, dimData: K }, q] = await Promise.all([m(g.merkleTreeHinkalByChain[x], B, V, T, z.externalActionId, z.externalAddress, z.externalActionMetadata, g.generateProofRemotely, I, R, x), p(x, T, A, B)]);
	return h(x, W, K, G, q, void 0, void 0, void 0, await l(S, W, K, G));
};
//#endregion
export { g as hinkalClaimUtxo };
