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

/**
 * Applies the given transformer to all keys in the given record's entries and
 * returns a new record containing the transformed entries.
 *
 * If the transformed entries contain the same key multiple times, only the last
 * one will appear in the returned record.
 *
 * @template T The type of the values in the input record.
 *
 * @param record The record to map keys from.
 * @param transformer The function to transform each key.
 *
 * @returns A new record with all keys transformed by the given transformer.
 *
 * @example Basic usage
 * ```ts
 * import { mapKeys } from "@std/collections/map-keys";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const counts = { a: 5, b: 3, c: 8 };
 *
 * assertEquals(
 *   mapKeys(counts, (key) => key.toUpperCase()),
 *   {
 *     A: 5,
 *     B: 3,
 *     C: 8,
 *   },
 * );
 * ```
 */
export function mapKeys<T>(
  record: Readonly<Record<string, T>>,
  transformer: (key: string) => string,
): Record<string, T> {
  const result: Record<string, T> = {};

  for (const [key, value] of Object.entries(record)) {
    const mappedKey = transformer(key);
    result[mappedKey] = value;
  }

  return result;
}

// denoCacheMetadata={"headers":{"cross-origin-resource-policy":"cross-origin","vary":"Origin","x-content-type-options":"nosniff","alt-svc":"h3=\":443\"; ma=86400","x-jsr-backend":"modules","access-control-allow-origin":"*","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=T.mtml46BKXX2rifw3aFsfxGpZp2_jhF9luW5PPjmk4-1785241874.9578357-1.0.1.1-yapSKOVpqP0lX97Cjnmbwmv3L88qsXuQDzOaJodgGaA9U2lhOofUIZNZS8yq2bKsUnOASmTJpiDC5DiLwCRy__RUYUmtgIpmTOsLNjC_KfCzvBpiuSEVBw0S2FHDG4kk; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","content-length":"1291","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","content-type":"text/typescript","server":"cloudflare","x-robots-tag":"noindex","access-control-expose-headers":"*","cf-ray":"a223e6564f137db6-LAX","etag":"\"1946a0ca31cb65e1b0587fe4b9e6a280\"","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=cBZt2vqv423IJbOmcXSDySAeYZmidNKcvTAz8fganb9NKtnKg7mkbPPI5RAEvTNmyT5HM3OFQckLH%2FyRyWUYKPuFG8FeNI7g3x9PJFFFl4TXzaOwxTUw%2BFw%3D\"}]}","cache-control":"public, max-age=31536000, immutable","date":"Tue, 28 Jul 2026 12:31:15 GMT"},"url":"https://jsr.io/@std/collections/0.224.2/map_keys.ts","time":1785241875}