/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { ansi, Wrapper } from "@nacho-iot/js-tools/ansi-text";
const MockLogger = {
  emitAll: false,
  injectExternalMessage: (source, text) => console.log(formatExternalMessage(source, text))
};
function formatExternalMessage(source, text) {
  return `${Wrapper.prefixStart}${ansi.black.bg.cyan.bold(` ${source} `)} ${Wrapper.prefixStop}${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;
    }
  });
}
export {
  LoggerHooks,
  MockLogger,
  loggerSetup
};
//# sourceMappingURL=logging.js.map
