"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const zod_1 = require("zod");
const mcpMablTool_1 = require("./mcpMablTool");
const mablApi_1 = require("@mablhq-internal/runtime/mablApi");
const common_1 = require("../utils/common");
const featureSet_1 = require("@mablhq-internal/runtime/api/featureSet");
const assignXrayToMablTest = (0, mcpMablTool_1.defineTool)({
    schema: {
        name: 'assign_xray_to_mabl_test',
        title: 'Assign an Xray test case to a mabl test',
        description: 'Assign an Xray test case to a mabl test. Example: Assign the Xray test case 123 to the mabl test 456',
        inputSchema: zod_1.z.object({
            mablTestId: zod_1.z
                .string()
                .min(1)
                .describe('The id of the mabl test to assign the Xray test case to'),
            xrayTestCase: zod_1.z
                .object({
                issue_id: zod_1.z.string().min(1),
                issue_key: zod_1.z.string().min(1),
                summary: zod_1.z.string().min(1),
            })
                .describe('The Xray test case to assign to the mabl test, with the issue_id, issue_key, and summary; all of these fields come from #get_xray_test_cases'),
            xrayProjectId: zod_1.z
                .string()
                .min(1)
                .describe('The id of the Xray project that the Xray test case belongs to, it is required to assign the Xray test case to the mabl test. This value comes from #get_xray_test_cases'),
            overwrite: zod_1.z
                .boolean()
                .describe('Whether to overwrite the existing Xray test case assignment to the mabl test. If true, the existing Xray test case assignment will be overwritten. If false, the existing Xray test case assignment will be kept.')
                .default(false),
        }),
        type: 'destructive',
    },
    handle: async (params, context) => {
        const { mablApiClient } = context.getCurrentContext();
        const { mablTestId, xrayTestCase, xrayProjectId, overwrite } = params;
        const { issue_id: xrayIssueId } = xrayTestCase;
        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);
        }
        const test = await mablApiClient.getJourney(mablTestId);
        if (test.test_type === mablApi_1.TestTypeEnum.Performance) {
            return (0, common_1.stringToToolResult)('Performance tests are not supported to be assigned to Xray', true);
        }
        const testId = test.invariant_id;
        const mablEntityType = mablApi_1.MablEntityTypeEnum.Test;
        const externalTcmPlatform = mablApi_1.ExternalTcmPlatformTypeEnum.Xray;
        const externalEntityId = xrayIssueId;
        const workspaceIntegrationId = xrayProjectId;
        if (!testId) {
            return (0, common_1.stringToToolResult)('The mabl test is missing an invariant id', true);
        }
        const xrayBinding = await mablApiClient.getExternalTcmBindingById(testId);
        if (xrayBinding && !overwrite) {
            return (0, common_1.stringToToolResult)(`This mabl test already has an Xray test case assigned (Xray Issue ID: ${xrayBinding.external_entity_key}). To replace it, call this tool again with overwrite set to true.`, true);
        }
        if (xrayBinding) {
            await mablApiClient.deleteExternalTcmBinding(externalTcmPlatform, mablEntityType, testId);
        }
        await mablApiClient.createExternalTcmBinding({
            external_entity_id: externalEntityId,
            mabl_entity_type: mablEntityType,
            mabl_entity_id: testId,
            workspace_integration_id: workspaceIntegrationId,
            external_tcm_platform: externalTcmPlatform,
        });
        return {
            content: [
                {
                    type: 'text',
                    text: `The Xray test case has been assigned to the mabl test case successfully. After the assignment, consider the previous response from #get_xray_test_cases stale, and when asked for new information call #get_xray_test_cases again to get the latest information.`,
                },
            ],
        };
    },
});
exports.default = [assignXrayToMablTest];
