import "./paths-C9FmTW1x.js";
import { U as theme } from "./agent-paths-BySKyq9I.js";
import { An as readConfigFileSnapshotForWrite, Cd as isTruthyEnvValue, Dn as loadConfig, Mn as writeConfigFile, Nn as validateConfigObjectWithPlugins, dd as formatCliCommand } from "./agent-scope-CLn_Ws3B.js";
import { d as defaultRuntime } from "./errors-Dx3S3Nrp.js";
import "./github-copilot-token-BA45-q56.js";
import "./chat-envelope-D_wSv26N.js";
import "./wallet-runtime-config-BlRWvEER.js";
import { a as readWalletProviderRegistry, s as resolveWalletUserRole } from "./wallet-provider-registry-ax0NJo0F.js";
import { a as resolveFederationBondWalletId, c as loadPersistedFederationToken, i as resolveFederationBaseUrl, o as resolveFederationHandle, r as resolveAgentPublicOrigin, t as DEFAULT_FEDERATION_BASE_URL, u as resolveFederationTokenPath } from "./runtime-bsvPuEXH.js";
import "./device-identity-DzUZ06fL.js";
import { t as resolveFederationBondWallet } from "./solana-bond-signing-CgswTuFe.js";
import { t as readManagedFederationTokenSummary } from "./federation-BrNm7xtG.js";
import { n as runCommandWithRuntime } from "./cli-utils-DJ7Nfw8J.js";
import { t as formatHelpExamples } from "./help-format-H4GNCFCs.js";

