//! Logout button. Posts to `/api/htmx/auth/logout`.

/// Render the logout button with a language-appropriate label.
pub fn render_logout_button() -> String {
    render_logout_button_for_lang("Sign out", "en")
}

/// Render the logout button with a custom label.
pub fn render_logout_button_for_lang(label: &str, lang: &str) -> String {
    let _ = lang;
    let ctx = minijinja::context! { label => label };
    rustelo_pages_htmx::fragment_env()
        .and_then(|env| {
            env.get_template("partials/nav/logout_button.j2")
                .and_then(|t| t.render(ctx))
                .ok()
        })
        .unwrap_or_else(|| {
            format!(
                r##"<button type="button" class="ds-btn-ghost-sm" hx-post="/api/htmx/auth/logout" hx-target="body" hx-swap="outerHTML">{}</button>"##,
                html_escape::encode_text(label)
            )
        })
}

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

    #[test]
    fn stub_posts_to_logout_endpoint() {
        assert!(render_logout_button().contains(r#"hx-post="/api/htmx/auth/logout""#));
    }
}
