"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.BuiltInMcpAdapter = void 0;
const logger_1 = require("../../lib/logger");
const mcpClient = __importStar(require("./client"));
const mcpServer = __importStar(require("./server"));
const mcpDbService = __importStar(require("./db"));
/**
 * Built-in MCP adapter.
 *
 * Manages:
 * - One MCP server (human queue) that exposes escalation tools
 * - Multiple MCP client connections to external servers
 */
class BuiltInMcpAdapter {
    constructor(options) {
        this.options = options || {};
    }
    async connect() {
        // Start MCP server (human queue)
        if (this.options.server?.enabled !== false) {
            await mcpServer.createHumanQueueServer({
                name: this.options.server?.name,
            });
            logger_1.loggerRegistry.info('[lt-mcp] human queue server started');
        }
        // Connect to auto-connect servers from DB
        await mcpClient.connectAutoServers();
        // Also connect to explicitly specified servers
        if (this.options.autoConnect?.length) {
            for (const serverId of this.options.autoConnect) {
                try {
                    const server = await mcpDbService.getMcpServer(serverId);
                    if (server) {
                        await mcpClient.connectToServer(server);
                    }
                    else {
                        logger_1.loggerRegistry.warn(`[lt-mcp] auto-connect server not found: ${serverId}`);
                    }
                }
                catch (err) {
                    logger_1.loggerRegistry.error(`[lt-mcp] auto-connect failed for ${serverId}: ${err.message}`);
                }
            }
        }
    }
    async disconnect() {
        await mcpClient.disconnectAll();
        await mcpServer.stopServer();
        logger_1.loggerRegistry.info('[lt-mcp] disconnected');
    }
    async connectClient(serverId) {
        const server = await mcpDbService.getMcpServer(serverId);
        if (!server)
            throw new Error(`MCP server ${serverId} not found`);
        await mcpClient.connectToServer(server);
    }
    async disconnectClient(serverId) {
        await mcpClient.disconnectFromServer(serverId);
    }
    async listTools(serverId) {
        return mcpClient.listServerTools(serverId);
    }
    async callTool(serverId, toolName, args, authContext) {
        return mcpClient.callServerTool(serverId, toolName, args, authContext);
    }
    async toolActivities(serverId) {
        return mcpClient.toolActivities(serverId);
    }
}
exports.BuiltInMcpAdapter = BuiltInMcpAdapter;
