//! Environment-driven wildcard host routing configuration.

use std::env;

use super::DEFAULT_WILDCARD_HOST_PATTERN;

const ENV_WILDCARD_HOST_PATTERN: &str = "ATHENA_WILDCARD_HOST_PATTERN";
const ENV_WILDCARD_HOST_ROUTING_ENABLED: &str = "ATHENA_WILDCARD_HOST_ROUTING_ENABLED";

/// Returns the effective wildcard host pattern.
pub fn wildcard_host_pattern() -> String {
    env::var(ENV_WILDCARD_HOST_PATTERN)
        .map(|value| value.trim().to_string())
        .ok()
        .filter(|value| !value.is_empty())
        .unwrap_or_else(|| DEFAULT_WILDCARD_HOST_PATTERN.to_string())
}

/// Returns whether wildcard host routing is enabled.
pub fn wildcard_host_routing_enabled() -> bool {
    env::var(ENV_WILDCARD_HOST_ROUTING_ENABLED)
        .ok()
        .map(|value| value.trim().to_ascii_lowercase())
        .map(|value| !matches!(value.as_str(), "0" | "false" | "no" | "off"))
        .unwrap_or(true)
}
