"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_Boot = require("#util/Boot.js");
var import_Cancelable = require("#util/Cancelable.js");
var import_MatterError = require("../MatterError.js");
var import_Time = require("../time/Time.js");
var import_Bytes = require("../util/Bytes.js");
var import_Diagnostic = require("./Diagnostic.js");
var import_LogDestination = require("./LogDestination.js");
var import_LogFormat = require("./LogFormat.js");
var import_LogLevel = require("./LogLevel.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class Logger {
  /**
   * Log destinations.
   *
   * By default there is a single destination named "default".  You can create new destinations using
   * {@link LogDestination}.  Add or remove destinations by modifying this object.
   *
   * Throws an error if you access a destination that doesn't exist.
   */
  static destinations = (0, import_LogDestination.LogDestinations)();
  /**
   * The number of indents to print with messages.
   */
  static nestingLevel;
  #name;
  /**
   * Create a new logger for a facility.
   *
   * @param name the name of the facility
   * @returns a new facility
   */
  static get(name) {
    return new Logger(name);
  }
  /**
   * Get the default log level.
   */
  static get level() {
    return import_LogDestination.LogDestination.defaults.level;
  }
  /**
   * Set log level as name or number for all destinations.
   */
  static set level(level) {
    level = (0, import_LogLevel.LogLevel)(level);
    import_LogDestination.LogDestination.defaults.level = level;
    for (const name in this.destinations) {
      this.destinations[name].level = level;
    }
  }
  /**
   * Get the default facility levels.
   */
  static get facilityLevels() {
    return import_LogDestination.LogDestination.defaults.facilityLevels;
  }
  /**
   * Set log level as name or number for facilities in all destinations.
   *
   * Existing levels that are not named in {@link levels} will remain unchanged.
   */
  static set facilityLevels(levels) {
    for (const name in levels) {
      levels[name] = (0, import_LogLevel.LogLevel)(levels[name]);
    }
    Object.assign(import_LogDestination.LogDestination.defaults.facilityLevels, levels);
    for (const name in this.destinations) {
      Object.assign(this.destinations[name].facilityLevels, levels);
    }
  }
  /**
   * Get the default format name.
   */
  static get format() {
    return import_LogDestination.LogDestination.defaults.format.name;
  }
  /**
   * Set the format for all destinations.
   */
  static set format(format) {
    format = (0, import_LogFormat.LogFormat)(format);
    import_LogDestination.LogDestination.defaults.format = format;
    for (const name in this.destinations) {
      this.destinations[name].format = format;
    }
  }
  /**
   * Mask a string with a given character. If unmaskedLength is provided then these number of characters will be
   * shown unmasked.
   *
   * @param str String to mask
   * @param maskChar character to mask with
   * @param unmaskedLength number of characters to show unmasked in the beginning
   */
  static maskString(str, maskChar = "*", unmaskedLength) {
    return str.substring(0, unmaskedLength ?? 0) + str.substring(unmaskedLength ?? 0).replace(/./g, maskChar);
  }
  /**
   * Perform operations in a nested logging context.  Messages will be
   * indented while the context executes.
   */
  static nest(context) {
    this.nestingLevel++;
    try {
      return context();
    } finally {
      this.nestingLevel--;
    }
  }
  /**
   * Async version of nest().
   */
  static async nestAsync(context) {
    this.nestingLevel++;
    try {
      return await context();
    } finally {
      this.nestingLevel--;
    }
  }
  /**
   * Unhandled error reporter.
   *
   * Some environments do not report full error details such as {@link Error#cause} and {@link AggregateError#errors}.
   *
   * To ensure these details are always recorded somewhere, unhandled errors may be reported here.
   *
   * To disable this behavior replace this function.
   */
  static reportUnhandledError(error) {
    try {
      Logger.get("Unhandled").fatal("Unhandled error detected:", error);
    } catch (e) {
    }
  }
  constructor(name) {
    this.#name = name;
  }
  debug(...values) {
    this.#log(import_LogLevel.LogLevel.DEBUG, values);
  }
  info(...values) {
    this.#log(import_LogLevel.LogLevel.INFO, values);
  }
  notice(...values) {
    this.#log(import_LogLevel.LogLevel.NOTICE, values);
  }
  warn(...values) {
    this.#log(import_LogLevel.LogLevel.WARN, values);
  }
  error(...values) {
    this.#log(import_LogLevel.LogLevel.ERROR, values);
  }
  fatal(...values) {
    this.#log(import_LogLevel.LogLevel.FATAL, values);
  }
  log(level, ...values) {
    this.#log(level, values);
  }
  #log(level, values) {
    for (const name in Logger.destinations) {
      const dest = Logger.destinations[name];
      if (level < (dest.facilityLevels?.[this.#name] ?? dest.level)) {
        continue;
      }
      if (!dest.context) {
        dest.context = import_Diagnostic.Diagnostic.Context();
      }
      dest.context.run(
        () => dest.add(
          import_Diagnostic.Diagnostic.message({
            now: import_Time.Time.now,
            facility: this.#name,
            level,
            prefix: nestingPrefix(),
            values
          })
        )
      );
    }
  }
  //
  // DEPRECATED API SURFACE FOLLOWS
  //
  /**
   * Stringify a value (BigInt aware) as JSON.
   *
   * @param data the value to stringify
   * @returns the stringified value
   *
   * @deprecated use {@link Diagnostic.json}
   */
  static toJSON(data) {
    return JSON.stringify(data, (_, value) => {
      if (typeof value === "bigint") {
        return value.toString();
      }
      if (import_Bytes.Bytes.isBytes(value)) {
        return import_Bytes.Bytes.toHex(value);
      }
      if (value === void 0) {
        return "undefined";
      }
      return value;
    });
  }
  /**
   * Add additional logger to the list of loggers including the default configuration.
   *
   * @deprecated use {@link destinations}
   */
  static addLogger(identifier, logger, options) {
    if (identifier in this.destinations) {
      throw new import_MatterError.ImplementationError(`Logger "${identifier}" already exists`);
    }
    const dest = (0, import_LogDestination.LogDestination)({ name: identifier });
    const legacy = adaptDestinationToLegacy(dest);
    legacy.log = logger;
    if (options?.defaultLogLevel !== void 0) {
      legacy.defaultLogLevel = options.defaultLogLevel;
    }
    if (options?.logLevels !== void 0) {
      legacy.logLevels = options.logLevels;
    }
    if (options?.logFormat !== void 0) {
      legacy.logFormatter = logFormatterFor(options.logFormat);
    }
    this.destinations[identifier] = dest;
  }
  /**
   * @deprecated use {@link destinations}
   */
  static removeLogger(identifier) {
    if (!(identifier in this.destinations)) {
      throw new import_MatterError.NotImplementedError(`Logger "${identifier}" does not exist`);
    }
    delete this.destinations[identifier];
  }
  /**
   * Check if a logger with the matching identifier exists.
   * @param identifier The identifier of the logger
   *
   * @deprecated use {@link destinations}
   */
  static hasLoggerForIdentifier(identifier) {
    return identifier in this.destinations;
  }
  /**
   * Get the logger with the matching identifier.
   * @param identifier The identifier of the logger
   *
   * @deprecated use {@link destinations}
   */
  static getLoggerForIdentifier(identifier) {
    const dest = this.destinations[identifier];
    if (dest === void 0) {
      throw new import_MatterError.NotImplementedError(`Unknown logger "${identifier}"`);
    }
    return adaptDestinationToLegacy(dest);
  }
  /**
   * @deprecated use {@link destinations}
   */
  static getLoggerforIdentifier(identifier) {
    return this.getLoggerForIdentifier(identifier);
  }
  /**
   * Set facility loglevels for the default logger.
   * @param levels The levels to set
   *
   * @deprecated use {@link destinations}
   */
  static set logLevels(levels) {
    Logger.setLogLevelsForLogger("default", levels);
  }
  /**
   * Get facility loglevels for the default logger.
   *
   * @deprecated use {@link Logger.facilityLevels}
   */
  static get logLevels() {
    return Logger.getLoggerForIdentifier("default").logLevels;
  }
  /**
   * Set default loglevel for the default logger.
   *
   * @param level The level to set
   *
   * @deprecated use {@link Logger.level}
   */
  static set defaultLogLevel(level) {
    Logger.setDefaultLoglevelForLogger("default", level);
  }
  /**
   * Get default loglevel for the default logger.
   *
   * @deprecated use {@link destinations}
   */
  static get defaultLogLevel() {
    return Logger.getLoggerForIdentifier("default").defaultLogLevel;
  }
  /**
   * Set the log function for the default logger.
   *
   * @param log The log function to set
   *
   * @deprecated use {@link destinations}
   */
  static set log(log) {
    Logger.setLogger("default", log);
  }
  /**
   * Get the log function for the default logger.
   *
   * @deprecated use {@link destinations}
   */
  static get log() {
    return Logger.getLoggerForIdentifier("default").log;
  }
  /**
   * Set the log formatter for the default logger.
   *
   * @param logFormatter
   *
   * @deprecated use {@link destinations}
   */
  static set logFormatter(logFormatter) {
    Logger.setLogFormatterForLogger("default", logFormatter);
  }
  /**
   * Get the log formatter for the default logger.
   *
   * @deprecated use {@link destinations}
   */
  static get logFormatter() {
    return Logger.getLoggerForIdentifier("default").logFormatter;
  }
  /**
   * Set logFormatter using configuration-style format name for the logger with the matching identifier.
   *
   * @param identifier The identifier of the logger
   * @param format the name of the formatter (see Format enum)
   *
   * @deprecated use {@link destinations}
   */
  static setFormatForLogger(identifier, format) {
    this.getLoggerForIdentifier(identifier).logFormatter = logFormatterFor(format);
  }
  /**
   * Set default loglevel for the logger with the matching identifier.
   *
   * @param identifier The identifier of the logger
   * @param level The level to set
   *
   * @deprecated use {@link destinations}
   */
  static setDefaultLoglevelForLogger(identifier, level) {
    this.getLoggerForIdentifier(identifier).defaultLogLevel = level;
  }
  /**
   * Set facility loglevels for the logger with the matching identifier.
   *
   * @param identifier The identifier of the logger
   * @param levels The levels to set
   *
   * @deprecated use {@link destinations}
   */
  static setLogLevelsForLogger(identifier, levels) {
    this.getLoggerForIdentifier(identifier).logLevels = levels;
  }
  /**
   * Set the log function for the logger with the matching identifier.
   *
   * @param identifier The identifier of the logger
   * @param log The log function to set
   *
   * @deprecated use {@link destinations}
   */
  static setLogger(identifier, log) {
    this.getLoggerForIdentifier(identifier).log = log;
  }
  /**
   * Set the log formatter for the logger with the matching identifier.
   *
   * @param identifier The identifier of the logger
   * @param logFormatter The log formatter to set
   *
   * @deprecated use {@link destinations}
   */
  static setLogFormatterForLogger(identifier, logFormatter) {
    this.getLoggerForIdentifier(identifier).logFormatter = logFormatter;
  }
}
function nestingPrefix() {
  if (Logger.nestingLevel) {
    return "\u23B8".padEnd(Logger.nestingLevel * 2);
  }
  return "";
}
import_Boot.Boot.init(() => {
  Logger.destinations = (0, import_LogDestination.LogDestinations)();
  Logger.nestingLevel = 0;
  if (typeof MatterHooks !== "undefined") {
    MatterHooks.loggerSetup?.(Logger);
  }
});
import_Cancelable.CancelablePromise.logger = Logger.get("CancelablePromise");
function logFormatterFor(formatName) {
  const format = (0, import_LogFormat.LogFormat)(formatName);
  return (now, level, facility, prefix, ...values) => format(import_Diagnostic.Diagnostic.message({ now, level, facility, prefix, values }));
}
function adaptDestinationToLegacy(destination) {
  return {
    get logIdentifier() {
      return destination.name;
    },
    get logFormatter() {
      return (now, level, facility, prefix, values) => destination.format(import_Diagnostic.Diagnostic.message({ now, level, facility, prefix, values }));
    },
    set logFormatter(logFormatter) {
      destination.format = (message) => logFormatter(message.now, message.level, message.facility, message.prefix, message.values);
    },
    get log() {
      return (level, formattedLog, facility) => destination.write(formattedLog, import_Diagnostic.Diagnostic.message({ level, facility }));
    },
    set log(log) {
      destination.write = (text, message) => log(message.level, text, message.facility);
    },
    get defaultLogLevel() {
      return destination.level;
    },
    set defaultLogLevel(level) {
      destination.level = level;
    },
    get logLevels() {
      return destination.facilityLevels;
    },
    set logLevels(levels) {
      destination.facilityLevels = levels;
    }
  };
}
//# sourceMappingURL=Logger.js.map
