import { buildSafeWebConfigPayload } from "../lib/config-payload.js";
import { applyAgentsPatch } from "./config-patch/agents.js";
import { applyChannelsPatch } from "./config-patch/channels.js";
import { applyGatewayPatch } from "./config-patch/gateway.js";
import { applyMiscPatch, validateGatewayAfterPatch } from "./config-patch/misc.js";
import "./config-patch/index.js";
//#region src/gateway/hono/routes/config.ts
function registerConfigRoutes(authenticated, deps) {
	const { service, strictRateLimitMiddleware } = deps;
	authenticated.post("/api/config/reload", strictRateLimitMiddleware, async (c) => {
		const result = await service.reloadConfig();
		return c.json({
			ok: true,
			payload: result
		});
	});
	authenticated.post("/api/heartbeat/trigger", strictRateLimitMiddleware, async (c) => {
		let reason = "manual";
		try {
			const body = await c.req.json();
			if (body && typeof body === "object" && typeof body.reason === "string") {
				const r = body.reason.trim();
				if (r) reason = r.slice(0, 120);
			}
		} catch {}
		service.requestHeartbeatNow({ reason });
		return c.json({
			ok: true,
			payload: { scheduled: true }
		});
	});
	authenticated.get("/api/config", async (c) => {
		const safeConfig = await buildSafeWebConfigPayload(service);
		return c.json({
			ok: true,
			payload: { config: safeConfig }
		});
	});
	authenticated.patch("/api/config", strictRateLimitMiddleware, async (c) => {
		const body = await c.req.json();
		const config = service.currentConfig;
		applyAgentsPatch(config, body);
		applyChannelsPatch(config, body);
		const gatewayResult = applyGatewayPatch(config, body);
		if (gatewayResult.ok === false) return c.json({
			ok: false,
			error: gatewayResult.error
		}, gatewayResult.status);
		const miscResult = await applyMiscPatch(config, body);
		if (miscResult.ok === false) return c.json({
			ok: false,
			error: miscResult.error
		}, miscResult.status);
		const finalGwCheck = validateGatewayAfterPatch(config, body);
		if (finalGwCheck.ok === false) return c.json({
			ok: false,
			error: finalGwCheck.error
		}, finalGwCheck.status);
		const result = await service.saveConfig(config);
		if (!result.saved) return c.json({
			ok: false,
			error: result.error
		}, 500);
		if (body.gateway?.heartbeat !== void 0 && typeof body.gateway.heartbeat === "object") service.reloadHeartbeatFromCurrentConfig();
		const safeConfig = await buildSafeWebConfigPayload(service);
		return c.json({
			ok: true,
			payload: { config: safeConfig }
		});
	});
	/** POST /api/gateway/reveal-auth-secret — plaintext gateway.auth token/password from config only. */
	authenticated.post("/api/gateway/reveal-auth-secret", strictRateLimitMiddleware, async (c) => {
		let field;
		try {
			const body = await c.req.json();
			field = body && typeof body === "object" ? body.field : void 0;
		} catch {
			field = void 0;
		}
		if (field !== "token" && field !== "password") return c.json({
			ok: false,
			error: { message: "field must be token or password" }
		}, 400);
		const config = service.currentConfig;
		const secret = field === "token" ? config.gateway?.auth?.token?.trim() || null : config.gateway?.auth?.password?.trim() || null;
		return c.json({
			ok: true,
			payload: {
				field,
				secret,
				source: secret ? "config" : "none"
			}
		});
	});
	/** POST /api/agents/browser/reveal-cloud-api-key — plaintext browser cloud apiKey from config only. */
	authenticated.post("/api/agents/browser/reveal-cloud-api-key", strictRateLimitMiddleware, async (c) => {
		const apiKey = service.currentConfig.agents?.defaults?.browser?.cloud?.apiKey?.trim() || null;
		return c.json({
			ok: true,
			payload: {
				apiKey,
				source: apiKey ? "config" : "none"
			}
		});
	});
	/** POST /api/tools/web/reveal-search-api-key — plaintext search provider apiKey by index. */
	authenticated.post("/api/tools/web/reveal-search-api-key", strictRateLimitMiddleware, async (c) => {
		let index = -1;
		try {
			const body = await c.req.json();
			if (body && typeof body === "object" && typeof body.index === "number") index = Math.floor(body.index);
		} catch {
			index = -1;
		}
		const providers = service.currentConfig.tools?.web?.search?.providers ?? [];
		if (index < 0 || index >= providers.length) return c.json({
			ok: false,
			error: { message: "Invalid provider index" }
		}, 400);
		const apiKey = providers[index]?.apiKey?.trim() || null;
		return c.json({
			ok: true,
			payload: {
				index,
				apiKey,
				source: apiKey ? "config" : "none"
			}
		});
	});
}
//#endregion
export { registerConfigRoutes };

//# sourceMappingURL=config.js.map