import { jsx as _jsx } from "@emotion/react/jsx-runtime";
import { createInstance } from "i18next";
import { memo, useEffect } from "react";
import { I18nextProvider, initReactI18next, Trans as TransI18nNext, useTranslation as useI18nNextTranslation, } from "react-i18next";
import { getTypedObjectEntries } from "./getTypedObjectEntries.js";
/**
 * Initializes an `i18next` instance and provides strongly-typed
 * translation utilities: `useTranslation`, `Trans`, and `TranslationProvider`.
 */
export function getTranslationServices({ defaultLanguageCode, namespace, resources, }) {
    const i18nInstance = createInstance();
    i18nInstance
        .use(initReactI18next)
        .init({
        defaultNS: namespace,
        ns: [namespace],
        fallbackLng: defaultLanguageCode,
        load: "currentOnly",
        keySeparator: false,
        interpolation: {
            escapeValue: false,
            skipOnVariables: false,
        },
        react: {
            bindI18nStore: "added",
            useSuspense: false,
        },
    })
        .catch((error) => {
        console.error(`[odyssey-contribution-tooling] Error initializing i18nInstance for namespace: ${namespace}`);
        console.error(error);
    });
    Object.entries(resources).forEach(([languageCode, bundle]) => {
        i18nInstance.addResourceBundle(languageCode, namespace, bundle);
    });
    const TranslationProvider = ({ children, languageCode, translationOverrides, }) => {
        useEffect(() => {
            const updatedLanguageCode = languageCode || window.navigator.language;
            i18nInstance.changeLanguage(updatedLanguageCode).catch((error) => {
                console.error(`[odyssey-contribution-tooling] Error changing language code in i18nInstance to: ${updatedLanguageCode}`);
                console.error(error);
            });
        }, [languageCode]);
        useEffect(() => {
            if (translationOverrides) {
                getTypedObjectEntries(translationOverrides).forEach(([languageCode, overrideBundle]) => {
                    i18nInstance.addResourceBundle(languageCode, namespace, overrideBundle, true, true);
                });
            }
        }, [translationOverrides]);
        return _jsx(I18nextProvider, { i18n: i18nInstance, children: children });
    };
    const MemoizedTranslationProvider = memo(TranslationProvider);
    MemoizedTranslationProvider.displayName = "TranslationProvider";
    const Trans = (props) => {
        return (_jsx(TransI18nNext, { ...props, i18n: i18nInstance }));
    };
    const useTranslation = (options) => {
        const [t, i18n, ready] = useI18nNextTranslation(namespace, {
            ...options,
            i18n: i18nInstance,
        });
        const typedT = t;
        const result = [typedT, i18n, ready];
        result.t = typedT;
        result.i18n = i18n;
        result.ready = ready;
        return result;
    };
    return {
        i18n: i18nInstance,
        TranslationProvider: MemoizedTranslationProvider,
        Trans,
        useTranslation,
    };
}
//# sourceMappingURL=getTranslationServices.js.map