import { init_paths, resolveConfigPath } from "../../../config/paths.js";
import { getContextWithOpts } from "../../context.js";
import { addGatewayClientOptions, parseGatewayClientOptions } from "../../utils/gateway-client-options.js";
import { Command } from "commander";
//#region src/cli/commands/gateway/probe.ts
init_paths();
function resolveProbeTargets(opts) {
	const targets = [];
	const port = opts.port;
	targets.push({
		label: "localhost",
		url: `http://127.0.0.1:${port}`
	});
	if (opts.url) {
		const normalized = opts.url.replace(/\/+$/, "");
		if (!normalized.includes("127.0.0.1") && !normalized.includes("localhost")) targets.push({
			label: "remote",
			url: normalized
		});
	}
	return targets;
}
async function probeTarget(callGatewayApi, target, token, timeoutMs) {
	const healthResult = await callGatewayApi("GET", "/api/health", {
		url: target.url,
		timeoutMs: timeoutMs ?? 5e3
	});
	if (!healthResult.ok) return {
		label: target.label,
		url: target.url,
		reachable: false,
		authenticated: false,
		durationMs: healthResult.durationMs,
		error: healthResult.error
	};
	let authenticated = false;
	if (token) authenticated = (await callGatewayApi("GET", "/api/status", {
		url: target.url,
		token,
		timeoutMs: timeoutMs ?? 5e3
	})).ok;
	return {
		label: target.label,
		url: target.url,
		reachable: true,
		authenticated,
		durationMs: healthResult.durationMs,
		version: healthResult.data?.version
	};
}
function createProbeCommand() {
	const cmd = new Command("probe").description("Probe gateway reachability and auth capability");
	addGatewayClientOptions(cmd);
	cmd.action(async (options) => {
		const configPath = getContextWithOpts().configPath || resolveConfigPath();
		const [{ loadConfig }, { callGatewayApi, resolveGatewayToken }] = await Promise.all([import("../../../config/index.js"), import("../../utils/gateway-client.js")]);
		const port = loadConfig(configPath)?.gateway?.port ?? 18790;
		const clientOpts = {
			...parseGatewayClientOptions(options),
			configPath
		};
		const token = resolveGatewayToken(clientOpts);
		const targets = resolveProbeTargets({
			url: clientOpts.url,
			port
		});
		const results = [];
		for (const target of targets) {
			const result = await probeTarget(callGatewayApi, target, token, clientOpts.timeoutMs);
			results.push(result);
		}
		if (clientOpts.json) {
			console.log(JSON.stringify({ targets: results }, null, 2));
			const anyReachable = results.some((r) => r.reachable);
			process.exit(anyReachable ? 0 : 1);
		}
		console.log("🔍 Gateway Probe");
		console.log("");
		for (const result of results) {
			if (result.reachable) {
				console.log(`✅ ${result.label} (${result.url})`);
				console.log(`   Reachable: yes (${result.durationMs}ms)`);
				console.log(`   Auth: ${result.authenticated ? "✅ authenticated" : "🔒 not authenticated (pass --token)"}`);
				if (result.version) console.log(`   Version: ${result.version}`);
			} else {
				console.log(`❌ ${result.label} (${result.url})`);
				console.log("   Reachable: no");
				console.log(`   Error: ${result.error}`);
			}
			console.log("");
		}
		const anyReachable = results.some((r) => r.reachable);
		if (!anyReachable) console.log("💡 Is the gateway running? Try: xopc gateway");
		process.exit(anyReachable ? 0 : 1);
	});
	return cmd;
}
//#endregion
export { createProbeCommand };

//# sourceMappingURL=probe.js.map