import { PluginType } from "@superblocksteam/shared";
import z from "zod";
import { UNSUPPORTED_INTEGRATIONS } from "../../../transform/api-builder/to-sdk-transformer.js";
import { createToolFactory, ToolCategory } from "../../tools2/types.js";
export const searchIntegrationsToolFactory = createToolFactory("searchIntegrations", (services) => {
    return {
        category: ToolCategory.METADATA,
        description: `
      Use this tool to search for third-party integrations configured in the user's application.
      This can include database integrations, API integrations, and other types of integrations that the user has connected to their application.
      You can search by integration name and/or type
    `,
        inputSchema: z.object({
            type: z
                .nativeEnum(PluginType)
                .optional()
                .describe("The type of integration to search for"),
            name: z
                .string()
                .optional()
                .describe("The name of the integration to search for"),
        }),
        execute: async (params) => {
            if (!params.type && !params.name) {
                throw new Error("Either type or name must be provided. Search again. Available types: " +
                    Object.values(PluginType).join(", "));
            }
            const integrations = await services.integrationStore.availableIntegrations();
            const filtered = !params.type && !params.name
                ? integrations
                : integrations
                    .filter((int) => params.type == undefined || int.plugin.type === params.type)
                    .filter((int) => {
                    if (params.name == undefined)
                        return true;
                    // Search in both name (slug) and displayName
                    const searchTerm = params.name.toLowerCase();
                    return (int.name.toLowerCase().includes(searchTerm) ||
                        int.displayName?.toLowerCase().includes(searchTerm));
                })
                    // Filter out integrations that are not yet supported by Clark's API generation
                    .filter((int) => !UNSUPPORTED_INTEGRATIONS.includes(int.plugin.id));
            // Return with displayName as "name" - so message shows original name
            return filtered.map(({ id, displayName, name, plugin }) => ({
                id,
                name: displayName || name,
                tagName: name,
                plugin,
            }));
        },
        readOnly: true,
        getSummary: (_input, output) => {
            const count = Array.isArray(output) ? output.length : 0;
            return {
                output: `${count} integration${count === 1 ? "" : "s"}`,
            };
        },
    };
});
//# sourceMappingURL=metadata.js.map