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

const currentLevel = LogLevel.INFO;

const PREFIX = {
  HaloService: "HaloService",
  PostService: "PostService",
  ImageService: "ImageService",
  TaxonomyService: "TaxonomyService",
  ContentService: "ContentService",
  HaloClient: "HaloClient",
  Plugin: "HaloPlugin",
  ImageUploader: "ImageUploader",
};

function formatMessage(prefix: string, message: string): string {
  return `[${prefix}] ${message}`;
}

function shouldLog(level: LogLevel): boolean {
  return level >= currentLevel;
}

export const logger = {
  debug(prefix: keyof typeof PREFIX, message: string, ...args: unknown[]): void {
    if (shouldLog(LogLevel.DEBUG)) {
      console.debug(formatMessage(PREFIX[prefix], message), ...args);
    }
  },

  info(prefix: keyof typeof PREFIX, message: string, ...args: unknown[]): void {
    if (shouldLog(LogLevel.INFO)) {
      console.log(formatMessage(PREFIX[prefix], message), ...args);
    }
  },

  warn(prefix: keyof typeof PREFIX, message: string, ...args: unknown[]): void {
    if (shouldLog(LogLevel.WARN)) {
      console.warn(formatMessage(PREFIX[prefix], message), ...args);
    }
  },

  error(prefix: keyof typeof PREFIX, message: string, error?: unknown): void {
    if (shouldLog(LogLevel.ERROR)) {
      if (error) {
        console.error(formatMessage(PREFIX[prefix], message), error);
      } else {
        console.error(formatMessage(PREFIX[prefix], message));
      }
    }
  },
};

export default logger;