"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const events_1 = require("../lib/events");
const nats_1 = require("../lib/events/nats");
const nats_ws_proxy_1 = require("../lib/events/nats-ws-proxy");
const router = (0, express_1.Router)();
/**
 * GET /api/nats-credentials
 * Returns NATS WebSocket URL and auth token.
 * Mounted behind requireAuth — only authenticated users receive the token.
 */
router.get('/', async (req, res) => {
    const natsAdapter = events_1.eventRegistry.getAdapter(nats_1.NatsEventAdapter);
    if (!natsAdapter) {
        return res.json({ natsWsUrl: null, natsToken: null });
    }
    // Derive wsUrl from request headers when proxy is active but no URL cached yet
    let wsUrl = natsAdapter.wsUrl;
    if (!wsUrl && natsAdapter.wsProxyTarget) {
        wsUrl = (0, nats_ws_proxy_1.deriveWsUrlFromRequest)(req, natsAdapter.wsProxyBasePath);
        natsAdapter.setWsUrl(wsUrl);
    }
    res.json({
        natsWsUrl: wsUrl,
        natsToken: natsAdapter.authToken,
    });
});
exports.default = router;
