///|
#non_exhaustive
#derive(Debug, Eq)
pub(all) suberror UrlError {
  InvalidUrl(String)
  InvalidPercentEncoding(String)
  NullByteForbidden
}

///|
#derive(Debug, Eq)
pub(all) struct Url {
  scheme : String
  host : String
  path : String
  raw_query : String
}

///|
fn hex_val(b : Byte) -> Int32? {
  let c = b.to_int()
  if c >= 48 && c <= 57 {
    Some(c - 48)
  } else if c >= 65 && c <= 70 {
    Some(c - 55)
  } else if c >= 97 && c <= 102 {
    Some(c - 87)
  } else {
    None
  }
}

///|
fn hex_digit_upper(n : Int32) -> Char {
  match n {
    0 => '0'
    1 => '1'
    2 => '2'
    3 => '3'
    4 => '4'
    5 => '5'
    6 => '6'
    7 => '7'
    8 => '8'
    9 => '9'
    10 => 'A'
    11 => 'B'
    12 => 'C'
    13 => 'D'
    14 => 'E'
    _ => 'F'
  }
}

///|
fn is_unreserved_byte(b : Byte) -> Bool {
  let c = b.to_int()
  (c >= 65 && c <= 90) ||
  (c >= 97 && c <= 122) ||
  (c >= 48 && c <= 57) ||
  c == 45 ||
  c == 46 ||
  c == 95 ||
  c == 126
}

///|
fn percent_decode_impl(
  input : StringView,
  plus_as_space : Bool,
) -> String raise UrlError {
  let raw_bytes = @utf8.encode(input)
  let len = raw_bytes.length()
  let out = @buffer.Buffer(capacity=len)
  let mut i = 0
  while i < len {
    let b = raw_bytes[i]
    if b == b'\x00' {
      raise UrlError::NullByteForbidden
    }
    if plus_as_space && b == b'+' {
      out.write_byte(b' ')
      i = i + 1
    } else if b == b'%' {
      if i + 2 >= len {
        raise UrlError::InvalidPercentEncoding(
          "truncated percent-encoding sequence",
        )
      }
      let hi = match hex_val(raw_bytes[i + 1]) {
        Some(v) => v
        None =>
          raise UrlError::InvalidPercentEncoding("invalid hex digit after '%'")
      }
      let lo = match hex_val(raw_bytes[i + 2]) {
        Some(v) => v
        None =>
          raise UrlError::InvalidPercentEncoding("invalid hex digit after '%'")
      }
      let decoded = ((hi << 4) | lo).to_byte()
      if decoded == b'\x00' {
        raise UrlError::NullByteForbidden
      }
      out.write_byte(decoded)
      i = i + 3
    } else {
      out.write_byte(b)
      i = i + 1
    }
  }
  @utf8.decode(out.to_bytes()) catch {
    _ => raise UrlError::InvalidPercentEncoding("invalid UTF-8 byte sequence")
  }
}

///|
/// 严格 RFC 3986 路径单段解码(保留 '+' 原义,严禁在分段前解码 '%2F',拒绝 '%00' 与非法 UTF-8)
pub fn percent_decode(input : StringView) -> String raise UrlError {
  percent_decode_impl(input, false)
}

///|
pub fn percent_encode(input : StringView) -> String {
  let bytes = @utf8.encode(input)
  let sb = StringBuilder()
  for b in bytes {
    if is_unreserved_byte(b) {
      sb.write_char(b.to_int().unsafe_to_char())
    } else {
      let n = b.to_int()
      sb.write_char('%')
      sb.write_char(hex_digit_upper((n >> 4) & 0xF))
      sb.write_char(hex_digit_upper(n & 0xF))
    }
  }
  sb.to_string()
}

///|
/// WHATWG URLSearchParams 查询参数编解码(先将 '+' 换空格再执行 '%XX' UTF-8 解码,拒绝 '%00' 与非法 UTF-8)
pub fn decode_query_component(input : StringView) -> String raise UrlError {
  percent_decode_impl(input, true)
}

///|
pub fn encode_query_component(input : StringView) -> String {
  let bytes = @utf8.encode(input)
  let sb = StringBuilder()
  for b in bytes {
    if b == b' ' {
      sb.write_char('+')
    } else if is_unreserved_byte(b) {
      sb.write_char(b.to_int().unsafe_to_char())
    } else {
      let n = b.to_int()
      sb.write_char('%')
      sb.write_char(hex_digit_upper((n >> 4) & 0xF))
      sb.write_char(hex_digit_upper(n & 0xF))
    }
  }
  sb.to_string()
}

///|
fn is_valid_scheme(s : StringView) -> Bool {
  if s.is_empty() {
    return false
  }
  let mut first = true
  for ch in s {
    let code = ch.to_int()
    let is_alpha = (code >= 65 && code <= 90) || (code >= 97 && code <= 122)
    let is_digit = code >= 48 && code <= 57
    if first {
      if !is_alpha {
        return false
      }
      first = false
    } else if !is_alpha && !is_digit && ch != '+' && ch != '-' && ch != '.' {
      return false
    }
  }
  true
}

