//! Process-wide Minijinja environment for HTMX fragment rendering.
//!
//! The project server registers its fully-configured `Environment` at startup
//! via [`register_fragment_env`]. Framework components that need to render HTML
//! fragments call [`fragment_env`] to obtain it.
//!
//! If no environment has been registered (e.g. in unit tests), `fragment_env`
//! returns `None` and callers must fall back to inline generation.

use std::sync::{Arc, OnceLock};

use minijinja::Environment;

static FRAGMENT_ENV: OnceLock<Arc<Environment<'static>>> = OnceLock::new();

/// Register the process-wide fragment `Environment`.
///
/// Must be called once at server startup before any fragment rendering happens.
/// Subsequent calls are silently ignored (the first registration wins).
pub fn register_fragment_env(env: Arc<Environment<'static>>) {
    let _ = FRAGMENT_ENV.set(env);
}

/// Return the registered fragment environment, or `None` if not yet registered.
pub fn fragment_env() -> Option<Arc<Environment<'static>>> {
    FRAGMENT_ENV.get().map(Arc::clone)
}
