import { _ as resolveStateDir } from "./paths-C9FmTW1x.js";
import fs from "node:fs";
import path from "node:path";

//#region src/managed/federation.ts
const MANAGED_AGENT_DOMAIN_SUFFIX = ".agents.fased.app";
function resolveManagedFederationPublicUrl(params) {
	const explicit = typeof params.publicUrl === "string" ? params.publicUrl.trim() : "";
	if (explicit) return explicit;
	const slug = typeof params.agentSlug === "string" ? params.agentSlug.trim() : "";
	if (!slug) return;
	return `https://${slug}${MANAGED_AGENT_DOMAIN_SUFFIX}`;
}
function resolveFederationAccessTokenPath(env = process.env) {
	return path.join(resolveStateDir(env), "federation", "access-token.json");
}
function readManagedFederationTokenSummary(env = process.env) {
	const tokenPath = resolveFederationAccessTokenPath(env);
	if (!fs.existsSync(tokenPath)) return {
		path: tokenPath,
		exists: false,
		hasZrokToken: false
	};
	try {
		const payload = JSON.parse(fs.readFileSync(tokenPath, "utf8"));
		const agentSlug = typeof payload.agentSlug === "string" ? payload.agentSlug : void 0;
		const publicUrl = resolveManagedFederationPublicUrl({
			publicUrl: typeof payload.publicUrl === "string" ? payload.publicUrl : void 0,
			agentSlug
		});
		return {
			path: tokenPath,
			exists: true,
			handle: typeof payload.handle === "string" ? payload.handle : void 0,
			tokenId: typeof payload.tokenId === "string" ? payload.tokenId : void 0,
			expiresAt: typeof payload.expiresAt === "string" ? payload.expiresAt : void 0,
			agentSlug,
			publicUrl,
			hasZrokToken: typeof payload.zrokToken === "string" && payload.zrokToken.trim().length > 0
		};
	} catch {
		return {
			path: tokenPath,
			exists: true,
			hasZrokToken: false
		};
	}
}

//#endregion
export { resolveManagedFederationPublicUrl as n, readManagedFederationTokenSummary as t };