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

/**
 * Applies the given selector to all elements of the given collection and
 * returns the min value of all elements. If an empty array is provided the
 * function will return undefined.
 *
 * @template T The type of the elements in the array.
 *
 * @param array The array to find the minimum element in.
 * @param selector The function to get the value to compare from each element.
 *
 * @returns The smallest value of the given function or undefined if there are
 * no elements.
 *
 * @example Basic usage
 * ```ts
 * import { minOf } from "@std/collections/min-of";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const inventory = [
 *   { name: "mustard", count: 2 },
 *   { name: "soy", count: 4 },
 *   { name: "tomato", count: 32 },
 * ];
 *
 * const minCount = minOf(inventory, (item) => item.count);
 *
 * assertEquals(minCount, 2);
 * ```
 */
export function minOf<T>(
  array: Iterable<T>,
  selector: (el: T) => number,
): number | undefined;
/**
 * Applies the given selector to all elements of the given collection and
 * returns the min value of all elements. If an empty array is provided the
 * function will return undefined.
 *
 * @template T The type of the elements in the array.
 *
 * @param array The array to find the minimum element in.
 * @param selector The function to get the value to compare from each element.
 *
 * @returns The first element that is the smallest value of the given function
 * or undefined if there are no elements.
 *
 * @example Basic usage
 * ```ts
 * import { minOf } from "@std/collections/min-of";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const inventory = [
 *   { name: "mustard", count: 2n },
 *   { name: "soy", count: 4n },
 *   { name: "tomato", count: 32n },
 * ];
 *
 * const minCount = minOf(inventory, (item) => item.count);
 *
 * assertEquals(minCount, 2n);
 * ```
 */
export function minOf<T>(
  array: Iterable<T>,
  selector: (el: T) => bigint,
): bigint | undefined;
export function minOf<T, S extends ((el: T) => number) | ((el: T) => bigint)>(
  array: Iterable<T>,
  selector: S,
): ReturnType<S> | undefined {
  let minimumValue: ReturnType<S> | undefined;

  for (const element of array) {
    const currentValue = selector(element) as ReturnType<S>;

    if (minimumValue === undefined || currentValue < minimumValue) {
      minimumValue = currentValue;
      continue;
    }

    if (Number.isNaN(currentValue)) {
      return currentValue;
    }
  }

  return minimumValue;
}

// denoCacheMetadata={"headers":{"server":"cloudflare","cross-origin-resource-policy":"cross-origin","set-cookie":"__cf_bm=cmIfLo6_1LDx6NTpt9RD_x3oSyTHPsiD9Vdk5_iM5OM-1785241874.9605021-1.0.1.1-29fGTgv_nyScLVlyXjI3DWuY5wMB9k6Iwgft1BDJ1U6Ix.uwcBDUuGyoFKGi4aQG5dZ8eYLpsYwnQVXWNTBHUatEQUpgz7.GvikPuFFv5JiledKle2cn5pJFl7qnsgZf; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","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-jsr-backend":"modules","date":"Tue, 28 Jul 2026 12:31:15 GMT","cf-ray":"a223e6564f227db6-LAX","access-control-allow-origin":"*","content-type":"text/typescript","cache-control":"public, max-age=31536000, immutable","content-length":"2618","vary":"Origin","x-content-type-options":"nosniff","access-control-expose-headers":"*","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=xYqLSWPigvZdFrF9AmMA%2BGQ5ZaSiOEH3ril2kkrFCdED1s5aYTCyF2NZRP5M86T31jN3wd4XAYwbFg54UoT%2BrQZpJeH2jgMe664feZKdqEVDo5szs%2B8lFN8%3D\"}]}","etag":"\"a0ec7e7ca14ee727e48aa24299028923\"","x-robots-tag":"noindex","alt-svc":"h3=\":443\"; ma=86400"},"url":"https://jsr.io/@std/collections/0.224.2/min_of.ts","time":1785241875}