"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSettings = getSettings;
const telemetry_1 = require("../lib/telemetry");
const events_1 = require("../lib/events");
const nats_1 = require("../lib/events/nats");
const socketio_1 = require("../lib/events/socketio");
const nats_ws_proxy_1 = require("../lib/events/nats-ws-proxy");
const config_1 = require("../modules/config");
const sso_1 = require("../modules/sso");
const defaults_1 = require("../modules/defaults");
const llm_1 = require("../services/llm");
/**
 * Resolve the NATS WebSocket URL for the browser.
 *
 * Priority:
 * 1. Explicit wsUrl on the adapter (set via config or auto-derived from a prior request)
 * 2. When a wsProxy is configured, derive from the current request's headers
 * 3. null — no NATS WS available
 */
function resolveNatsWsUrl(adapter, req) {
    if (adapter.wsUrl)
        return adapter.wsUrl;
    if (adapter.wsProxyTarget && req) {
        const derived = (0, nats_ws_proxy_1.deriveWsUrlFromRequest)(req, adapter.wsProxyBasePath);
        // Cache for future requests and for the proxy's onWsUrlDerived
        adapter.setWsUrl(derived);
        return derived;
    }
    return null;
}
/**
 * Return platform settings for the current deployment.
 *
 * Includes telemetry configuration (trace URL), escalation claim duration
 * options, and event transport details (socket.io, NATS, or none).
 *
 * @param req — the incoming HTTP request, used to derive NATS WS URL from headers when proxying
 * @returns `{ status: 200, data: { telemetry, escalation, events } }`
 */
async function getSettings(req) {
    try {
        const hasSocketIO = !!events_1.eventRegistry.getAdapter(socketio_1.SocketIOEventAdapter);
        const natsAdapter = events_1.eventRegistry.getAdapter(nats_1.NatsEventAdapter);
        // Dashboard transport: Socket.IO is the default (works in-process, zero config).
        // NATS is only reported when explicitly opted in via EVENT_TRANSPORT=nats,
        // which a multi-container deployment sets when it wants the dashboard to
        // connect via NATS instead of Socket.IO.
        const forceNats = config_1.config.EVENT_TRANSPORT === 'nats';
        const transport = forceNats && natsAdapter ? 'nats' : hasSocketIO ? 'socketio' : natsAdapter ? 'nats' : 'none';
        return {
            status: 200,
            data: {
                telemetry: {
                    traceUrl: telemetry_1.telemetryRegistry.traceUrl ?? null,
                },
                escalation: {
                    claimDurations: defaults_1.CLAIM_DURATION_OPTIONS,
                },
                events: {
                    transport,
                    natsWsUrl: natsAdapter ? resolveNatsWsUrl(natsAdapter, req) : null,
                },
                auth: {
                    sso: (0, sso_1.isSSOEnabled)(),
                    ssoLogoutUrl: (0, sso_1.getSSOConfig)()?.logoutUrl ?? null,
                },
                ai: {
                    enabled: (0, llm_1.hasLLMApiKey)(),
                },
            },
        };
    }
    catch (err) {
        return { status: 500, error: err.message };
    }
}
