"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.logTestResults = logTestResults;
exports.getPlatformFromTestRun = getPlatformFromTestRun;
const chalk_1 = __importDefault(require("chalk"));
const loggingProvider_1 = require("@mablhq-internal/runtime/providers/logging/loggingProvider");
const reporter_1 = require("@mablhq-internal/runtime/reporters/reporter");
const testsUtil_1 = require("@mablhq-internal/runtime/core/execution/testsUtil");
const mobileUtil_1 = require("@mablhq-internal/mobile-runtime/execution/mobile/util/mobileUtil");
async function logTestResults(results, parsed, commandStartTime, rerunCommandTemplate, apiClient, browser, verbose) {
    loggingProvider_1.logger.logNewLine();
    loggingProvider_1.logger.info(`Test count: ${results.numTotalTests}`);
    loggingProvider_1.logger.info(`Passed: ${results.numPassedTests}`);
    if (results.numPassedTests) {
        results.testResults.forEach((result) => {
            if (result.status === 'passed') {
                loggingProvider_1.logger.info(` - ${getTestResultDescription(result)} (${(0, testsUtil_1.calculateTotalTimeSeconds)(result.startTime, result.endTime)}s)`);
            }
        });
    }
    loggingProvider_1.logger.info(`Skipped: ${results.numSkippedTests}`);
    if (results.numSkippedTests) {
        results.testResults.forEach((result) => {
            if (result.status === 'skipped') {
                loggingProvider_1.logger.info(chalk_1.default.yellow(` - ${getTestResultDescription(result)}`));
            }
        });
    }
    loggingProvider_1.logger.info(`Failed: ${results.numFailedTests}`);
    if (results.numFailedTests) {
        results.testResults.forEach((result) => {
            if (result.status === 'failed') {
                loggingProvider_1.logger.info(chalk_1.default.red(` - ${getTestResultDescription(result)} (${(0, testsUtil_1.calculateTotalTimeSeconds)(result.startTime, result.endTime)}s)`));
            }
        });
        if (parsed.labels || parsed['from-plan-id'] || process.env.CI) {
            loggingProvider_1.logger.info(`Rerun any failed test with the following command template:`);
            loggingProvider_1.logger.info(`${chalk_1.default.magenta(rerunCommandTemplate)}`);
        }
    }
    loggingProvider_1.logger.info(`Total time: ${(0, testsUtil_1.calculateTotalTimeSeconds)(commandStartTime, Date.now())} seconds`);
    if (parsed.reporter) {
        await (0, reporter_1.handleReportingForTestsRun)(parsed.reporter, results, apiClient, parsed['reporter-options'], browser, parsed.fromPlanId, parsed.environmentId, parsed.applicationId, verbose);
    }
}
function getTestResultDescription(result) {
    return `${result.testName}${result.scenarioName ? ` - Scenario: ${result.scenarioName}` : ''}`;
}
function getPlatformFromTestRun(testRun) {
    const platform = testRun.journey_parameters?.mobile_device?.platform;
    if (!platform) {
        throw new Error(`Unable to determine mobile platform from test run: ${testRun.id}`);
    }
    return (0, mobileUtil_1.parsePlatformName)(platform);
}
