"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.registerMcpServerTools = registerMcpServerTools;
const mcpDb = __importStar(require("../../../services/mcp/db"));
const mcp_1 = require("../../../services/mcp");
const schemas_1 = require("./schemas");
function registerMcpServerTools(server) {
    // mirrors GET /api/mcp/servers
    server.registerTool('list_mcp_servers', {
        title: 'List MCP Servers',
        description: 'List registered MCP servers with optional filters by status, tags, ' +
            'or search term. Returns server name, status, tags, and tool count.',
        inputSchema: schemas_1.listMcpServersSchema,
    }, async (args) => {
        const tagsArray = args.tags
            ? args.tags.split(',').map((t) => t.trim()).filter(Boolean)
            : undefined;
        const { servers, total } = await mcpDb.listMcpServers({
            status: args.status,
            tags: tagsArray,
            search: args.search,
            limit: args.limit,
            offset: args.offset,
        });
        return {
            content: [{
                    type: 'text',
                    text: JSON.stringify({
                        total,
                        count: servers.length,
                        servers: servers.map((s) => ({
                            id: s.id,
                            name: s.name,
                            description: s.description,
                            status: s.status,
                            tags: s.tags,
                            auto_connect: s.auto_connect,
                            tool_count: s.tool_manifest?.length ?? 0,
                        })),
                    }),
                }],
        };
    });
    // mirrors PUT /api/mcp/servers/:id
    server.registerTool('update_mcp_server', {
        title: 'Update MCP Server',
        description: 'Update an MCP server registration. Use this to change tags ' +
            '(which controls tool discovery scope), description, or auto_connect.',
        inputSchema: schemas_1.updateMcpServerSchema,
    }, async (args) => {
        const { id, ...updates } = args;
        const updated = await mcpDb.updateMcpServer(id, updates);
        if (!updated) {
            return {
                content: [{ type: 'text', text: JSON.stringify({ error: 'MCP server not found' }) }],
                isError: true,
            };
        }
        return {
            content: [{
                    type: 'text',
                    text: JSON.stringify({
                        updated: true,
                        server: { id: updated.id, name: updated.name, tags: updated.tags, status: updated.status },
                    }),
                }],
        };
    });
    // mirrors POST /api/mcp/servers/:id/connect
    server.registerTool('connect_mcp_server', {
        title: 'Connect MCP Server',
        description: 'Connect to a registered MCP server. Re-establishes the transport ' +
            'and refreshes the tool manifest.',
        inputSchema: schemas_1.connectMcpServerSchema,
    }, async (args) => {
        const adapter = mcp_1.mcpRegistry.current;
        if (!adapter) {
            return {
                content: [{ type: 'text', text: JSON.stringify({ error: 'MCP adapter not registered' }) }],
                isError: true,
            };
        }
        await adapter.connectClient(args.id);
        return {
            content: [{
                    type: 'text',
                    text: JSON.stringify({ connected: true, server_id: args.id }),
                }],
        };
    });
    // mirrors POST /api/mcp/servers/:id/disconnect
    server.registerTool('disconnect_mcp_server', {
        title: 'Disconnect MCP Server',
        description: 'Disconnect from an MCP server. The server remains registered ' +
            'but its tools become unavailable until reconnected.',
        inputSchema: schemas_1.disconnectMcpServerSchema,
    }, async (args) => {
        const adapter = mcp_1.mcpRegistry.current;
        if (!adapter) {
            return {
                content: [{ type: 'text', text: JSON.stringify({ error: 'MCP adapter not registered' }) }],
                isError: true,
            };
        }
        await adapter.disconnectClient(args.id);
        return {
            content: [{
                    type: 'text',
                    text: JSON.stringify({ disconnected: true, server_id: args.id }),
                }],
        };
    });
}
