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

/** Return type for {@linkcode invertBy}. */
export type InvertByResult<
  T extends Record<PropertyKey, PropertyKey>,
  K extends keyof T,
> = Record<PropertyKey, K[]>;

/**
 * Composes a new record with all keys and values inverted.
 *
 * The new record is generated from the result of running each element of the
 * input record through the given transformer function.
 *
 * The corresponding inverted value of each inverted key is an array of keys
 * responsible for generating the inverted value.
 *
 * @template R The type of the input record.
 * @template T The type of the iterator function.
 *
 * @param record The record to invert.
 * @param transformer The function to transform keys.
 *
 * @returns A new record with all keys and values inverted.
 *
 * @example Basic usage
 * ```ts
 * import { invertBy } from "@std/collections/invert-by";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const record = { a: "x", b: "y", c: "z" };
 *
 * assertEquals(
 *   invertBy(record, (key) => String(key).toUpperCase()),
 *   { X: ["a"], Y: ["b"], Z: ["c"] }
 * );
 * ```
 */
export function invertBy<
  R extends Record<PropertyKey, PropertyKey>,
  T extends (key: PropertyKey) => PropertyKey,
>(record: Readonly<R>, transformer: T): InvertByResult<R, keyof R> {
  const result = {} as InvertByResult<R, keyof R>;

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

  return result;
}

// denoCacheMetadata={"headers":{"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';","x-content-type-options":"nosniff","server":"cloudflare","x-robots-tag":"noindex","access-control-allow-origin":"*","cf-ray":"a223e6564f037db6-LAX","content-length":"1705","cache-control":"public, max-age=31536000, immutable","vary":"Origin","access-control-expose-headers":"*","date":"Tue, 28 Jul 2026 12:31:15 GMT","cross-origin-resource-policy":"cross-origin","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=JfijPO%2BVIuN2Pzkxu%2BObhiEbDRxfEiDz6oKbZNsxwXHFCKDe%2FXADCTu1ZqklDr808b3C9Qj7PsnNu9ZgIn3SRXB20N%2BCvEpiXCsmZeyvomRwsH1pUgExEYY%3D\"}]}","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","content-type":"text/typescript","alt-svc":"h3=\":443\"; ma=86400","x-jsr-backend":"modules","etag":"\"4247ce16be76c00a6573024ccb976ecf\"","set-cookie":"__cf_bm=YJke5FOAHD7qjbWzrMDp.cflUvFmrYrIuOitKQRDs8I-1785241874.9560893-1.0.1.1-g_AJeSGn0pF7x_2kVECleR8sOslRroo31DmBwVTkJEAZxv5yememYEKYU4xzlHg9QdtZJRjBhtTrHIVniTsEncRt3vTj75owaqGcInN431VRf6wUMTu4S06j8OM6V3CW; 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/invert_by.ts","time":1785241875}