export enum LogLevel {
  none = -1,
  error = 0,
  warn = 1,
  log = 2,
  info = 3,
}

let logLevel: LogLevel = LogLevel.info;

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

export function parseLogLevel(levelStr: string): LogLevel {
  const map: Record<string, LogLevel> = {
    'none': LogLevel.none,
    'error': LogLevel.error,
    'warn': LogLevel.warn,
    'log': LogLevel.log,
    'info': LogLevel.info,
  };
  return map[levelStr.toLowerCase()] ?? LogLevel.info;
}

function formatTimestamp(): string {
  return new Date().toISOString();
}

export function log(level: LogLevel, ...args: any[]): void {
  if (level > logLevel || logLevel === LogLevel.none) {
    return;
  }
  const timestamp = formatTimestamp();
  const prefix = `[${timestamp}] [${LogLevel[level].toUpperCase()}]`;
  switch (level) {
    case LogLevel.error:
      console.error(prefix, ...args);
      break;
    case LogLevel.warn:
      console.warn(prefix, ...args);
      break;
    case LogLevel.info:
      console.info(prefix, ...args);
      break;
    default:
      console.log(prefix, ...args);
      break;
  }
}
