///|
pub fn normalize_method(value : String) -> String {
  value.trim().to_upper().to_owned()
}

///|
/// Metadata supplied to the cache policy. The request time is injected by the
/// caller and never read from a system clock.
#derive(Eq, Debug)
pub(all) struct RequestMeta {
  http_method : String
  uri : String
  headers : HeaderMap
  request_time : Timestamp
}

///|
pub fn RequestMeta::new(
  http_method : String,
  uri : String,
  headers : HeaderMap,
  request_time : Timestamp,
) -> RequestMeta {
  {
    http_method: normalize_method(http_method),
    uri: uri.trim().to_owned(),
    headers,
    request_time,
  }
}

///|
pub fn RequestMeta::simple(http_method : String, uri : String) -> RequestMeta {
  RequestMeta::new(http_method, uri, HeaderMap::new(), Timestamp::zero())
}

///|
pub fn RequestMeta::with_headers(
  self : RequestMeta,
  headers : HeaderMap,
) -> RequestMeta {
  {
    http_method: self.http_method,
    uri: self.uri,
    headers,
    request_time: self.request_time,
  }
}

///|
pub fn RequestMeta::with_request_time(
  self : RequestMeta,
  request_time : Timestamp,
) -> RequestMeta {
  {
    http_method: self.http_method,
    uri: self.uri,
    headers: self.headers,
    request_time,
  }
}

///|
/// Normalized response metadata. `body_complete` lets adapters conservatively
/// reject interrupted or size-limited bodies.
#derive(Eq, Debug)
pub(all) struct ResponseMeta {
  status : Int32
  headers : HeaderMap
  request_time : Timestamp
  response_time : Timestamp
  body_complete : Bool
}

///|
pub fn ResponseMeta::new(
  status : Int32,
  headers : HeaderMap,
  request_time : Timestamp,
  response_time : Timestamp,
  body_complete : Bool,
) -> ResponseMeta {
  { status, headers, request_time, response_time, body_complete, }
}

///|
pub fn ResponseMeta::complete(
  status : Int32,
  headers : HeaderMap,
  request_time : Timestamp,
  response_time : Timestamp,
) -> ResponseMeta {
  ResponseMeta::new(status, headers, request_time, response_time, true)
}

///|
#derive(Eq, Compare, Debug)
pub(all) enum CacheMode {
  Private
  Shared
}

///|
pub fn CacheMode::label(self : CacheMode) -> String {
  match self {
    Private => "private"
    Shared => "shared"
  }
}

///|
pub fn CacheMode::from_string(value : String) -> CacheMode? {
  match value.trim().to_lower().to_owned() {
    "private" => Some(Private)
    "shared" => Some(Shared)
    _ => None
  }
}

///|
/// Policy settings with privacy-preserving defaults.
#derive(Eq, Debug)
pub(all) struct CacheOptions {
  mode : CacheMode
  allow_heuristic_freshness : Bool
  heuristic_fraction_per_mille : Int32
  max_heuristic_lifetime : DeltaSeconds
  allow_shared_authorized : Bool
  cache_head_responses : Bool
  strip_sensitive_metadata : Bool
}

///|
pub fn CacheOptions::private_cache() -> CacheOptions {
  {
    mode: Private,
    allow_heuristic_freshness: true,
    heuristic_fraction_per_mille: 100,
    max_heuristic_lifetime: DeltaSeconds::from_seconds(86_400),
    allow_shared_authorized: false,
    cache_head_responses: true,
    strip_sensitive_metadata: true,
  }
}

///|
pub fn CacheOptions::shared_cache() -> CacheOptions {
  {
    mode: Shared,
    allow_heuristic_freshness: true,
    heuristic_fraction_per_mille: 100,
    max_heuristic_lifetime: DeltaSeconds::from_seconds(86_400),
    allow_shared_authorized: false,
    cache_head_responses: true,
    strip_sensitive_metadata: true,
  }
}

///|
pub fn CacheOptions::with_heuristic_freshness(
  self : CacheOptions,
  enabled : Bool,
) -> CacheOptions {
  { ..self, allow_heuristic_freshness: enabled, }
}

///|
pub fn CacheOptions::with_shared_authorized(
  self : CacheOptions,
  enabled : Bool,
) -> CacheOptions {
  { ..self, allow_shared_authorized: enabled, }
}

///|
pub fn CacheOptions::with_cache_head_responses(
  self : CacheOptions,
  enabled : Bool,
) -> CacheOptions {
  { ..self, cache_head_responses: enabled, }
}

///|
pub fn CacheOptions::with_heuristic_settings(
  self : CacheOptions,
  fraction_per_mille : Int32,
  maximum : DeltaSeconds,
) -> CacheOptions {
  {
    ..self,
    heuristic_fraction_per_mille: fraction_per_mille,
    max_heuristic_lifetime: maximum,
  }
}

///|
#derive(Eq, Debug)
pub(all) struct StoredPolicy {
  cacheable : Bool
  freshness_lifetime : DeltaSeconds
  requires_revalidation : Bool
  no_store : Bool
  vary_star : Bool
  must_revalidate : Bool
  proxy_revalidate : Bool
  is_private : Bool
  is_public : Bool
}

///|
pub fn StoredPolicy::uncacheable() -> StoredPolicy {
  {
    cacheable: false,
    freshness_lifetime: DeltaSeconds::zero(),
    requires_revalidation: false,
    no_store: true,
    vary_star: false,
    must_revalidate: false,
    proxy_revalidate: false,
    is_private: false,
    is_public: false,
  }
}

///|
pub fn StoredPolicy::cacheable(
  lifetime : DeltaSeconds,
  requires_revalidation : Bool,
) -> StoredPolicy {
  {
    cacheable: true,
    freshness_lifetime: lifetime,
    requires_revalidation,
    no_store: false,
    vary_star: false,
    must_revalidate: false,
    proxy_revalidate: false,
    is_private: false,
    is_public: false,
  }
}

///|
pub fn StoredPolicy::from_directives(
  lifetime : DeltaSeconds,
  directives : CacheControl,
  vary_star : Bool,
) -> StoredPolicy {
  {
    cacheable: !directives.no_store && !vary_star,
    freshness_lifetime: lifetime,
    requires_revalidation: directives.no_cache,
    no_store: directives.no_store,
    vary_star,
    must_revalidate: directives.must_revalidate,
    proxy_revalidate: directives.proxy_revalidate,
    is_private: directives.is_private,
    is_public: directives.is_public,
  }
}

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

///|
pub extend CacheMode with Compare::{op_lt, op_le, op_ge, compare, op_gt}

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

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

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

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

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

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

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

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

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