//! Generic static page renderer (about, services, legal, privacy, product pages).

use std::collections::HashMap;

use minijinja::{Environment, context};
use rustelo_core_lib::load_texts_from_ftl;

use rustelo_pages_htmx::render::render_safe;

pub fn render(env: &Environment, page_id: &str, lang: &str) -> String {
    let texts = load_texts(lang);
    let ctx = context! { page_id => page_id, texts => texts, lang => lang };
    let specific = format!("pages/{page_id}.j2");
    if env.get_template(&specific).is_ok() {
        render_safe(env, &specific, ctx)
    } else {
        render_safe(env, "pages/static.j2", ctx)
    }
}

fn load_texts(lang: &str) -> HashMap<String, String> {
    load_texts_from_ftl(lang)
        .map(|t| t.get_translations_for_language(lang))
        .unwrap_or_default()
}
