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

import { extensionsByType } from "./extensions_by_type.ts";

/**
 * Returns the most relevant extension for the given media type, or `undefined`
 * if no extension can be found.
 *
 * Extensions are returned without a leading `.`.
 *
 * @param type The media type to get the extension for.
 *
 * @returns The extension for the given media type, or `undefined` if no
 * extension is found.
 *
 * @example Usage
 * ```ts
 * import { extension } from "@std/media-types/extension";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * assertEquals(extension("text/plain"), "txt");
 * assertEquals(extension("application/json"), "json");
 * assertEquals(extension("text/html; charset=UTF-8"), "html");
 * assertEquals(extension("application/foo"), undefined);
 * ```
 */
export function extension(type: string): string | undefined {
  return extensionsByType(type)?.[0];
}

// denoCacheMetadata={"headers":{"alt-svc":"h3=\":443\"; ma=86400","date":"Tue, 28 Jul 2026 12:31:15 GMT","content-type":"text/typescript","etag":"\"99e068bf8e6f90454f4ccab1cb829d93\"","x-content-type-options":"nosniff","cache-control":"public, max-age=31536000, immutable","vary":"Origin","content-length":"996","x-robots-tag":"noindex","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';","set-cookie":"__cf_bm=7YLXjLeWVDX62V.nLnjk0XULxqO2Vqs5Ji7SbeDcUig-1785241875.1270354-1.0.1.1-gfmJPdp6MqJBj6mTKAx2tFrlbFSC7CUbpe3uP7_fH.z3DzvyNM5gSNLN.3AmRcqIYuK_993JvQV2duysXCdoHaDerXt4oFVrFWbFqzPXxy2epJF_emPGqHQzXyLGGu0k; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","server":"cloudflare","x-jsr-backend":"modules","access-control-expose-headers":"*","cf-ray":"a223e65789d37db6-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=RDlUKzLnArNlFjGqqgyYXCypkQJoqUnMwZmGwLTOZQsHIKlYa0%2FAPfxJMhB3ye7Zda8WNOxHsnlIhodevoE%2FAbGiWTOzqQVsrqB0gmPfkd1hnY5S%2BP8yDgw%3D\"}]}","access-control-allow-origin":"*","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}"},"url":"https://jsr.io/@std/media-types/0.224.1/extension.ts","time":1785241875}