// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

import type { BaseHandler } from "./base_handler.ts";
import { DEFAULT_CONFIG, DEFAULT_LEVEL } from "./_config.ts";
import { type LogConfig, Logger } from "./logger.ts";
import { state } from "./_state.ts";

/** Setup logger config. */
export function setup(config: LogConfig) {
  state.config = {
    handlers: { ...DEFAULT_CONFIG.handlers, ...config.handlers },
    loggers: { ...DEFAULT_CONFIG.loggers, ...config.loggers },
  };

  // tear down existing handlers
  state.handlers.forEach((handler) => {
    handler.destroy();
  });
  state.handlers.clear();

  // setup handlers
  const handlers = state.config.handlers ?? {};

  for (const [handlerName, handler] of Object.entries(handlers)) {
    handler.setup();
    state.handlers.set(handlerName, handler);
  }

  // remove existing loggers
  state.loggers.clear();

  // setup loggers
  const loggers = state.config.loggers ?? {};
  for (const [loggerName, loggerConfig] of Object.entries(loggers)) {
    const handlerNames = loggerConfig.handlers ?? [];
    const handlers: BaseHandler[] = [];

    handlerNames.forEach((handlerName) => {
      const handler = state.handlers.get(handlerName);
      if (handler) {
        handlers.push(handler);
      }
    });

    const levelName = loggerConfig.level ?? DEFAULT_LEVEL;
    const logger = new Logger(loggerName, levelName, { handlers: handlers });
    state.loggers.set(loggerName, logger);
  }
}

setup(DEFAULT_CONFIG);

// denoCacheMetadata={"headers":{"report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=dFgcgwSojLBcHioaeVwD%2BtOp3g95Tiv18Da4SQDRp8LkF3lRF5PXuj1Flb8e5C%2BlXi64iyl7alHGjoPcGjWqeshQFW%2BW4Xt9l6py90bgHySgygqySi8xfgU%3D\"}]}","x-robots-tag":"noindex","etag":"\"8b007ec97e97e650c599fd61c8ca038a\"","content-type":"text/typescript","vary":"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';","date":"Tue, 28 Jul 2026 12:31:15 GMT","access-control-expose-headers":"*","access-control-allow-origin":"*","cross-origin-resource-policy":"cross-origin","set-cookie":"__cf_bm=IKT8xmHkXQ5AxwM7.x19VieNThJNAm.ifBCbTy8afaM-1785241875.1261568-1.0.1.1-fX2o4jjKwB6AV7zs7SjJ4xz4FR2toalcE_FIYCffRWOYd55_YT0tZzqnhpeBxtrCyGFl2jg8b9n_Auuj5Mo6Ovyys4PmXABHqN4V4QuzwmZNjJQvUMEH_QVOO3WptPEq; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","server":"cloudflare","cache-control":"public, max-age=31536000, immutable","cf-ray":"a223e65789cf7db6-LAX","alt-svc":"h3=\":443\"; ma=86400","x-jsr-backend":"modules","content-length":"1548","x-content-type-options":"nosniff"},"url":"https://jsr.io/@std/log/0.224.7/setup.ts","time":1785241875}