"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMablTestsOutputSchema = void 0;
const common_1 = require("../utils/common");
const testUtils_1 = require("../utils/testUtils");
const mcpMablTool_1 = require("./mcpMablTool");
const zod_1 = require("zod");
exports.getMablTestsOutputSchema = zod_1.z.object({
    tests: zod_1.z.array(zod_1.z.object({
        id: zod_1.z.string().optional(),
        name: zod_1.z.string().optional(),
        description: zod_1.z.string().optional(),
        type: zod_1.z.string().optional(),
        stepsChunks: zod_1.z
            .array(zod_1.z.object({
            annotatedStepDescriptions: zod_1.z.string().optional(),
            context: zod_1.z.string().optional(),
        }))
            .optional(),
        viewTestUrl: zod_1.z.string().optional(),
    })),
});
const getMablTests = (0, mcpMablTool_1.defineTool)({
    schema: {
        name: 'get_mabl_tests',
        title: 'Get mabl tests based on a query',
        description: 'Get an array of mabl tests based on a query. Example: Get mabl datatable tests',
        inputSchema: zod_1.z.object({
            query: zod_1.z.string().describe('The query to search for tests'),
            applicationId: zod_1.z
                .string()
                .describe('The id of the application to search for tests if the mabl application is in the context')
                .optional(),
        }),
        type: 'readOnly',
        outputSchema: exports.getMablTestsOutputSchema,
    },
    handle: async (params, context) => {
        const limit = 50;
        const { mablApiClient, workspaceId } = context.getCurrentContext();
        if (!params.query) {
            return (0, common_1.stringToToolResult)('No query provided', true);
        }
        const tests = await mablApiClient.queryTests(workspaceId, params.query, limit, params.applicationId);
        const testsArray = tests.tests.map((test) => ({
            id: test.test_invariant_id ?? '',
            name: test.test_name ?? '',
            description: test.test_description ?? '',
            type: test.test_type ?? '',
            stepsChunks: test.test_step_chunks.map((stepChunk) => ({
                annotatedStepDescriptions: stepChunk.annotated_step_descriptions ?? '',
                context: stepChunk.context ?? '',
            })),
            viewTestUrl: (0, testUtils_1.getViewTestUrl)(workspaceId, test.test_invariant_id),
        }));
        return {
            content: [
                {
                    type: 'text',
                    text: (0, common_1.jsonPrettyPrint)(testsArray),
                },
            ],
            structuredContent: { tests: testsArray },
        };
    },
});
exports.default = [getMablTests];
