import { networkRegistry as e } from "../../constants/chains.constants.mjs";
import { ExternalActionId as t } from "../../types/external-action.types.mjs";
import { AdminTransactionType as n } from "../../types/admin.types.mjs";
import "../../types/index.mjs";
import { transactionErrorCodes as r } from "../../error-handling/error-codes.constants.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 } from "../../functions/pre-transaction/sendV0Transaction.mjs";
import { getFeeStructure as l } from "../../functions/pre-transaction/getFeeStructure.mjs";
import "../../constants/index.mjs";
import { emitTxPublicData as u } from "../../API/admin-calls.mjs";
import "../AccountActions/index.mjs";
import { buildSolanaTransferInstructions as d } from "../../functions/pre-transaction/solanaTransfer.utils.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 } = e[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 === t.Lifi ? await _(u, r) : await g(u, r);
	return {
		connection: u,
		walletPublicKey: d,
		instructions: p,
		lookupTableAccounts: m
	};
}, y = async (e, a, s, p, m, h, g, _, y) => {
	let b = i(a), x = h === t.Lifi, [{ connection: S, walletPublicKey: C, instructions: w, lookupTableAccounts: T }, E, D] = await Promise.all([
		v(b, p, m, h),
		x ? Promise.resolve(null) : e.getRandomRelay(b),
		e.getEthereumAddressByChain(b)
	]), O = w;
	if (!x) {
		if (!E) throw Error(r.RELAYER_NOT_AVAILABLE);
		let e = a[1], n = s[1] * (_ ?? await l(b, g, a.map((e) => e.erc20TokenAddress), t.Emporium)).variableRate / 10000n, { instructions: i } = await d(S, C, new f(E), e, n);
		O = [...w, ...i];
	}
	let k = await c({
		connection: S,
		chainId: b,
		subAccount: m,
		payerPublicKey: C,
		instructions: O,
		lookupTableAccounts: T
	}), A = x ? n.PublicBridgeSwap : n.PublicSwap;
	return u(o(y ?? A, b, a.map((e) => e.erc20TokenAddress), s, D, a)), k;
};
//#endregion
export { y as hinkalSolanaProxySwap };
