import { BeeArgumentError } from "../utils/error.js";
export const SPAN_SIZE = 8; // we limit the maximum span size in 32 bits to avoid BigInt compatibility issues

const MAX_SPAN_LENGTH = Math.pow(2, 32) - 1;
/**
 * Create a span for storing the length of the chunk
 *
 * The length is encoded in 64-bit little endian.
 *
 * @param length The length of the span
 */

export function makeSpan(length) {
  if (length <= 0) {
    throw new BeeArgumentError('invalid length for span', length);
  }

  if (length > MAX_SPAN_LENGTH) {
    throw new BeeArgumentError('invalid length (> MAX_SPAN_LENGTH)', length);
  }

  const span = new Uint8Array(SPAN_SIZE);
  const dataView = new DataView(span.buffer);
  const littleEndian = true;
  const lengthLower32 = length & 0xffffffff;
  dataView.setUint32(0, lengthLower32, littleEndian);
  return span;
}