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

import { parseMediaType } from "./parse_media_type.ts";
import type { DBEntry } from "./_util.ts";
import { db, type KeyOfDb } from "./_db.ts";

/**
 * Given a media type or header value, identify the encoding charset. If the
 * charset cannot be determined, the function returns `undefined`.
 *
 * @param type The media type or header value to get the charset for.
 *
 * @returns The charset for the given media type or header value, or `undefined`
 * if the charset cannot be determined.
 *
 * @example Usage
 * ```ts
 * import { getCharset } from "@std/media-types/get-charset";
 * import { assertEquals } from "@std/assert";
 *
 * assertEquals(getCharset("text/plain"), "UTF-8");
 * assertEquals(getCharset("application/foo"), undefined);
 * assertEquals(getCharset("application/news-checkgroups"), "US-ASCII");
 * assertEquals(getCharset("application/news-checkgroups; charset=UTF-8"), "UTF-8");
 * ```
 */
export function getCharset(type: string): string | undefined {
  try {
    const [mediaType, params] = parseMediaType(type);
    if (params?.charset) {
      return params.charset;
    }
    const entry = db[mediaType as KeyOfDb] as DBEntry;
    if (entry?.charset) {
      return entry.charset;
    }
    if (mediaType.startsWith("text/")) {
      return "UTF-8";
    }
  } catch {
    // just swallow errors, returning undefined
  }
  return undefined;
}

// denoCacheMetadata={"headers":{"content-length":"1483","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';","x-robots-tag":"noindex","alt-svc":"h3=\":443\"; ma=86400","cross-origin-resource-policy":"cross-origin","cf-ray":"a223e658fe117db6-LAX","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","date":"Tue, 28 Jul 2026 12:31:15 GMT","content-type":"text/typescript","x-jsr-backend":"modules","etag":"\"fed32021fd7f43294657bb7d5a64e657\"","x-content-type-options":"nosniff","accept-ranges":"bytes","set-cookie":"__cf_bm=_u9v1eApKdZWBSAQynk4G0o9MAkQOwo5phlH6yKqAvc-1785241875.360486-1.0.1.1-Xjz.5F9oO4nkR7xVzIXMggihS9nRm4Z2Q48ZDOWLo.NoJvRf2BYV0b2okkgxSlLcVW0.0XFLPQgnEhQDsDW5LP0MMs1trknuXEAhsrh4.eU9daQPssnaqc2IY0NAU.FQ; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","cache-control":"public, max-age=31536000, immutable","access-control-expose-headers":"*","age":"29176","access-control-allow-origin":"*","cf-cache-status":"HIT","server":"cloudflare","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=x491TiZ%2Bqs6FdNwN6lW88ibKygDKTY27iMiT2GtmmpTg6AO%2FxH1Hx%2BRoFLccUT9BCY8BiHinuKeQ2ax%2B7VmhiS%2F%2Bx8TBgQc8%2FCKtgRcoomrFblLOSm%2BS78A%3D\"}]}","vary":"Origin"},"url":"https://jsr.io/@std/media-types/1.0.3/get_charset.ts","time":1785241875}