///|
#derive(Debug, Eq)
pub(all) enum FormEntry {
  Text(String)
  File(filename~ : String, content_type~ : String, bytes~ : Bytes)
}

///|
#derive(Debug, Eq)
pub struct FormData {
  entries : Array[(String, FormEntry)]
}

///|
pub fn FormData::FormData(
  entries~ : Array[(String, FormEntry)] = [],
) -> FormData {
  { entries, }
}

///|
pub fn FormData::append_text(
  self : FormData,
  name : StringView,
  value : StringView,
) -> Unit {
  self.entries.push((name.to_owned(), FormEntry::Text(value.to_owned())))
}

///|
pub fn FormData::append_file(
  self : FormData,
  name : StringView,
  filename~ : String,
  content_type~ : String = "application/octet-stream",
  bytes~ : Bytes,
) -> Unit {
  self.entries.push(
    (name.to_owned(), FormEntry::File(filename~, content_type~, bytes~)),
  )
}

///|
pub fn FormData::get(self : FormData, name : StringView) -> FormEntry? {
  let target = name.to_owned()
  for pair in self.entries {
    if pair.0 == target {
      return Some(pair.1)
    }
  }
  None
}

///|
pub fn FormData::get_text(self : FormData, name : StringView) -> String? {
  match self.get(name) {
    Some(FormEntry::Text(v)) => Some(v)
    _ => None
  }
}

///|
pub fn FormData::get_all(
  self : FormData,
  name : StringView,
) -> Array[FormEntry] {
  let target = name.to_owned()
  let out : Array[FormEntry] = []
  for pair in self.entries {
    if pair.0 == target {
      out.push(pair.1)
    }
  }
  out
}

///|
pub fn FormData::entries(self : FormData) -> Array[(String, FormEntry)] {
  self.entries.copy()
}

///|
pub fn FormData::parse_urlencoded(
  input : StringView,
) -> FormData raise @url.UrlError {
  let fd = FormData::FormData()
  if input.is_empty() {
    return fd
  }
  for part in input.split("&") {
    if part.is_empty() {
      continue
    }
    match part.find("=") {
      Some(idx) => {
        let k = @url.decode_query_component(part[:idx])
        let v = @url.decode_query_component(part[idx + 1:])
        fd.append_text(k, v)
      }
      None => {
        let k = @url.decode_query_component(part)
        fd.append_text(k, "")
      }
    }
  }
  fd
}

///|
pub fn FormData::encode_urlencoded(self : FormData) -> String {
  let parts : Array[String] = []
  for pair in self.entries {
    match pair.1 {
      FormEntry::Text(v) =>
        parts.push(
          "\{@url.encode_query_component(pair.0)}=\{@url.encode_query_component(v)}",
        )
      FormEntry::File(filename~, ..) =>
        parts.push(
          "\{@url.encode_query_component(pair.0)}=\{@url.encode_query_component(filename)}",
        )
    }
  }
  parts.join("&")
}

///|
fn sanitize_header_quote(s : String) -> String {
  let sb = StringBuilder()
  for ch in s {
    if ch == '"' || ch == '\r' || ch == '\n' {
      sb.write_char('_')
    } else {
      sb.write_char(ch)
    }
  }
  sb.to_string()
}

///|
fn payload_contains_boundary(
  entries : Array[(String, FormEntry)],
  boundary : String,
) -> Bool {
  let needle = @utf8.encode(boundary)
  for pair in entries {
    if bytes_find_sub(@utf8.encode(pair.0)[:], needle[:], 0) is Some(_) {
      return true
    }
    match pair.1 {
      FormEntry::Text(v) =>
        if bytes_find_sub(@utf8.encode(v)[:], needle[:], 0) is Some(_) {
          return true
        }
      FormEntry::File(filename~, content_type~, bytes~) =>
        if bytes_find_sub(@utf8.encode(filename)[:], needle[:], 0) is Some(_) ||
          bytes_find_sub(@utf8.encode(content_type)[:], needle[:], 0) is Some(_) ||
          bytes_find_sub(bytes[:], needle[:], 0) is Some(_) {
          return true
        }
    }
  }
  false
}

///|
fn sanitize_boundary_base(requested : String) -> String {
  let sb = StringBuilder()
  for ch in requested {
    let c = ch.to_int()
    if c >= 33 &&
      c <= 126 &&
      ch != '"' &&
      ch != '\\' &&
      sb.to_string().length() < 60 {
      sb.write_char(ch)
    }
  }
  let cleaned = sb.to_string()
  if cleaned.is_empty() {
    "----cialloFormBoundary7MA4YWxkTrZu0gW"
  } else {
    cleaned
  }
}

