package gs.std.v0.core

import java.{util => ju}

/** Implementation of [[Decoder]] for Base64 strings.
  *
  * Supports base64-url decoding as well.
  */
object Base64Decoder extends Decoder[B64]:
  private lazy val d: ju.Base64.Decoder  = ju.Base64.getDecoder()
  private lazy val du: ju.Base64.Decoder = ju.Base64.getUrlDecoder()

  /** @inheritDocs
    */
  override def decode(input: B64): Array[Byte] =
    d.decode(input.data)

  /** Decode some arbitrary string data.
    *
    * @param input
    *   The data to decode.
    * @return
    *   The decoded bytes.
    */
  def decodeUnsafe(input: String): Array[Byte] =
    d.decode(input)

  /** Decode the base64-url encoded input.
    *
    * @param input
    *   The base64-url encoded data.
    * @return
    *   The decoded bytes.
    */
  def decodeUrl(input: B64Url): Array[Byte] =
    du.decode(input.data)
