"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 McpRegistryService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.McpRegistryService = void 0;
const common_1 = require("@nestjs/common");
const core_1 = require("@nestjs/core");
const mcp_tool_decorator_1 = require("./decorators/mcp-tool.decorator");
let McpRegistryService = McpRegistryService_1 = class McpRegistryService {
    constructor(discovery, reflector) {
        this.discovery = discovery;
        this.reflector = reflector;
        this.logger = new common_1.Logger(McpRegistryService_1.name);
        this.tools = new Map();
    }
    onModuleInit() {
        var _a;
        const providers = this.discovery.getProviders();
        for (const wrapper of providers) {
            const { instance } = wrapper;
            if (!instance || typeof instance !== 'object')
                continue;
            const prototype = Object.getPrototypeOf(instance);
            if (!prototype)
                continue;
            const methodNames = Object.getOwnPropertyNames(prototype).filter((key) => {
                if (key === 'constructor')
                    return false;
                const descriptor = Object.getOwnPropertyDescriptor(prototype, key);
                return descriptor !== undefined && typeof descriptor.value === 'function';
            });
            for (const methodName of methodNames) {
                const definition = this.reflector.get(mcp_tool_decorator_1.MCP_TOOL_METADATA, prototype[methodName]);
                if (!definition)
                    continue;
                if (this.tools.has(definition.name)) {
                    this.logger.warn(`Duplicate MCP tool ignored: ${definition.name} from ${(_a = prototype.constructor) === null || _a === void 0 ? void 0 : _a.name}.${methodName}`);
                    continue;
                }
                this.logger.log(`Registered MCP tool: ${definition.name}`);
                this.tools.set(definition.name, {
                    definition,
                    handler: instance[methodName].bind(instance),
                });
            }
        }
        this.logger.log(`McpRegistryService: ${this.tools.size} tools registered`);
    }
    getAll() {
        return Array.from(this.tools.values());
    }
    get(toolName) {
        return this.tools.get(toolName);
    }
    has(toolName) {
        return this.tools.has(toolName);
    }
};
exports.McpRegistryService = McpRegistryService;
exports.McpRegistryService = McpRegistryService = McpRegistryService_1 = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [core_1.DiscoveryService,
        core_1.Reflector])
], McpRegistryService);
//# sourceMappingURL=mcp-registry.service.js.map