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

import { types } from "./_db.ts";

/**
 * Returns the media type associated with the file extension, or `undefined` if
 * no media type is found.
 *
 * Values are normalized to lower case and matched irrespective of a leading
 * `.`.
 *
 * @param extension The file extension to get the media type for.
 *
 * @returns The media type associated with the file extension, or `undefined` if
 * no media type is found.
 *
 * @example Usage
 * ```ts
 * import { typeByExtension } from "@std/media-types/type-by-extension";
 * import { assertEquals } from "@std/assert";
 *
 * assertEquals(typeByExtension("js"), "text/javascript");
 * assertEquals(typeByExtension(".HTML"), "text/html");
 * assertEquals(typeByExtension("foo"), undefined);
 * assertEquals(typeByExtension("file.json"), undefined);
 * ```
 */
export function typeByExtension(extension: string): string | undefined {
  extension = extension.startsWith(".") ? extension.slice(1) : extension;
  // @ts-ignore Work around https://github.com/denoland/dnt/issues/148
  return types.get(extension.toLowerCase());
}

// denoCacheMetadata={"headers":{"x-robots-tag":"noindex","vary":"Origin","accept-ranges":"bytes","access-control-expose-headers":"*","content-type":"text/typescript","age":"29176","cf-cache-status":"HIT","server":"cloudflare","x-jsr-backend":"modules","alt-svc":"h3=\":443\"; ma=86400","x-content-type-options":"nosniff","cf-ray":"a223e658fe197db6-LAX","set-cookie":"__cf_bm=qpLMSvCL8IloSS.SLWObIQtu8S54cIxy7pPQ_o1Jc24-1785241875.363035-1.0.1.1-XbUT48fPm53ro4blU1Zafk1N4ZGANhfE.ZwKVJXWHVLjMozqMFEChXz4n2IS3Xmt0ZJ435rw0wsfbTFE5D46Xyst53_zuzRThDp9ac3wOmh7PLkBlurMTQWTP78yAlhd; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=sEYjwJt2qdgWR%2BWw4swXscQx%2Fu8uk4USeLv2McegSORtovySIPcoDiiTcazl2VTmT%2BLSMfPQGGrQD3%2B%2BdNHy%2F1Q4rVEPQaQ%2BpPB9WkgsgusaIc9lFNzZzV8%3D\"}]}","cross-origin-resource-policy":"cross-origin","cache-control":"public, max-age=31536000, immutable","access-control-allow-origin":"*","content-length":"1182","etag":"\"3f04a01b145c0f263b2acf3cc2113388\"","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';","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","date":"Tue, 28 Jul 2026 12:31:15 GMT"},"url":"https://jsr.io/@std/media-types/1.0.3/type_by_extension.ts","time":1785241875}