import dotenv from 'dotenv';

dotenv.config();

export enum LogLevel {
    NONE = 0,
    ERROR = 1,
    WARN = 2,
    INFO = 3,
    DEBUG = 4,
}

const getLogLevelFromString = (level: string | undefined): LogLevel => {
    if (!level) {
        return LogLevel.INFO;
    }
    switch (level.toUpperCase()) {
        case 'NONE':
            return LogLevel.NONE;
        case 'ERROR':
            return LogLevel.ERROR;
        case 'WARN':
            return LogLevel.WARN;
        case 'INFO':
            return LogLevel.INFO;
        case 'DEBUG':
            return LogLevel.DEBUG;
        default:
            return LogLevel.INFO;
    }
};

let currentLogLevel: LogLevel = getLogLevelFromString(process.env.LOG_LEVEL);

export function setLogLevel(level: LogLevel) {
    currentLogLevel = level;
}

export const logger = {
    error: (...args: any[]) => {
        if (currentLogLevel >= LogLevel.ERROR) {
            console.error(...args);
        }
    },
    warn: (...args: any[]) => {
        if (currentLogLevel >= LogLevel.WARN) {
            console.warn(...args);
        }
    },
    info: (...args: any[]) => {
        if (currentLogLevel >= LogLevel.INFO) {
            console.info(...args);
        }
    },
    debug: (...args: any[]) => {
        if (currentLogLevel >= LogLevel.DEBUG) {
            console.debug(...args);
        }
    },
};
