// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import db from "./vendor/mime-db.v1.52.0.ts";
import type { DBEntry } from "./_util.ts";

export type KeyOfDb = keyof typeof db;

/** A map of the media type for a given extension */
export const types = new Map<string, KeyOfDb>();

/** A map of extensions for a given media type. */
const extensions: Map<string, string[]> = new Map();

/** Internal function to populate the maps based on the Mime DB. */
const preference = ["nginx", "apache", undefined, "iana"];

for (const type of Object.keys(db) as KeyOfDb[]) {
  const mime = db[type] as DBEntry;
  const exts = mime.extensions;

  if (!exts || !exts.length) {
    continue;
  }

  // @ts-ignore Work around https://github.com/denoland/dnt/issues/148
  extensions.set(type, exts);

  for (const ext of exts) {
    const current = types.get(ext);
    if (current) {
      const from = preference.indexOf((db[current] as DBEntry).source);
      const to = preference.indexOf(mime.source);

      if (
        current !== "application/octet-stream" &&
        (from > to ||
          // @ts-ignore work around https://github.com/denoland/dnt/issues/148
          (from === to && current.startsWith("application/")))
      ) {
        continue;
      }
    }

    types.set(ext, type);
  }
}

export { db, extensions };

// denoCacheMetadata={"headers":{"cf-ray":"a223e6582b9b7db6-LAX","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=v4W2gQga8PekF%2FLpiQjR00BnbGj87QGi0es74i4IU1k4HDWPQto7Q5XbMmLhcAyM0PPc9dNWIzrBVOSKlhWuMLEBOx4gR8p7ZMXRFSvvYeYdy6TwtbtyMfc%3D\"}]}","server":"cloudflare","cross-origin-resource-policy":"cross-origin","x-content-type-options":"nosniff","content-type":"text/typescript","content-length":"1347","cf-cache-status":"HIT","access-control-allow-origin":"*","x-jsr-backend":"modules","x-robots-tag":"noindex","set-cookie":"__cf_bm=K4HcMXco8wy8_NFWKsE8ColkhxvR5dUWpaOVzmLHICI-1785241875.2301912-1.0.1.1-M7DqwKJCc0DMcUTMksE27KMtIynyFAmGQkt2XM6UHd9VUxupFEP0h1aiBm0HaWlB.A0IHbVrSImabvixzJ2SbQiS7QgH9CSZIodkLqGkJIkiFjH2hkeKFCHH_olohzug; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","alt-svc":"h3=\":443\"; ma=86400","access-control-expose-headers":"*","date":"Tue, 28 Jul 2026 12:31:15 GMT","etag":"\"a5937910703dccd91b43d9b18ac32aa6\"","cache-control":"public, max-age=31536000, immutable","vary":"Origin","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","accept-ranges":"bytes","age":"112265","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';"},"url":"https://jsr.io/@std/media-types/0.224.1/_db.ts","time":1785241875}