"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getXrayTestCasesOutputSchema = void 0;
const zod_1 = require("zod");
const mcpMablTool_1 = require("./mcpMablTool");
const common_1 = require("../utils/common");
const mablApi_1 = require("@mablhq-internal/runtime/mablApi");
const featureSet_1 = require("@mablhq-internal/runtime/api/featureSet");
exports.getXrayTestCasesOutputSchema = zod_1.z.object({
    xrayTests: zod_1.z.array(zod_1.z.object({
        issue_id: zod_1.z.string().optional(),
        project_id: zod_1.z.string().optional(),
        issue_key: zod_1.z.string().optional(),
        summary: zod_1.z.string().optional(),
        description: zod_1.z.string().optional(),
    })),
    xrayProjectId: zod_1.z.string().optional(),
    nextCursor: zod_1.z.string().optional(),
});
const getXrayTestCases = (0, mcpMablTool_1.defineTool)({
    schema: {
        name: 'get_xray_test_cases',
        title: 'Get all Xray test cases for a specific Xray project',
        description: 'Get Xray test cases for a specific Xray project. ' +
            'Returns test cases that are not already assigned to a mabl test. ' +
            'Results are paginated: if nextCursor is present in the response, ' +
            'pass it as the cursor parameter in a follow-up call to retrieve the next page.',
        inputSchema: zod_1.z.object({
            xrayProjectId: zod_1.z
                .string()
                .describe('The id of the Xray project to get test cases for, if not provided, all Xray projects will be queried and if there are multiple, the user will be prompted to select from which of these projects to get test cases')
                .optional(),
            limit: zod_1.z
                .number()
                .min(1)
                .max(100)
                .optional()
                .describe('Maximum number of test cases to return (default: 50)'),
            cursor: zod_1.z
                .string()
                .optional()
                .describe('The cursor to use for pagination, obtained from the nextCursor field of a previous response'),
            search: zod_1.z
                .string()
                .optional()
                .describe('Search string to filter test cases by key or text content'),
        }),
        outputSchema: exports.getXrayTestCasesOutputSchema,
        type: 'readOnly',
    },
    handle: async (params, context) => {
        const { mablApiClient, workspaceId } = context.getCurrentContext();
        let xrayProjectId = params.xrayProjectId;
        const limit = params.limit || 50;
        const cursor = params.cursor;
        const search = params.search;
        if (!context.featureIsAvailable(featureSet_1.XRAY_INTEGRATION_FEATURE_FLAG)) {
            return (0, common_1.stringToToolResult)('Xray integration is not available for your account. Please contact your workspace owner or mabl support for assistance in activating this feature.', true);
        }
        if (!xrayProjectId) {
            const { integrations } = await mablApiClient.queryIntegrations(workspaceId, mablApi_1.IntegrationTypeEnum.XraySync);
            const xrayProjects = integrations.filter((integration) => integration.enabled);
            if (!xrayProjects || xrayProjects.length === 0) {
                return (0, common_1.stringToToolResult)('There are no Xray projects configured for this workspace. Go into your workspace to set it up.', true);
            }
            if (xrayProjects.length > 1) {
                return (0, common_1.stringToToolResult)(`Multiple Xray projects found. Available projects: ${xrayProjects
                    .map((project) => `${project.name} (${project.id})`)
                    .join(', ')}`, true);
            }
            xrayProjectId = xrayProjects[0].id;
        }
        const result = await mablApiClient.queryXrayTests(xrayProjectId, search, limit, cursor);
        const { tests: xrayTests } = result;
        const start = result.start ?? 0;
        const total = result.total ?? 0;
        const nextCursor = start + limit < total ? String(start + limit) : undefined;
        const output = { xrayTests, xrayProjectId, nextCursor };
        return {
            content: [{ type: 'text', text: (0, common_1.jsonPrettyPrint)(output) }],
            structuredContent: output,
        };
    },
});
exports.default = [getXrayTestCases];
