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

/**
 * Applies the given selector to elements in the given array until a value is
 * produced that is neither `null` nor `undefined` and returns that value.
 * Returns `undefined` if no such value is produced.
 *
 * @template T The type of the elements in the input array.
 * @template O The type of the value produced by the selector function.
 *
 * @param array The array to select a value from.
 * @param selector The function to extract a value from an element.
 *
 * @returns The first non-`null` and non-`undefined` value produced by the
 * selector function, or `undefined` if no such value is produced.
 *
 * @example Basic usage
 * ```ts
 * import { firstNotNullishOf } from "@std/collections/first-not-nullish-of";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const tables = [
 *   { number: 11, order: null },
 *   { number: 12, order: "Soup" },
 *   { number: 13, order: "Salad" },
 * ];
 *
 * const nextOrder = firstNotNullishOf(tables, (table) => table.order);
 *
 * assertEquals(nextOrder, "Soup");
 * ```
 */
export function firstNotNullishOf<T, O>(
  array: Iterable<T>,
  selector: (item: T) => O | undefined | null,
): NonNullable<O> | undefined {
  for (const current of array) {
    const selected = selector(current);

    if (selected !== null && selected !== undefined) {
      return selected as NonNullable<O>;
    }
  }

  return undefined;
}

// denoCacheMetadata={"headers":{"access-control-expose-headers":"*","etag":"\"b89e70afb8f7d3262dd1cce886be7f2c\"","content-type":"text/typescript","cross-origin-resource-policy":"cross-origin","set-cookie":"__cf_bm=47BUNo7WUkKoJS.GKqBz8bYLkL3P8x4WUW8KQzNtHy8-1785241874.9545352-1.0.1.1-BEEQHPRtlPBlMFw28v331EKODaKEh7iADBzy5CcEAFzw2S2.Jtv0.aaXmDRIEYWvTA8bp1k2PfvkTFCETBDF80eIoYnuyyILfgea0ZvjC7uVhtTYYvoZkAKYhgHR0qV7; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","alt-svc":"h3=\":443\"; ma=86400","x-robots-tag":"noindex","cache-control":"public, max-age=31536000, immutable","vary":"Origin","date":"Tue, 28 Jul 2026 12:31:15 GMT","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';","x-content-type-options":"nosniff","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=wN6%2FLTt1IMfSNmDbOFsx3%2FkJVWnjoXlz9yFx7woDaZf9Y10cTNi6b66XoA5KQo14KimeQsPHrz1E6OkKhwGJWzE6xS50IfbOl3myVjwYfTONflykHF2Cv%2B4%3D\"}]}","x-jsr-backend":"modules","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","content-length":"1506","server":"cloudflare","cf-ray":"a223e6564efd7db6-LAX"},"url":"https://jsr.io/@std/collections/0.224.2/first_not_nullish_of.ts","time":1785241875}