import { ExternalActionId as e } from "../../types/external-action.types.mjs";
import { networkRegistry as t } from "../../constants/chains.constants.mjs";
import { transactionErrorCodes as n } from "../../error-handling/error-codes.constants.mjs";
import "../../constants/index.mjs";
import { emitTxPublicData as r } from "../../API/admin-calls.mjs";
import { validateAndGetChainId as i } from "../../functions/utils/token-check.utils.mjs";
import { createCustomSolanaConnection as a } from "../../functions/utils/create-provider.mjs";
import { constructAdminData as o } from "../../functions/pre-transaction/constructAdminData.mjs";
import { AccountActions as s } from "../AccountActions/AccountActions.mjs";
import { sendProxyV0Transaction as c, serializeProxyV0Transaction as l } from "../../functions/pre-transaction/sendV0Transaction.mjs";
import { getFeeStructure as u } from "../../functions/pre-transaction/getFeeStructure.mjs";
import { buildSolanaTransferInstructions as d } from "../../functions/pre-transaction/solanaTransfer.utils.mjs";
import "../AccountActions/index.mjs";
import { PublicKey as f, TransactionInstruction as p, TransactionMessage as m, VersionedTransaction as h } from "@solana/web3.js";
//#region libs/shared/common/src/data-structures/Hinkal/hinkalSolanaProxySwap.ts
var g = async (e, t) => {
	let { addressLookupTableAccount: n, instructionLists: r } = JSON.parse(t).data, i = await Promise.all(n.map(async (t) => {
		let n = await e.getAddressLookupTable(new f(t));
		if (!n.value) throw Error(`Failed to fetch lookup table: ${t}`);
		return n.value;
	}));
	if (r.length === 0) throw Error("No instructions found");
	return {
		instructions: r.map((e) => new p({
			keys: e.accounts.map((e) => ({
				pubkey: new f(e.pubkey),
				isSigner: e.isSigner,
				isWritable: e.isWritable
			})),
			programId: new f(e.programId),
			data: Buffer.from(e.data, "base64")
		})),
		lookupTableAccounts: i
	};
}, _ = async (e, t) => {
	let n = h.deserialize(Buffer.from(t, "base64")), r = await Promise.all(n.message.addressTableLookups.map(async ({ accountKey: t }) => {
		let n = await e.getAddressLookupTable(t);
		if (!n.value) throw Error(`Failed to fetch lookup table: ${t.toBase58()}`);
		return n.value;
	})), { instructions: i } = m.decompile(n.message, { addressLookupTableAccounts: r });
	return {
		instructions: i,
		lookupTableAccounts: r
	};
}, v = async (n, r, i, o) => {
	let c = s.getAddressFromSubAccount(n, i);
	if (!c) throw Error("Public account address is not available");
	let { fetchRpcUrl: l } = t[n];
	if (!l) throw Error("RPC URL not found for the specified chain ID");
	let u = a(l), d = new f(c), { instructions: p, lookupTableAccounts: m } = o === e.Lifi ? await _(u, r) : await g(u, r);
	return {
		connection: u,
		walletPublicKey: d,
		instructions: p,
		lookupTableAccounts: m
	};
}, y = async (t, a, s, l, p, m, h, g, _) => {
	let y = i(a), b = m === e.Lifi, [{ connection: x, walletPublicKey: S, instructions: C, lookupTableAccounts: w }, T, E] = await Promise.all([
		v(y, l, p, m),
		b ? Promise.resolve(null) : t.getRandomRelay(y),
		t.getEthereumAddressByChain(y)
	]), D = C;
	if (!b) {
		if (!T) throw Error(n.RELAYER_NOT_AVAILABLE);
		let t = a[1], r = s[1] * (g ?? await u(y, h, a.map((e) => e.erc20TokenAddress), e.Emporium)).variableRate / 10000n, { instructions: i } = await d(x, S, new f(T), t, r);
		D = [...C, ...i];
	}
	let O = await c({
		connection: x,
		chainId: y,
		subAccount: p,
		payerPublicKey: S,
		instructions: D,
		lookupTableAccounts: w
	});
	return r(o(_, y, a.map((e) => e.erc20TokenAddress), s, E, a)), O;
}, b = async (t, n, r, i) => {
	if (i === e.Lifi) return n;
	let { connection: a, walletPublicKey: o, instructions: s, lookupTableAccounts: c } = await v(t, n, r, i);
	return l(a, o, s, c);
};
//#endregion
export { b as getSolanaProxySwapSerializedTransaction, y as hinkalSolanaProxySwap };
