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

/** Options for {@linkcode slidingWindows}. */
export interface SlidingWindowsOptions {
  /**
   * If step is set, each window will start that many elements after the last
   * window's start.
   *
   * @default {1}
   */
  step?: number;
  /**
   * If partial is set, windows will be generated for the last elements of the
   * collection, resulting in some undefined values if size is greater than 1.
   *
   * @default {false}
   */
  partial?: boolean;
}

/**
 * Generates sliding views of the given array of the given size and returns a
 * new array containing all of them.
 *
 * If step is set, each window will start that many elements after the last
 * window's start. (Default: 1)
 *
 * If partial is set, windows will be generated for the last elements of the
 * collection, resulting in some undefined values if size is greater than 1.
 *
 * @template T The type of the array elements.
 *
 * @param array The array to generate sliding windows from.
 * @param size The size of the sliding windows.
 * @param options The options for generating sliding windows.
 *
 * @returns A new array containing all sliding windows of the given size.
 *
 * @example Usage
 * ```ts
 * import { slidingWindows } from "@std/collections/sliding-windows";
 * import { assertEquals } from "@std/assert/assert-equals";
 * const numbers = [1, 2, 3, 4, 5];
 *
 * const windows = slidingWindows(numbers, 3);
 * assertEquals(windows, [
 *   [1, 2, 3],
 *   [2, 3, 4],
 *   [3, 4, 5],
 * ]);
 *
 * const windowsWithStep = slidingWindows(numbers, 3, { step: 2 });
 * assertEquals(windowsWithStep, [
 *   [1, 2, 3],
 *   [3, 4, 5],
 * ]);
 *
 * const windowsWithPartial = slidingWindows(numbers, 3, { partial: true });
 * assertEquals(windowsWithPartial, [
 *   [1, 2, 3],
 *   [2, 3, 4],
 *   [3, 4, 5],
 *   [4, 5],
 *   [5],
 * ]);
 * ```
 */
export function slidingWindows<T>(
  array: readonly T[],
  size: number,
  options: SlidingWindowsOptions = {},
): T[][] {
  const { step = 1, partial = false } = options;

  if (
    !Number.isInteger(size) || !Number.isInteger(step) || size <= 0 || step <= 0
  ) {
    throw new RangeError("Both size and step must be positive integer.");
  }

  return Array.from(
    { length: Math.floor((array.length - (partial ? 1 : size)) / step + 1) },
    (_, i) => array.slice(i * step, i * step + size),
  );
}

// denoCacheMetadata={"headers":{"x-content-type-options":"nosniff","cross-origin-resource-policy":"cross-origin","content-length":"2449","set-cookie":"__cf_bm=8AzT4HrBKi6H7DHY.YLJLYNojqpLrPG8lcu155RwL68-1785241874.9647608-1.0.1.1-vg41J3_G8FcZOVICS_34pcdXip79PMzUdYyITDf0L3E7cviKlW907XSb4ATAa74cPGsuC7y14Gbe3o9l4R3MeluxS9dVbZh1Q6mSYRG6zbAVz2hyuVKRoPp_T3.tAmNZ; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","server":"cloudflare","cache-control":"public, max-age=31536000, immutable","cf-ray":"a223e6564f377db6-LAX","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","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","content-type":"text/typescript","etag":"\"bd0d7f21c4245ed974705d934287a459\"","alt-svc":"h3=\":443\"; ma=86400","access-control-expose-headers":"*","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=s6ssXbrkgkjXiW%2FHB5vm3rQurfwm%2FKNnmZ2MOTWFxqqLfWAsnglWvQk2BNFNLyJF81TCcKJFK6I6xC2wKTXYW%2BIbargMhqU8a8wTrf5RKP09T%2FkLA8cokGk%3D\"}]}","date":"Tue, 28 Jul 2026 12:31:15 GMT"},"url":"https://jsr.io/@std/collections/0.224.2/sliding_windows.ts","time":1785241875}