import { isSolanaLike as e, networkRegistry as t } from "../../constants/chains.constants.mjs";
import { RELAYER_TX_CONFIRMATION_TIMEOUT as n } from "../../constants/tx-confirmation.constants.mjs";
import "../../constants/index.mjs";
import { UserKeys as r } from "../../data-structures/crypto-keys/keys.mjs";
import { waitLittle as i } from "./time.utils.mjs";
import "../../data-structures/crypto-keys/index.mjs";
import { ErrorWithIndex as a } from "../../error-handling/customErrors/ErrorWithIndex.mjs";
import { formatMintAddress as o } from "./solanaMint.utils.mjs";
import { Logger as s } from "../../error-handling/logger.mjs";
import { validateAndGetChainId as c } from "./token-check.utils.mjs";
import { getErrorMessage as l } from "../../error-handling/get-error.message.mjs";
import "../../error-handling/index.mjs";
import { fetchSolanaTransaction as u } from "../pre-transaction/sendV0Transaction.mjs";
import "../pre-transaction/index.mjs";
import { getRecipientInfoFromUserKeys as d } from "./getRecipientInfoFromUserKeys.mjs";
import { getUtxosFromReceipt as f } from "./getUtxosFromReceipt.utils.mjs";
import { getUtxosFromReceiptSolana as p } from "./getUtxosFromReceiptSolana.mjs";
import { waitForEthereumTransactionConfirmation as m, waitForTransactionConfirmation as h } from "./tx-confirmation.utils.mjs";
//#region libs/shared/common/src/functions/utils/transfer-claimable.utils.ts
var g = async (e, n, r, i, a, s, c) => {
	let { hinkalIdl: l } = t[e].contractData;
	if (!l) throw Error("No IDL configured for Solana program on this network");
	let d = r.getSolanaProgram(l);
	await d.provider.connection.confirmTransaction(n, "finalized");
	let f = await u(d.provider.connection, n, "finalized");
	if (!f) throw Error(`Could not fetch Solana transaction for signature ${n}`);
	let { compressedAddress: m } = o(c), h = p(f, d, i, m)[0];
	if (!h) throw Error("Could not find recipient UTXO in Solana tx for claimable transfer");
	return {
		chainId: e,
		senderAddress: s,
		recipientAddress: a.recipientAddress,
		utxo: h
	};
}, _ = async (e, t, r, i, a, o, s) => {
	let c = f(await m(e, t, { evmTimeoutMs: n }), r, e, s, i)[0];
	if (!c) throw Error("Could not find recipient UTXO in EVM txHash for claimable transfer");
	return {
		chainId: e,
		senderAddress: o,
		recipientAddress: a.recipientAddress,
		utxo: c
	};
}, v = (e, t, n, i, a) => {
	if (e.recipientInfo) return { recipientInfo: e.recipientInfo };
	let o = t - n, s = i + BigInt(o), c = a.userKeys.getClaimableSignatureFromNonce(s), l = new r(c), u = l.getShieldedPrivateKey();
	return {
		recipientInfo: d(l),
		recipientKeys: l,
		recipientShieldedPrivateKey: u,
		claimableSignature: c
	};
}, y = async (t, r, o, u, d, f, p, m) => {
	if (o.length === 0) return {
		pendingEnclaveUtxos: [],
		txHashes: [],
		failedRecipientErrorMessage: void 0
	};
	if (o.length !== u.length) throw Error("recipients and allocations length mismatch");
	let y = c([r]), b = await t.getEthereumAddressByChain(y), x = o.map((e, t) => ({
		recipient: e,
		recipientInfo: u[t]?.recipientInfo
	})), S = x.filter((e) => !!e.recipientInfo), C = x.filter((e) => !e.recipientInfo), w = [], T = [], E, D = [...S, ...C];
	for (let o = 0; o < D.length; o += 1) try {
		let a = D[o], s = !!a.recipientInfo, { recipientInfo: c, recipientKeys: l, recipientShieldedPrivateKey: u, claimableSignature: x } = v(a, o, S.length, d, t), C = await t.transfer([r], [-a.recipient.amount], c, f, p, m);
		if (T.push(C), s) await h(r.chainId, C, {
			solanaCommitment: "finalized",
			evmTimeoutMs: n
		});
		else {
			if (!l || !u) throw Error("Missing recipient keys for fallback claimable transfer");
			let n = e(y) ? await g(y, C, t, l, a.recipient, b, r.erc20TokenAddress) : await _(y, C, t, u, a.recipient, b, r.erc20TokenAddress);
			w.push({
				...n,
				claimableSignature: x
			});
		}
		o < D.length - 1 && (await i(2e3), await t.resetMerkleTreesIfNecessary([r.chainId]));
	} catch (e) {
		s.log("Error in transferClaimableUtxos loop", {
			error: e,
			index: o
		}), E = new a(o, l(e)), T.push("");
		break;
	}
	return {
		pendingEnclaveUtxos: w,
		txHashes: T,
		failedRecipientErrorMessage: E
	};
};
//#endregion
export { y as transferClaimableUtxos };
