import { chainIds as e, networkRegistry as t } from "../../constants/chains.constants.mjs";
import { TRON_DEFAULT_FEE_LIMIT_SUN as n } from "../../constants/protocol.constants.mjs";
import { ENCLAVE_URL as r } from "../snarkjs/constant.mjs";
import { httpClient as i } from "../../data-structures/http/HttpClient.mjs";
import { ethers as a } from "ethers";
import { TronWeb as o } from "tronweb";
//#region libs/shared/common/src/functions/utils/tron.utils.ts
var s = 1000n, c = (e, n) => {
	let r = t[e].fetchRpcUrl, i = n ? new o({
		fullHost: r,
		privateKey: n
	}) : new o({ fullHost: r });
	return n || i.setAddress("T9yD14Nj9j7xAB4dbGeiX9h8unkKHxuWwb"), i;
}, l = (e) => e?.startsWith("T") ? e : o.address.fromHex(e), u = (e) => {
	if (e.startsWith("0x")) return e;
	if (e.startsWith("41")) return e.replace("41", "0x");
	if (e.startsWith("T")) return u(o.address.toHex(e));
	throw Error("Failed to convert address to hex format");
}, d = (t) => {
	if (t === e.tronNile) return "https://nile.trongrid.io";
	if (t === e.tronMainnet) return "https://api.trongrid.io";
	throw Error(`Unsupported Tron chain for wallet RPC: ${t}`);
}, f = (e, t) => e.map((e) => Array.isArray(e) ? f(e, t) : t(e)), p = (e, t, n) => {
	let r = new a.Interface(e);
	if (!r.getFunction(t)) throw Error(`${t} ABI is missing for Tron call data creation`);
	let i = f(n, (e) => typeof e == "string" && e.startsWith("T") ? u(e) : e);
	return r.encodeFunctionData(t, i);
}, m = (e, t, n) => e ? BigInt(Math.max((e[t] ?? 0) - (e[n] ?? 0), 0)) : 0n, h = (e, t) => {
	let n = e.find((e) => e.key === t);
	if (!n || typeof n.value != "number") throw Error(`Missing Tron chain parameter: ${t}`);
	return BigInt(n.value);
}, g = async (e) => {
	let t = `${d(e)}/wallet/getchainparameters`, n = await i.post(t, {});
	if (!n?.chainParameter?.length) throw Error("Failed to fetch Tron chain parameters from wallet RPC");
	return n.chainParameter;
}, _ = async (e, t) => {
	let n = `${d(e)}/wallet/getaccountresource`, r = u(t), a = r.startsWith("0x") ? `41${r.slice(2)}` : r;
	return await i.post(n, {
		address: a,
		visible: !1
	});
}, v = async (e, t, r, i, a, o) => {
	let c = {
		input: a,
		callValue: Number(o),
		feeLimit: n
	}, [l, u] = await Promise.all([e.transactionBuilder.estimateEnergy(r, "", c, [], i), e.transactionBuilder.triggerConstantContract(r, "", c, [], i)]), d = await g(t), f = h(d, "getEnergyFee"), p = h(d, "getTransactionFee"), v = await _(t, i), y = BigInt(l?.energy_required ?? u?.energy_used ?? 0), b = m(v, "EnergyLimit", "EnergyUsed"), x = (y > b ? y - b : 0n) * f, S = BigInt(Math.ceil((u?.transaction?.raw_data_hex?.length ?? 0) / 2)), C = m(v, "freeNetLimit", "freeNetUsed") + m(v, "NetLimit", "NetUsed"), w = x + (S > C ? S - C : 0n) * p, T = w + w * s / 10000n;
	return T > BigInt(1e9) ? BigInt(n) : T;
}, y = (e) => {
	let t = e[0].map(BigInt), n = e[1][0].map(BigInt), r = e[1][1].map(BigInt), i = e[2].map(BigInt), a = e[3].map(BigInt);
	return {
		a: t,
		b: [[n[1], n[0]], [r[1], r[0]]],
		c: i,
		publicSignals: a
	};
}, b = (e, t) => {
	let n = a.AbiCoder.defaultAbiCoder().encode([
		"uint16",
		"uint16",
		"uint16",
		"uint256"
	], [
		e.tokenNumber,
		e.nullifierAmount,
		e.outputAmount,
		t
	]);
	return BigInt(a.keccak256(n));
}, x = async (e, t, n, o, s, c) => {
	let l = b(s, c), u = e.map(String), d = n.map(String), f = o.map(String), p = {
		a: u,
		b: [t[0].map(String), t[1].map(String)],
		c: d,
		inputs: f,
		verifier_id: String(l)
	}, m = await i.post(`${r}/sign-proof`, p), { v: h, r: g, s: _ } = a.Signature.from(m.signature.trim());
	return {
		v: h,
		r: g,
		s: _
	};
}, S = (e) => [
	e.rootHashHinkal,
	e.erc20TokenAddresses,
	e.tokenIds,
	e.amountChanges,
	e.onChainCreation,
	e.slippageValues,
	e.inputNullifiers,
	e.outCommitments,
	e.encryptedOutputs,
	[
		e.feeStructure.feeToken,
		e.feeStructure.flatFee,
		e.feeStructure.variableRate
	],
	e.timeStamp,
	[
		e.stealthAddressStructure.extraRandomization,
		e.stealthAddressStructure.stealthAddress,
		e.stealthAddressStructure.H0,
		e.stealthAddressStructure.H1
	],
	e.rootHashAccessToken,
	e.calldataHash,
	e.publicSignalCount,
	e.relay,
	e.externalAddress,
	e.externalActionId,
	e.externalActionMetadata,
	[
		e.hinkalLogicArgs.hinkalLogicAction,
		e.hinkalLogicArgs.inHinkalAddress,
		e.hinkalLogicArgs.executeApprovalChanges,
		e.hinkalLogicArgs.doPreTxApproval,
		e.hinkalLogicArgs.useApprovalUtxoData.map((e) => [
			e.approvalChanges,
			e.externalApprovalAddresses,
			e.conversionInHinkalAddress
		])
	],
	[
		e.hookData.preHookContract,
		e.hookData.hookContract,
		e.hookData.preHookMetadata,
		e.hookData.postHookMetadata
	],
	[
		e.signatureData.v,
		e.signatureData.r,
		e.signatureData.s,
		e.signatureData.accessKey,
		e.signatureData.nonce,
		e.signatureData.ethereumAddress
	],
	e.originalSender
], C = async (e, t, n, r, i = !0) => {
	if (e) {
		let { a: e, b: a, c: o, publicSignals: s } = y(t), c = await x(e, a, o, s, n, BigInt(r.externalActionId)), l = {
			v: c.v,
			r: c.r,
			s: c.s
		};
		return i && (t[1] = [[t[1][0][1], t[1][0][0]], [t[1][1][1], t[1][1][0]]]), l;
	}
};
//#endregion
export { u as addressToHexFormat, S as circomDataToArray, p as createTronCallData, c as createTronWeb, v as estimateTronFeeSunWithPadding, l as evmHexToTronBase58Address, _ as fetchTronAccountResources, g as fetchTronChainParameters, x as generateProofSignatureRemotely, m as getAvailableResource, h as getChainParameterValue, b as getVerifierId, y as parseZkCalldata, C as reorderZkCallData };
