import config from './config.js';

class Logger {
    static LEVELS = {
        debug: 0,
        info: 1,
        warn: 2,
        error: 3,
        none: 4
    };

    static currentLevel = Logger.LEVELS.debug;

    static setLevel(level) {
        if (level in Logger.LEVELS) {
            Logger.currentLevel = Logger.LEVELS[level];
        }
    }

    static shouldLog(level, instanceLevel) {
        return Logger.LEVELS[level] >= instanceLevel;
    }

    constructor(level = 'debug', scope = '') {
        this.level = Logger.LEVELS[level] ?? Logger.LEVELS.debug;
        this.scope = scope.toUpperCase();

        for (const levelName of Object.keys(Logger.LEVELS)) {
            if (levelName === 'none') continue;

            this[levelName] = (...args) => {
                if (Logger.shouldLog(levelName, this.level) && typeof console[levelName] === 'function') {
                    console[levelName](
                        `[${levelName.toUpperCase()}]${this.scope ? ` [${this.scope}]` : ''}`,
                        ...args
                    );
                }
            };
        }
    }
}

// Create a default global logger with no scope, using config value
const logger = new Logger(config.loggerLevel);

export default logger;
export { Logger };
