import { networkRegistry as e } from "../../constants/chains.constants.mjs";
import "../../constants/protocol.constants.mjs";
import { buildAnchorStealthAddressStructure as t, getMerkleAccountPublicKey as n, getStorageAccountPublicKey as r, getStorageVaultPublicKey as i, getTokenLimitStorageAccountPublicKey as a } from "../../functions/pre-transaction/solana.mjs";
import { hinkalCheckSolanaTokenRegistry as o } from "./hinkalCheckSolanaTokenRegistry.mjs";
import { AdminTransactionType as s } from "../../types/admin.types.mjs";
import { transactionErrorCodes as c } from "../../error-handling/error-codes.constants.mjs";
import { randomBigInt as l } from "../../functions/web3/etherFunctions.mjs";
import { Utxo as u } from "../utxo/Utxo.mjs";
import { formatMintAddress as d } from "../../functions/utils/solanaMint.utils.mjs";
import { constructStealthAddressStructure as f, isValidPrivateAddress as p } from "../../functions/utils/addresses.mjs";
import { API as m } from "../../API/API.mjs";
import { validateAndGetChainId as h } from "../../functions/utils/token-check.utils.mjs";
import { calcStealthAddressStructure as g } from "../../functions/snarkjs/common.snarkjs.mjs";
import { constructAdminData as _ } from "../../functions/pre-transaction/constructAdminData.mjs";
import { shouldRetryForSolanaError as v } from "../../error-handling/retrySolanaError.mjs";
import { emitTxPublicData as y } from "../../API/admin-calls.mjs";
import "../../API/index.mjs";
import { signAndSendSerializedTransaction as b } from "../../functions/web3/signAndSendSerializedTransaction.mjs";
import { BN as x } from "@coral-xyz/anchor";
import { PublicKey as S } from "@solana/web3.js";
//#region libs/shared/common/src/data-structures/Hinkal/hinkalSolanaDeposit.ts
var C = async (s, c, l, u, f) => {
	if (l.length === 0) throw Error("amounts must not be empty");
	if (l.length !== u.length) throw Error("amounts and stealthAddressStructures length mismatch");
	if (l.some((e) => e <= 0n)) throw Error("all amounts must be positive");
	let p = h([c]), g = c.erc20TokenAddress, { hinkalIdl: C, hinkalAddress: w, originalDeployer: T } = e[p].contractData;
	if (!C) throw Error("No IDL configured for Solana program on this network");
	if (!T) throw Error("missing original deployer");
	let E = _(f, p, Array(l.length).fill(g), l, await s.getEthereumAddress()), D = s.getSolanaProgram(C), O = s.getSolanaPublicKey(), k = new S(T), A = new S(w), { mintPublicKey: j } = d(g), M = j.toString(), N = r(A, k), P = i(A, k), F = n(A, k), I = u.map((e) => t(e)), L = l.reduce((e, t) => e + t, 0n), R = await o(D, new S(k), [g], [L]), z = l.map((e) => e.toString()), B = a(D.programId, k, j), V = M === "11111111111111111111111111111111" ? null : j, H = M === "11111111111111111111111111111111" ? null : void 0;
	for (let e = 0; e < 10; e += 1) {
		let t = e === 9;
		try {
			if (R) {
				let { serializedTransaction: e, blockhash: t, lastValidBlockHeight: n } = await m.generateMultiPaymentDepositLimitless({
					amounts: z,
					payer: O.toBase58(),
					ethereumAddress: await s.getEthereumAddressByChain(p),
					mint: g,
					stealthAddressStructures: I,
					chainId: p,
					createBlockedUtxos: !1
				}), r = await b(D.provider, e, t, n);
				return await y(E), r;
			}
			let e = await D.methods.multiPaymentDeposit(z.map((e) => new x(e)), I, !1).accounts({
				mint: V,
				signer: O,
				signerAta: H,
				originalDeployer: k,
				storageAccount: N,
				storageVault: P,
				merkleAccount: F,
				tokenLimitStorage: B,
				storageVaultAta: H,
				accessTokenOwner: null
			}).rpc();
			return await y(E), e;
		} catch (e) {
			if (v(e) && !t) continue;
			throw e;
		}
	}
	return "";
}, w = async (e, t, n) => {
	let r = e.userKeys.getShieldedPrivateKey(), i = g(u.findCorrectRandomization(l(31), r), r);
	return C(e, n, [t], [i], s.ProoflessDeposit);
}, T = async (e, t, n, r) => {
	if (!p(r)) throw Error(c.RECIPIENT_FORMAT_INCORRECT);
	let i = f(r);
	return C(e, n, [t], [i], s.PaymentLink);
};
//#endregion
export { w as hinkalSolanaDeposit, T as hinkalSolanaDepositForOther, C as submitSolanaProoflessDeposit };
