pub fn format_when(value: Option<&str>, date_format: &str, clock_24h: bool) -> String {
    let Some(value) = value else {
        return String::new();
    };
    chrono::DateTime::parse_from_rfc3339(value)
        .map(|date_time| {
            let date = match date_format {
                "us" => date_time.format("%m/%d/%Y").to_string(),
                "european" => date_time.format("%d/%m/%Y").to_string(),
                "long" => date_time.format("%b %-d, %Y").to_string(),
                _ => date_time.format("%Y-%m-%d").to_string(),
            };
            let time = if clock_24h {
                date_time.format("%H:%M").to_string()
            } else {
                date_time.format("%-I:%M%P").to_string()
            };
            format!("{date} {time}")
        })
        .unwrap_or_else(|_| value.to_owned())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn formats_configured_date_and_clock_styles() {
        let value = "2026-08-18T19:15:00+02:00";
        assert_eq!(
            format_when(Some(value), "european", false),
            "18/08/2026 7:15pm"
        );
        assert_eq!(format_when(Some(value), "iso", true), "2026-08-18 19:15");
    }
}
