"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MobileTestsRunner = void 0;
exports.createMobileTestsRunner = createMobileTestsRunner;
const execution_1 = require("@mablhq-internal/runtime/execution");
const MobileTestRunner_1 = require("./MobileTestRunner");
const chalk_1 = __importDefault(require("chalk"));
const messaging_1 = require("@mablhq-internal/runtime/core/messaging/messaging");
async function createMobileTestsRunner(testRunConfig, apiClient, observerConfig) {
    const runner = new MobileTestsRunner(testRunConfig, observerConfig);
    await runner.initializeTestRunner(apiClient);
    return runner;
}
class MobileTestsRunner extends execution_1.MablTestsRunner {
    constructor(testRunConfig, observerConfig) {
        super(testRunConfig, observerConfig);
    }
    async initializeTestRunner(apiClient, executionMessage, preResolvedApplication) {
        if (!apiClient) {
            await this.initApiClient();
        }
        if (this.testRunConfig.runId) {
            messaging_1.mablEventEmitter.log(chalk_1.default.yellow(`Warning: A test run ID was supplied. Mabl will ensure the same mobile app file 
          and platform is used for the test run but can't ensure the same mobile device is used
          as the one used in the cloud. A default device from the machine will be used 
          unless device info is supplied.`));
            const testRun = await this.apiClient.getJourneyRun(this.testRunConfig.runId);
            const testRunPlatform = testRun?.journey_parameters?.mobile_device?.platform;
            const appFileId = testRun?.journey_parameters?.deployment?.mobile_app_file_id;
            if (!this.testRunConfig.mobileConfig?.platformName) {
                this.testRunConfig.mobileConfig = {
                    ...this.testRunConfig.mobileConfig,
                    platformName: testRunPlatform,
                };
            }
            if (!this.testRunConfig.mobileConfig?.mobileAppFileId &&
                !this.testRunConfig.mobileConfig?.path) {
                this.testRunConfig.mobileConfig = {
                    ...this.testRunConfig.mobileConfig,
                    mobileAppFileId: appFileId,
                };
            }
        }
        await super.initializeTestRunner(apiClient, executionMessage, preResolvedApplication);
    }
    createTestRunner(mablTestRunnerConfig) {
        if (!mablTestRunnerConfig.testRunConfig.useTestExecutionProxy &&
            mablTestRunnerConfig.testRunConfig.isCloudRun) {
            mablTestRunnerConfig.testRunConfig.useTestExecutionProxy = true;
        }
        return new MobileTestRunner_1.MobileTestRunner(mablTestRunnerConfig);
    }
}
exports.MobileTestsRunner = MobileTestsRunner;