//#region src/cli/federation-cli.ts
function runFederationCommand(action, label) {
	return runCommandWithRuntime(defaultRuntime, action, (err) => {
		const message = err instanceof Error ? err.message : String(err);
		defaultRuntime.error(label ? `${label}: ${message}` : message);
		defaultRuntime.exit(1);
	});
}
async function buildFederationStatus() {
	const token = await loadPersistedFederationToken(process.env);
	const managedToken = readManagedFederationTokenSummary(process.env);
	const autoConnectEnabled = isTruthyEnvValue(process.env.FASED_FEDERATION_AUTO_CONNECT);
	return {
		configured: autoConnectEnabled || Boolean(process.env.FASED_FEDERATION_BASE_URL?.trim()) || Boolean(process.env.FASED_A2A_HANDLE?.trim()) || Boolean(process.env.FASED_FEDERATION_HANDLE?.trim()) || Boolean(token),
		autoConnectEnabled,
		baseUrl: resolveFederationBaseUrl(process.env),
		defaultBaseUrl: DEFAULT_FEDERATION_BASE_URL,
		handle: resolveFederationHandle({ env: process.env }),
		publicOrigin: resolveAgentPublicOrigin(process.env),
		tokenPath: resolveFederationTokenPath(process.env),
		tokenPresent: Boolean(token),
		tokenId: token?.tokenId,
		expiresAt: token?.expiresAt,
		scopes: token?.scopes,
		trustState: token?.trustState,
		hostedState: token?.hostedState,
		agentSlug: token?.agentSlug,
		publicUrl: token?.publicUrl ?? managedToken.publicUrl,
		lastAttestOrRenewAt: token?.lastAttestOrRenewAt,
		managedToken
	};
}
function renderFederationStatus(payload) {
	defaultRuntime.log(theme.heading("Federation"));
	defaultRuntime.log(`${theme.muted("Configured:")} ${payload.configured ? "yes" : "no"}`);
	defaultRuntime.log(`${theme.muted("Auto-connect:")} ${payload.autoConnectEnabled ? "enabled" : "disabled"}`);
	defaultRuntime.log(`${theme.muted("Base URL:")} ${payload.baseUrl || payload.defaultBaseUrl}`);
	defaultRuntime.log(`${theme.muted("Handle:")} ${payload.handle}`);
	defaultRuntime.log(`${theme.muted("Public origin:")} ${payload.publicOrigin}`);
	defaultRuntime.log(`${theme.muted("Token path:")} ${payload.tokenPath}`);
	defaultRuntime.log(`${theme.muted("Token:")} ${payload.tokenPresent ? "present" : "missing"}`);
	if (payload.trustState) defaultRuntime.log(`${theme.muted("Trust state:")} ${payload.trustState}`);
	if (payload.hostedState) defaultRuntime.log(`${theme.muted("Hosted state:")} ${payload.hostedState}`);
	if (payload.publicUrl) defaultRuntime.log(`${theme.muted("Public URL:")} ${payload.publicUrl}`);
	if (payload.expiresAt) defaultRuntime.log(`${theme.muted("Expires:")} ${payload.expiresAt}`);
	const cfg = loadConfig();
	const bondWalletId = resolveFederationBondWalletId({
		env: process.env,
		cfg
	});
	if (bondWalletId) defaultRuntime.log(`${theme.muted("Bond Vault:")} ${bondWalletId}`);
}
async function updateFederationBondWalletConfig(walletId) {
	const writeSnapshot = await readConfigFileSnapshotForWrite();
	const baseConfig = structuredClone(writeSnapshot.snapshot.resolved ?? {});
	if (walletId) {
		baseConfig.federation = baseConfig.federation ?? {};
		baseConfig.federation.bond = baseConfig.federation.bond ?? {};
		baseConfig.federation.bond.walletId = walletId;
	} else if (baseConfig.federation?.bond) {
		delete baseConfig.federation.bond.walletId;
		if (Object.keys(baseConfig.federation.bond).length === 0) delete baseConfig.federation.bond;
		if (baseConfig.federation && Object.keys(baseConfig.federation).length === 0) delete baseConfig.federation;
	}
	const validated = validateConfigObjectWithPlugins(baseConfig);
	if (!validated.ok) {
		const detail = validated.issues.slice(0, 3).map((issue) => `${issue.path || "<root>"}: ${issue.message}`).join("; ");
		throw new Error(detail || "invalid federation bond config");
	}
	await writeConfigFile(validated.config, writeSnapshot.writeOptions);
	return { walletId };
}
function registerFederationCli(program) {
	const federation = program.command("federation").description("Inspect federation runtime, token, and hosted routing state").addHelpText("after", () => `\n${theme.heading("Examples:")}\n${formatHelpExamples([
		[formatCliCommand("fased federation status"), "Show live federation runtime state."],
		[formatCliCommand("fased federation token --json"), "Inspect the persisted federation token summary."],
		[formatCliCommand("fased federation paths"), "Show where federation state is stored on disk."]
	])}\n`);
	federation.command("status").description("Show federation runtime, handle, and hosted state").option("--json", "Output JSON", false).action(async (opts) => {
		await runFederationCommand(async () => {
			const payload = await buildFederationStatus();
			if (opts.json) {
				defaultRuntime.log(JSON.stringify(payload, null, 2));
				return;
			}
			renderFederationStatus(payload);
		}, "Federation status failed");
	});
	federation.command("token").description("Show the persisted federation token summary").option("--json", "Output JSON", false).action(async (opts) => {
		await runFederationCommand(async () => {
			const token = await loadPersistedFederationToken(process.env);
			const managed = readManagedFederationTokenSummary(process.env);
			const payload = {
				path: resolveFederationTokenPath(process.env),
				token,
				managed
			};
			if (opts.json) {
				defaultRuntime.log(JSON.stringify(payload, null, 2));
				return;
			}
			if (!token) {
				defaultRuntime.log("No federation token is persisted.");
				defaultRuntime.log(`${theme.muted("Path:")} ${payload.path}`);
				return;
			}
			defaultRuntime.log(theme.heading("Federation Token"));
			defaultRuntime.log(`${theme.muted("Token ID:")} ${token.tokenId}`);
			defaultRuntime.log(`${theme.muted("Handle:")} ${token.handle}`);
			defaultRuntime.log(`${theme.muted("Issued:")} ${token.issuedAt}`);
			defaultRuntime.log(`${theme.muted("Expires:")} ${token.expiresAt}`);
			defaultRuntime.log(`${theme.muted("Trust state:")} ${token.trustState ?? "pending"}`);
			defaultRuntime.log(`${theme.muted("Hosted state:")} ${token.hostedState ?? "disabled"}`);
			defaultRuntime.log(`${theme.muted("Scopes:")} ${(token.scopes ?? []).join(", ") || "none"}`);
			defaultRuntime.log(`${theme.muted("Path:")} ${payload.path}`);
			if (managed.publicUrl) defaultRuntime.log(`${theme.muted("Public URL:")} ${managed.publicUrl}`);
		}, "Federation token inspection failed");
	});
	federation.command("paths").description("Show federation state file locations").option("--json", "Output JSON", false).action(async (opts) => {
		await runFederationCommand(async () => {
			const managed = readManagedFederationTokenSummary(process.env);
			const payload = {
				tokenPath: resolveFederationTokenPath(process.env),
				managedTokenPath: managed.path
			};
			if (opts.json) {
				defaultRuntime.log(JSON.stringify(payload, null, 2));
				return;
			}
			defaultRuntime.log(theme.heading("Federation Paths"));
			defaultRuntime.log(`${theme.muted("Token path:")} ${payload.tokenPath}`);
			defaultRuntime.log(`${theme.muted("Managed token path:")} ${payload.managedTokenPath}`);
		}, "Federation paths failed");
	});
	const bondWallet = federation.command("bond-wallet").description("Inspect or set the Vault wallet assigned to federation bond");
	bondWallet.command("status").description("Show the currently configured federation bond Vault").option("--json", "Output JSON", false).action(async (opts) => {
		await runFederationCommand(async () => {
			const cfg = loadConfig();
			const walletId = resolveFederationBondWalletId({
				env: process.env,
				cfg
			}) || null;
			const registry = readWalletProviderRegistry(process.env);
			const wallet = walletId ? registry.wallets.find((entry) => entry.id === walletId) : void 0;
			let walletAddress;
			if (walletId) try {
				walletAddress = (await resolveFederationBondWallet({
					env: process.env,
					cfg,
					walletId
				})).walletAddress;
			} catch {
				walletAddress = wallet?.addresses?.solana;
			}
			const payload = {
				walletId,
				walletName: wallet?.name,
				walletAddress: walletAddress ?? wallet?.addresses?.solana
			};
			if (opts.json) {
				defaultRuntime.log(JSON.stringify(payload, null, 2));
				return;
			}
			defaultRuntime.log(theme.heading("Federation Bond Vault"));
			defaultRuntime.log(`${theme.muted("Wallet ID:")} ${payload.walletId ?? "not set"}`);
			if (payload.walletName) defaultRuntime.log(`${theme.muted("Wallet name:")} ${payload.walletName}`);
			if (payload.walletAddress) defaultRuntime.log(`${theme.muted("Wallet address:")} ${payload.walletAddress}`);
		}, "Federation bond Vault status failed");
	});
	bondWallet.command("set").description("Set the Vault wallet assigned to federation bond").argument("<walletId>", "Wallet id from the local wallet registry").action(async (walletId) => {
		await runFederationCommand(async () => {
			const normalized = walletId.trim();
			if (!normalized) throw new Error("walletId is required");
			const registry = readWalletProviderRegistry(process.env);
			const wallet = registry.wallets.find((entry) => entry.id === normalized);
			if (!wallet) throw new Error(`walletId not found: ${normalized}`);
			const purpose = resolveWalletUserRole(wallet);
			if (normalized === registry.defaultWalletId || purpose === "agent") throw new Error("Federation bond requires a Vault wallet, not an Agent wallet.");
			if (purpose === "mining") throw new Error("Federation bond requires a Vault wallet, not the Mining wallet.");
			if (purpose !== "vault") throw new Error("Federation bond requires a Vault wallet.");
			if (!wallet.addresses?.solana?.trim()) throw new Error("Federation bond requires a Vault wallet with a Solana address.");
			await updateFederationBondWalletConfig(normalized);
			defaultRuntime.log(`Federation bond Vault set to ${normalized}`);
		}, "Federation bond Vault update failed");
	});
	bondWallet.command("clear").description("Clear the configured federation bond Vault").action(async () => {
		await runFederationCommand(async () => {
			await updateFederationBondWalletConfig(null);
			defaultRuntime.log("Federation bond Vault cleared");
		}, "Federation bond Vault clear failed");
	});
}

//#endregion
export { registerFederationCli };