// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import type { BufReader } from "./buf_reader.ts";
import { readInt } from "./read_int.ts";

const MAX_SAFE_INTEGER = BigInt(Number.MAX_SAFE_INTEGER);

/**
 * Read big endian 64bit long from a {@linkcode BufReader}.
 *
 * @example Usage
 * ```ts
 * import { Buffer } from "@std/io/buffer"
 * import { BufReader } from "@std/io/buf-reader";
 * import { readLong } from "@std/io/read-long";
 * import { assertEquals } from "@std/assert/equals";
 *
 * const buf = new BufReader(new Buffer(new Uint8Array([0, 0, 0, 0x12, 0x34, 0x56, 0x78, 0x9a])));
 * const long = await readLong(buf);
 * assertEquals(long, 0x123456789a);
 * ```
 *
 * @param buf The BufReader to read from
 * @returns The 64bit long
 * @throws {Deno.errors.UnexpectedEof} If the reader returns unexpected EOF
 * @throws {RangeError} If the long value is too big to be represented as a JavaScript number
 *
 * @deprecated This will be removed in 1.0.0. Use the {@link https://developer.mozilla.org/en-US/docs/Web/API/Streams_API | Web Streams API} instead.
 */
export async function readLong(buf: BufReader): Promise<number | null> {
  const high = await readInt(buf);
  if (high === null) return null;
  const low = await readInt(buf);
  if (low === null) throw new Deno.errors.UnexpectedEof();
  const big = (BigInt(high) << 32n) | BigInt(low);
  // We probably should provide a similar API that returns BigInt values.
  if (big > MAX_SAFE_INTEGER) {
    throw new RangeError(
      "Long value too big to be represented as a JavaScript number.",
    );
  }
  return Number(big);
}

// denoCacheMetadata={"headers":{"date":"Tue, 28 Jul 2026 12:31:15 GMT","vary":"Origin","etag":"\"6d16767c0b841ca9ad2de617ec5cf9fa\"","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","server":"cloudflare","cf-ray":"a223e656f8707db6-LAX","alt-svc":"h3=\":443\"; ma=86400","access-control-expose-headers":"*","x-robots-tag":"noindex","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=KECMMpeNhbCh6M2%2Fv3vwYrvMFIWgm%2B9pGR4m%2FXalwwv7jYzVs%2BDmPmUuP7E3mwyMyV%2FmBfJz%2BBXDLyQ1KkGjpLwtXjqKLayXoYL2paTYP2Wkug3X%2BiSjm9Q%3D\"}]}","cross-origin-resource-policy":"cross-origin","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","set-cookie":"__cf_bm=K35hPdKArv7pAHgSf7ocl0ZwzfKhLaxKgxWDtaLUN8Q-1785241875.0350375-1.0.1.1-nvPuXtfH0VD48DPESnsrlDnGWwBAW0P9A0wF2JgnfYEw.c5G5ijH6If98BwJQJco3LrCnFsyIshaUSI5Fr8NF8RT2a2rx_dQQCTi5S6SavvRvSBH1GEtQkAXoPLUZbkP; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","access-control-allow-origin":"*","content-length":"1622","cache-control":"public, max-age=31536000, immutable","content-type":"text/typescript","x-jsr-backend":"modules"},"url":"https://jsr.io/@std/io/0.224.8/read_long.ts","time":1785241875}