package gs.std.v0.core

/** Represents a blob -- some array of bytes.
  *
  * @param data
  *   The underlying data.
  */
final class Blob(private val data: Array[Byte]) extends IndexedSeq[Byte]:

  /** @inheritDocs
    */
  override def apply(i: Int): Byte = byteAt(i)

  /** @inheritDocs
    */
  override def length: Int = data.length

  /** @return
    *   The number of bytes in this blob, expressed as a count.
    */
  def numberOfBytes: ByteCount = ByteCount(data.length)

  /** @inheritDocs
    */
  override def equals(obj: Any): Boolean =
    obj match
      case other: Blob        => data.sameElements(other.data)
      case other: Array[Byte] => data.sameElements(other)
      case _                  => false

  /** @inheritDocs
    */
  override def hashCode(): Int = data.hashCode()

  /** Retrieve the byte at the given index.
    *
    * @param index
    *   The index.
    * @return
    *   The byte stored at the given index.
    */
  def byteAt(index: Int): Byte = data.apply(index)

  /** @return
    *   This byte array, encoded as a base64 string.
    */
  def base64(): B64 = Base64Encoder.encode(data)
