"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.I18nProvider = I18nProvider;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const react_i18next_1 = require("react-i18next");
/**
 * Simple wrapper around I18nextProvider for consistent setup
 * The actual i18n instance should be configured at the app level
 * Waits for i18next to be initialized AND language loaded to prevent FOUC
 */
function I18nProvider({ children, i18n }) {
    const [isReady, setIsReady] = (0, react_1.useState)(false);
    (0, react_1.useEffect)(() => {
        // Check if both initialized and language is loaded
        const checkReady = () => {
            const initialized = i18n.isInitialized;
            const hasCommon = i18n.hasLoadedNamespace("common");
            console.log("i18n ready check:", {
                initialized,
                hasCommon,
                language: i18n.language,
            });
            if (initialized && hasCommon) {
                setIsReady(true);
            }
        };
        checkReady();
        // Fallback: if not ready after 300ms, just proceed anyway
        const fallbackTimeout = setTimeout(() => {
            if (!isReady) {
                console.log("i18n ready timeout - proceeding anyway");
                setIsReady(true);
            }
        }, 300);
        // Listen for both initialization and language loaded events
        const handleInitialized = () => {
            checkReady();
        };
        const handleLanguageLoaded = () => {
            checkReady();
        };
        i18n.on("initialized", handleInitialized);
        i18n.on("languageChanged", handleLanguageLoaded);
        i18n.on("loaded", handleLanguageLoaded);
        return () => {
            clearTimeout(fallbackTimeout);
            i18n.off("initialized", handleInitialized);
            i18n.off("languageChanged", handleLanguageLoaded);
            i18n.off("loaded", handleLanguageLoaded);
        };
    }, [i18n, isReady]);
    if (!isReady) {
        return null;
    }
    return (0, jsx_runtime_1.jsx)(react_i18next_1.I18nextProvider, { i18n: i18n, children: children });
}
//# sourceMappingURL=provider.js.map