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

/**
 * Returns a new array that drops all elements in the given collection until the
 * last element that does not match the given predicate.
 *
 * @template T The type of the elements in the input array.
 *
 * @param array The array to drop elements from.
 * @param predicate The function to test each element for a condition.
 *
 * @returns A new array that drops all elements until the last element that does
 * not match the given predicate.
 *
 * @example Basic usage
 * ```ts
 * import { dropLastWhile } from "@std/collections/drop-last-while";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const numbers = [20, 33, 44];
 *
 * const notFortyFour = dropLastWhile(numbers, (number) => number > 30);
 *
 * assertEquals(notFortyFour, [20]);
 * ```
 */
export function dropLastWhile<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(0, offset);
}

// denoCacheMetadata={"headers":{"access-control-expose-headers":"*","cf-ray":"a223e6564eec7db6-LAX","x-robots-tag":"noindex","set-cookie":"__cf_bm=qSkzy.CNd7R12BEQgxYGH7n.jEMvxeRcOECg_Di0VFg-1785241874.9513934-1.0.1.1-kFxvMEXMgz.tTgcR5GIYq1q9raXKhiJuhfPqm0kE1Qq_7Z0x.802fIKUW9StYhr_AGuHwAdJg7Pit084iRFdpK6vPnxB70vKvSSqIbkB1BvoQ.ilq.o0aSDzlMyEnF7i; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","server":"cloudflare","x-jsr-backend":"modules","cache-control":"public, max-age=31536000, immutable","alt-svc":"h3=\":443\"; ma=86400","content-length":"1121","content-type":"text/typescript","cross-origin-resource-policy":"cross-origin","x-content-type-options":"nosniff","date":"Tue, 28 Jul 2026 12:31:15 GMT","etag":"\"da87268420f4a77d4e69e4a0c2a202f6\"","vary":"Origin","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';","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=5qb879bYdhEb7iYsj%2FRV5WOYjJaj7E8gVwO%2FVWtG3Nvz5wLUGMIV9yHfedzumnqIVvsfFceQDLcPPH3RFxbpU%2FMVjCC0wdZJ34oaZIwkAI570gBbqk1k7cU%3D\"}]}","access-control-allow-origin":"*"},"url":"https://jsr.io/@std/collections/0.224.2/drop_last_while.ts","time":1785241875}