// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { copy } from "./copy.ts";

/**
 * Returns a new byte slice composed of `count` repetitions of the `source`
 * array.
 *
 * @param source Source array to repeat.
 * @param count Number of times to repeat the source array.
 * @returns A new byte slice composed of `count` repetitions of the `source`
 * array.
 *
 * @example Basic usage
 * ```ts
 * import { repeat } from "@std/bytes/repeat";
 *
 * const source = new Uint8Array([0, 1, 2]);
 *
 * repeat(source, 3); // Uint8Array(9) [0, 1, 2, 0, 1, 2, 0, 1, 2]
 *
 * repeat(source, 0); // Uint8Array(0) []
 *
 * repeat(source, -1); // Throws `RangeError`
 * ```
 */
export function repeat(source: Uint8Array, count: number): Uint8Array {
  if (count < 0 || !Number.isInteger(count)) {
    throw new RangeError("Count must be a non-negative integer");
  }

  const repeated = new Uint8Array(source.length * count);
  let offset = 0;

  while (offset < repeated.length) {
    offset += copy(source, repeated, offset);
  }

  return repeated;
}

// denoCacheMetadata={"headers":{"cf-cache-status":"HIT","vary":"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';","access-control-allow-origin":"*","server":"cloudflare","x-robots-tag":"noindex","content-length":"1111","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=%2FK59%2BpcfVH6CwqXBiN22eRoTZr1oxfcK0TZY3NdgRV29SEaMIlmp8B%2F%2BrPHFAxwoVDud0TkecauRbmrNGNR8uXbW9CaiAzejMmK1L%2FuneFRKxuxIC0IH5Vc%3D\"}]}","etag":"\"d1295995658602e509b61cf020e2321e\"","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","alt-svc":"h3=\":443\"; ma=86400","content-type":"text/typescript","accept-ranges":"bytes","cache-control":"public, max-age=31536000, immutable","cross-origin-resource-policy":"cross-origin","cf-ray":"a223e6564ed47db6-LAX","set-cookie":"__cf_bm=DyIXTQ6xelq6byPxeYExl3Iz7Ez3Ou1qpRukR58OF9k-1785241874.947098-1.0.1.1-CLlEE0HsqzVU_aAyjwBLMKDFAVZallmieBC4zb.0UphvPQgb08JZpioS2Rfaej6lUpSXOznUuE8kkHqYcjp6A0MeerPnF9ES0W.cs.xT4zUt6TrvWBDv3mzGRtQIgs6K; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:14 GMT","x-jsr-backend":"modules","x-content-type-options":"nosniff","age":"458713","access-control-expose-headers":"*","date":"Tue, 28 Jul 2026 12:31:14 GMT"},"url":"https://jsr.io/@std/bytes/0.224.0/repeat.ts","time":1785241874}