import { isSolanaLike as e, networkRegistry as t } from "../../constants/chains.constants.mjs";
import { PAY_SEND_VARIABLE_RATE as n } from "../../constants/protocol.constants.mjs";
import { transactionErrorCodes as r } from "../../error-handling/error-codes.constants.mjs";
import "../../constants/index.mjs";
import { Utxo as i } from "../../data-structures/utxo/Utxo.mjs";
import { formatMintAddress as a } from "./solanaMint.utils.mjs";
import { validateAndGetChainId as o } from "./token-check.utils.mjs";
import "../../error-handling/index.mjs";
import { fetchSolanaTransaction as s } from "../pre-transaction/sendV0Transaction.mjs";
import "../pre-transaction/index.mjs";
import { getOnChainUtxosFromReceipt as c } from "./getUtxosFromReceipt.utils.mjs";
import { getOnChainUtxosFromReceiptSolana as l } from "./getUtxosFromReceiptSolana.mjs";
import { waitForEthereumTransactionConfirmation as u } from "./tx-confirmation.utils.mjs";
import { buildClaimableDepositPlan as d } from "./buildClaimableDepositPlan.mjs";
//#region libs/shared/common/src/functions/utils/deposit-claimable.utils.ts
var f = 10000n, p = (e) => {
	if (e.isNewStyle && e.H0) return e.H0[0];
	if (e.randomization !== void 0) return e.randomization;
}, m = (e, t) => {
	if (e.amount !== t.amount) return !1;
	let n = p(t), r = p(e);
	return n !== void 0 && r !== void 0 && n === r;
}, h = async ({ chainId: e, fallbackRecipients: n, hinkal: r, senderAddress: o, tokenAddress: c, txHash: u }) => {
	let { hinkalIdl: d } = t[e].contractData;
	if (!d) throw Error("No IDL configured for Solana program on this network");
	let f = r.getSolanaProgram(d);
	await f.provider.connection.confirmTransaction(u, "finalized");
	let p = await s(f.provider.connection, u, "finalized");
	if (!p) throw Error(`Could not fetch Solana transaction for signature ${u}`);
	let { compressedAddress: h } = a(c), g = [...l(p, f, r.userKeys, h)], _ = [];
	return n.forEach(({ recipientIndex: t, recipientAddress: n, shieldedPrivateKey: r, claimableSignature: a, utxo: s }) => {
		let c = g.find((e) => m(e, s));
		if (!c) throw Error(`Could not match deposited UTXO for recipient index ${t}.`);
		g.splice(g.indexOf(c), 1);
		let l = i.createFrom(c, {
			nullifyingKey: r,
			spendingPublicKey: s.spendingPublicKey,
			isNewStyle: s.isNewStyle
		});
		l.getStealthAddress(), _.push({
			chainId: e,
			senderAddress: o,
			recipientAddress: n,
			claimableSignature: a,
			utxo: l
		});
	}), _;
}, g = async ({ chainId: e, fallbackRecipients: t, hinkal: n, senderAddress: r, tokenAddress: a, txHash: o }) => {
	let s = await u(e, o);
	if (!s) throw Error(`Could not fetch receipt for tx ${o}`);
	let l = [...c(s, n, e, a)], d = [];
	return t.forEach(({ recipientIndex: t, recipientAddress: n, shieldedPrivateKey: a, claimableSignature: o, utxo: s }) => {
		let c = l.find((e) => m(e, s));
		if (!c) throw Error(`Could not match deposited UTXO for recipient index ${t}.`);
		l.splice(l.indexOf(c), 1), d.push({
			chainId: e,
			senderAddress: r,
			recipientAddress: n,
			claimableSignature: o,
			utxo: i.createFrom(c, {
				nullifyingKey: a,
				spendingPublicKey: s.spendingPublicKey,
				isNewStyle: s.isNewStyle
			})
		});
	}), d;
}, _ = async (t, i, a, s, c, l, u) => {
	if (a.length === 0) throw Error("recipientAmounts must not be empty");
	if (s.length !== a.length) throw Error("recipientAddresses must have the same length as recipientAmounts");
	if (l && l.length !== a.length) throw Error("recipientInfos must have the same length as recipientAmounts");
	if (c < 0n) throw Error("nonce must be non-negative");
	let p = o([i]), m = await t.getEthereumAddressByChain(p), _ = i.erc20TokenAddress, v = a.reduce((e, t) => {
		if (t <= 0n) throw Error(r.RECIPIENT_AMOUNT_INVALID);
		return e + t;
	}, 0n) * n / f, { stealthAddressStructures: y, fallbackRecipients: b } = d({
		userKeys: t.userKeys,
		recipientAmounts: a,
		recipientAddresses: s,
		recipientInfos: l,
		nonce: c,
		tokenAddress: _
	}), x;
	return x = v > 0n ? await t.prooflessDepositWithPublicFee(i, a, y, v, u) : await t.prooflessDeposit(a.map(() => i), a, y, u, !0), b.length === 0 ? {
		pendingEnclaveUtxos: [],
		txHash: x
	} : {
		pendingEnclaveUtxos: e(p) ? await h({
			chainId: p,
			fallbackRecipients: b,
			hinkal: t,
			senderAddress: m,
			tokenAddress: _,
			txHash: x
		}) : await g({
			chainId: p,
			fallbackRecipients: b,
			hinkal: t,
			senderAddress: m,
			tokenAddress: _,
			txHash: x
		}),
		txHash: x
	};
};
//#endregion
export { _ as depositClaimableUtxos };
