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

/**
 * Returns all elements in the given array after the last element that does not
 * match the given predicate.
 *
 * @template T The type of the array elements.
 *
 * @param array The array to take elements from.
 * @param predicate The predicate function to determine if an element should be
 * included.
 *
 * @returns A new array containing all elements after the last element that does
 * not match the predicate.
 *
 * @example Basic usage
 * ```ts
 * import { takeLastWhile } from "@std/collections/take-last-while";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const numbers = [1, 2, 3, 4, 5, 6];
 *
 * const result = takeLastWhile(numbers, (number) => number > 4);
 *
 * assertEquals(result, [5, 6]);
 * ```
 */
export function takeLastWhile<T>(
  array: readonly T[],
  predicate: (el: T) => boolean,
): T[] {
  let offset = array.length;
  while (0 < offset && predicate(array[offset - 1] as T)) offset--;

  return array.slice(offset, array.length);
}

// denoCacheMetadata={"headers":{"cf-ray":"a223e6564f407db6-LAX","server":"cloudflare","vary":"Origin","x-robots-tag":"noindex","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-jsr-backend":"modules","x-content-type-options":"nosniff","etag":"\"c2710105ff9523edeee765e18c9a1aab\"","cross-origin-resource-policy":"cross-origin","cache-control":"public, max-age=31536000, immutable","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","date":"Tue, 28 Jul 2026 12:31:15 GMT","content-type":"text/typescript","content-length":"1102","access-control-expose-headers":"*","set-cookie":"__cf_bm=WrTA7kJnY2Ust7vCxky9t8jdFFWNP7TwUC0.Rf8.Iz4-1785241874.9659705-1.0.1.1-HDKjXGnW_rANtX1JEPjypAQDOBbONGLyNjOy8M4xs2Yy.VKWHxTE2SnaXRTVmRVi_JIVg2H4nH0Zsi1MWTERiVr7gkeF0mxcNyTWAx0KhOHBvDoEVMaENdv4XngGlB6T; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=m%2F7imzlszalZ14JIcekham5wtSRqISHKEcMbnqVumMJ%2F6WiBgkGK7gBd7qi1haccc%2BwEj6by%2F6F1wCNca0HG8jO6%2B7oLBGCqwvdnPSVXvAGbixMNKuYc4aY%3D\"}]}","alt-svc":"h3=\":443\"; ma=86400"},"url":"https://jsr.io/@std/collections/0.224.2/take_last_while.ts","time":1785241875}