import { ExternalActionId as e } from "../../types/external-action.types.mjs";
import { networkRegistry as t } from "../../constants/chains.constants.mjs";
import { solanaNativeAddress as n } from "../../constants/protocol.constants.mjs";
import { StorageKeys as r } from "../../types/cache.types.mjs";
import "../../types/index.mjs";
import { transactionErrorCodes as i } from "../../error-handling/error-codes.constants.mjs";
import { getAmountInToken as a, getAmountInWei as o, randomBigInt as s } from "../../functions/web3/etherFunctions.mjs";
import { UserKeys as c } from "../crypto-keys/keys.mjs";
import { getCurrentTimeInSeconds as l } from "../../functions/utils/time.utils.mjs";
import { validateAndGetChainId as u } from "../../functions/utils/token-check.utils.mjs";
import { addPaddingToUtxos as d } from "../../functions/web3/events/getShieldedBalance.mjs";
import { outputUtxoProcessing as f } from "../../functions/pre-transaction/outputUtxoProcessing.mjs";
import { calcEncryptedOutputs as p } from "../../functions/snarkjs/common.snarkjs.mjs";
import { getStorageAccountPublicKey as m, getStorageVaultPublicKey as h, getSwapperAccountPublicKeyFromSalt as g } from "../../functions/pre-transaction/solana.mjs";
import { constructAdminData as _ } from "../../functions/pre-transaction/constructAdminData.mjs";
import { convertOKXToHinkalInstructions as v } from "../../functions/pre-transaction/convert-okx-instructions.mjs";
import { ensureAmountChanges as y } from "../../functions/pre-transaction/ensureAmountChanges.mjs";
import { getFeeStructure as ee } from "../../functions/pre-transaction/getFeeStructure.mjs";
import { buildCommitmentValidationData as b } from "../../functions/pre-transaction/buildCommitmentValidationData.mjs";
import { constructSolanaZkProof as x } from "../../functions/snarkjs/constructSolanaZkProof.mjs";
import { solanaTransactCallRelayer as S } from "../../functions/web3/functionCalls/transactCallRelayer.mjs";
import { ethers as C } from "ethers";
import { PublicKey as w } from "@solana/web3.js";
//#region libs/shared/common/src/data-structures/Hinkal/hinkalSolanaSwap.ts
var T = async (e, t, n, r) => {
	let i = [...await d(e, t, n, r, 6)], a = l().toString(), o = [];
	for (let t = 0; t < n.length; t += 1) {
		let { outputUtxos: n } = f(e.userKeys, i[t], r[t], a);
		o.push([n[0]]);
	}
	return {
		inputUtxosArray: i,
		outputUtxosArray: o
	};
}, E = async (l, d, f, E, te, ne, re, ie, D) => {
	let O = u(d), k = new w(n), { hinkalAddress: A, originalDeployer: j } = t[O].contractData;
	if (!j) throw Error("No IDL configured for Solana program on this network");
	let M = new w(j), N = new w(A), P = d.map((e) => e.erc20TokenAddress), { userKeys: F, merkleTreeHinkalByChain: I } = l, L = ie ?? await ee(O, re, P, e.Transact), R = f[1], z = Number(l.cacheDevice.get(r.SLIPPAGE_PERCENTAGE) ?? 0), B = d[1], V = a(B, R), H = Number(V) * z / 100;
	f[1] -= o(B, H.toFixed(B.decimals));
	let U = R * BigInt(L.variableRate ?? 0) / 10000n + L.flatFee;
	if (f[1] -= U, f[1] < 0) throw Error(i.LOW_OUTPUT_AMOUNT);
	let W = L.variableRate.toString(), [G, ae, { inputUtxosArray: K, outputUtxosArray: q }] = await Promise.all([
		l.getRandomRelay(O, !0),
		l.getEthereumAddressByChain(O),
		T(l, O, P, f)
	]);
	if (!G) throw Error(i.RELAYER_NOT_AVAILABLE);
	let J = l.userKeys.getShieldedPrivateKey(), oe = c.findCorrectRandomization(s(31), J), Y = {
		tokenNumber: 2,
		nullifierAmount: K[0].length,
		outputAmount: 1
	}, X = p(q).map((e) => [...C.getBytes(e[0])]), Z = g(N, M, E), { remainingAccounts: Q, hinkalInstructions: $ } = v(te, Z);
	y(K, q, f);
	let [{ proofAArr: se, proofBArr: ce, proofCArr: le, publicInputsArr: ue }, de] = await Promise.all([x(l.generateProofRemotely, I[O], F, P, K, q, oe, L.flatFee, L.variableRate, k.toString(), G, Y, X, O, $, Q, E), b(O, F, P, K)]), fe = m(N, M), pe = h(N, M), me = d[0].erc20TokenAddress === "11111111111111111111111111111111" ? null : new w(d[0].erc20TokenAddress), he = d[1].erc20TokenAddress === "11111111111111111111111111111111" ? null : new w(d[1].erc20TokenAddress), ge = {
		recipient: k.toString(),
		storageAccount: fe.toString(),
		storageVault: pe.toString(),
		swapperAccount: Z.toString(),
		mintFrom: me?.toString() ?? null,
		mintTo: he?.toString() ?? null,
		remainingAccounts: Q.map((e) => ({
			pubkey: e.pubkey.toString(),
			isSigner: e.isSigner,
			isWritable: e.isWritable
		})),
		addressLookupTableAccount: ne
	};
	return S({
		chainId: O,
		relayAddress: G,
		functionName: "swap",
		args: {
			encryptedOutputs: X,
			proofAArr: se,
			proofBArr: ce,
			proofCArr: le,
			publicInputsArr: ue,
			relayerFee: L.flatFee.toString(),
			variableRate: W,
			dimensions: Y,
			hinkalInstructions: $.map((e) => ({
				accountIndexes: [...e.accountIndexes],
				data: [...e.data],
				programIndex: e.programIndex
			}))
		},
		accounts: ge,
		commitmentValidationData: de,
		adminData: _(D, O, P, f, ae, d)
	});
};
//#endregion
export { E as hinkalSolanaSwap };
