// src/logger/types.ts
var LogLevel = /* @__PURE__ */ ((LogLevel2) => {
  LogLevel2[LogLevel2["DEBUG"] = 0] = "DEBUG";
  LogLevel2[LogLevel2["INFO"] = 1] = "INFO";
  LogLevel2[LogLevel2["WARN"] = 2] = "WARN";
  LogLevel2[LogLevel2["ERROR"] = 3] = "ERROR";
  LogLevel2[LogLevel2["NONE"] = 4] = "NONE";
  return LogLevel2;
})(LogLevel || {});

// src/logger/console-adapter.ts
var ConsoleLoggerAdapter = class {
  constructor() {
    this.colors = {
      DEBUG: "\x1B[36m",
      // Cyan
      INFO: "\x1B[32m",
      // Green
      WARN: "\x1B[33m",
      // Yellow
      ERROR: "\x1B[31m",
      // Red
      RESET: "\x1B[0m"
    };
  }
  log(entry) {
    const { level, message, timestamp, data, context, error } = entry;
    let logMessage = "";
    if (timestamp) {
      logMessage += "[" + this.formatTimestamp(timestamp) + "] ";
    }
    const levelName = this.getLevelName(level);
    logMessage += levelName + ": ";
    if (context) {
      logMessage += "[" + context + "] ";
    }
    logMessage += message;
    switch (level) {
      case 0:
        console.debug(this.colorize(logMessage, "DEBUG"), data || "");
        break;
      case 1:
        console.info(this.colorize(logMessage, "INFO"), data || "");
        break;
      case 2:
        console.warn(this.colorize(logMessage, "WARN"), data || "");
        break;
      case 3:
        console.error(this.colorize(logMessage, "ERROR"), data || "");
        if (error) {
          console.error(error);
        }
        break;
    }
  }
  formatTimestamp(date) {
    return date.toISOString();
  }
  getLevelName(level) {
    const names = ["DEBUG", "INFO", "WARN", "ERROR", "NONE"];
    return names[level] || "UNKNOWN";
  }
  colorize(message, level) {
    if (typeof process !== "undefined" && process.stdout?.isTTY) {
      return this.colors[level] + message + this.colors.RESET;
    }
    return message;
  }
};

// src/logger/Logger.ts
var Logger = class _Logger {
  constructor(config, context) {
    const isProduction = typeof process !== "undefined" ? process.env.NODE_ENV === "production" : false;
    this.config = {
      minLevel: config?.minLevel ?? (isProduction ? 1 : 0),
      // INFO in prod, DEBUG in dev
      enableTimestamp: config?.enableTimestamp ?? true,
      enableContext: config?.enableContext ?? true,
      environment: config?.environment ?? (isProduction ? "production" : "development"),
      adapter: config?.adapter ?? new ConsoleLoggerAdapter()
    };
    this.adapter = this.config.adapter;
    this.context = context;
  }
  /**
   * 创建带上下文的子 Logger
   */
  createChild(context) {
    return new _Logger(this.config, context);
  }
  /**
   * 调试日志
   */
  debug(message, data) {
    this.log(0, message, data);
  }
  /**
   * 信息日志
   */
  info(message, data) {
    this.log(1, message, data);
  }
  /**
   * 警告日志
   */
  warn(message, data) {
    this.log(2, message, data);
  }
  /**
   * 错误日志
   */
  error(message, error) {
    this.log(
      3,
      // LogLevel.ERROR
      message,
      error instanceof Error ? void 0 : error,
      error instanceof Error ? error : void 0
    );
  }
  /**
   * 核心日志方法
   */
  log(level, message, data, error) {
    if (level < this.config.minLevel) {
      return;
    }
    if (typeof window !== "undefined" && typeof localStorage !== "undefined") {
      const loggerDebug = localStorage.getItem("logger-debug");
      if (loggerDebug === "false" && level < 3) {
        return;
      }
    }
    const entry = {
      level,
      message,
      timestamp: this.config.enableTimestamp ? /* @__PURE__ */ new Date() : void 0,
      data,
      context: this.config.enableContext ? this.context : void 0,
      error
    };
    this.adapter.log(entry);
  }
  /**
   * 设置日志级别
   */
  setLevel(level) {
    this.config.minLevel = level;
  }
  /**
   * 获取当前日志级别
   */
  getLevel() {
    return this.config.minLevel;
  }
};
var logger = new Logger();
function createLogger(context, config) {
  return new Logger(config, context);
}

export { ConsoleLoggerAdapter, LogLevel, Logger, createLogger, logger };
//# sourceMappingURL=chunk-3DXPQ4YV.mjs.map
//# sourceMappingURL=chunk-3DXPQ4YV.mjs.map