// logger.ts
var MorphLogger = class {
  enabled;
  fileStream;
  /** Resolves once the file stream is initialized (or immediately if no file logging). */
  ready;
  constructor() {
    this.enabled = typeof process !== "undefined" && (process.env.MORPH_DEBUG === "1" || !!process.env.MORPH_LOG_FILE);
    this.fileStream = null;
    const f = typeof process !== "undefined" ? process.env.MORPH_LOG_FILE : void 0;
    if (f) {
      this.ready = import("fs").then((fs) => {
        this.fileStream = fs.createWriteStream(f, { flags: "a" });
      }).catch(() => {
      });
    } else {
      this.ready = Promise.resolve();
    }
  }
  debug(component, msg, data) {
    this._log("debug", component, msg, data);
  }
  info(component, msg, data) {
    this._log("info", component, msg, data);
  }
  warn(component, msg, data) {
    this._log("warn", component, msg, data);
  }
  error(component, msg, data) {
    this._log("error", component, msg, data);
  }
  enable() {
    this.enabled = true;
  }
  get isEnabled() {
    return this.enabled;
  }
  _log(level, component, msg, data) {
    if (level !== "error" && !this.enabled) return;
    const ts = (/* @__PURE__ */ new Date()).toISOString();
    const prefix = `[${ts}] [${level.toUpperCase()}] [${component}]`;
    console.error(data ? `${prefix} ${msg} ${JSON.stringify(data)}` : `${prefix} ${msg}`);
    this.fileStream?.write(JSON.stringify({ ts, level, component, msg, ...data && { data } }) + "\n");
  }
};
var logger = new MorphLogger();

export {
  logger
};
//# sourceMappingURL=chunk-F3NCFNUX.js.map