"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Log = void 0;
var colors_1 = require("colors");
var Log = /** @class */ (function () {
    function Log(verbose) {
        this.verbose = verbose;
    }
    Log.prototype.error = function (message) { this.log(colors_1.red(message)); };
    Log.prototype.info = function (message) { this.log(colors_1.yellow(message)); };
    Log.prototype.success = function (message) { this.log(colors_1.green(message)); };
    Log.prototype.debug = function (message) { this.verbose && this.log(colors_1.dim(message)); };
    Log.prototype.outgoing = function (topic, payload) {
        this.log(colors_1.cyan(this.prettify('MESSAGE SENT', [
            ['TOPIC', topic],
            ['MESSAGE', this.prettyPayload(payload)],
        ])));
    };
    Log.prototype.incoming = function (topic, payload) {
        this.log(colors_1.magenta(this.prettify('MESSAGE RECEIVED', [
            ['TOPIC', topic],
            ['MESSAGE', this.prettyPayload(payload)],
        ])));
    };
    Log.prototype.prettify = function (header, body) {
        return "\n************** " + header + " ***********\n" + body.map(function (_a) {
            var key = _a[0], val = _a[1];
            return "" + ((key === null || key === void 0 ? void 0 : key.length) ? key + ": " + val : '');
        }).join('\n') + "\n**************" + '*'.repeat(header.length + 2) + "***********\n";
    };
    Log.prototype.log = function (coloredMessage) { console.log(coloredMessage + "\n"); };
    Log.prototype.prettyPayload = function (payload) {
        var _a;
        var isBuffer = (_a = payload) === null || _a === void 0 ? void 0 : _a.buffer;
        try {
            if (isBuffer) {
                var bufStr = payload.toString();
                var parsed = JSON.parse(bufStr);
                return JSON.stringify(parsed, null, 2);
            }
            return JSON.stringify(payload, null, 2);
        }
        catch (err) {
            this.debug("Error unwrapping payload. Error: \"" + err + "\". Payload: " + payload);
            // squelch error, this tells us payload is not valid JSON, just return payload
            return isBuffer ? payload.toString() : payload;
        }
    };
    return Log;
}());
exports.Log = Log;