///|
fn choose_collision_free_boundary(
  entries : Array[(String, FormEntry)],
  requested : String,
) -> String {
  let safe_base = sanitize_boundary_base(requested)
  let mut candidate = safe_base
  let mut attempt = 1
  while payload_contains_boundary(entries, candidate) {
    // Keep within RFC 2046 70-char boundary limit
    let suffix = "x\{attempt}"
    let base = if safe_base.length() + suffix.length() > 70 {
      safe_base[:70 - suffix.length()].to_owned()
    } else {
      safe_base
    }
    candidate = "\{base}\{suffix}"
    attempt += 1
  }
  candidate
}

///|
pub fn FormData::encode_multipart(
  self : FormData,
  boundary~ : String = "----cialloFormBoundary7MA4YWxkTrZu0gW",
) -> (String, Bytes) {
  let eff_boundary = choose_collision_free_boundary(self.entries, boundary)
  let buf = Buffer()
  for pair in self.entries {
    buf.write_bytesview(@utf8.encode("--\{eff_boundary}\r\n")[:])
    let safe_name = sanitize_header_quote(pair.0)
    match pair.1 {
      FormEntry::Text(v) => {
        buf.write_bytesview(
          @utf8.encode(
            "Content-Disposition: form-data; name=\"\{safe_name}\"\r\n\r\n",
          )[:],
        )
        buf.write_bytesview(@utf8.encode(v)[:])
        buf.write_bytesview(@utf8.encode("\r\n")[:])
      }
      FormEntry::File(filename~, content_type~, bytes~) => {
        let safe_file = sanitize_header_quote(filename)
        let safe_ct = sanitize_header_value(content_type)
        buf.write_bytesview(
          @utf8.encode(
            "Content-Disposition: form-data; name=\"\{safe_name}\"; filename=\"\{safe_file}\"\r\nContent-Type: \{safe_ct}\r\n\r\n",
          )[:],
        )
        buf.write_bytesview(bytes[:])
        buf.write_bytesview(@utf8.encode("\r\n")[:])
      }
    }
  }
  buf.write_bytesview(@utf8.encode("--\{eff_boundary}--\r\n")[:])
  ("multipart/form-data; boundary=\{eff_boundary}", buf.to_bytes())
}

///|
fn extract_boundary(content_type : StringView) -> String? {
  for part in content_type.split(";") {
    let trimmed = part.trim()
    match trimmed.find("=") {
      Some(eq_idx) => {
        let k = trimmed[:eq_idx].trim().to_lower()
        if k == "boundary" {
          let mut v = trimmed[eq_idx + 1:].trim().to_owned()
          if v.length() >= 2 && v.has_prefix("\"") && v.has_suffix("\"") {
            v = v[1:v.length() - 1].to_owned()
          }
          if !v.is_empty() {
            return Some(v)
          }
        }
      }
      None => ()
    }
  }
  None
}

