// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import {
  getLevelByName,
  getLevelName,
  type LevelName,
  type LogLevel,
} from "./levels.ts";
import type { LogRecord } from "./logger.ts";

export type FormatterFunction = (logRecord: LogRecord) => string;
const DEFAULT_FORMATTER: FormatterFunction = ({ levelName, msg }) =>
  `${levelName} ${msg}`;

export interface BaseHandlerOptions {
  formatter?: FormatterFunction;
}

export abstract class BaseHandler {
  #levelName: LevelName;
  #level: LogLevel;
  formatter: FormatterFunction;

  constructor(
    levelName: LevelName,
    options?: BaseHandlerOptions,
  ) {
    const { formatter = DEFAULT_FORMATTER } = options ?? {};
    this.#levelName = levelName;
    this.#level = getLevelByName(levelName);
    this.formatter = formatter;
  }

  get level(): LogLevel {
    return this.#level;
  }

  set level(level: LogLevel) {
    this.#level = level;
    this.#levelName = getLevelName(level);
  }

  get levelName(): LevelName {
    return this.#levelName;
  }
  set levelName(levelName: LevelName) {
    this.#levelName = levelName;
    this.#level = getLevelByName(levelName);
  }

  handle(logRecord: LogRecord) {
    if (this.level > logRecord.level) return;

    const msg = this.format(logRecord);
    this.log(msg);
  }

  format(logRecord: LogRecord): string {
    return this.formatter(logRecord);
  }

  abstract log(msg: string): void;
  setup() {}
  destroy() {}

  [Symbol.dispose]() {
    this.destroy();
  }
}

// denoCacheMetadata={"headers":{"nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","x-robots-tag":"noindex","x-jsr-backend":"modules","content-length":"1552","x-content-type-options":"nosniff","access-control-allow-origin":"*","vary":"Origin","etag":"\"d80cd6caadbce2d095a4d1ba32c3fac9\"","date":"Tue, 28 Jul 2026 12:31:15 GMT","cross-origin-resource-policy":"cross-origin","content-security-policy":"default-src 'none'; script-src 'none'; style-src 'none'; img-src 'none'; font-src 'none'; connect-src 'none'; frame-src 'none'; object-src 'none'; frame-ancestors 'none'; sandbox; form-action 'none';","set-cookie":"__cf_bm=uaYhF84B5qW7hZM3RdZwCCt2v47W.g5M0z1dm0t7ocU-1785241875.091874-1.0.1.1-gvHASdMgkaec8NkdrOWZgHfTz9RCoDP.KPENz_GdSMatzoJjJqHnCM.MisdPp7.gGUHfxiDVMSYYvuo6b6.pCTKg0WSQ0cCmv_Ve3rkgvTZiw8nDnP75rN2H6p1LsgYY; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","alt-svc":"h3=\":443\"; ma=86400","cache-control":"public, max-age=31536000, immutable","access-control-expose-headers":"*","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=gwC%2BBCQwMzuXAcQ7qOPgRrKg9Sjg4nsSmpW2Jv0oxoLxw5KmegDGSnXtA1qSledfiU0vGzGzDF0hHb3ATmxFvv%2B8XJZ1eatU0%2BlsOCRcTXe1Z1gVDo2NweQ%3D\"}]}","content-type":"text/typescript","cf-ray":"a223e657595e7db6-LAX","server":"cloudflare"},"url":"https://jsr.io/@std/log/0.224.7/base_handler.ts","time":1785241875}