"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var logger_exports = {};
__export(logger_exports, {
  logger: () => logger
});
module.exports = __toCommonJS(logger_exports);
var import_logLevel = require("./logLevel.js");
var import_baseLogObject = require("./baseLogObject.js");
var import_console = require("./transport/console.js");
var import_validations = require("./validations.js");
var import_redactPII = require("./redactPII.js");
const patchLog = (logMessage) => {
  if (typeof logMessage === "object" && !logMessage.message && logMessage.msg) {
    logMessage.message = logMessage.msg;
  }
  return logMessage;
};
const logger = (options) => {
  if (!options) throw new Error("options are required");
  const { transport = (0, import_console.Console)(), logEverythingToConsole } = options;
  (0, import_validations.validateOptions)({ ...options, transport });
  let curLogLevel = import_logLevel.LogLevels.INFO;
  const baseLogObject = (0, import_baseLogObject.initBaseLogObject)(options);
  const log = (logLevel, logData) => {
    const message = patchLog(logData);
    (0, import_validations.validateMessage)(message);
    const logMessage = typeof message === "string" ? { message } : message;
    if (logLevel >= curLogLevel) {
      const msg = (0, import_redactPII.redactPii)({
        ...baseLogObject,
        level: import_logLevel.LogLevels[logLevel].toLowerCase(),
        "@timestamp": (/* @__PURE__ */ new Date()).toISOString(),
        ...logMessage
      });
      transport.log(msg, logLevel).catch(() => {
      });
      if (logLevel >= import_logLevel.LogLevels.ERROR || logEverythingToConsole) {
        (0, import_console.Console)().log(msg).catch(() => {
        });
      }
    }
  };
  return {
    /**
     * set log level. default: {@link LogLevels.INFO}
     * @param level {@link LogLevels}
     */
    setLogLevel: (level) => {
      curLogLevel = level || import_logLevel.LogLevels.INFO;
    },
    /**
     * get current log level
     * @returns current log level. {@link LogLevels}
     */
    getLogLevel: () => curLogLevel,
    /**
     * Change logger properties after creating it
     * @param options {@link RuntimeLoggerOptions}
     * @param options.environment
     * @param options.instanceId
     * @param options.customerId
     * @param options.userId
     * @param options.appVersion
     * @param options.loanId
     */
    setOptions: ({
      environment = baseLogObject.environment,
      instanceId = baseLogObject.instanceId,
      customerId = baseLogObject.customerId,
      userId = baseLogObject.userId,
      appVersion = baseLogObject.appVersion,
      loanId = baseLogObject.loanId
    }) => {
      baseLogObject.environment = environment;
      baseLogObject.instanceId = instanceId;
      baseLogObject.customerId = customerId;
      baseLogObject.userId = userId;
      baseLogObject.appVersion = appVersion;
      baseLogObject.loanId = loanId;
    },
    /**
     * log debug message
     */
    debug: log.bind(null, import_logLevel.LogLevels.DEBUG),
    /**
     * log info message
     */
    info: log.bind(null, import_logLevel.LogLevels.INFO),
    /**
     * log audit message
     */
    audit: log.bind(null, import_logLevel.LogLevels.AUDIT),
    /**
     * log warn message
     */
    warn: log.bind(null, import_logLevel.LogLevels.WARN),
    /**
     * log error message
     */
    error: log.bind(null, import_logLevel.LogLevels.ERROR),
    /**
     * log fatal message
     */
    fatal: log.bind(null, import_logLevel.LogLevels.FATAL)
  };
};
