import { isPlainObject as p } from "../../core/utilities.mjs";
import { LOG_LEVEL as r } from "../levels.mjs";
class y {
  timer;
  queue = [];
  defaultStyle = [
    "border-radius: 3px",
    "font-size: 14px",
    "font-weight: bold",
    "color: white",
    "-webkit-text-stroke: .6px black",
    "padding: 2px 5px",
    "margin-right: 2px"
  ].join(";");
  config;
  color;
  levelColorMap = {
    1: "#00BCD4",
    //cyan
    2: "#f39c12",
    //yellow
    3: "#c0392b",
    //red
    4: "#2ecc71",
    //green
    5: "#7f8c8d"
    //gray
  };
  constructor(t) {
    this.config = t, this.color = this.colorize(t.namespace);
  }
  getStyle(t = r.DEFAULT) {
    return `background: ${this.levelColorMap[t] ?? this.levelColorMap[5]}; ${this.defaultStyle}`;
  }
  report(t, e) {
    const { enabled: o, grouped: i, namespace: a } = this.config, { args: g = [], date: n, group: h, level: l } = e;
    if (o)
      if (i)
        this.queue.push({ ...e, message: t }), this.bouncingOutput();
      else {
        const d = this.getConsole(l), c = `color: ${this.levelColorMap[l]}`, s = [];
        for (const u of g)
          s.push(p(u) ? JSON.parse(JSON.stringify(u)) : u);
        d(
          `%c${a}%c${n}%c${h}%c${t}`,
          `background: ${this.color}; ${this.defaultStyle}`,
          this.getStyle(5),
          this.getStyle(l),
          c,
          ...s
        );
      }
  }
  reportInfo(t, e, ...o) {
    const i = new Intl.DateTimeFormat(["pt-PT", "de-DE", "en-GB"], {
      year: "numeric",
      month: "2-digit",
      day: "2-digit",
      hour: "2-digit",
      minute: "2-digit",
      second: "2-digit"
    }).format(Date.now());
    this.report(t, { level: r.INFO, group: e, date: i, args: o });
  }
  reportWarning(t, e, ...o) {
    const i = new Intl.DateTimeFormat(["pt-PT", "de-DE", "en-GB"], {
      year: "numeric",
      month: "2-digit",
      day: "2-digit",
      hour: "2-digit",
      minute: "2-digit",
      second: "2-digit"
    }).format(Date.now());
    this.report(t, { level: r.WARN, group: e, date: i, args: o });
  }
  reportError(t, e, ...o) {
    const i = new Intl.DateTimeFormat(["pt-PT", "de-DE", "en-GB"], {
      year: "numeric",
      month: "2-digit",
      day: "2-digit",
      hour: "2-digit",
      minute: "2-digit",
      second: "2-digit"
    }).format(Date.now());
    this.report(t, { level: r.ERROR, group: e, date: i, args: o });
  }
  getOptions() {
    return this.config;
  }
  stdout() {
    const { namespace: t, title: e } = this.config;
    console.group(
      `%c🟣 ${t.toUpperCase()}%c${e}`,
      `background: ${this.colorize(t)}; ${this.defaultStyle}`,
      this.getStyle(3)
    );
    for (const o of this.queue) {
      const { args: i = [], date: a, group: g, level: n, message: h } = o, l = this.getConsole(n), d = `color: ${this.levelColorMap[n]}`, c = [];
      for (const s of i)
        c.push(p(s) ? JSON.parse(JSON.stringify(s)) : s);
      l(`%c${a}%c${g}%c${h}`, this.getStyle(5), this.getStyle(n), d, ...c);
    }
    console.groupEnd(), this.reset();
  }
  getConsole(t) {
    let e = console.log.bind(console);
    switch (t) {
      case r.ERROR:
        e = console.error.bind(console);
        break;
      case r.INFO:
        e = console.info.bind(console);
        break;
      case r.WARN:
        e = console.warn.bind(console);
        break;
    }
    return e;
  }
  colorize(t) {
    let e = 0;
    for (let i = 0; i < t.length; e = t.charCodeAt(i++) + ((e << 5) - e)) ;
    const o = Math.floor(Math.abs(Math.sin(e) * 1e4 % 1 * 16777216)).toString(16);
    return "#" + Array(6 - o.length + 1).join("0") + o;
  }
  bouncingOutput() {
    this.timer && clearTimeout(this.timer), this.timer = setTimeout(() => this.stdout(), this.config.delay);
  }
  reset() {
    this.queue = [], this.timer && clearTimeout(this.timer);
  }
}
export {
  y as BrowserReporter
};
