"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.McpController = void 0;
const api_1 = require("@hed-hog/api");
const common_1 = require("@nestjs/common");
const setting_service_1 = require("../setting/setting.service");
const mcp_auth_guard_1 = require("./guards/mcp-auth.guard");
const mcp_permission_guard_1 = require("./guards/mcp-permission.guard");
const mcp_server_service_1 = require("./mcp-server.service");
let McpController = class McpController {
    constructor(mcpServer, setting) {
        this.mcpServer = mcpServer;
        this.setting = setting;
    }
    async handle(req, res) {
        var _a, _b, _c, _d, _e;
        const settings = await this.setting.getSettingValues(['mcp-enabled']);
        if (settings['mcp-enabled'] === false) {
            res.status(503).json({ message: 'MCP server is disabled.' });
            return;
        }
        const auth = req.auth;
        const locale = (_c = (_b = (_a = req.headers['accept-language']) === null || _a === void 0 ? void 0 : _a.split(',')[0]) === null || _b === void 0 ? void 0 : _b.split('-')[0]) !== null && _c !== void 0 ? _c : 'en';
        const body = req.body;
        const toolName = (body === null || body === void 0 ? void 0 : body.method) === 'tools/call' ? ((_e = (_d = body === null || body === void 0 ? void 0 : body.params) === null || _d === void 0 ? void 0 : _d.name) !== null && _e !== void 0 ? _e : '') : '';
        await this.mcpServer.handleRequest(req, res, {
            userId: auth.sub,
            sessionId: String(auth.sessionId),
            locale,
            toolName,
        });
    }
};
exports.McpController = McpController;
__decorate([
    (0, common_1.All)(),
    __param(0, (0, common_1.Req)()),
    __param(1, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object]),
    __metadata("design:returntype", Promise)
], McpController.prototype, "handle", null);
exports.McpController = McpController = __decorate([
    (0, api_1.NoRole)(),
    (0, common_1.UseGuards)(mcp_auth_guard_1.McpAuthGuard, mcp_permission_guard_1.McpPermissionGuard),
    (0, common_1.Controller)('mcp'),
    __metadata("design:paramtypes", [mcp_server_service_1.McpServerService,
        setting_service_1.SettingService])
], McpController);
//# sourceMappingURL=mcp.controller.js.map