import { getLogLevel } from "./level.js";
import { createConsoleLogger } from "./transport.console.js";
const REDACT_PATHS = [
  "password",
  "*.password",
  "token",
  "*.token",
  "secret",
  "*.secret",
  "authorization",
  "*.authorization",
  "headers.authorization",
  "req.headers.authorization"
];
const REDACT_CENSOR = "[Redacted]";
const OM_LOG_DESTINATION_ENV = "OM_LOG_DESTINATION";
function isStderrDestinationToken(raw) {
  return typeof raw === "string" && raw.trim().toLowerCase() === "stderr";
}
function resolveDestinationStream(nodeProcess) {
  return isStderrDestinationToken(nodeProcess.env?.[OM_LOG_DESTINATION_ENV]) ? nodeProcess.stderr : void 0;
}
let cachedRoot;
let rootGeneration = 0;
function loadPinoRoot() {
  if (cachedRoot !== void 0) return cachedRoot;
  try {
    const nodeProcess = typeof process === "undefined" ? void 0 : process;
    const moduleBuiltin = nodeProcess?.getBuiltinModule?.("node:module");
    if (!nodeProcess || !moduleBuiltin) {
      cachedRoot = null;
      return cachedRoot;
    }
    const requireFromApp = moduleBuiltin.createRequire(`${nodeProcess.cwd()}/package.json`);
    const pinoExport = requireFromApp("pino");
    const pinoFactory = typeof pinoExport === "function" ? pinoExport : pinoExport.default;
    const destination = resolveDestinationStream(nodeProcess);
    const options = {
      level: getLogLevel(),
      redact: { paths: REDACT_PATHS, censor: REDACT_CENSOR }
    };
    cachedRoot = destination ? pinoFactory(options, destination) : pinoFactory(options);
  } catch {
    cachedRoot = null;
  }
  return cachedRoot;
}
function wrapPinoLogger(pinoLogger) {
  return {
    debug: (msg, fields) => pinoLogger.debug(fields ?? {}, msg),
    info: (msg, fields) => pinoLogger.info(fields ?? {}, msg),
    warn: (msg, fields) => pinoLogger.warn(fields ?? {}, msg),
    error: (msg, fields) => pinoLogger.error(fields ?? {}, msg),
    child: (childBindings) => wrapPinoLogger(pinoLogger.child(childBindings))
  };
}
function createServerLogger(namespace, bindings = {}) {
  let delegate = null;
  let delegateGeneration = -1;
  const resolveDelegate = () => {
    if (delegate && delegateGeneration === rootGeneration) return delegate;
    delegateGeneration = rootGeneration;
    const root = loadPinoRoot();
    delegate = root ? wrapPinoLogger(root.child({ name: namespace, ...bindings })) : createConsoleLogger(namespace, bindings);
    return delegate;
  };
  return {
    debug: (msg, fields) => resolveDelegate().debug(msg, fields),
    info: (msg, fields) => resolveDelegate().info(msg, fields),
    warn: (msg, fields) => resolveDelegate().warn(msg, fields),
    error: (msg, fields) => resolveDelegate().error(msg, fields),
    child: (childBindings) => createServerLogger(namespace, { ...bindings, ...childBindings })
  };
}
function resetServerLoggerCache() {
  cachedRoot = void 0;
  rootGeneration += 1;
}
export {
  OM_LOG_DESTINATION_ENV,
  createServerLogger,
  isStderrDestinationToken,
  resetServerLoggerCache
};
//# sourceMappingURL=transport.server.js.map
