"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = exports.prefix = void 0;
const chalk_1 = require("chalk");
const util_1 = require("util");
exports.prefix = {
    info: chalk_1.green('[INFO]'),
    error: chalk_1.red('[ERROR]'),
};
class Logger {
    constructor() {
        this._logs = [];
    }
    setIO(io) {
        this._io = io;
    }
    get logs() {
        return this._logs;
    }
    log(scope) {
        const logLevel = ['silent', 'silly', 'debug', 'info', 'warn', 'error'].includes(process.env.MILA_LOG_LEVEL)
            ? process.env.MILA_LOG_LEVEL
            : 'silent';
        const inScope = process.env.MILA_DEBUG === '*' || (process.env.MILA_DEBUG && process.env.MILA_DEBUG.split(',').includes(scope));
        const isPrimitive = (arg) => typeof arg === 'string' || typeof arg === 'number' || typeof arg === 'boolean';
        const toEvent = (level, args) => ({
            level,
            date: new Date().toISOString(),
            scope,
            args: args.map((arg) => (isPrimitive(arg) ? arg : util_1.inspect(arg, null, 2, false))),
        });
        const toIO = (event) => {
            if (this._io) {
                this._io.eventLogAdded(event);
            }
        };
        return {
            silly: (...args) => {
                const event = toEvent('silly', args);
                if (['silly'].includes(logLevel) && inScope) {
                    console.debug(chalk_1.cyan('[SILLY]'), chalk_1.bold(scope), ...args);
                }
                this._logs.push(event);
                toIO(event);
            },
            debug: (...args) => {
                const event = toEvent('debug', args);
                if (['silly', 'debug'].includes(logLevel) && inScope) {
                    console.debug(chalk_1.blue('[DEBUG]'), chalk_1.bold(scope), ...args);
                }
                this._logs.push(event);
                toIO(event);
            },
            info: (...args) => {
                const event = toEvent('info', args);
                if (['silly', 'debug', 'info'].includes(logLevel)) {
                    console.info(exports.prefix.info, ...args);
                }
                this._logs.push(event);
                toIO(event);
            },
            warn: (...args) => {
                const event = toEvent('warn', args);
                if (['silly', 'debug', 'info', 'warn'].includes(logLevel)) {
                    console.info(chalk_1.yellow('[WARNING]', ...args));
                }
                this._logs.push(event);
                toIO(event);
            },
            error: (...args) => {
                const event = toEvent('error', args);
                if (['silly', 'debug', 'info', 'warn', 'error'].includes(logLevel)) {
                    console.info(exports.prefix.error, ...args);
                }
                this._logs.push(event);
                toIO(event);
            },
        };
    }
}
exports.Logger = Logger;
//# sourceMappingURL=logger.js.map