/**
 * @author    Martin Micunda {@link https://schedulino.com}
 * @copyright Copyright (c) 2016, Schedulino ltd.
 * @license   MIT
 *
 * The simple lambda logger that add the log level prefix to improve log filtering in CloudWatch.
 *
 * @module @schedulino/lambda-logger
 * @example
 *  ```js
 *      import logger from '@schedulino/lambda-logger';
 *      logger.config({ level: 'DEBUG' });
 *
 *      logger.info('Hi');
 *  ```
 */
class Logger {
    constructor(level = 'INFO') {
        this.levelValue = Logger.getLevelValue(level);
    }

    static getLevelValue(level) {
        let levelValue;
        switch (level) {
            case 'DEBUG':
                levelValue = 0;
                break;
            case 'INFO':
                levelValue = 1;
                break;
            case 'WARN':
                levelValue = 2;
                break;
            case 'ERROR':
                levelValue = 3;
                break;
            default:
                levelValue = 100;
        }

        return levelValue;
    }

    config(options) {
        this.levelValue = Logger.getLevelValue(options.level);
    }

    /* eslint-disable prefer-rest-params */
    debug() {
        if (this.levelValue <= 0) {
            console.log('DEBUG: ', ...arguments);
        }
    }

    info() {
        if (this.levelValue <= 1) {
            console.info('INFO: ', ...arguments);
        }
    }

    warn() {
        if (this.levelValue <= 2) {
            console.warn('WARN: ', ...arguments);
        }
    }

    error() {
        if (this.levelValue <= 3) {
            console.error('ERROR: ', ...arguments);
        }
    }
}

export default new Logger(process.env.LOGGER_LEVEL);
