"use strict";
const log4js = require("log4js");
const util = require("util");
const stream = require("stream");
const marked = require("marked");
let TerminalRenderer = require("marked-terminal");
let chalk = require("chalk");
class Logger {
    constructor($config, $options) {
        this.$options = $options;
        this.log4jsLogger = null;
        this.encodeRequestPaths = ['/appbuilder/api/itmstransporter/applications?username='];
        this.encodeBody = false;
        this.passwordRegex = /[Pp]assword=(.*?)(['&,]|$)|\"[Pp]assword\":\"(.*?)\"/;
        this.requestBodyRegex = /^\"(.*?)\"$/;
        let appenders = [];
        if (!$config.CI_LOGGER) {
            appenders.push({
                type: "console",
                layout: {
                    type: "messagePassThrough"
                }
            });
        }
        log4js.configure({ appenders: appenders });
        this.log4jsLogger = log4js.getLogger();
        if (this.$options.log) {
            this.log4jsLogger.setLevel(this.$options.log);
        }
        else {
            this.log4jsLogger.setLevel($config.DEBUG ? "TRACE" : "INFO");
        }
    }
    setLevel(level) {
        this.log4jsLogger.setLevel(level);
    }
    getLevel() {
        return this.log4jsLogger.level.toString();
    }
    fatal(...args) {
        this.log4jsLogger.fatal.apply(this.log4jsLogger, args);
    }
    error(...args) {
        let message = util.format.apply(null, args);
        let colorizedMessage = message.red;
        this.log4jsLogger.error.apply(this.log4jsLogger, [colorizedMessage]);
    }
    warn(...args) {
        let message = util.format.apply(null, args);
        let colorizedMessage = message.yellow;
        this.log4jsLogger.warn.apply(this.log4jsLogger, [colorizedMessage]);
    }
    warnWithLabel(...args) {
        let message = util.format.apply(null, args);
        this.warn(`${Logger.LABEL} ${message}`);
    }
    info(...args) {
        this.log4jsLogger.info.apply(this.log4jsLogger, args);
    }
    debug(...args) {
        let encodedArgs = this.getPasswordEncodedArguments(args);
        this.log4jsLogger.debug.apply(this.log4jsLogger, encodedArgs);
    }
    trace(...args) {
        let encodedArgs = this.getPasswordEncodedArguments(args);
        this.log4jsLogger.trace.apply(this.log4jsLogger, encodedArgs);
    }
    out(...args) {
        console.log(util.format.apply(null, args));
    }
    write(...args) {
        process.stdout.write(util.format.apply(null, args));
    }
    prepare(item) {
        if (typeof item === "undefined" || item === null) {
            return "[no content]";
        }
        if (typeof item === "string") {
            return item;
        }
        if (item instanceof stream.Readable) {
            return "[ReadableStream]";
        }
        return JSON.stringify(item);
    }
    printInfoMessageOnSameLine(message) {
        if (!this.$options.log || this.$options.log === "info") {
            this.write(message);
        }
    }
    printMsgWithTimeout(message, timeout) {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                this.printInfoMessageOnSameLine(message);
                resolve();
            }, timeout);
        });
    }
    printMarkdown(...args) {
        let opts = {
            unescape: true,
            link: chalk.red,
            tableOptions: {
                chars: { 'mid': '', 'left-mid': '', 'mid-mid': '', 'right-mid': '' },
                style: {
                    'padding-left': 1,
                    'padding-right': 1,
                    head: ['green', 'bold'],
                    border: ['grey'],
                    compact: false
                }
            }
        };
        marked.setOptions({ renderer: new TerminalRenderer(opts) });
        let formattedMessage = marked(util.format.apply(null, args));
        this.write(formattedMessage);
    }
    getPasswordEncodedArguments(args) {
        return _.map(args, argument => {
            if (typeof argument !== 'string') {
                return argument;
            }
            let passwordMatch = this.passwordRegex.exec(argument);
            if (passwordMatch) {
                let password = passwordMatch[1] || passwordMatch[3];
                return this.getHiddenPassword(password, argument);
            }
            if (this.encodeBody) {
                let bodyMatch = this.requestBodyRegex.exec(argument);
                if (bodyMatch) {
                    return this.getHiddenPassword(bodyMatch[1], argument);
                }
            }
            _.each(this.encodeRequestPaths, path => {
                if (argument.indexOf('path') > -1) {
                    this.encodeBody = argument.indexOf(path) > -1;
                    return false;
                }
            });
            return argument;
        });
    }
    getHiddenPassword(password, originalString) {
        password = password || '';
        return originalString.replace(password, new Array(password.length + 1).join('*'));
    }
}
Logger.LABEL = "[WARNING]:";
exports.Logger = Logger;
$injector.register("logger", Logger);
