'use client';
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
import { importDateFNSLocale, t } from '@payloadcms/translations';
import { enUS } from 'date-fns/locale/en-US';
import { useRouter } from 'next/navigation.js';
import React, { createContext, use, useEffect, useState } from 'react';
const Context = /*#__PURE__*/ createContext({
    // Use `any` here to be replaced later with a more specific type when used
    i18n: {
        dateFNS: enUS,
        dateFNSKey: 'en-US',
        fallbackLanguage: 'en',
        language: 'en',
        t: (key)=>key,
        translations: {}
    },
    languageOptions: undefined,
    switchLanguage: undefined,
    t: (key)=>undefined
});
export const TranslationProvider = ({ children, dateFNSKey, fallbackLang, language, languageOptions, switchLanguageServerAction, translations })=>{
    const router = useRouter();
    const [dateFNS, setDateFNS] = useState();
    const nextT = React.useCallback((key, vars)=>t({
            key,
            translations,
            vars
        }), [
        translations
    ]);
    const switchLanguage = React.useCallback(async (lang)=>{
        try {
            await switchLanguageServerAction(lang);
            router.refresh();
        } catch (error) {
            // eslint-disable-next-line no-console
            console.error(`Error loading language: "${lang}"`, error);
        }
    }, [
        switchLanguageServerAction,
        router
    ]);
    useEffect(()=>{
        const loadDateFNS = async ()=>{
            const imported = await importDateFNSLocale(dateFNSKey);
            setDateFNS(imported);
        };
        void loadDateFNS();
    }, [
        dateFNSKey
    ]);
    return /*#__PURE__*/ _jsxDEV(Context, {
        value: {
            i18n: {
                dateFNS,
                dateFNSKey,
                fallbackLanguage: fallbackLang,
                language,
                t: nextT,
                translations
            },
            languageOptions,
            switchLanguage,
            t: nextT
        },
        children: children
    }, void 0, false, {
        fileName: "src/providers/Translation/index.tsx",
        lineNumber: 104,
        columnNumber: 5
    }, this);
};
export const useTranslation = ()=>use(Context);

//# sourceMappingURL=index.js.map