"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultTestToolResult = exports.performanceTestToolResult = exports.environmentRequiredToolResult = void 0;
const mcpMablTool_1 = require("./mcpMablTool");
const zod_1 = require("zod");
const constants_1 = require("@mablhq-internal/runtime/core/constants");
const mablApi_1 = require("@mablhq-internal/runtime/mablApi");
const flowResolver_1 = require("@mablhq-internal/runtime/util/flowResolver");
const export_1 = require("../../../tests/tests_cmds/export");
const common_1 = require("../utils/common");
const execution_1 = require("@mablhq-internal/runtime/execution");
exports.environmentRequiredToolResult = (0, common_1.stringToToolResult)(`
    ## Instructions
    Tell the user that the environmentId is required to export the test to Playwright with good locators.
    Call the get-environments tool to get the list of environments for the test.
    Ask them to select the environmentId from the list of environments and then call this tool again with the environmentId.
  `, true);
exports.performanceTestToolResult = (0, common_1.stringToToolResult)(`
    ## Instructions
    Tell the user that the test provided is a performance test and it can't be exported to playwright.
  `, true);
exports.defaultTestToolResult = (0, common_1.stringToToolResult)(`
    ## Instructions
    Tell the user that default tests can not be exported to playwright.
  `, true);
const exportToPlaywrightSchema = zod_1.z.object({
    testId: zod_1.z
        .string()
        .describe('The id of the mabl test to export to Playwright'),
    environmentId: zod_1.z
        .string()
        .optional()
        .describe('The id of the mabl environment'),
});
const exportToPlaywright = (0, mcpMablTool_1.defineTool)({
    schema: {
        name: 'export_to_playwright',
        title: 'Export a mabl test to Playwright',
        description: 'Export a mabl test to Playwright, you need to provide the testId and optionally the environmentId. Example: Export mabl test "Login in mabl" to Playwright using the environment Development',
        inputSchema: exportToPlaywrightSchema,
        type: 'readOnly',
    },
    handle: async (params, context) => {
        const { mablApiClient } = context.getCurrentContext();
        const testId = params.testId;
        const environmentId = params.environmentId;
        if (!environmentId) {
            return exports.environmentRequiredToolResult;
        }
        const journey = await mablApiClient.getJourney(testId, constants_1.DefaultBranchName, constants_1.OutputFormats.Playwright);
        if (journey.test_type === mablApi_1.TestTypeEnum.Performance) {
            return exports.performanceTestToolResult;
        }
        const flowArray = await (0, flowResolver_1.getJourneyFlowArray)(journey, mablApiClient, constants_1.DefaultBranchName);
        if (flowArray
            .map((flow) => flow.flow_type === mablApi_1.FlowTypeEnum.Mablscript ||
            flow.flow_type === mablApi_1.FlowTypeEnum.Api ||
            flow.flow_type === mablApi_1.FlowTypeEnum.Mobile)
            .includes(false)) {
            return exports.defaultTestToolResult;
        }
        const nestedFlows = await (0, flowResolver_1.resolveAllNestedFlowReferences)(flowArray, mablApiClient, constants_1.DefaultBranchName);
        const code = await (0, export_1.getPlaywrightTest)(journey, flowArray, mablApiClient, execution_1.playwrightExportFunctionTemplate, environmentId, nestedFlows);
        return {
            content: [
                {
                    type: 'text',
                    text: `
          ## Instructions
          This is the code you are going to insert into the user's playwright test file.
          ${code}

          This code also needs a function called replaceMablVariables. If the replaceMablVariables is not in the file, add this:

          ${execution_1.playwrightExportExtraFunctionTemplate}
          `,
                },
            ],
        };
    },
});
exports.default = [exportToPlaywright];
