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

import { parseMediaType } from "./parse_media_type.ts";
import { extensions } from "./_db.ts";

export { extensions };

/**
 * Returns the extensions known to be associated with the media type `type`, or
 * `undefined` if no extensions are found.
 *
 * Extensions are returned without a leading `.`.
 *
 * @param type The media type to get the extensions for.
 *
 * @returns The extensions for the given media type, or `undefined` if no
 * extensions are found.
 *
 * @example
 * ```ts
 * import { extensionsByType } from "@std/media-types/extensions-by-type";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * assertEquals(extensionsByType("application/json"), ["json", "map"]);
 * assertEquals(extensionsByType("text/html; charset=UTF-8"), ["html", "htm", "shtml"]);
 * assertEquals(extensionsByType("application/foo"), undefined);
 * ```
 */
export function extensionsByType(type: string): string[] | undefined {
  try {
    const [mediaType] = parseMediaType(type);
    return extensions.get(mediaType);
  } catch {
    // just swallow errors, returning undefined
  }
}

// denoCacheMetadata={"headers":{"access-control-allow-origin":"*","etag":"\"3a20f5091281016f847457361106f068\"","x-jsr-backend":"modules","cross-origin-resource-policy":"cross-origin","date":"Tue, 28 Jul 2026 12:31:15 GMT","vary":"Origin","content-length":"1205","set-cookie":"__cf_bm=HrMvpDiAqD1P9wlgLArb.zXbDASZ3LN02VhsyIGH4hg-1785241875.1274629-1.0.1.1-Q96LQqnCyUBGiesvbUlgWU2rqlA9v3SiS.T2MM.f5.CvuRU2rvsQEPYjGbDlDIBocal5YZc66Mcg8HIezuile6WSgqVzSCH81NXTBh6MiBWn1Z.7Qmq42LmxYn1LdprD; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","content-type":"text/typescript","x-robots-tag":"noindex","alt-svc":"h3=\":443\"; ma=86400","x-content-type-options":"nosniff","cache-control":"public, max-age=31536000, immutable","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';","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=X13BnAODCA8jIIvS42bxYgqBjHwRnx9QfNdy95zNDj0wiTWls%2F6bNj0z9HRUaRVeeGVsBi%2F0w%2BV%2FoF6G8zYciM%2BPy%2F%2Fvog0OUXwvdEhz1dsvn98RCo2DlGQ%3D\"}]}","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","server":"cloudflare","access-control-expose-headers":"*","cf-ray":"a223e65789d57db6-LAX"},"url":"https://jsr.io/@std/media-types/0.224.1/extensions_by_type.ts","time":1785241875}