export type LogConfig = {
  debug?: boolean;
  onLog?: (...params: any[]) => void;
};

export enum LogType {
  LOG = "log",
  INFO = "info",
  WARN = "warn",
  ERROR = "error",
}

export class Logger {
  TAG: string;
  config: LogConfig;

  constructor(TAG: string, config?: LogConfig) {
    this.TAG = `[${TAG}]`;

    if (config) {
      this.setConfig(config);
    }
  }

  setConfig(config: LogConfig) {
    this.config = Object.assign(this.config || {}, config || {});
  }

  _debug(type: LogType, msg: string, params: any[]) {
    const { debug, onLog } = this.config || {};
    if (type !== LogType.ERROR && !debug) return;

    if (onLog) onLog(this.TAG, type, msg, ...params);
    else console[type].call(console, this.TAG, msg, ...params);
  }

  log(msg: string, ...params: any[]) {
    this._debug(LogType.LOG, msg, params);
  }

  info(msg: string, ...params: any[]) {
    this._debug(LogType.INFO, msg, params);
  }

  warn(msg: string, ...params: any[]) {
    this._debug(LogType.WARN, msg, params);
  }

  error(msg: string, ...params: any[]) {
    this._debug(LogType.ERROR, msg, params);
  }
}
