"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatResultsAsGithubMarkdown = formatResultsAsGithubMarkdown;
exports.humanizeMablStatus = humanizeMablStatus;
exports.humanizedMablStatusToGithubIcon = humanizedMablStatusToGithubIcon;
exports.maybeCapitalize = maybeCapitalize;
exports.journeyRunToElapsedTime = journeyRunToElapsedTime;
exports.summaryToElapsedTime = summaryToElapsedTime;
const change_case_1 = require("change-case");
const mablApi_1 = require("../mablApi");
const timeUtil_1 = require("./timeUtil");
const table = require('markdown-table');
var HumanizedMablStatuses;
(function (HumanizedMablStatuses) {
    HumanizedMablStatuses["Failed"] = "Failed";
    HumanizedMablStatuses["Passed"] = "Passed";
    HumanizedMablStatuses["RateLimited"] = "Quota Reached";
    HumanizedMablStatuses["Running"] = "Running";
    HumanizedMablStatuses["Stopped"] = "Stopped";
})(HumanizedMablStatuses || (HumanizedMablStatuses = {}));
function formatResultsAsGithubMarkdown(results) {
    const tableRows = [];
    tableRows.push(['Plan', 'Test', 'Browser', 'Status', 'Run Time']);
    const finalExecutions = results?.executions ?? [];
    finalExecutions.forEach((summary) => {
        const retryPrefix = summary?.plan_execution?.is_retry ? '[Retry] ' : '';
        const planName = retryPrefix + (summary.plan?.name ?? '<plan name unavailable>');
        const humanizedPlanRunStatus = humanizeMablStatus(summary?.plan_execution?.status);
        const planRunStatus = humanizedMablStatusToGithubIcon(humanizedPlanRunStatus) +
            ' ' +
            maybeCapitalize(humanizedPlanRunStatus?.toString());
        const planHref = summary.plan?.app_href;
        const journeyMap = new Map();
        summary.journeys?.forEach((journeySummary) => journeyMap.set(journeySummary.id, journeySummary));
        tableRows.push([
            `[${planName}](${planHref})`,
            '---',
            '---',
            planRunStatus,
            summaryToElapsedTime(summary),
        ]);
        summary.journey_executions?.forEach((journeyRun) => {
            const humanizedJourneyRunStatus = humanizeMablStatus(journeyRun.status);
            const journeyStatus = humanizedMablStatusToGithubIcon(humanizedJourneyRunStatus) +
                ' ' +
                maybeCapitalize(humanizedJourneyRunStatus?.toString());
            const journeyBrowser = maybeCapitalize(journeyRun.browser_type);
            const journeySummary = journeyMap.get(journeyRun.journey_id);
            let journeyLabel = '<test name unavailable>';
            if (journeySummary) {
                journeyLabel = `[${retryPrefix}${journeySummary.name}](${journeySummary.app_href})`;
            }
            tableRows.push([
                '',
                journeyLabel,
                journeyBrowser,
                `[${journeyStatus}](${journeyRun.app_href})`,
                journeyRunToElapsedTime(journeyRun),
            ]);
        });
    });
    return table(tableRows, {
        align: ['l', 'l', 'l', 'l'],
    });
}
function humanizeMablStatus(status) {
    if (status) {
        switch (status) {
            case mablApi_1.TestRunResult.StatusEnum.Completed:
            case mablApi_1.PlanExecutionResult.StatusEnum.Completed:
            case mablApi_1.PlanExecutionResult.StatusEnum.Succeeded:
                return HumanizedMablStatuses.Passed;
            case mablApi_1.TestRunResult.StatusEnum.Failed:
            case mablApi_1.PlanExecutionResult.StatusEnum.Failed:
                return HumanizedMablStatuses.Failed;
            case mablApi_1.TestRunResult.StatusEnum.Terminated:
            case mablApi_1.PlanExecutionResult.StatusEnum.Terminated:
            case mablApi_1.PlanExecutionResult.StatusEnum.Cancelled:
                return HumanizedMablStatuses.Stopped;
            case mablApi_1.TestRunResult.StatusEnum.Queued:
            case mablApi_1.PlanExecutionResult.StatusEnum.Queued:
            case mablApi_1.PlanExecutionResult.StatusEnum.Scheduled:
            case mablApi_1.PlanExecutionResult.StatusEnum.Scheduling:
            case mablApi_1.TestRunResult.StatusEnum.Running:
            case mablApi_1.PlanExecutionResult.StatusEnum.PostExecution:
            case mablApi_1.PlanExecutionResult.StatusEnum.PreExecution:
                return HumanizedMablStatuses.Running;
            case mablApi_1.TestRunResult.StatusEnum.RateLimited:
                return HumanizedMablStatuses.RateLimited;
        }
    }
    return;
}
function humanizedMablStatusToGithubIcon(status) {
    if (!status) {
        return '';
    }
    switch (status) {
        case HumanizedMablStatuses.Passed:
            return ':heavy_check_mark:';
        case HumanizedMablStatuses.Failed:
            return ':x:';
        case HumanizedMablStatuses.Stopped:
            return ':large_orange_diamond:';
        case HumanizedMablStatuses.Running:
            return '🔃';
        default:
            throw new Error(`Unhandled humanized status [${status}]`);
    }
}
function maybeCapitalize(input) {
    if (input) {
        return (0, change_case_1.capitalCase)(input.replace('_', ' '));
    }
    return '';
}
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);
}
