import { appendFileSync, mkdirSync } from "node:fs";
import { join } from "node:path";
import { getConfig } from "./config.js";
const LEVEL_PRIORITY = {
  debug: 0,
  info: 1,
  warn: 2,
  error: 3
};
let tuiLogPath = null;
function getTuiLogPath() {
  if (!tuiLogPath) {
    const dataDir = getConfig().dataDir;
    mkdirSync(dataDir, { recursive: true });
    tuiLogPath = join(dataDir, "init.log");
  }
  return tuiLogPath;
}
function appendToLogFile(line) {
  try {
    appendFileSync(getTuiLogPath(), line + "\n");
  } catch {
  }
}
function writeJson(level, module, msg, data) {
  const line = JSON.stringify({
    ts: (/* @__PURE__ */ new Date()).toISOString(),
    level,
    msg,
    module,
    ...data ? { data } : {}
  });
  if (process.env.WIGOLO_TUI_MODE === "true") {
    appendToLogFile(line);
    return;
  }
  process.stderr.write(line + "\n");
}
function writeText(level, module, msg, data) {
  const ts = (/* @__PURE__ */ new Date()).toISOString();
  const dataStr = data ? " " + Object.entries(data).map(([k, v]) => `${k}=${v}`).join(" ") : "";
  const line = `[${ts}] ${level.toUpperCase().padEnd(5)} [${module}] ${msg}${dataStr}`;
  if (process.env.WIGOLO_TUI_MODE === "true") {
    appendToLogFile(line);
    return;
  }
  process.stderr.write(line + "\n");
}
let suppressionFloor = null;
function setLogSuppression(level) {
  suppressionFloor = level;
}
function getLogSuppression() {
  return suppressionFloor;
}
function createLogger(module) {
  const config = getConfig();
  const minPriority = LEVEL_PRIORITY[config.logLevel];
  const write = config.logFormat === "json" ? writeJson : writeText;
  function log(level, msg, data) {
    if (suppressionFloor !== null && LEVEL_PRIORITY[level] < LEVEL_PRIORITY[suppressionFloor]) {
      return;
    }
    if (LEVEL_PRIORITY[level] >= minPriority) {
      write(level, module, msg, data);
    }
  }
  return {
    debug: (msg, data) => log("debug", msg, data),
    info: (msg, data) => log("info", msg, data),
    warn: (msg, data) => log("warn", msg, data),
    error: (msg, data) => log("error", msg, data)
  };
}
export {
  createLogger,
  getLogSuppression,
  setLogSuppression
};
//# sourceMappingURL=logger.js.map