import { MAX_MICRO_LAMPORTS_PER_CU as e } from "../../constants/protocol.constants.mjs";
import { waitLittle as t } from "../utils/time.utils.mjs";
import { AddressLookupTableProgram as n, ComputeBudgetProgram as r, PublicKey as i, Transaction as a, sendAndConfirmTransaction as o } from "@solana/web3.js";
//#region libs/shared/common/src/functions/pre-transaction/addressTableLookup.ts
var s = 30, c = async (e, t) => {
	if (!t) return;
	let n = [...new Set(t)];
	return Promise.all(n.map(async (t) => {
		let n = (await e.connection.getAddressLookupTable(new i(t))).value;
		if (!n) throw Error(`failed to fetch lookup table ${t}`);
		return n;
	}));
}, l = async (i, c, l) => {
	let [u, { blockhash: d }] = await Promise.all([i.connection.getSlot("finalized"), i.connection.getLatestBlockhash("finalized")]), f = u, [p, m] = n.createLookupTable({
		authority: l.publicKey,
		payer: l.publicKey,
		recentSlot: f
	}), h = r.setComputeUnitPrice({ microLamports: e }), g = new a().add(h).add(p);
	g.recentBlockhash = d, g.feePayer = l.publicKey, g.sign(l), await o(i.connection, g, [l], { commitment: "confirmed" });
	for (let e = 0; e < 20 && !(await i.connection.getAddressLookupTable(m, { commitment: "confirmed" })).value; e += 1) {
		if (e === 19) throw Error(`Lookup table ${m.toBase58()} not visible after creation`);
		await t(500);
	}
	for (let e = 0; e < c.length; e += s) {
		let t = c.slice(e, e + s), r = n.extendLookupTable({
			payer: l.publicKey,
			authority: l.publicKey,
			lookupTable: m,
			addresses: t
		}), { blockhash: u } = await i.connection.getLatestBlockhash(), d = new a().add(r);
		d.recentBlockhash = u, d.feePayer = l.publicKey, d.sign(l), await o(i.connection, d, [l], { commitment: "confirmed" });
	}
	return m;
};
//#endregion
export { l as createAndExtendAddressLookupTable, c as getAddressLookupTableAccounts };
