import { networkRegistry as e } from "../../constants/chains.constants.mjs";
import { solanaNativeAddress as t } from "../../constants/protocol.constants.mjs";
import { constructStealthAddressStructure as n, isValidPrivateAddress as r } from "../../functions/utils/addresses.mjs";
import { transactionErrorCodes as i } from "../../error-handling/error-codes.constants.mjs";
import { randomBigInt as a } from "../../functions/web3/etherFunctions.mjs";
import { emitTxPublicData as o } from "../../API/admin-calls.mjs";
import { UserKeys as s } from "../crypto-keys/keys.mjs";
import { formatMintAddress as c } from "../../functions/utils/solanaMint.utils.mjs";
import { validateAndGetChainId as l } from "../../functions/utils/token-check.utils.mjs";
import { calcStealthAddressStructure as u } from "../../functions/snarkjs/common.snarkjs.mjs";
import { buildAnchorStealthAddressStructure as d, getMerkleAccountPublicKey as f, getStorageAccountPublicKey as p, getStorageVaultPublicKey as m } from "../../functions/pre-transaction/solana.mjs";
import { constructAdminData as h } from "../../functions/pre-transaction/constructAdminData.mjs";
import { shouldRetryForSolanaError as g } from "../../error-handling/retrySolanaError.mjs";
import { buildSolanaTransferInstructions as _ } from "../../functions/pre-transaction/solanaTransfer.utils.mjs";
import { assertNoDuplicateStealthAddressStructures as v } from "./hinkalProoflessDeposit.utils.mjs";
import { PublicKey as y, Transaction as b, TransactionMessage as x, VersionedTransaction as S } from "@solana/web3.js";
import { BN as C } from "@coral-xyz/anchor";
//#region libs/shared/common/src/data-structures/Hinkal/hinkalSolanaDeposit.ts
var w = (n, r) => {
	let i = l([r]), a = r.erc20TokenAddress, { hinkalIdl: o, hinkalAddress: s, originalDeployer: u } = e[i].contractData;
	if (!o) throw Error("No IDL configured for Solana program on this network");
	if (!u) throw Error("missing original deployer");
	let d = n.getSolanaProgram(o), h = n.getSolanaPublicKey(), { connection: g, wallet: _ } = d.provider;
	if (!_) throw Error("missing solana wallet");
	let v = new y(u), b = new y(s), { mintPublicKey: x } = c(a), S = x.toString() === t;
	return {
		chainId: i,
		mintAddress: a,
		program: d,
		publicKey: h,
		connection: g,
		wallet: _,
		originalDeployer: v,
		mintPublicKey: x,
		mint: S ? null : x,
		atasValue: S ? null : void 0,
		storageAccount: p(b, v),
		storageVault: m(b, v),
		merkleAccount: f(b, v)
	};
}, T = (e, t, n) => {
	let r = n.map((e) => d(e));
	return e.program.methods.multiPaymentDeposit(t.map((e) => new C(e.toString())), r, !1).accounts({
		mint: e.mint,
		signer: e.publicKey,
		signerAta: e.atasValue,
		originalDeployer: e.originalDeployer,
		storageAccount: e.storageAccount,
		storageVault: e.storageVault,
		merkleAccount: e.merkleAccount,
		storageVaultAta: e.atasValue
	}).instruction();
}, E = async (e, t, n, r) => {
	for (let i = 0; i < 10; i += 1) {
		let a = i === 9;
		try {
			let { blockhash: i, lastValidBlockHeight: a } = await e.getLatestBlockhash(), o = new S(new x({
				payerKey: n,
				recentBlockhash: i,
				instructions: r
			}).compileToV0Message()), s = await t.signTransaction(o), c = await e.sendTransaction(s);
			return await e.confirmTransaction({
				signature: c,
				blockhash: i,
				lastValidBlockHeight: a
			}, "confirmed"), c;
		} catch (e) {
			if (g(e) && !a) continue;
			throw e;
		}
	}
	return "";
}, D = (e, t) => {
	if (e.length === 0) throw Error("amounts must not be empty");
	if (e.length !== t.length) throw Error("amounts and stealthAddressStructures length mismatch");
	if (e.some((e) => e <= 0n)) throw Error("all amounts must be positive");
	v(t);
}, O = async (e, t, n, r, i, a = !1) => {
	D(n, r);
	let s = w(e, t), c = h(i, s.chainId, Array(n.length).fill(s.mintAddress), n, await e.getEthereumAddress()), l = await T(s, n, r);
	if (a) {
		let { blockhash: e } = await s.connection.getLatestBlockhash(), t = new b().add(l);
		return t.recentBlockhash = e, t.feePayer = s.publicKey, Buffer.from(t.serialize({ requireAllSignatures: !1 })).toString("base64");
	}
	let u = await E(s.connection, s.wallet, s.publicKey, [l]);
	return await o(c), u;
}, k = async (e, t, n, r, i, a) => {
	D(n, r);
	let s = w(e, t), c = await e.getRandomRelay(s.chainId, !1);
	if (!c) throw Error(`No relay available for chainId ${s.chainId}`);
	let { instructions: l } = await _(s.connection, s.publicKey, new y(c), t, i), u = await T(s, n, r), d = h(a, s.chainId, Array(n.length).fill(s.mintAddress), n, await e.getEthereumAddress()), f = await E(s.connection, s.wallet, s.publicKey, [...l, u]);
	return await o(d), f;
}, A = async (e, t, n, r = !1) => {
	let i = e.userKeys.getShieldedPrivateKey(), o = u(s.findCorrectRandomization(a(31), i), i, e.userKeys.getSpendingKeyPair().pubSpendingBJJPoint);
	return O(e, n, [t], [o], void 0, r);
}, j = async (e, t, a, o, s = !1, c) => {
	if (!r(o)) throw Error(i.RECIPIENT_FORMAT_INCORRECT);
	let l = n(o);
	return O(e, a, [t], [l], c, s);
};
//#endregion
export { A as hinkalSolanaDeposit, j as hinkalSolanaDepositForOther, k as hinkalSolanaProoflessDepositWithPublicFee, O as submitSolanaProoflessDeposit };
