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

/**
 * Applies the given transformer to all entries in the given record and returns
 * a new record containing the results.
 *
 * @template T The type of the values in the input record.
 * @template O The type of the values in the output record.
 *
 * @param record The record to map entries from.
 * @param transformer The function to transform each entry.
 *
 * @returns A new record with all entries transformed by the given transformer.
 *
 * @example Basic usage
 * ```ts
 * import { mapEntries } from "@std/collections/map-entries";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const usersById = {
 *   "a2e": { name: "Kim", age: 22 },
 *   "dfe": { name: "Anna", age: 31 },
 *   "34b": { name: "Tim", age: 58 },
 * };
 *
 * const agesByNames = mapEntries(usersById, ([id, { name, age }]) => [name, age]);
 *
 * assertEquals(
 *   agesByNames,
 *   {
 *     Kim: 22,
 *     Anna: 31,
 *     Tim: 58,
 *   },
 * );
 * ```
 */
export function mapEntries<T, O>(
  record: Readonly<Record<string, T>>,
  transformer: (entry: [string, T]) => [string, O],
): Record<string, O> {
  const result: Record<string, O> = {};
  const entries = Object.entries(record);

  for (const entry of entries) {
    const [mappedKey, mappedValue] = transformer(entry);

    result[mappedKey] = mappedValue;
  }

  return result;
}

// denoCacheMetadata={"headers":{"date":"Tue, 28 Jul 2026 12:31:15 GMT","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';","content-length":"1450","server":"cloudflare","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","vary":"Origin","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=%2Btz10LagW0GDZC1RChQU6MHC9Z2UTu5GkxPgjatoOi5FUEDMPqwZ3lTtGgSC5VCvy09xx3M0uvf5%2FnUrNdLqjz3Gmi%2Bpuh%2Fd5abCKsBOYyKmIwvxt%2FfX6y8%3D\"}]}","content-type":"text/typescript","alt-svc":"h3=\":443\"; ma=86400","x-content-type-options":"nosniff","access-control-allow-origin":"*","access-control-expose-headers":"*","x-robots-tag":"noindex","cf-ray":"a223e6564f117db6-LAX","etag":"\"5cfe73fedbeccf66cb0f067bbf50d9f5\"","x-jsr-backend":"modules","cross-origin-resource-policy":"cross-origin","cache-control":"public, max-age=31536000, immutable","set-cookie":"__cf_bm=ubVIYDfVTDc_wf.JaAmZ00IZUEu5SLsqprs7euBomuY-1785241874.9574702-1.0.1.1-gPYedG9.x.ELmCdFb_B6Cqn4jkQmx0C5HrH7AGAZY4BnjcWWMv2AXhnan_E71qjZBIFV6kBwV5DYW1cdWl0dRXnYKv_jNW8_FKCH.l222X4QXwUrDU.ta2TlTJUISHkd; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT"},"url":"https://jsr.io/@std/collections/0.224.2/map_entries.ts","time":1785241875}