///|
fn ascii_lower_str(s : StringView) -> String {
  let sb = StringBuilder()
  for ch in s {
    let code = ch.to_int()
    if code >= 65 && code <= 90 {
      sb.write_char((code + 32).unsafe_to_char())
    } else {
      sb.write_char(ch)
    }
  }
  sb.to_string()
}

///|
fn default_port_for_scheme(scheme : String) -> String {
  match scheme {
    "http" | "ws" => "80"
    "https" | "wss" => "443"
    _ => ""
  }
}

///|
fn parse_valid_port(port_str : StringView) -> String raise UrlError {
  if port_str.is_empty() {
    return ""
  }
  let mut val = 0
  for ch in port_str {
    let code = ch.to_int()
    if code < 48 || code > 57 {
      raise UrlError::InvalidUrl("invalid port number")
    }
    val = val * 10 + (code - 48)
    if val > 65535 {
      raise UrlError::InvalidUrl("port out of range (0..65535)")
    }
  }
  val.to_string()
}

///|
/// Validates and splits authority host into (hostname, normalized_port, normalized_host)
fn is_valid_ipv6_literal(s : String) -> Bool {
  if s.is_empty() || s.contains(":::") {
    return false
  }
  let mut double_colon = 0
  let mut i = 0
  while i + 1 < s.length() {
    if s.code_unit_at(i) == ':' && s.code_unit_at(i + 1) == ':' {
      double_colon += 1
      i += 2
    } else {
      i += 1
    }
  }
  if double_colon > 1 {
    return false
  }
  if s.has_prefix(":") && !s.has_prefix("::") {
    return false
  }
  if s.has_suffix(":") && !s.has_suffix("::") {
    return false
  }
  let mut groups = 0
  let mut has_colon = false
  for ch in s {
    if ch == ':' {
      has_colon = true
    }
  }
  if !has_colon {
    return false
  }
  for part in s.split(":") {
    let seg = part.to_owned()
    if seg.is_empty() {
      continue
    }
    if seg.contains(".") {
      // Embedded IPv4 tail (must be 4 decimal octets)
      let mut octs = 0
      for op in seg.split(".") {
        let o = op.to_owned()
        if o.is_empty() || o.length() > 3 {
          return false
        }
        let mut n = 0
        for ch in o {
          let c = ch.to_int()
          if c < 48 || c > 57 {
            return false
          }
          n = n * 10 + (c - 48)
        }
        if n > 255 {
          return false
        }
        octs += 1
      }
      if octs != 4 {
        return false
      }
      groups += 2
    } else {
      if seg.length() > 4 {
        return false
      }
      for ch in seg {
        let c = ch.to_int()
        let ok = (c >= 48 && c <= 57) ||
          (c >= 65 && c <= 70) ||
          (c >= 97 && c <= 102)
        if !ok {
          return false
        }
      }
      groups += 1
    }
  }
  if double_colon == 1 {
    groups < 8
  } else {
    groups == 8
  }
}

///|
fn parse_and_normalize_host(
  raw_host : String,
  scheme : String,
) -> (String, String, String) raise UrlError {
  if raw_host.is_empty() {
    raise UrlError::InvalidUrl("missing host in absolute URL")
  }
  let (raw_hostname, raw_port) = if raw_host.has_prefix("[") {
    match raw_host.find("]") {
      None => raise UrlError::InvalidUrl("unclosed IPv6 bracket in host")
      Some(close_idx) => {
        if close_idx <= 1 {
          raise UrlError::InvalidUrl("empty IPv6 literal in host")
        }
        let inner = raw_host[1:close_idx].to_owned()
        if !is_valid_ipv6_literal(inner) {
          raise UrlError::InvalidUrl("invalid IPv6 literal in host")
        }
        let host_part = raw_host[:close_idx + 1].to_owned()
        if close_idx + 1 == raw_host.length() {
          (host_part, "")
        } else if raw_host.code_unit_at(close_idx + 1) == ':' {
          (host_part, raw_host[close_idx + 2:].to_owned())
        } else {
          raise UrlError::InvalidUrl(
            "unexpected trailing characters after IPv6 bracket",
          )
        }
      }
    }
  } else {
    match raw_host.find(":") {
      None => (raw_host, "")
      Some(colon_idx) => {
        let rest = raw_host[colon_idx + 1:].to_owned()
        if rest.contains(":") {
          raise UrlError::InvalidUrl("IPv6 host must be enclosed in brackets")
        }
        let host_part = raw_host[:colon_idx].to_owned()
        if host_part.is_empty() {
          raise UrlError::InvalidUrl("missing hostname before port")
        }
        (host_part, rest)
      }
    }
  }
  let hostname = raw_hostname
  let port = parse_valid_port(raw_port)
  let def_port = default_port_for_scheme(scheme)
  let norm_port = if !port.is_empty() && port == def_port { "" } else { port }
  let norm_host = if norm_port.is_empty() {
    hostname
  } else {
    "\{hostname}:\{norm_port}"
  }
  (hostname, norm_port, norm_host)
}

