import { LogLevels } from "./logLevel.js";
import { initBaseLogObject } from "./baseLogObject.js";
import { Console } from "./transport/console.js";
import { validateOptions, validateMessage } from "./validations.js";
import { redactPii } from "./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 = Console(), logEverythingToConsole } = options;
  validateOptions({ ...options, transport });
  let curLogLevel = LogLevels.INFO;
  const baseLogObject = initBaseLogObject(options);
  const log = (logLevel, logData) => {
    const message = patchLog(logData);
    validateMessage(message);
    const logMessage = typeof message === "string" ? { message } : message;
    if (logLevel >= curLogLevel) {
      const msg = redactPii({
        ...baseLogObject,
        level: LogLevels[logLevel].toLowerCase(),
        "@timestamp": (/* @__PURE__ */ new Date()).toISOString(),
        ...logMessage
      });
      transport.log(msg, logLevel).catch(() => {
      });
      if (logLevel >= LogLevels.ERROR || logEverythingToConsole) {
        Console().log(msg).catch(() => {
        });
      }
    }
  };
  return {
    /**
     * set log level. default: {@link LogLevels.INFO}
     * @param level {@link LogLevels}
     */
    setLogLevel: (level) => {
      curLogLevel = level || 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, LogLevels.DEBUG),
    /**
     * log info message
     */
    info: log.bind(null, LogLevels.INFO),
    /**
     * log audit message
     */
    audit: log.bind(null, LogLevels.AUDIT),
    /**
     * log warn message
     */
    warn: log.bind(null, LogLevels.WARN),
    /**
     * log error message
     */
    error: log.bind(null, LogLevels.ERROR),
    /**
     * log fatal message
     */
    fatal: log.bind(null, LogLevels.FATAL)
  };
};
export {
  logger
};
