import createDebug from 'debug';

const LOG_LEVELS = ['error', 'warn', 'info', 'debug'] as const;
type LogLevel = typeof LOG_LEVELS[number];

const envLevel = process.env.LOG_LEVEL || 'info';
const levelIndex = LOG_LEVELS.indexOf(envLevel as LogLevel);
const enabledLevels = LOG_LEVELS.slice(levelIndex);

const debug: any = createDebug('recipe-manager');

export function logError(...args: any[]) {
  if (enabledLevels.includes('error')) {
    console.error('[ERROR]', ...args);
  }
}

export function logWarn(...args: any[]) {
  if (enabledLevels.includes('warn')) {
    console.warn('[WARN]', ...args);
  }
}

export function logInfo(...args: any[]) {
  if (enabledLevels.includes('info')) {
    console.info('[INFO]', ...args);
  }
}

export function logDebug(...args: any[]) {
  if (enabledLevels.includes('debug')) {
    debug(...(args as any[]));
  }
}

export const logger = {
  error: logError,
  warn: logWarn,
  info: logInfo,
  debug: logDebug,
};
