///|
fn bstr_cbor(bytes : Bytes) -> @cbor.CborValue {
  @cbor.Bytes(bytes)
}

///|
fn text_cbor(text : String) -> @cbor.CborValue {
  @cbor.Text(text)
}

///|
fn payload_bytes(payload : Bytes?) -> Bytes {
  match payload {
    Some(b) => b
    None => bytes_empty()
  }
}

///|
/// `Sig_structure` from RFC 9052. `sign_protected` is present only for the
/// `"Signature"` and `"CounterSignature"` contexts.
pub fn sig_structure(
  context : String,
  body_protected : Bytes,
  sign_protected : Bytes?,
  external_aad : Bytes,
  payload : Bytes,
) -> Bytes {
  let items : Array[@cbor.CborValue] = [
    text_cbor(context),
    bstr_cbor(body_protected),
  ]
  match sign_protected {
    Some(raw) => items.push(bstr_cbor(raw))
    None => ()
  }
  items.push(bstr_cbor(external_aad))
  items.push(bstr_cbor(payload))
  @cbor.encode(@cbor.Array(items))
}

///|
/// `MAC_structure` from RFC 9052. `context` is `"MAC0"` or `"MAC"`.
pub fn mac_structure(
  context : String,
  prot : Bytes,
  external_aad : Bytes,
  payload : Bytes,
) -> Bytes {
  @cbor.encode(
    @cbor.Array([
      text_cbor(context),
      bstr_cbor(prot),
      bstr_cbor(external_aad),
      bstr_cbor(payload),
    ]),
  )
}

///|
/// `Enc_structure` from RFC 9052. This byte string is the AEAD AAD.
/// `context` is `"Encrypt0"` or `"Encrypt"`.
pub fn enc_structure(
  context : String,
  prot : Bytes,
  external_aad : Bytes,
) -> Bytes {
  @cbor.encode(
    @cbor.Array([text_cbor(context), bstr_cbor(prot), bstr_cbor(external_aad)]),
  )
}

///|
fn is_cose_message_tag(tag : UInt64) -> Bool {
  tag == 16UL ||
  tag == 17UL ||
  tag == 18UL ||
  tag == 96UL ||
  tag == 97UL ||
  tag == 98UL
}

///|
fn message_array(
  items : Array[@cbor.CborValue],
  arity : Int32,
  what : String,
  tagged : Bool,
) -> (Bool, Array[@cbor.CborValue]) raise CoseError {
  if items.length() != arity {
    raise BadStructure("\{what} has \{items.length()} items, want \{arity}")
  }
  (tagged, items)
}

///|
fn decode_message(
  bytes : Bytes,
  expected_tag : UInt64,
  arity : Int32,
  what : String,
) -> (Bool, Array[@cbor.CborValue]) raise CoseError {
  let mut value = decode_cbor(bytes)
  // Unrelated tags may wrap the COSE tag. A tag directly on the array must
  // be the registered COSE tag, or be absent (RFC 9052 section 2).
  for i = 0; i < 8; i = i + 1 {
    ignore(i)
    match value {
      @cbor.Tag(tag, inner) =>
        if is_cose_message_tag(tag) {
          break
        } else {
          match inner {
            @cbor.Tag(_, _) => value = inner
            _ => raise BadStructure("unexpected CBOR tag for \{what}")
          }
        }
      _ => break
    }
  }
  match value {
    @cbor.Array(items) => message_array(items, arity, what, false)
    @cbor.Tag(tag, @cbor.Array(items)) =>
      if tag == expected_tag {
        message_array(items, arity, what, true)
      } else {
        raise BadStructure("unexpected CBOR tag for \{what}")
      }
    @cbor.Tag(_, _) => raise BadStructure("\{what} tag does not wrap an array")
    _ => raise BadStructure("\{what} is not an array")
  }
}

///|
fn cbor_bstr_field(
  value : @cbor.CborValue,
  what : String,
) -> Bytes raise CoseError {
  match cbor_unwrap(value) {
    @cbor.Bytes(b) => b
    _ => raise BadStructure("\{what} must be a byte string")
  }
}

///|
fn cbor_payload_field(value : @cbor.CborValue) -> Bytes? raise CoseError {
  match cbor_unwrap(value) {
    @cbor.Bytes(b) => Some(b)
    @cbor.Simple(22) => None
    _ => raise BadStructure("payload must be a byte string or null")
  }
}

///|
fn payload_to_cbor(payload : Bytes?) -> @cbor.CborValue {
  match payload {
    Some(b) => @cbor.Bytes(b)
    None => @cbor.Simple(22)
  }
}

///|
fn encode_tagged(
  tag : UInt64,
  tagged : Bool,
  items : Array[@cbor.CborValue],
) -> Bytes {
  let value = if tagged {
    @cbor.Tag(tag, @cbor.Array(items))
  } else {
    @cbor.Array(items)
  }
  @cbor.encode(value)
}