"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExecutionResultPresenter = void 0;
const change_case_1 = require("change-case");
const mablApi_1 = require("@mablhq-internal/runtime/mablApi");
const cli_table3_1 = __importDefault(require("cli-table3"));
const timeUtil_1 = require("@mablhq-internal/runtime/util/timeUtil");
const chalk = require('chalk');
class ExecutionResultPresenter {
    entityToString(results, columnWidth) {
        const columnLabels = [
            'Plan Name',
            'Test Name',
            'Id',
            'Browser',
            'Status',
            'Run Time',
        ];
        const tableConfig = {
            head: columnLabels,
        };
        if (columnWidth) {
            tableConfig.colWidths = Array(columnLabels.length - 1).fill(columnWidth);
        }
        const table = new cli_table3_1.default(tableConfig);
        const finalExecutions = results?.executions ?? [];
        finalExecutions.forEach((summary) => {
            const retryPrefix = summary?.plan_execution?.is_retry ? '[Retry] ' : '';
            const planId = summary.plan.id;
            const planName = retryPrefix + (summary.plan?.name ?? '<plan name unavailable>');
            const planRunStatus = summary.plan_execution.status;
            const journeyMap = new Map();
            summary?.journeys?.forEach((journeySummary) => journeyMap.set(journeySummary.id, journeySummary));
            const statusChalk = this.mapPlanRunStatusToColor(planRunStatus);
            table.push([
                `${planName}`,
                '---',
                planId,
                '---',
                statusChalk(this.maybeCapitalize(planRunStatus.toString())),
                summaryToElapsedTime(summary),
            ]);
            summary?.journey_executions?.forEach((journeyRun) => {
                const journeyId = journeyRun.journey_execution_id;
                const journeyStatus = this.maybeCapitalize(journeyRun.status?.toString() ?? '<status unavailable>');
                const journeyBrowser = this.maybeCapitalize(journeyRun.browser_type ?? '<unknown browser>');
                const journeySummary = journeyMap.get(journeyRun.journey_id);
                const journeyName = `${retryPrefix}${journeySummary?.name ?? '<test name >'}`;
                const statusChalk = this.mapJourneyRunStatusToColor(journeyRun.status);
                table.push([
                    '',
                    `${journeyName}`,
                    journeyId,
                    journeyBrowser,
                    statusChalk(journeyStatus),
                    journeyRunToElapsedTime(journeyRun),
                ]);
            });
        });
        return table.toString();
    }
    maybeCapitalize(input) {
        if (input) {
            return (0, change_case_1.capitalCase)(input.replace('_', ' '));
        }
        return;
    }
    mapPlanRunStatusToColor(status) {
        switch (status) {
            case mablApi_1.PlanExecutionResult.StatusEnum.Queued:
                return chalk.gray;
            case mablApi_1.PlanExecutionResult.StatusEnum.Scheduled:
                return chalk.white;
            case mablApi_1.PlanExecutionResult.StatusEnum.Succeeded:
            case mablApi_1.PlanExecutionResult.StatusEnum.Completed:
                return chalk.green;
            case mablApi_1.PlanExecutionResult.StatusEnum.Failed:
                return chalk.red;
            case mablApi_1.PlanExecutionResult.StatusEnum.Terminated:
            case mablApi_1.PlanExecutionResult.StatusEnum.Cancelled:
                return chalk.yellow;
            default:
                return chalk.reset;
        }
    }
    mapJourneyRunStatusToColor(status) {
        switch (status) {
            case mablApi_1.TestRunResult.StatusEnum.Queued:
            case mablApi_1.TestRunResult.StatusEnum.RateLimited:
                return chalk.gray;
            case mablApi_1.TestRunResult.StatusEnum.Running:
                return chalk.white;
            case mablApi_1.TestRunResult.StatusEnum.Completed:
                return chalk.green;
            case mablApi_1.TestRunResult.StatusEnum.Failed:
                return chalk.red;
            case mablApi_1.TestRunResult.StatusEnum.Terminated:
            case mablApi_1.TestRunResult.StatusEnum.Skipped:
                return chalk.yellow;
            default:
                return chalk.reset;
        }
    }
}
exports.ExecutionResultPresenter = ExecutionResultPresenter;
function journeyRunToElapsedTime(journeyRun) {
    return (0, timeUtil_1.elapsedTimeToString)(journeyRun.start_time, journeyRun.stop_time);
}
function summaryToElapsedTime(summary) {
    return (0, timeUtil_1.elapsedTimeToString)(summary.start_time, summary.stop_time);
}
