"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAllMessages = exports.getErrors = exports.displayResults = exports.executionFailed = exports.getExecutionStatistics = exports.getStatusReports = void 0;
const types_1 = require("./types");
const helpers_1 = require("./helpers");
const _collection = require("lodash/collection");
const chalk = require("chalk").default ?? require("chalk");
const urlMap = {
    job: "scheduler",
    project: "projects",
    script: "scripts",
};
const colorByStatus = (status, message) => {
    const colors = {
        ERROR: chalk.red,
        WARNING: chalk.yellow,
        SUCCESS: chalk.green,
    };
    const colorize = colors[status] ?? ((text) => text);
    return colorize(message || status);
};
const getDuration = (duration) => parseInt(duration, 10) / 1000;
const createLogUrl = (host, scriptId, sessionId, scriptLogId) => `${host.replace("/api", "").replace("001", "002")}/scripts/${scriptId}/history/${sessionId}/${scriptLogId}`;
const createEntityUrl = (host, type, id) => `${host}${urlMap[type]}/${id}`;
const getStatusCount = (executionOutput, status) => executionOutput.filter((log) => log.status === status).length;
const getMessages = (stepLogs) => stepLogs
    .filter((stepLog) => stepLog.messages && stepLog.messages.length > 0)
    .map((stepLog) => stepLog.messages
    .map((message) => `STEP ${stepLog.label} MESSAGE [${message.type}] ${JSON.stringify(message.text)}`)
    .join("\n"))
    .join("\n");
const getStatusReports = (executionOutput) => {
    const grouped = _collection.groupBy(executionOutput, "status");
    const host = (0, helpers_1.getEaApiUrl)();
    return Object.keys(grouped).map((status) => {
        const logs = grouped[status];
        return {
            status: status,
            logs: `${logs
                .map((scriptLog) => `SCRIPT [${scriptLog.scriptId}] ${scriptLog.name}\nDURATION ${getDuration(scriptLog.duration)}sec\nURL ${createLogUrl(host, scriptLog.scriptId, scriptLog.sessionId, scriptLog.id)} ${status === "ERROR" || status === "WARNING"
                ? `\n${getMessages(scriptLog.stepLogs)}`
                : ""}`)
                .join("\n")}`,
        };
    });
};
exports.getStatusReports = getStatusReports;
const getReportHeader = ({ type, id }) => {
    const eaApiUrl = (0, helpers_1.getEaApiUrl)();
    return `EXECUTION REPORT FOR ${type} ${id} URL: ${createEntityUrl(eaApiUrl, type, id)}`;
};
const displayStatusReport = (statusReport) => {
    console.log(colorByStatus(statusReport.status), "\n");
    console.log(colorByStatus(statusReport.status, statusReport.logs), "\n");
};
const getExecutionStatistics = (executionOutput) => `TOTAL: ${executionOutput.length} ${colorByStatus(types_1.ExecutionStatus.SUCCESS)}: ${getStatusCount(executionOutput, types_1.ExecutionStatus.SUCCESS)} ${colorByStatus(types_1.ExecutionStatus.ERROR)}: ${getStatusCount(executionOutput, types_1.ExecutionStatus.ERROR)} ${colorByStatus(types_1.ExecutionStatus.WARNING)}: ${getStatusCount(executionOutput, types_1.ExecutionStatus.WARNING)}`;
exports.getExecutionStatistics = getExecutionStatistics;
const executionFailed = (executionOutput) => executionOutput
    ? executionOutput.filter((scriptLog) => scriptLog.status === types_1.ExecutionStatus.ERROR).length > 0
    : true;
exports.executionFailed = executionFailed;
const displayResults = (executionOutput, executionParams) => {
    console.log("\n\n", getReportHeader(executionParams), "\n");
    console.log((0, exports.getExecutionStatistics)(executionOutput), "\n");
    const statusReports = (0, exports.getStatusReports)(executionOutput);
    statusReports.forEach((statusReport) => {
        displayStatusReport(statusReport);
    });
};
exports.displayResults = displayResults;
const getErrors = (executionOutput) => {
    return executionOutput.stepLogs
        .filter((stepLog) => stepLog.messages &&
        stepLog.messages.length > 0 &&
        stepLog.messages.some((m) => m.type === types_1.MESSAGE_TYPE.ERROR))
        .map((stepLog) => {
        return `${stepLog.messages
            .filter((message) => message.type === types_1.MESSAGE_TYPE.ERROR)
            .map((message) => `STEP ${stepLog.lineNum}. ${stepLog.label} \n ${JSON.stringify(message.text)}`)
            .join("\n")}`;
    })
        .join("\n");
};
exports.getErrors = getErrors;
const getAllMessages = (executionOutput) => {
    return executionOutput.stepLogs
        .filter((stepLog) => stepLog.messages && stepLog.messages.length > 0)
        .map((stepLog) => {
        return `STEP ${stepLog.lineNum}. ${stepLog.label} \n ${stepLog.messages
            .map((message) => `MESSAGE [${message.type}] ${JSON.stringify(message.text)}`)
            .join("\n")}`;
    })
        .join("\n");
};
exports.getAllMessages = getAllMessages;
//# sourceMappingURL=report.js.map