import { appendFileSync, writeFileSync } from "fs";
import { tmpdir } from "os";
import { join } from "path";
function createDebugLogger(name) {
  const envVar = `DEXTO_DEBUG_${name.toUpperCase()}`;
  const logPath = join(tmpdir(), `dexto-debug-${name.toLowerCase()}.log`);
  const isEnabled = () => {
    return process.env[envVar] === "true" || process.env.DEXTO_DEBUG_ALL === "true";
  };
  const log = (msg, data) => {
    if (!isEnabled()) return;
    try {
      const timestamp = (/* @__PURE__ */ new Date()).toISOString().split("T")[1];
      const dataStr = data ? ` ${JSON.stringify(data)}` : "";
      const line = `[${timestamp}] ${msg}${dataStr}
`;
      appendFileSync(logPath, line);
    } catch {
    }
  };
  const reset = (header) => {
    if (!isEnabled()) return;
    const defaultHeader = `=== DEXTO DEBUG [${name.toUpperCase()}] ${(/* @__PURE__ */ new Date()).toISOString()} ===`;
    try {
      writeFileSync(logPath, `${header ?? defaultHeader}
`);
    } catch {
    }
  };
  return {
    log,
    isEnabled,
    getLogPath: () => logPath,
    reset
  };
}
export {
  createDebugLogger
};
