///|
#derive(Debug, Eq)
pub(all) enum Method {
  Options
  Reqmod
  Respmod
}

///|
#derive(Debug, Eq)
pub(all) enum StartLine {
  Request(Method, String)
  Response(Int32, String)
}

///|
#derive(Debug, Eq)
pub(all) enum ProtocolError {
  Incomplete
  Malformed(String, Int32)
  LimitExceeded(String)
}

///|
#derive(Debug, Eq)
pub(all) enum BodyKind {
  NoBody
  RequestBody
  ResponseBody
  OptionsBody
}

///|
#derive(Debug, Eq)
pub(all) struct Limits {
  max_head : Int32
  max_body : Int32
  max_headers : Int32
  max_chunk : Int32
}

///|
pub fn limits(
  max_head~ : Int32 = 65536,
  max_body~ : Int32 = 16777216,
  max_headers~ : Int32 = 128,
  max_chunk~ : Int32 = 1048576,
) -> Limits {
  { max_head, max_body, max_headers, max_chunk, }
}

///|
#derive(Debug, Eq)
pub(all) struct Message {
  start : StartLine
  headers : Array[(String, String)]
  request_headers : Bytes?
  response_headers : Bytes?
  body_kind : BodyKind
  body : Bytes
  ieof : Bool
  trailers : Array[(String, String)]
}

///|
#derive(Debug, Eq)
pub(all) struct Decoded {
  message : Message
  consumed : Int32
}

///|
pub fn method_name(verb : Method) -> String {
  match verb {
    Options => "OPTIONS"
    Reqmod => "REQMOD"
    Respmod => "RESPMOD"
  }
}

///|
fn parse_method(name : String) -> Method? {
  match name {
    "OPTIONS" => Some(Options)
    "REQMOD" => Some(Reqmod)
    "RESPMOD" => Some(Respmod)
    _ => None
  }
}

///|
pub fn header(headers : Array[(String, String)], name : String) -> String? {
  let name = name.to_lower()
  for (key, value) in headers {
    if key.to_lower() == name {
      return Some(value)
    }
  }
  None
}

///|
fn token(value : String) -> Bool {
  if value.is_empty() {
    return false
  }
  for c in value.to_array() {
    if !(c >= 'a' && c <= 'z') &&
      !(c >= 'A' && c <= 'Z') &&
      !(c >= '0' && c <= '9') &&
      !"!#$%&'*+-.^_`|~".contains(c.to_string()) {
      return false
    }
  }
  true
}

///|
fn safe_value(value : String) -> Bool {
  for c in value.to_array() {
    if c == '\r' ||
      c == '\n' ||
      (c.to_int() < 32 && c != '\t') ||
      c.to_int() == 127 ||
      c.to_int() > 255 {
      return false
    }
  }
  true
}

///|
fn decimal(value : String) -> Int32? {
  if value.is_empty() {
    return None
  }
  let mut number = 0
  for c in value.to_array() {
    if c < '0' || c > '9' {
      return None
    }
    let digit = c.to_int() - 48
    if number > (2147483647 - digit) / 10 {
      return None
    }
    number = number * 10 + digit
  }
  Some(number)
}

///|
fn raw(value : String) -> Bytes {
  let output : Array[Byte] = []
  for c in value.to_array() {
    output.push(c.to_int().to_byte())
  }
  Bytes::from_array(output)
}

///|
fn text(value : Bytes) -> String? {
  let output : Array[Char] = []
  for b in value {
    output.push(b.to_char())
  }
  Some(String::from_array(output))
}

///|
fn bytes_slice(value : Bytes, start : Int32, end : Int32) -> Bytes {
  let result : Array[Byte] = []
  for i = start; i < end; i = i + 1 {
    result.push(value[i])
  }
  Bytes::from_array(result)
}

///|
fn append_bytes(output : Array[Byte], value : Bytes) -> Unit {
  for b in value {
    output.push(b)
  }
}

///|
pub fn message(
  start : StartLine,
  body_kind~ : BodyKind = NoBody,
  body~ : Bytes = b"",
  headers~ : Array[(String, String)] = [],
  request_headers~ : Bytes? = None,
  response_headers~ : Bytes? = None,
  ieof~ : Bool = false,
  trailers~ : Array[(String, String)] = [],
) -> Message {
  {
    start,
    body_kind,
    body,
    headers,
    request_headers,
    response_headers,
    ieof,
    trailers,
  }
}

///|
pub extend Method with Eq::{equal, not_equal}

///|
pub extend Method with @debug.Debug::{to_repr}

///|
pub extend StartLine with Eq::{equal, not_equal}

///|
pub extend StartLine with @debug.Debug::{to_repr}

///|
pub extend ProtocolError with Eq::{equal, not_equal}

///|
pub extend ProtocolError with @debug.Debug::{to_repr}

///|
pub extend BodyKind with Eq::{equal, not_equal}

///|
pub extend BodyKind with @debug.Debug::{to_repr}

///|
pub extend Limits with Eq::{equal, not_equal}

///|
pub extend Limits with @debug.Debug::{to_repr}

///|
pub extend Message with Eq::{equal, not_equal}

///|
pub extend Message with @debug.Debug::{to_repr}

///|
pub extend Decoded with Eq::{equal, not_equal}

///|
pub extend Decoded with @debug.Debug::{to_repr}

///|
pub extend ServiceUri with Eq::{equal, not_equal}

///|
pub extend ServiceUri with @debug.Debug::{to_repr}