import configuration from '@intlayer/config/built';
import type {
DeclaredLocales,
Dictionary,
LocalesValues,
StrictModeLocaleMap,
} from '@intlayer/types';
import { IntlayerServerContext } from './IntlayerServerProvider';
import { getServerContext } from './serverContext';
import { useDictionary } from './useDictionary';
/**
* 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 server context
*/
export const useDictionaryAsync = async <
T extends Dictionary,
L extends LocalesValues = DeclaredLocales,
>(
dictionaryPromise: StrictModeLocaleMap<() => Promise<T>>,
locale?: L
) => {
const localeTarget =
locale ??
getServerContext<LocalesValues>(IntlayerServerContext) ??
configuration?.internationalization.defaultLocale;
const dictionary = await (dictionaryPromise as any)[localeTarget]?.();
return useDictionary<T, L>(dictionary, localeTarget as L);
};