import { useEffect, useState } from "react"

import fr from "../dictionaries/fr.js"
import en from "../dictionaries/en.js"
import { getCookie } from "../helpers.js"

export type useLang = {
    lang: string
}

export default function useLang({ lang }: useLang) {
    const [l, setLangue]: any = useState(lang)
    const dictionaries: any = { fr, en }
    useEffect(function () {
    
        const language = getCookie("lang") || lang
        setLangue(language)
        localStorage.setItem("lang", language)
    }, [])
    return {
        l,
        setLangue,
        dico: function (key: string) {
            return dictionaries[l][key] || dictionaries[l]['default']
        }
    }
}