///|
fn bytes_find_sub(hay : BytesView, needle : BytesView, start : Int32) -> Int32? {
  let hlen = hay.length()
  let nlen = needle.length()
  if nlen == 0 {
    return Some(start)
  }
  if start < 0 || start + nlen > hlen {
    return None
  }
  let mut i = start
  while i + nlen <= hlen {
    let mut matched = true
    for j in 0.. String? {
  let len = header.length()
  let mut i = 0
  let mut seg_start = 0
  let mut in_quotes = false
  let mut escaped = false
  fn check_seg(seg : StringView) -> String? {
    let trimmed = seg.trim()
    match trimmed.find("=") {
      Some(eq_idx) => {
        let k = trimmed[:eq_idx].trim().to_lower()
        if k == key {
          let mut v = trimmed[eq_idx + 1:].trim().to_owned()
          if v.length() >= 2 && v.has_prefix("\"") && v.has_suffix("\"") {
            let inner = v[1:v.length() - 1]
            let sb = StringBuilder()
            let mut esc = false
            for ch in inner {
              if esc {
                sb.write_char(ch)
                esc = false
              } else if ch == '\\' {
                esc = true
              } else {
                sb.write_char(ch)
              }
            }
            v = sb.to_string()
          }
          return Some(v)
        }
      }
      None => ()
    }
    None
  }
  while i < len {
    let c = header.code_unit_at(i)
    if escaped {
      escaped = false
    } else if in_quotes {
      if c == '\\' {
        escaped = true
      } else if c == '"' {
        in_quotes = false
      }
    } else if c == '"' {
      in_quotes = true
    } else if c == ';' {
      match check_seg(header[seg_start:i]) {
        Some(found) => return Some(found)
        None => ()
      }
      seg_start = i + 1
    }
    i += 1
  }
  if seg_start <= len {
    match check_seg(header[seg_start:len]) {
      Some(found) => return Some(found)
      None => ()
    }
  }
  None
}

///|
fn decode_rfc5987_value(raw : String) -> String? {
  // Format: charset'language'value-chars (e.g. UTF-8''%E4%B8%AD%E6%96%87.txt)
  let first_quote = match raw.find("'") {
    Some(i) => i
    None => return None
  }
  let rest = raw[first_quote + 1:].to_owned()
  let second_quote = match rest.find("'") {
    Some(i) => i
    None => return None
  }
  let charset = raw[:first_quote].trim().to_lower()
  if charset != "utf-8" && charset != "utf8" {
    return None
  }
  let encoded = rest[second_quote + 1:]
  Some(@url.percent_decode(encoded)) catch {
    _ => None
  }
}

///|
pub fn FormData::parse_multipart(
  content_type : StringView,
  body : BytesView,
  max_parts~ : Int32 = 1024,
  max_part_headers_bytes~ : Int32 = 8192,
) -> FormData raise StreamError {
  let boundary = match extract_boundary(content_type) {
    Some(b) => b
    None => raise StreamError::StreamReadError("missing multipart boundary")
  }
  let delim = @utf8.encode("--\{boundary}")
  let crlfcrlf = @utf8.encode("\r\n\r\n")
  let fd = FormData::FormData()
  let mut part_count = 0
  let mut pos = match bytes_find_sub(body, delim[:], 0) {
    Some(idx) => idx + delim.length()
    None => raise StreamError::StreamReadError("multipart boundary not found")
  }
  for ;; {
    if pos + 2 <= body.length() && body[pos] == b'-' && body[pos + 1] == b'-' {
      break
    }
    if pos + 2 <= body.length() && body[pos] == b'\r' && body[pos + 1] == b'\n' {
      pos = pos + 2
    } else if pos < body.length() && body[pos] == b'\n' {
      pos = pos + 1
    } else {
      break
    }
    part_count += 1
    if max_parts > 0 && part_count > max_parts {
      raise StreamError::PayloadTooLarge(max_parts.to_int64())
    }
    let next_delim = match bytes_find_sub(body, delim[:], pos) {
      Some(idx) => idx
      None =>
        raise StreamError::StreamReadError("unterminated multipart segment")
    }
    let mut part_end = next_delim
    if part_end >= pos + 2 &&
      body[part_end - 2] == b'\r' &&
      body[part_end - 1] == b'\n' {
      part_end = part_end - 2
    } else if part_end >= pos + 1 && body[part_end - 1] == b'\n' {
      part_end = part_end - 1
    }
    let part = body[pos:part_end]
    let header_scan_limit = if max_part_headers_bytes > 0 &&
      max_part_headers_bytes + 4 < part.length() {
      max_part_headers_bytes + 4
    } else {
      part.length()
    }
    let hdr_sep = match
      bytes_find_sub(part[:header_scan_limit], crlfcrlf[:], 0) {
      Some(idx) => idx
      None =>
        if max_part_headers_bytes > 0 && part.length() > max_part_headers_bytes {
          raise StreamError::PayloadTooLarge(max_part_headers_bytes.to_int64())
        } else {
          raise StreamError::StreamReadError(
            "missing header separator in multipart part",
          )
        }
    }
    if max_part_headers_bytes > 0 && hdr_sep > max_part_headers_bytes {
      raise StreamError::PayloadTooLarge(max_part_headers_bytes.to_int64())
    }
    let header_text = @utf8.decode(part[:hdr_sep]) catch {
      _ =>
        raise StreamError::StreamReadError("invalid utf-8 in multipart headers")
    }
    let payload = part[hdr_sep + 4:].to_owned()
    let mut name : String? = None
    let mut filename : String? = None
    let mut part_ct = "application/octet-stream"
    for line in header_text.split("\r\n") {
      match line.find(":") {
        Some(c_idx) => {
          let hk = line[:c_idx].trim().to_lower()
          let hv = line[c_idx + 1:].trim().to_owned()
          if hk == "content-disposition" {
            name = extract_disposition_param(hv, "name")
            let ext_filename = match
              extract_disposition_param(hv, "filename*") {
              Some(raw_ext) => decode_rfc5987_value(raw_ext)
              None => None
            }
            filename = match ext_filename {
              Some(decoded) => Some(decoded)
              None => extract_disposition_param(hv, "filename")
            }
          } else if hk == "content-type" {
            part_ct = hv
          }
        }
        None => ()
      }
    }
    match name {
      Some(field_name) =>
        match filename {
          Some(fname) =>
            fd.append_file(
              field_name,
              filename=fname,
              content_type=part_ct,
              bytes=payload,
            )
          None => {
            let text = @utf8.decode(payload) catch {
              _ =>
                raise StreamError::StreamReadError(
                  "invalid utf-8 in multipart text field",
                )
            }
            fd.append_text(field_name, text)
          }
        }
      None => ()
    }
    pos = next_delim + delim.length()
  }
  fd
}