"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 logging_exports = {};
__export(logging_exports, {
  LoggerHooks: () => LoggerHooks,
  MockLogger: () => MockLogger,
  loggerSetup: () => loggerSetup
});
module.exports = __toCommonJS(logging_exports);
var import_ansi_text = require("@nacho-iot/js-tools/ansi-text");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const MockLogger = {
  emitAll: false,
  injectExternalMessage: (source, text) => console.log(formatExternalMessage(source, text))
};
function formatExternalMessage(source, text) {
  return `${import_ansi_text.Wrapper.prefixStart}${import_ansi_text.ansi.black.bg.cyan.bold(` ${source} `)} ${import_ansi_text.Wrapper.prefixStop}${import_ansi_text.ansi.dim(text)}`;
}
const LoggerHooks = {
  beforeEach: Array(),
  afterEach: Array()
};
function loggerSetup(Logger) {
  Logger.format = "ansi";
  let messageBuffer;
  const defaultWrite = Logger.destinations.default.write;
  function passMessage(args) {
    defaultWrite.apply(Logger, args);
  }
  function interceptingWriter(...args) {
    let emitAll = MockLogger.emitAll;
    if (MatterHooks?.loggerSink) {
      MatterHooks.loggerSink(...args);
    } else if (!emitAll) {
      if (messageBuffer) {
        messageBuffer.push(args);
      } else {
        emitAll = true;
      }
    }
    if (emitAll) {
      passMessage(args);
    }
  }
  MockLogger.injectExternalMessage = (source, text) => interceptingWriter(formatExternalMessage(source, text), { level: 0 });
  Logger.destinations.default.write = interceptingWriter;
  LoggerHooks.beforeEach.push(function() {
    if (!MockLogger.emitAll) {
      messageBuffer = [];
    }
  });
  LoggerHooks.afterEach.push((mocha) => {
    if (messageBuffer?.length) {
      if (mocha.suite.ctx.currentTest?.isFailed()) {
        for (const args of messageBuffer) {
          passMessage(args);
        }
      }
      messageBuffer = void 0;
    }
  });
}
//# sourceMappingURL=logging.js.map
