"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.LogLevel = exports.AppConsoleLogger = void 0;
/* eslint-disable no-console,@typescript-eslint/no-explicit-any */
let LogLevel = /*#__PURE__*/function (LogLevel) {
  LogLevel[LogLevel["test"] = 0] = "test";
  LogLevel[LogLevel["trace"] = 1] = "trace";
  LogLevel[LogLevel["debug"] = 2] = "debug";
  LogLevel[LogLevel["info"] = 3] = "info";
  LogLevel[LogLevel["warn"] = 4] = "warn";
  LogLevel[LogLevel["error"] = 5] = "error";
  LogLevel[LogLevel["fatal"] = 6] = "fatal";
  LogLevel[LogLevel["off"] = 7] = "off";
  return LogLevel;
}({});
exports.LogLevel = LogLevel;
const replaceError = (_, value) => {
  if (value instanceof Error) {
    const newValue = Object.getOwnPropertyNames(value).reduce((obj, propName) => {
      obj[propName] = value[propName];
      return obj;
    }, {
      name: value.name
    });
    return newValue;
  }
  return value;
};
class AppConsoleLogger {
  // Map our log levels to console levels
  consoleLogMap = {
    [LogLevel.test]: 'log',
    [LogLevel.trace]: 'log',
    [LogLevel.debug]: 'debug',
    [LogLevel.info]: 'info',
    [LogLevel.warn]: 'warn',
    [LogLevel.error]: 'error',
    [LogLevel.fatal]: 'error'
  };
  constructor() {
    let logLevel = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : LogLevel.off;
    this.logLevel = logLevel;
  }
  log(level, message, data) {
    // Get console method from mapping
    const consoleLevel = this.consoleLogMap[level];

    // Get logger prefix from log level names in enum
    const prefix = LogLevel[level].toUpperCase();

    // Return early if logging is not enabled for this level
    if (!this.isEnabled(level)) {
      console.log('logger disabled');
      return;
    }

    // Log, with or without data
    if (data) {
      console[consoleLevel](`${prefix}: ${new Date().toISOString()} - ${message}`, JSON.stringify(data, replaceError, 2));
    } else {
      console[consoleLevel](`${prefix}:  ${new Date().toISOString()} - ${message}`);
    }
  }
  isEnabled(logLevel) {
    return logLevel >= this.logLevel;
  }
}
exports.AppConsoleLogger = AppConsoleLogger;
//# sourceMappingURL=logger.js.map