var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m") throw new TypeError("Private method is not writable");
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _BaseHandler_levelName, _BaseHandler_level;
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.
import { getLevelByName, getLevelName, } from "./levels.js";
const DEFAULT_FORMATTER = ({ levelName, msg }) => `${levelName} ${msg}`;
/**
 * A base class for all log handlers.
 *
 * This class is abstract and should not be instantiated directly. Instead, it
 * should be extended by other classes that implement the `log` method.
 *
 * @example Usage
 * ```ts
 * import { BaseHandler } from "@std/log/base-handler";
 * import { assertInstanceOf } from "@std/assert/instance-of";
 *
 * class MyHandler extends BaseHandler {
 *   log(msg: string) {
 *     console.log(msg);
 *   }
 * }
 *
 * const handler = new MyHandler("INFO");
 * assertInstanceOf(handler, BaseHandler);
 * ```
 */
export class BaseHandler {
    /**
     * Constructs a new instance.
     *
     * @param levelName The name of the log level to handle.
     * @param options Options for the handler.
     */
    constructor(levelName, options) {
        _BaseHandler_levelName.set(this, void 0);
        _BaseHandler_level.set(this, void 0);
        /**
         * The function that formats log records.
         *
         * @example Usage
         * ```ts
         * import { BaseHandler } from "@std/log/base-handler";
         * import { LogRecord } from "@std/log/logger";
         * import { LogLevels } from "@std/log/levels";
         * import { assertEquals } from "@std/assert/equals";
         *
         * class MyHandler extends BaseHandler {
         *  log(msg: string) {
         *   console.log(msg);
         * }
         * }
         *
         * const handler = new MyHandler("INFO");
         * const record = new LogRecord({
         *   msg: "Hello, world!",
         *   args: ["foo", "bar"],
         *   level: LogLevels.INFO,
         *   loggerName: "example",
         * });
         * const formatted = handler.formatter(record);
         * assertEquals(formatted, "INFO Hello, world!");
         * ```
         */
        Object.defineProperty(this, "formatter", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        const { formatter = DEFAULT_FORMATTER } = options ?? {};
        __classPrivateFieldSet(this, _BaseHandler_levelName, levelName, "f");
        __classPrivateFieldSet(this, _BaseHandler_level, getLevelByName(levelName), "f");
        this.formatter = formatter;
    }
    /**
     * Getter for the log level that this handler will handle.
     *
     * @example Usage
     * ```ts
     * import { BaseHandler } from "@std/log/base-handler";
     * import { LogLevels } from "@std/log/levels";
     * import { assertEquals } from "@std/assert/equals";
     *
     * class MyHandler extends BaseHandler {
     *   log(msg: string) {
     *     console.log(msg);
     *   }
     * }
     *
     * const handler = new MyHandler("INFO");
     * assertEquals(handler.level, LogLevels.INFO);
     * ```
     *
     * @returns The log level to handle.
     */
    get level() {
        return __classPrivateFieldGet(this, _BaseHandler_level, "f");
    }
    /**
     * Setter for the log level that this handler will handle.
     *
     * @example Usage
     * ```ts
     * import { BaseHandler } from "@std/log/base-handler";
     * import { LogLevels } from "@std/log/levels";
     * import { assertEquals } from "@std/assert/equals";
     *
     * class MyHandler extends BaseHandler {
     *   log(msg: string) {
     *     console.log(msg);
     *   }
     * }
     *
     * const handler = new MyHandler("INFO");
     * handler.level = LogLevels.DEBUG;
     * assertEquals(handler.level, LogLevels.DEBUG);
     * ```
     *
     * @param level The log level to handle.
     */
    set level(level) {
        __classPrivateFieldSet(this, _BaseHandler_level, level, "f");
        __classPrivateFieldSet(this, _BaseHandler_levelName, getLevelName(level), "f");
    }
    /**
     * Getter for the name of the log level that this handler will handle.
     *
     * @example Usage
     * ```ts
     * import { BaseHandler } from "@std/log/base-handler";
     * import { assertEquals } from "@std/assert/equals";
     *
     * class MyHandler extends BaseHandler {
     *   log(msg: string) {
     *     console.log(msg);
     *   }
     * }
     *
     * const handler = new MyHandler("INFO");
     * assertEquals(handler.levelName, "INFO");
     * ```
     *
     * @returns The name of the log level to handle.
     */
    get levelName() {
        return __classPrivateFieldGet(this, _BaseHandler_levelName, "f");
    }
    /**
     * Setter for the name of the log level that this handler will handle.
     *
     * @param levelName The name of the log level to handle.
     *
     * @example Usage
     * ```ts
     * import { BaseHandler } from "@std/log/base-handler";
     * import { assertEquals } from "@std/assert/equals";
     *
     * class MyHandler extends BaseHandler {
     *   log(msg: string) {
     *     console.log(msg);
     *   }
     * }
     *
     * const handler = new MyHandler("INFO");
     * handler.levelName = "DEBUG";
     * assertEquals(handler.levelName, "DEBUG");
     * ```
     */
    set levelName(levelName) {
        __classPrivateFieldSet(this, _BaseHandler_levelName, levelName, "f");
        __classPrivateFieldSet(this, _BaseHandler_level, getLevelByName(levelName), "f");
    }
    /**
     * Handles a log record.
     *
     * @param logRecord The log record to handle.
     *
     * @example Usage
     * ```ts
     * import { BaseHandler } from "@std/log/base-handler";
     * import { LogRecord } from "@std/log/logger";
     * import { LogLevels } from "@std/log/levels";
     * import { assertInstanceOf } from "@std/assert/instance-of";
     *
     * class MyHandler extends BaseHandler {
     *   log(msg: string) {
     *     console.log(msg);
     *   }
     * }
     *
     * const handler = new MyHandler("INFO");
     * const record = new LogRecord({
     *   msg: "Hello, world!",
     *   args: ["foo", "bar"],
     *   level: LogLevels.INFO,
     *   loggerName: "example",
     * });
     * handler.handle(record);
     *
     * assertInstanceOf(handler, BaseHandler);
     * ```
     */
    handle(logRecord) {
        if (this.level > logRecord.level)
            return;
        const msg = this.format(logRecord);
        this.log(msg);
    }
    /**
     * Formats a log record.
     *
     * @param logRecord The log record to format.
     * @returns A string representation of the log record.
     *
     * @example Usage
     * ```ts
     * import { BaseHandler } from "@std/log/base-handler";
     * import { LogRecord } from "@std/log/logger";
     * import { LogLevels } from "@std/log/levels";
     * import { assertEquals } from "@std/assert/equals";
     *
     * class MyHandler extends BaseHandler {
     *   log(msg: string) {
     *     console.log(msg);
     *   }
     * }
     *
     * const handler = new MyHandler("INFO");
     * const record = new LogRecord({
     *   msg: "Hello, world!",
     *   args: ["foo", "bar"],
     *   level: LogLevels.INFO,
     *   loggerName: "example",
     * });
     * const formatted = handler.format(record);
     * assertEquals(formatted, "INFO Hello, world!");
     * ```
     */
    format(logRecord) {
        return this.formatter(logRecord);
    }
    /**
     * Initializes the handler.
     *
     * This method is called when the handler is added to a logger. It can be
     * used to perform any setup that is required by the handler.
     *
     * @example Usage
     * ```ts
     * import { BaseHandler } from "@std/log/base-handler";
     * import { assertInstanceOf } from "@std/assert/instance-of";
     *
     * class MyHandler extends BaseHandler {
     *   log(msg: string) {
     *     console.log(msg);
     *   }
     *
     *   override setup() {
     *     console.log("Handler setup!");
     *   }
     * }
     *
     * const handler = new MyHandler("INFO");
     * handler.setup(); // Prints "Handler setup!"
     *
     * assertInstanceOf(handler, BaseHandler);
     * ```
     */
    setup() { }
    /**
     * Destroys the handler, performing any cleanup that is required.
     *
     * This method is called when the handler is removed from a logger. It can be
     * used to perform any cleanup that is required by the handler.
     *
     * @example Usage
     * ```ts
     * import { BaseHandler } from "@std/log/base-handler";
     * import { assertInstanceOf } from "@std/assert/instance-of";
     *
     * class MyHandler extends BaseHandler {
     *   log(msg: string) {
     *     console.log(msg);
     *   }
     *
     *   override destroy() {
     *     console.log("Handler destroyed!");
     *   }
     * }
     *
     * const handler = new MyHandler("INFO");
     * handler.destroy(); // Prints "Handler destroyed!"
     * assertInstanceOf(handler, BaseHandler);
     * ```
     */
    destroy() { }
    /**
     * Automatically disposes of the handler when instantiated with the `using`
     * keyword by calling the {@linkcode BaseHandler.destroy} method.
     *
     * @example Usage
     * ```ts
     * import { BaseHandler } from "@std/log/base-handler";
     * import { LogRecord } from "@std/log/logger";
     * import { assertInstanceOf } from "@std/assert/instance-of";
     *
     * class MyHandler extends BaseHandler {
     *   log(msg: string) {
     *     console.log(msg);
     *   }
     * }
     *
     * using handler = new MyHandler("INFO");
     * assertInstanceOf(handler, BaseHandler);
     * ```
     */
    [(_BaseHandler_levelName = new WeakMap(), _BaseHandler_level = new WeakMap(), Symbol.dispose)]() {
        this.destroy();
    }
}
