"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogLineColor = void 0;
exports.registerConsoleLoggerForOutput = registerConsoleLoggerForOutput;
const messaging_1 = require("./messaging");
const moment = require("moment");
const chalk_1 = __importDefault(require("chalk"));
const env_1 = require("../../env/env");
const loggingProvider_1 = require("../../providers/logging/loggingProvider");
var LogLineColor;
(function (LogLineColor) {
    LogLineColor["cyan"] = "cyan";
    LogLineColor["magenta"] = "magenta";
    LogLineColor["red"] = "red";
    LogLineColor["white"] = "white";
    LogLineColor["yellow"] = "yellow";
})(LogLineColor || (exports.LogLineColor = LogLineColor = {}));
function registerConsoleLoggerForOutput(eventEmitter) {
    eventEmitter
        .getOutputEventChannel()
        .on(messaging_1.EventChannelMessageType.outputLogLine, (line, timestamp, color, metadata) => {
        const stepNumberPrefix = metadata?.stepNumberPrefix ?? '';
        const lineWithPrefix = stepNumberPrefix + line;
        const formatted = color ? chalk_1.default[color](lineWithPrefix) : lineWithPrefix;
        if (metadata?.shouldOutputInConsole === false) {
            return;
        }
        if (timestamp) {
            let formattedTimestamp = '';
            formattedTimestamp = `${moment
                .unix(timestamp / 1000)
                .format('LTS')} - `;
            loggingProvider_1.logger.info(`${chalk_1.default.yellow(formattedTimestamp)}${formatted}`);
        }
        else {
            loggingProvider_1.logger.info(formatted);
        }
    });
    if (env_1.ENV === 'local' && env_1.DEBUG_EVENT_EMITTER) {
        eventEmitter
            .getOutputEventChannel()
            .on(messaging_1.EventChannelMessageType.coreAction, (action) => {
            loggingProvider_1.logger.info(action);
        });
    }
}
