// MoonBit adaptation and modifications by justjavac, 2026.
// Based on uWebSockets HttpParser.h, authored by Alex Hultman, 2018-2026.
// Intellectual property of third-party. Upstream Apache-2.0: LICENSE-APACHE and NOTICE.

///|
/// Returns the compatibility method with every original byte ORed with 32.
/// The immutable result is cached; this does not mutate the original method.
/// This reproduces upstream's getter for its permissively accepted methods.
pub fn Request::get_method(self : Request) -> BytesView {
  self.lowercase_method[:]
}

///|
/// Returns the exact original method bytes, including their original case.
/// Calling `get_method` first does not change this result. The view is retainable.
pub fn Request::get_case_sensitive_method(self : Request) -> BytesView {
  self.original_method
}

///|
/// Returns the request target before its first `?`, without percent decoding.
/// Offsets count bytes; origin-form paths and the accepted `OPTIONS *` stay ASCII.
pub fn Request::get_url(self : Request) -> BytesView {
  self.target[:self.query_separator]
}

///|
/// Returns the complete original request target, including any query string.
/// This immutable view remains valid after subsequent parser operations.
pub fn Request::get_full_url(self : Request) -> BytesView {
  self.target
}

///|
/// Returns the raw query after the first `?`, without decoding or mutation.
/// `None` means there was no `?`; `Some(b"")` represents an explicitly empty query.
pub fn Request::get_query(self : Request) -> BytesView? {
  if self.query_separator == self.target.length() {
    None
  } else {
    Some(self.target[self.query_separator + 1:])
  }
}

///|
/// Iterates normalized names and trimmed values in wire order, including duplicates.
/// Values preserve every allowed byte, including invalid UTF-8 and DEL.
/// Returned views share immutable request storage and may be retained.
pub fn Request::headers(self : Request) -> Iter[(BytesView, BytesView)] {
  self.fields.iter().map(field => (field.name[:], field.value))
}

///|
/// Finds the first field whose normalized name equals `lowercase_name` exactly.
/// The caller supplies a lowercase byte key; a mixed-case key does not match.
/// `None` means missing, while `Some(b"")` means present with an empty value.
/// Duplicate fields are available through `headers` and `are_identical`.
pub fn Request::get_header(
  self : Request,
  lowercase_name : BytesView,
) -> BytesView? {
  for field in self.fields {
    if field.name[:] == lowercase_name {
      return Some(field.value)
    }
  }
  None
}

///|
/// Checks every field named `lowercase_name` against an exact trimmed byte value.
/// The caller supplies a lowercase name. The result is vacuously true when the
/// field is absent; no case folding or numeric normalization applies to values.
pub fn Request::are_identical(
  self : Request,
  lowercase_name : BytesView,
  expected_value : BytesView,
) -> Bool {
  for field in self.fields {
    if field.name[:] == lowercase_name && field.value != expected_value {
      return false
    }
  }
  true
}

///|
/// Returns false because the pinned parser accepts only HTTP/1.1 requests.
/// This compatibility getter does not imply HTTP/1.0 or response support.
pub fn Request::is_ancient(_self : Request) -> Bool {
  false
}

///|
/// Returns the routing yield flag, initially false for each request.
/// Assignment aliases this flag; `copy` creates independent routing metadata.
pub fn Request::get_yield(self : Request) -> Bool {
  self.yielded
}

///|
/// Sets the routing yield flag without changing parser or wire state.
/// Other references to this request observe the change; independent copies do not.
pub fn Request::set_yield(self : Request, yielded : Bool) -> Unit {
  self.yielded = yielded
}

///|
/// Replaces indexed routing parameters with a copy of the caller's container.
/// Byte values are immutable. Existing name offsets are retained; out-of-range
/// offsets simply return `None`. This operation does not infer routes from URLs.
pub fn Request::set_parameters(
  self : Request,
  parameters : ArrayView[Bytes],
) -> Unit {
  self.parameters = parameters.to_owned()
}

///|
/// Replaces name-to-index routing offsets with an independent copy of the map.
/// Names are case sensitive; negative and out-of-range indices remain safe and
/// return `None` during lookup. The request does not mutate the caller's map.
pub fn Request::set_parameter_offsets(
  self : Request,
  offsets : Map[String, Int32],
) -> Unit {
  self.parameter_offsets = offsets.copy()
}

///|
/// Returns an indexed routing parameter or `None` for any out-of-range index.
/// An explicitly empty parameter returns `Some(b"")`; the byte view is retainable.
pub fn Request::get_parameter(self : Request, index : Int32) -> BytesView? {
  self.parameters.get(index).map(value => value[:])
}

///|
/// Looks up a case-sensitive parameter name through externally supplied offsets.
/// Missing names and invalid indices return `None`; no URL decoding is performed.
pub fn Request::get_named_parameter(
  self : Request,
  name : String,
) -> BytesView? {
  match self.parameter_offsets.get(name) {
    Some(index) => self.get_parameter(index)
    None => None
  }
}

///|
/// Copies mutable routing metadata while sharing immutable request bytes.
/// Changing yield flags, parameter containers, or offsets in either copy cannot
/// affect the other. Copying does not mutate or consume the original request.
pub fn Request::copy(self : Request) -> Request {
  {
    original_method: self.original_method,
    lowercase_method: self.lowercase_method,
    target: self.target,
    query_separator: self.query_separator,
    fields: self.fields,
    yielded: self.yielded,
    parameters: self.parameters[:].to_owned(),
    parameter_offsets: self.parameter_offsets.copy(),
  }
}