use minijinja::Environment;
use std::sync::Arc;

#[derive(Clone)]
pub struct State {
    template_env: Arc<Environment<'static>>,
}

impl State {
    pub async fn new() -> anyhow::Result<Self> {
        let mut env = Environment::new();

        #[cfg(debug_assertions)]
        {
            env.set_loader(minijinja::path_loader("templates"));
        }

        #[cfg(not(debug_assertions))]
        {
            env.add_template("base.html", include_str!("../../../templates/base.html"))?;
            env.add_template("index.html", include_str!("../../../templates/index.html"))?;
        }

        Ok(Self {
            template_env: Arc::new(env),
        })
    }

    pub fn template_env(&self) -> Arc<Environment<'static>> {
        self.template_env.clone()
    }
}
