use crate::locales::catalogues::get_dictionary;

mod catalogues;

#[derive(Debug, Clone)]
pub struct Locales(pub Vec<Option<String>>);

pub fn init_locales() {
    #[cfg(not(feature = "ssr"))]
    {
        use leptos::*;
        let loc = Locales(
            window()
                .navigator()
                .languages()
                .into_iter()
                .map(|val| val.as_string())
                .collect::<Vec<_>>(),
        );
        provide_context(loc);
    }
}

pub fn trl(phrase: &str) -> impl Fn() -> String {
    let mut translated = phrase;
    if let Some(dict) = get_dictionary() {
        if let Some(p) = dict.get(phrase.to_string().as_str()) {
            translated = *p;
        }
    }

    let out = translated.to_string();

    move || { out.clone() }
}