function toByteArray(bytes) {
  if (!bytes) return []
  if (bytes instanceof ArrayBuffer) return Array.prototype.slice.call(new Uint8Array(bytes))
  if (ArrayBuffer.isView(bytes)) return Array.prototype.slice.call(new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength))

  return Array.prototype.slice.call(bytes)
}

function bytesToBase64(bytes) {
  const source = toByteArray(bytes)
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  let output = ''

  for (let index = 0; index < source.length; index += 3) {
    const first = source[index] & 0xFF
    const second = index + 1 < source.length ? source[index + 1] & 0xFF : 0
    const third = index + 2 < source.length ? source[index + 2] & 0xFF : 0
    const triple = (first << 16) | (second << 8) | third

    output += alphabet[(triple >> 18) & 0x3F]
    output += alphabet[(triple >> 12) & 0x3F]
    output += index + 1 < source.length ? alphabet[(triple >> 6) & 0x3F] : '='
    output += index + 2 < source.length ? alphabet[triple & 0x3F] : '='
  }

  return output
}

function bytesToBin(bytes) {
  return toByteArray(bytes).map((byte) => (byte & 0xFF).toString(2).padStart(8, '0')).join('')
}

function bytesToHex(bytes, separator = '') {
  return toByteArray(bytes).map((byte) => (byte & 0xFF).toString(16).toUpperCase().padStart(2, '0')).join(separator)
}

function bytesToAsciiText(bytes = []) {
  return String.fromCharCode.apply(null, trimTrailingNullBytes(bytes).map((byte) => byte & 0xFF))
}

function bytesToUtf8Text(bytes = []) {
  const trimmed = trimTrailingNullBytes(bytes)
  if (!trimmed.length) return ''

  let encoded = ''

  trimmed.forEach((byte) => {
    encoded += `%${(byte & 0xFF).toString(16).padStart(2, '0').toUpperCase()}`
  })

  try {
    return decodeURIComponent(encoded)
  } catch (error) {
    return bytesToAsciiText(trimmed)
  }
}

function formatBytes(byteLength) {
  const length = Number(byteLength) || 0
  if (length >= 1024 && length % 1024 === 0) return `${length / 1024} KB`
  if (length >= 1024) return `${(length / 1024).toFixed(2)} KB`

  return `${length} bytes`
}

function bytesToWords(bytes = []) {
  const words = []

  for (let index = 0; index + 1 < bytes.length; index += 2) {
    const highByte = bytes[index] || 0
    const lowByte = bytes[index + 1] || 0
    words.push(((highByte << 8) | lowByte) & 0xFFFF)
  }

  return words
}

function getByteFromWord(word, byteOffset = 0) {
  const value = Number(word) & 0xFFFF

  return byteOffset === 0 ? ((value >> 8) & 0xFF) : (value & 0xFF)
}

function stringToUtf8Bytes(text) {
  const bytes = []
  const encoded = encodeURIComponent(String(text || ''))

  for (let index = 0; index < encoded.length; index += 1) {
    const char = encoded[index]
    if (char === '%') {
      bytes.push(parseInt(encoded.slice(index + 1, index + 3), 16) & 0xFF)
      index += 2
    } else {
      bytes.push(char.charCodeAt(0) & 0xFF)
    }
  }

  return bytes
}

function trimTrailingNullBytes(bytes = []) {
  let end = bytes.length

  while (end > 0 && bytes[end - 1] === 0x00) {
    end -= 1
  }

  return bytes.slice(0, end)
}

function wordsToBytes(words = [], byteLength = words.length * 2) {
  const bytes = []

  for (let index = 0; index < words.length; index += 1) {
    const word = Number(words[index]) & 0xFFFF
    bytes.push((word >> 8) & 0xFF, word & 0xFF)
  }

  return bytes.slice(0, Math.max(0, byteLength))
}

module.exports = {
  bytesToBase64,
  bytesToBin,
  bytesToHex,
  bytesToAsciiText,
  bytesToUtf8Text,
  bytesToWords,
  formatBytes,
  getByteFromWord,
  stringToUtf8Bytes,
  toByteArray,
  trimTrailingNullBytes,
  wordsToBytes
}
