const decoder = new TextDecoder()
export const toUTF8String = (
  input,
  start = 0,
  end = input.length
) => decoder.decode(input.slice(start, end))

export const toHexString = (input, start = 0, end = input.length) =>
  input
    .slice(start, end)
    .reduce((memo, i) => memo + ('0' + i.toString(16)).slice(-2), '')

export const readInt16LE = (input, offset = 0) => {
  const val = input[offset] + input[offset + 1] * 2 ** 8
  return val | ((val & (2 ** 15)) * 0x1fffe)
}

export const readUInt16BE = (input, offset = 0) =>
  input[offset] * 2 ** 8 + input[offset + 1]

export const readUInt16LE = (input, offset = 0) =>
  input[offset] + input[offset + 1] * 2 ** 8

export const readUInt24LE = (input, offset = 0) =>
  input[offset] + input[offset + 1] * 2 ** 8 + input[offset + 2] * 2 ** 16

export const readInt32LE = (input, offset = 0) =>
  input[offset] +
  input[offset + 1] * 2 ** 8 +
  input[offset + 2] * 2 ** 16 +
  (input[offset + 3] << 24)

export const readUInt32BE = (input, offset = 0) =>
  input[offset] * 2 ** 24 +
  input[offset + 1] * 2 ** 16 +
  input[offset + 2] * 2 ** 8 +
  input[offset + 3]

export const readUInt32LE = (input, offset = 0) =>
  input[offset] +
  input[offset + 1] * 2 ** 8 +
  input[offset + 2] * 2 ** 16 +
  input[offset + 3] * 2 ** 24

// Abstract reading multi-byte unsigned integers
const methods = {
  readUInt16BE,
  readUInt16LE,
  readUInt32BE,
  readUInt32LE
}

export function readUInt (
  input,
  bits,
  offset,
  isBigEndian
) {
  offset = offset || 0
  const endian = isBigEndian ? 'BE' : 'LE'
  const methodName = 'readUInt' + bits + endian
  return methods[methodName](input, offset)
}

function readBox (buffer, offset) {
  if (buffer.length - offset < 4) return
  const boxSize = readUInt32BE(buffer, offset)
  if (buffer.length - offset < boxSize) return
  return {
    name: toUTF8String(buffer, 4 + offset, 8 + offset),
    offset,
    size: boxSize
  }
}

export function findBox (buffer, boxName, offset) {
  while (offset < buffer.length) {
    const box = readBox(buffer, offset)
    if (!box) break
    if (box.name === boxName) return box
    offset += box.size
  }
}
