/**
 * Logger utility with configurable log levels
 * Levels: 1 (info), 3 (warning), 5 (error)
 * Levels 2 and 4 are reserved for future use
 */

// Default log level
let globalLogLevel = 1;

// Log level constants
export const LOG_LEVELS = {
  INFO: 1,
  WARNING: 3,
  ERROR: 5,
};

/**
 * Set the global log level
 * @param {number} level - Log level (1, 3, or 5)
 */
export function setLogLevel(level) {
  if ([1, 2, 3, 4, 5].includes(level)) {
    globalLogLevel = level;
  } else {
    console.warn(`Invalid log level: ${level}. Using default level 1.`);
    globalLogLevel = 1;
  }
}

/**
 * Get the current global log level
 * @returns {number} Current log level
 */
export function getLogLevel() {
  return globalLogLevel;
}

/**
 * Check if a log level should be displayed
 * @param {number} level - Log level to check
 * @returns {boolean} True if should display
 */
function shouldLog(level) {
  return level <= globalLogLevel;
}

/**
 * Log info message (level 1)
 * @param {...any} args - Arguments to log
 */
export function logInfo(...args) {
  if (shouldLog(LOG_LEVELS.INFO)) {
    console.log(...args);
  }
}

/**
 * Log warning message (level 3)
 * @param {...any} args - Arguments to log
 */
export function logWarning(...args) {
  if (shouldLog(LOG_LEVELS.WARNING)) {
    console.warn(...args);
  }
}

/**
 * Log error message (level 5)
 * @param {...any} args - Arguments to log
 */
export function logError(...args) {
  if (shouldLog(LOG_LEVELS.ERROR)) {
    console.error(...args);
  }
}

/**
 * Logger instance that can be created with a specific prefix
 */
export class Logger {
  constructor(prefix = '') {
    this.prefix = prefix;
  }

  /**
   * Set or update the prefix for this logger instance
   * @param {string} prefix - New prefix to use
   */
  setPrefix(prefix) {
    this.prefix = prefix || '';
  }

  /**
   * Get the current prefix
   * @returns {string} Current prefix
   */
  getPrefix() {
    return this.prefix;
  }

  /**
   * Log info with prefix
   * @param {...any} args - Arguments to log
   */
  info(...args) {
    if (shouldLog(LOG_LEVELS.INFO)) {
      const message = this.prefix ? `[${this.prefix}]` : '';
      console.log(message, ...args);
    }
  }

  /**
   * Log warning with prefix
   * @param {...any} args - Arguments to log
   */
  warn(...args) {
    if (shouldLog(LOG_LEVELS.WARNING)) {
      const message = this.prefix ? `[${this.prefix}]` : '';
      console.warn(message, ...args);
    }
  }

  /**
   * Log error with prefix
   * @param {...any} args - Arguments to log
   */
  error(...args) {
    if (shouldLog(LOG_LEVELS.ERROR)) {
      const message = this.prefix ? `[${this.prefix}]` : '';
      console.error(message, ...args);
    }
  }
}

// Export default logger instance
export default {
  setLogLevel,
  getLogLevel,
  info: logInfo,
  warn: logWarning,
  error: logError,
  Logger,
  LOG_LEVELS,
};
