"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.registerControlPlaneTools = registerControlPlaneTools;
const api = __importStar(require("../../../api/controlplane"));
const schemas_1 = require("./schemas");
function registerControlPlaneTools(server) {
    // mirrors GET /api/controlplane/apps
    server.registerTool('list_apps', {
        title: 'List Apps',
        description: 'List available HotMesh application namespaces.',
        inputSchema: schemas_1.listAppsSchema,
    }, async (_args) => {
        const result = await api.listApps();
        if (result.error) {
            return { content: [{ type: 'text', text: JSON.stringify({ error: result.error }) }], isError: true };
        }
        return { content: [{ type: 'text', text: JSON.stringify(result.data) }] };
    });
    // mirrors GET /api/controlplane/rollcall
    server.registerTool('rollcall', {
        title: 'Roll Call',
        description: 'Execute a roll call — discovers all engines and workers in the mesh. ' +
            'Returns topology of active participants.',
        inputSchema: schemas_1.rollCallSchema,
    }, async (args) => {
        const result = await api.rollCall({
            appId: args.app_id || 'durable',
            delay: args.delay,
        });
        if (result.error) {
            return { content: [{ type: 'text', text: JSON.stringify({ error: result.error }) }], isError: true };
        }
        return { content: [{ type: 'text', text: JSON.stringify(result.data) }] };
    });
    // mirrors POST /api/controlplane/throttle
    server.registerTool('apply_throttle', {
        title: 'Apply Throttle',
        description: 'Apply a throttle to the mesh. Values: -1 = pause, 0 = resume, ' +
            '>0 = delay in ms per message.',
        inputSchema: schemas_1.applyThrottleSchema,
    }, async (args) => {
        const result = await api.applyThrottle(args);
        if (result.error) {
            return { content: [{ type: 'text', text: JSON.stringify({ error: result.error }) }], isError: true };
        }
        return { content: [{ type: 'text', text: JSON.stringify(result.data) }] };
    });
    // mirrors GET /api/controlplane/streams
    server.registerTool('get_stream_stats', {
        title: 'Get Stream Stats',
        description: 'Stream processing statistics — pending count and processed volume ' +
            'by time range.',
        inputSchema: schemas_1.getStreamStatsSchema,
    }, async (args) => {
        const result = await api.getStreamStats(args);
        if (result.error) {
            return { content: [{ type: 'text', text: JSON.stringify({ error: result.error }) }], isError: true };
        }
        return { content: [{ type: 'text', text: JSON.stringify(result.data) }] };
    });
    // mirrors GET /api/controlplane/stream-messages
    server.registerTool('list_stream_messages', {
        title: 'List Stream Messages',
        description: 'Browse stream messages with pagination, filtering, and sorting. ' +
            'Both namespace and source are required.',
        inputSchema: schemas_1.listStreamMessagesSchema,
    }, async (args) => {
        const result = await api.listStreamMessages(args);
        if (result.error) {
            return { content: [{ type: 'text', text: JSON.stringify({ error: result.error }) }], isError: true };
        }
        return { content: [{ type: 'text', text: JSON.stringify(result.data) }] };
    });
}