///|
fn split_host_components(host : String, scheme : String) -> (String, String) {
  if host.is_empty() {
    return ("", "")
  }
  let (hostname, raw_port) = if host.has_prefix("[") {
    match host.find("]") {
      Some(close_idx) => {
        let h = host[:close_idx + 1].to_owned()
        if close_idx + 1 < host.length() &&
          host.code_unit_at(close_idx + 1) == ':' {
          (h, host[close_idx + 2:].to_owned())
        } else {
          (h, "")
        }
      }
      None => (host, "")
    }
  } else {
    match host.find(":") {
      Some(colon_idx) => {
        let rest = host[colon_idx + 1:].to_owned()
        if rest.contains(":") {
          (host, "")
        } else {
          (host[:colon_idx].to_owned(), rest)
        }
      }
      None => (host, "")
    }
  }
  let def_port = default_port_for_scheme(scheme)
  let port = if !raw_port.is_empty() && raw_port == def_port {
    ""
  } else {
    raw_port
  }
  (hostname, port)
}

///|
/// Returns the hostname portion of the URL (retaining brackets for IPv6 literals per WHATWG URL, without `:port`).
pub fn Url::hostname(self : Url) -> String {
  split_host_components(self.host, self.scheme).0
}

///|
/// Returns the non-default port string of the URL (or `""` when omitted or equal to scheme default port).
pub fn Url::port(self : Url) -> String {
  split_host_components(self.host, self.scheme).1
}

///|
pub fn Url::parse(raw : StringView) -> Url raise UrlError {
  let s = raw.to_owned()
  if s.is_empty() {
    raise UrlError::InvalidUrl("empty URL")
  }
  for ch in s {
    let code = ch.to_int()
    if code == 0 {
      raise UrlError::NullByteForbidden
    }
    if code == 10 || code == 13 {
      raise UrlError::InvalidUrl("URL contains CR or LF")
    }
  }
  // Drop fragment (#...)
  let without_frag = match s.find("#") {
    Some(idx) => s[:idx].to_owned()
    None => s
  }
  // Split query (?...)
  let (before_query, raw_query) = match without_frag.find("?") {
    Some(idx) =>
      (without_frag[:idx].to_owned(), without_frag[idx + 1:].to_owned())
    None => (without_frag, "")
  }
  if before_query.is_empty() {
    raise UrlError::InvalidUrl("missing path or authority")
  }
  // Check for absolute URL with scheme://
  match before_query.find("://") {
    Some(sep_idx) => {
      let scheme_part = before_query[:sep_idx]
      if !is_valid_scheme(scheme_part) {
        raise UrlError::InvalidUrl("invalid URL scheme")
      }
      let scheme = ascii_lower_str(scheme_part)
      let rest = before_query[sep_idx + 3:].to_owned()
      let (raw_host, path) = match rest.find("/") {
        Some(slash_idx) =>
          (rest[:slash_idx].to_owned(), rest[slash_idx:].to_owned())
        None => (rest, "/")
      }
      let (_hostname, _port, host) = parse_and_normalize_host(raw_host, scheme)
      { scheme, host, path, raw_query, }
    }
    None =>
      if before_query.has_prefix("/") {
        { scheme: "", host: "", path: before_query, raw_query, }
      } else {
        raise UrlError::InvalidUrl(
          "relative URL must start with '/' or include scheme://",
        )
      }
  }
}

///|
pub fn Url::query_params(
  self : Url,
) -> Map[String, Array[String]] raise UrlError {
  let result : Map[String, Array[String]] = Map([])
  if self.raw_query.is_empty() {
    return result
  }
  for part in self.raw_query.split("&") {
    if part.is_empty() {
      continue
    }
    let part_str = part.to_owned()
    let (raw_k, raw_v) = match part_str.find("=") {
      Some(eq_idx) =>
        (part_str[:eq_idx].to_owned(), part_str[eq_idx + 1:].to_owned())
      None => (part_str, "")
    }
    let key = decode_query_component(raw_k)
    let val = decode_query_component(raw_v)
    match result.get(key) {
      Some(arr) => arr.push(val)
      None => result[key] = [val]
    }
  }
  result
}

///|
/// Serializes the URL back into its full string form (`scheme://host/path?query` or `/path?query`).
pub fn Url::href(self : Url) -> String {
  let sb = StringBuilder()
  if !self.scheme.is_empty() && !self.host.is_empty() {
    sb.write_string(self.scheme)
    sb.write_string("://")
    sb.write_string(self.host)
  }
  sb.write_string(self.path)
  if !self.raw_query.is_empty() {
    sb.write_char('?')
    sb.write_string(self.raw_query)
  }
  sb.to_string()
}

///|
pub fn Url::to_string(self : Url) -> String {
  self.href()
}

///|
pub fn Url::query_first(self : Url, key : StringView) -> String? raise UrlError {
  let params = self.query_params()
  match params.get(key.to_owned()) {
    Some(arr) => if arr.is_empty() { None } else { Some(arr[0]) }
    None => None
  }
}