var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });

// src/browser.ts
import inspect from "object-inspect";

// src/shared.ts
import supportsColor from "supports-color";
import { Time } from "cosmokit";
var c16 = [6, 2, 3, 4, 5, 1];
var c256 = [
  20,
  21,
  26,
  27,
  32,
  33,
  38,
  39,
  40,
  41,
  42,
  43,
  44,
  45,
  56,
  57,
  62,
  63,
  68,
  69,
  74,
  75,
  76,
  77,
  78,
  79,
  80,
  81,
  92,
  93,
  98,
  99,
  112,
  113,
  129,
  134,
  135,
  148,
  149,
  160,
  161,
  162,
  163,
  164,
  165,
  166,
  167,
  168,
  169,
  170,
  171,
  172,
  173,
  178,
  179,
  184,
  185,
  196,
  197,
  198,
  199,
  200,
  201,
  202,
  203,
  204,
  205,
  206,
  207,
  208,
  209,
  214,
  215,
  220,
  221
];
function isAggregateError(error) {
  return error instanceof Error && Array.isArray(error["errors"]);
}
__name(isAggregateError, "isAggregateError");
var Level = /* @__PURE__ */ ((Level2) => {
  Level2[Level2["SILENT"] = 0] = "SILENT";
  Level2[Level2["SUCCESS"] = 1] = "SUCCESS";
  Level2[Level2["ERROR"] = 1] = "ERROR";
  Level2[Level2["INFO"] = 2] = "INFO";
  Level2[Level2["WARN"] = 2] = "WARN";
  Level2[Level2["DEBUG"] = 3] = "DEBUG";
  return Level2;
})(Level || {});
var Logger = class {
  constructor(options, factory) {
    this.factory = factory;
    Object.assign(this, options);
    this.success = this._method("success", 1 /* SUCCESS */);
    this.error = this._method("error", 1 /* ERROR */);
    this.info = this._method("info", 2 /* INFO */);
    this.warn = this._method("warn", 2 /* WARN */);
    this.debug = this._method("debug", 3 /* DEBUG */);
  }
  static {
    __name(this, "Logger");
  }
  static color(exporter, code, value, decoration = "") {
    if (!exporter.colors) return "" + value;
    return `\x1B[3${code < 8 ? code : "8;5;" + code}${exporter.colors >= 2 ? decoration : ""}m${value}\x1B[0m`;
  }
  static code(name, level) {
    let hash = 0;
    for (let i = 0; i < name.length; i++) {
      hash = (hash << 3) - hash + name.charCodeAt(i) + 13;
      hash |= 0;
    }
    const colors = !level ? [] : level >= 2 ? c256 : c16;
    return colors[Math.abs(hash) % colors.length];
  }
  _method(type, level) {
    return (...args) => {
      if (args.length === 1 && args[0] instanceof Error) {
        if (args[0].cause) {
          this[type](args[0].cause);
        } else if (isAggregateError(args[0])) {
          args[0].errors.forEach((error) => this[type](error));
          return;
        }
      }
      const sn = ++this.factory._snMessage;
      const ts = Date.now();
      for (const exporter of this.factory.exporters.values()) {
        const targetLevel = exporter.levels?.[this.name] ?? exporter.levels?.default ?? this.level ?? 2 /* INFO */;
        if (targetLevel < level) continue;
        const body = this._format(exporter, args.slice());
        const message = { sn, ts, type, level, name: this.name, ...this.meta, body };
        exporter.export(message);
      }
    };
  }
  _format(exporter, args) {
    if (args[0] instanceof Error) {
      args[0] = args[0].stack || args[0].message;
      args.unshift("%s");
    } else if (typeof args[0] !== "string") {
      args.unshift("%o");
    }
    let format = args.shift();
    format = format.replace(/%([a-zA-Z%])/g, (match, char) => {
      if (match === "%%") return "%";
      const formatter = this.factory.formatters[char];
      if (typeof formatter === "function") {
        const value = args.shift();
        return formatter(value, exporter, this);
      }
      return match;
    });
    for (let arg of args) {
      if (typeof arg === "object" && arg) {
        arg = this.factory.formatters["o"](arg, exporter, this);
      }
      format += " " + arg;
    }
    const { maxLength = 10240 } = exporter;
    return format.split(/\r?\n/g).map((line) => {
      return line.slice(0, maxLength) + (line.length > maxLength ? "..." : "");
    }).join("\n");
  }
};
var Factory = class _Factory {
  static {
    __name(this, "Factory");
  }
  static formatters = {
    s: /* @__PURE__ */ __name((value) => value, "s"),
    j: /* @__PURE__ */ __name((value) => JSON.stringify(value), "j"),
    c: /* @__PURE__ */ __name((value, exporter, logger) => {
      return Logger.color(exporter, Logger.code(logger.name, exporter.colors), value);
    }, "c")
  };
  _snMessage = 0;
  _snExporter = 0;
  exporters = /* @__PURE__ */ new Map();
  formatters = Object.create(_Factory.formatters);
  createLogger(name, options = {}) {
    return new Logger({ name, ...options }, this);
  }
  addExporter(exporter) {
    this.exporters.set(++this._snExporter, exporter);
    return () => this.exporters.delete(this._snExporter);
  }
};
var Exporter;
((Exporter2) => {
  class Console {
    static {
      __name(this, "Console");
    }
    constructor(options) {
      Object.assign(this, {
        colors: supportsColor.stdout ? supportsColor.stdout.level : 0,
        showTime: "yyyy-MM-dd hh:mm:ss ",
        showDiff: false,
        ...options
      });
    }
    export(message) {
      console.log(this.render(message));
    }
    render(message) {
      const prefix = `[${message.type[0].toUpperCase()}]`;
      const space = " ".repeat(this.label?.margin ?? 1);
      let indent = 3 + space.length, output = "";
      if (this.showTime) {
        indent += this.showTime.length;
        output += Logger.color(this, 8, Time.template(this.showTime));
      }
      const code = Logger.code(message.name, this.colors);
      const label = Logger.color(this, code, message.name, ";1");
      const padLength = (this.label?.width ?? 0) + label.length - message.name.length;
      if (this.label?.align === "right") {
        output += label.padStart(padLength) + space + prefix + space;
        indent += (this.label.width ?? 0) + space.length;
      } else {
        output += prefix + space + label.padEnd(padLength) + space;
      }
      output += message.body.replace(/\n/g, "\n" + " ".repeat(indent));
      if (this.showDiff && this.timestamp) {
        const diff = message.ts - this.timestamp;
        output += Logger.color(this, code, " +" + Time.format(diff));
      }
      this.timestamp = message.ts;
      return output;
    }
  }
  Exporter2.Console = Console;
})(Exporter || (Exporter = {}));

// src/browser.ts
Factory.formatters["o"] = (value, target) => {
  return inspect(value, { depth: Infinity });
};
export {
  Exporter,
  Factory,
  Level,
  Logger
};
