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

/**
 * Use this to retrieve the numeric log level by it's associated name.
 * Defaults to INFO.
 */
export const LogLevels = {
  NOTSET: 0,
  DEBUG: 10,
  INFO: 20,
  WARN: 30,
  ERROR: 40,
  CRITICAL: 50,
} as const;

/** Union of valid log levels */
export type LogLevel = typeof LogLevels[LevelName];

/** Union of valid log level names */
export type LevelName = Exclude<keyof typeof LogLevels, number>;

/** Permitted log level names */
export const LogLevelNames: LevelName[] = Object.keys(LogLevels).filter((key) =>
  isNaN(Number(key))
) as LevelName[];

const byLevel: Record<LogLevel, LevelName> = {
  [LogLevels.NOTSET]: "NOTSET",
  [LogLevels.DEBUG]: "DEBUG",
  [LogLevels.INFO]: "INFO",
  [LogLevels.WARN]: "WARN",
  [LogLevels.ERROR]: "ERROR",
  [LogLevels.CRITICAL]: "CRITICAL",
};

/**
 * Returns the numeric log level associated with the passed,
 * stringy log level name.
 */
export function getLevelByName(name: LevelName): LogLevel {
  const level = LogLevels[name];
  if (level !== undefined) {
    return level;
  }
  throw new Error(`Cannot get log level: no level named ${name}`);
}

/** Returns the stringy log level name provided the numeric log level. */
export function getLevelName(level: LogLevel): LevelName {
  const levelName = byLevel[level as LogLevel];
  if (levelName) {
    return levelName;
  }
  throw new Error(`Cannot get log level: no name for level: ${level}`);
}

// denoCacheMetadata={"headers":{"access-control-allow-origin":"*","access-control-expose-headers":"*","server":"cloudflare","date":"Tue, 28 Jul 2026 12:31:15 GMT","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';","alt-svc":"h3=\":443\"; ma=86400","etag":"\"f2a3dc1b834c3d3cc8ff74c2462ba369\"","x-robots-tag":"noindex","x-content-type-options":"nosniff","set-cookie":"__cf_bm=Wtkl6jX92jq5WT_aRgT9G.IjFbYf.Z.y8zMKr3gESAQ-1785241875.1024015-1.0.1.1-U_IhuZwBSSAec0OYxSdtLgMncEka00IQW0rHRmk4_JeOMLDgyI869Ob6y0GHgo_liSKRm5M3H1D011hPLPXCk58R_H191XNm9nahs3f9bMC9DgEY10ByF.C6Xq6Bi9XL; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","content-type":"text/typescript","cache-control":"public, max-age=31536000, immutable","x-jsr-backend":"modules","cf-ray":"a223e657697a7db6-LAX","cross-origin-resource-policy":"cross-origin","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=VNFI4l2WcxKYzs%2FUbQ8B7MFK1knUJstR7ZAmHeCgxit67Wtmr5l01Dcncmi%2FPvLSd3%2BMi0Y6RgA6ydXatems9EeS5jro9YOhM%2FMb0e%2BLtNmG3g0ijxL7aJ8%3D\"}]}","content-length":"1522","vary":"Origin","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}"},"url":"https://jsr.io/@std/log/0.224.7/levels.ts","time":1785241875}