"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerAdapters = registerAdapters;
const logger_1 = require("../lib/logger");
const pino_1 = require("../lib/logger/pino");
const telemetry_1 = require("../lib/telemetry");
const honeycomb_1 = require("../lib/telemetry/honeycomb");
const events_1 = require("../lib/events");
const nats_1 = require("../lib/events/nats");
const socketio_1 = require("../lib/events/socketio");
const maintenance_1 = require("../services/maintenance");
const maintenance_2 = require("../modules/maintenance");
const mcp_1 = require("../services/mcp");
const adapter_1 = require("../services/mcp/adapter");
const escalation_strategy_1 = require("../services/escalation-strategy");
const default_1 = require("../services/escalation-strategy/default");
const mcp_2 = require("../services/escalation-strategy/mcp");
const socket_auth_1 = require("./socket-auth");
/**
 * Register all adapters (logging, telemetry, events, maintenance,
 * escalation strategy, MCP) based on the startup config.
 */
function registerAdapters(startConfig) {
    // Logging (register first so subsequent log calls use it)
    if (startConfig.logging?.adapter) {
        logger_1.loggerRegistry.register(startConfig.logging.adapter);
    }
    else if (startConfig.logging?.pino) {
        logger_1.loggerRegistry.register(new pino_1.PinoLoggerAdapter(startConfig.logging.pino));
    }
    // Telemetry
    if (startConfig.telemetry?.adapter) {
        telemetry_1.telemetryRegistry.register(startConfig.telemetry.adapter);
    }
    else if (startConfig.telemetry?.honeycomb) {
        telemetry_1.telemetryRegistry.register(new honeycomb_1.HoneycombTelemetryAdapter(startConfig.telemetry.honeycomb));
    }
    // Events — register the configured transport.
    // Socket.IO is the default for single-container ("nothing but Postgres").
    // When NATS or custom adapters are configured, Socket.IO is NOT registered —
    // the dashboard auto-detects the transport via GET /api/settings and connects
    // directly (e.g., NATS WebSocket). Socket.IO never interferes in production.
    if (startConfig.events?.adapters) {
        for (const adapter of startConfig.events.adapters) {
            events_1.eventRegistry.register(adapter);
        }
    }
    else if (startConfig.events?.nats) {
        events_1.eventRegistry.register(new nats_1.NatsEventAdapter(startConfig.events.nats));
    }
    else {
        events_1.eventRegistry.register(new socketio_1.SocketIOEventAdapter({
            authenticate: (0, socket_auth_1.createSocketIOAuthenticator)(startConfig),
        }));
    }
    // CallbackEventAdapter is registered later in start/workers.ts
    // so it can be wired to the NATS bridge before connect().
    // Do NOT register a duplicate here.
    // Maintenance — opt-in only. Pass `true` for defaults or a config object.
    if (startConfig.maintenance === true) {
        maintenance_1.maintenanceRegistry.register(maintenance_2.defaultMaintenanceConfig);
    }
    else if (startConfig.maintenance) {
        maintenance_1.maintenanceRegistry.register(startConfig.maintenance);
    }
    // Escalation strategy
    if (startConfig.escalation?.adapter) {
        escalation_strategy_1.escalationStrategyRegistry.register(startConfig.escalation.adapter);
    }
    else if (startConfig.escalation?.strategy === 'mcp') {
        escalation_strategy_1.escalationStrategyRegistry.register(new mcp_2.McpEscalationStrategy());
    }
    else {
        escalation_strategy_1.escalationStrategyRegistry.register(new default_1.DefaultEscalationStrategy());
    }
    // MCP
    if (startConfig.mcp?.adapter) {
        mcp_1.mcpRegistry.register(startConfig.mcp.adapter);
    }
    else if (startConfig.mcp) {
        mcp_1.mcpRegistry.register(new adapter_1.BuiltInMcpAdapter({
            server: startConfig.mcp.server,
            autoConnect: startConfig.mcp.autoConnect,
        }));
    }
}
