import { bytesToHex } from "./hex.js";
/**
 * Type guard for `Bytes<T>` type
 *
 * @param b       The byte array
 * @param length  The length of the byte array
 */

export function isBytes(b, length) {
  return b instanceof Uint8Array && b.length === length;
}
/**
 * Function that verifies if passed data are Bytes and if the array has "length" number of bytes under given offset.
 * @param data
 * @param offset
 * @param length
 */

export function hasBytesAtOffset(data, offset, length) {
  if (!(data instanceof Uint8Array)) {
    throw new TypeError('Data has to an Uint8Array!');
  }

  const offsetBytes = data.slice(offset, offset + length);
  return isBytes(offsetBytes, length);
}
/**
 * Verifies if a byte array has a certain length
 *
 * @param b       The byte array
 * @param length  The specified length
 */

export function assertBytes(b, length) {
  if (!isBytes(b, length)) {
    throw new TypeError(`Parameter is not valid Bytes of length: ${length} !== ${b.length}`);
  }
}
/**
 * Type guard for FlexBytes<Min,Max> type
 *
 * @param b       The byte array
 * @param min     Minimum size of the array
 * @param max     Maximum size of the array
 */

export function isFlexBytes(b, min, max) {
  return b instanceof Uint8Array && b.length >= min && b.length <= max;
}
/**
 * Verifies if a byte array has a certain length between min and max
 *
 * @param b       The byte array
 * @param min     Minimum size of the array
 * @param max     Maximum size of the array
 */

export function assertFlexBytes(b, min, max) {
  if (!isFlexBytes(b, min, max)) {
    throw new TypeError(`Parameter is not valid FlexBytes of  min: ${min}, max: ${max}, length: ${b.length}`);
  }
}
/**
 * Return `length` bytes starting from `offset`
 *
 * @param data   The original data
 * @param offset The offset to start from
 * @param length The length of data to be returned
 */

export function bytesAtOffset(data, offset, length) {
  const offsetBytes = data.slice(offset, offset + length); // We are returning strongly typed Bytes so we have to verify that length is really what we claim

  assertBytes(offsetBytes, length);
  return offsetBytes;
}
/**
 * Return flex bytes starting from `offset`
 *
 * @param data   The original data
 * @param offset The offset to start from
 * @param _min   The minimum size of the data
 * @param _max   The maximum size of the data
 */

export function flexBytesAtOffset(data, offset, // eslint-disable-next-line @typescript-eslint/no-unused-vars
_min, // eslint-disable-next-line @typescript-eslint/no-unused-vars
_max) {
  return data.slice(offset);
}
/**
 * Returns true if two byte arrays are equal
 *
 * @param a Byte array to compare
 * @param b Byte array to compare
 */

export function bytesEqual(a, b) {
  return a.length === b.length && a.every((value, index) => value === b[index]);
}
/**
 * Returns a new byte array filled with zeroes with the specified length
 *
 * @param length The length of data to be returned
 */

export function makeBytes(length) {
  return new Uint8Array(length);
}
export function wrapBytesWithHelpers(data) {
  return Object.assign(data, {
    text: () => new TextDecoder('utf-8').decode(data),
    json: () => JSON.parse(new TextDecoder('utf-8').decode(data)),
    hex: () => bytesToHex(data)
  });
}