"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSearchWorkflowNodesTool = void 0;
const zod_1 = __importDefault(require("zod"));
const mcp_constants_1 = require("../../mcp.constants");
const constants_1 = require("./constants");
const inputSchema = {
    queries: zod_1.default
        .array(zod_1.default.string())
        .min(1)
        .describe('Search queries for n8n nodes — service names (e.g. "gmail", "slack"), trigger types (e.g. "schedule trigger", "webhook"), or utility nodes (e.g. "set", "if", "merge", "code")'),
};
const outputSchema = {
    results: zod_1.default
        .string()
        .describe('Search results with matching node IDs, discriminators, and related nodes'),
};
const createSearchWorkflowNodesTool = (user, workflowBuilderToolsService, telemetry) => ({
    name: constants_1.CODE_BUILDER_SEARCH_NODES_TOOL.toolName,
    config: {
        description: 'Search for n8n nodes by service name, trigger type, or utility function. Returns node IDs, discriminators (resource/operation/mode), and related nodes needed for get_workflow_node_types.',
        inputSchema,
        outputSchema,
        annotations: {
            title: constants_1.CODE_BUILDER_SEARCH_NODES_TOOL.displayTitle,
            readOnlyHint: true,
            destructiveHint: false,
            idempotentHint: true,
            openWorldHint: false,
        },
    },
    handler: async ({ queries }) => {
        const telemetryPayload = {
            user_id: user.id,
            tool_name: constants_1.CODE_BUILDER_SEARCH_NODES_TOOL.toolName,
            parameters: { queries },
        };
        try {
            const result = await workflowBuilderToolsService.searchNodes(queries);
            telemetryPayload.results = { success: true, data: { queryCount: queries.length } };
            telemetry.track(mcp_constants_1.USER_CALLED_MCP_TOOL_EVENT, telemetryPayload);
            return {
                content: [{ type: 'text', text: result }],
                structuredContent: { results: result },
            };
        }
        catch (error) {
            telemetryPayload.results = {
                success: false,
                error: error instanceof Error ? error.message : String(error),
            };
            telemetry.track(mcp_constants_1.USER_CALLED_MCP_TOOL_EVENT, telemetryPayload);
            throw error;
        }
    },
});
exports.createSearchWorkflowNodesTool = createSearchWorkflowNodesTool;
//# sourceMappingURL=search-workflow-nodes.tool.js.map