import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React from "react";
import { Redirect, Route, Switch } from "react-router-dom";
import { useLocale } from "ttag";
const Wrapper = (props) => {
    React.useEffect(() => {
        if (window.localStorage) {
            window.localStorage.setItem(props.localStorageKey, props.locale);
        }
    }, [props.locale, props.localStorageKey]);
    useLocale(props.locale);
    return _jsx(_Fragment, { children: props.children });
};
const Locale = (props) => {
    const langs = props.langs.length ? props.langs : ["en"];
    const defaultLang = props.defaultLang || "en";
    const localStorageKey = props.localStorageKey || "language";
    let locale = langs.reduce((prev, locale) => window.navigator.language.startsWith(locale) ? locale : prev, defaultLang);
    if (window.localStorage) {
        const storedLocale = window.localStorage.getItem(localStorageKey);
        if (storedLocale) {
            locale = storedLocale;
        }
    }
    return (_jsxs(Switch, { children: [langs.map((lang) => (_jsx(Route, { path: `/${lang}`, children: _jsx(Wrapper, { locale: lang, localStorageKey: localStorageKey, children: props.children }) }, lang))), _jsx(Redirect, { from: "/*404*", to: `/${locale}/404`, children: props.children }), _jsx(Route
            // TODO: fix types
            // @ts-ignore
            , { 
                // TODO: fix types
                // @ts-ignore
                render: ({ location }) => (_jsx(Redirect, { to: `/${locale}${location.pathname}${location.search}${location.hash}` })) })] }));
};
export default Locale;
//# sourceMappingURL=index.js.map