'use client';
import type {
Dictionary,
DictionaryKeys,
LocalesValues,
StrictModeLocaleMap,
} from '@intlayer/types';
import { useContext, useMemo } from 'preact/hooks';
import { IntlayerClientContext } from './IntlayerProvider';
import { useDictionary } from './useDictionary';
import { useLoadDynamic } from './useLoadDynamic';
/**
* On the server side, Hook that transform a dictionary and return the content
*
* If the locale is not provided, it will use the locale from the client context
*/
export const useDictionaryDynamic = <
T extends Dictionary,
K extends DictionaryKeys,
>(
dictionaryPromise: StrictModeLocaleMap<() => Promise<T>>,
key: K,
locale?: LocalesValues
) => {
const { locale: currentLocale } = useContext(IntlayerClientContext);
const localeTarget = useMemo(
() => locale ?? currentLocale,
[currentLocale, locale]
);
const dictionary = useLoadDynamic<T>(
`${String(key)}.${localeTarget}`,
(dictionaryPromise as any)[localeTarget]?.()
) as T;
return useDictionary(dictionary, localeTarget);
};