"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatter = void 0;
const colorize_1 = require("./colorize");
const util_1 = require("util");
const utils = __importStar(require("@applitools/utils"));
const defaultColorTheme = {
    label: 'cyan',
    tags: 'blueBright',
    timestamp: 'greenBright',
    level: {
        info: ['bgBlueBright', 'black'],
        warn: ['bgYellowBright', 'black'],
        error: ['bgRedBright', 'white'],
        fatal: ['bgRed', 'white'],
    },
};
function formatter(chunks, { prelude = true, label, timestamp = new Date(), level = 'info', tags, colors } = {}) {
    var _a;
    if (utils.types.isBoolean(colors))
        colors = colors ? defaultColorTheme : undefined;
    const message = [];
    if (prelude) {
        if (label) {
            const text = label;
            const color = colors === null || colors === void 0 ? void 0 : colors.label;
            message.push(color ? (0, colorize_1.colorize)(text, { color }) : text);
        }
        if (tags && !utils.types.isEmpty(tags)) {
            const text = `(${utils.types.isArray(tags[0]) ? tags.map(tags => tags.join('/')).join(' & ') : tags.join('/')})`;
            const color = colors === null || colors === void 0 ? void 0 : colors.tags;
            message.push(color ? (0, colorize_1.colorize)(text, { color }) : text);
        }
        if (!colors && (label || (tags && !utils.types.isEmpty(tags)))) {
            message.push('|');
        }
        if (timestamp) {
            timestamp = timestamp === true ? new Date() : timestamp;
            const text = timestamp.toISOString();
            const color = colors === null || colors === void 0 ? void 0 : colors.timestamp;
            message.push(color ? (0, colorize_1.colorize)(text, { color }) : text);
        }
        if (level) {
            const text = level.toUpperCase().padEnd(5);
            const color = (_a = colors === null || colors === void 0 ? void 0 : colors.level) === null || _a === void 0 ? void 0 : _a[level];
            message.push(color ? (0, colorize_1.colorize)(` ${text} `, { color }) : `[${text}]`);
        }
    }
    if (chunks && chunks.length > 0) {
        const color = colors === null || colors === void 0 ? void 0 : colors.message;
        const strings = chunks.map(chunk => {
            return utils.types.isString(chunk)
                ? (0, colorize_1.colorize)(chunk, { color })
                : util_1.inspect === null || util_1.inspect === void 0 ? void 0 : (0, util_1.inspect)(chunk, { colors: Boolean(colors), compact: 5, depth: 5 });
        });
        message.push(strings.join(' '));
    }
    return message.join(' ');
}
exports.formatter = formatter;
