import { parseBooleanToken } from "../boolean.js";
import { isLevelEnabled } from "./level.js";
import { formatBindings } from "./transport.console.js";
import { OM_LOG_DESTINATION_ENV, isStderrDestinationToken } from "./transport.server.js";
const OM_LOG_PRETTY_ENV = "OM_LOG_PRETTY";
function readProcessEnv() {
  return typeof process === "undefined" ? {} : process.env;
}
function resolvePrettyMode(env = readProcessEnv()) {
  const parsed = parseBooleanToken(env[OM_LOG_PRETTY_ENV]);
  if (parsed !== null) return parsed;
  return env.NODE_ENV !== "production";
}
let cachedPrettyMode = null;
function isPrettyModeEnabled() {
  if (cachedPrettyMode === null) cachedPrettyMode = resolvePrettyMode();
  return cachedPrettyMode;
}
function resetLogPrettyCache() {
  cachedPrettyMode = null;
}
const LEVEL_LABELS = {
  debug: "DEBUG",
  info: "INFO ",
  warn: "WARN ",
  error: "ERROR"
};
const ANSI_RESET = "\x1B[0m";
const ANSI_DIM = "\x1B[2m";
const LEVEL_COLORS = {
  debug: ANSI_DIM,
  info: "\x1B[36m",
  warn: "\x1B[33m",
  error: "\x1B[31m"
};
function resolveStream() {
  const useStderr = isStderrDestinationToken(readProcessEnv()[OM_LOG_DESTINATION_ENV]);
  return useStderr ? process.stderr : process.stdout;
}
function formatTimestamp(date) {
  const hours = String(date.getHours()).padStart(2, "0");
  const minutes = String(date.getMinutes()).padStart(2, "0");
  const seconds = String(date.getSeconds()).padStart(2, "0");
  const millis = String(date.getMilliseconds()).padStart(3, "0");
  return `${hours}:${minutes}:${seconds}.${millis}`;
}
function createPrettyLogger(namespace, bindings = {}) {
  const emit = (level, msg, fields) => {
    if (!isLevelEnabled(level)) return;
    const stream = resolveStream();
    const { err, ...rest } = { ...bindings, ...fields };
    let scope = namespace;
    if (typeof rest.component === "string" && rest.component) {
      scope = `${namespace}:${rest.component}`;
      delete rest.component;
    }
    const tail = formatBindings(err instanceof Error || err === void 0 ? rest : { ...rest, err });
    const colored = stream.isTTY === true;
    const timestamp = formatTimestamp(/* @__PURE__ */ new Date());
    const timestampPart = colored ? `${ANSI_DIM}${timestamp}${ANSI_RESET}` : timestamp;
    const label = LEVEL_LABELS[level];
    const levelPart = colored ? `${LEVEL_COLORS[level]}${label}${ANSI_RESET}` : label;
    let line = `${timestampPart} ${levelPart} [${scope}] ${msg}`;
    if (tail) line += ` ${tail}`;
    if (err instanceof Error) line += `
${err.stack ?? err.message}`;
    stream.write(`${line}
`);
  };
  return {
    debug: (msg, fields) => emit("debug", msg, fields),
    info: (msg, fields) => emit("info", msg, fields),
    warn: (msg, fields) => emit("warn", msg, fields),
    error: (msg, fields) => emit("error", msg, fields),
    child: (childBindings) => createPrettyLogger(namespace, { ...bindings, ...childBindings })
  };
}
export {
  OM_LOG_PRETTY_ENV,
  createPrettyLogger,
  isPrettyModeEnabled,
  resetLogPrettyCache,
  resolvePrettyMode
};
//# sourceMappingURL=transport.pretty.js.map
