{"version":3,"file":"useDictionary.mjs","names":[],"sources":["../../../src/client/useDictionary.ts"],"sourcesContent":["'use client';\n\nimport {\n  getDictionarySelectorCacheKey,\n  resolveDictionaryArgument,\n} from '@intlayer/core/dictionaryManipulator';\nimport type {\n  Dictionary,\n  DictionarySelectorForGroup,\n  QualifiedDictionaryGroup,\n} from '@intlayer/types/dictionary';\nimport type {\n  DeclaredLocales,\n  LocalesValues,\n} from '@intlayer/types/module_augmentation';\nimport { useContext, useMemo } from 'react';\nimport { getDictionary } from '../getDictionary';\nimport { IntlayerClientContext } from './IntlayerProvider';\n\n/**\n * Client-side hook that transforms a dictionary (or qualified dictionary\n * group) and returns the content.\n *\n * If the locale is not provided (directly or through the selector), it will\n * use the locale from the client context. The same applies to the variant,\n * which falls back to the provider's ambient `variant`.\n */\nexport const useDictionary = <\n  const T extends Dictionary | QualifiedDictionaryGroup,\n  const A extends LocalesValues | DictionarySelectorForGroup<T> =\n    DeclaredLocales,\n>(\n  dictionary: T,\n  localeOrSelector?: A\n) => {\n  const { locale: currentLocale, variant: contextVariant } =\n    useContext(IntlayerClientContext) ?? {};\n\n  // Layers the provider's locale and variant under the call-site argument.\n  // This is the seam the build-time optimize transform lands on: it rewrites\n  // `useIntlayer('key', selector)` into `useDictionary(dict, selector)`, so an\n  // ambient variant has to be applied here to survive the rewrite.\n  const argument =\n    process.env.INTLAYER_DICTIONARY_SELECTOR !== 'false'\n      ? resolveDictionaryArgument({\n          localeOrSelector,\n          contextLocale: currentLocale,\n          contextVariant,\n          dictionaryKey: dictionary.key,\n        })\n      : (localeOrSelector ?? currentLocale);\n\n  // Stable identity of the resolved argument for memoization — a provider\n  // variant is often a fresh object literal on every render, so the dependency\n  // has to be its serialization, never its reference.\n  const argumentIdentity =\n    process.env.INTLAYER_DICTIONARY_SELECTOR !== 'false' &&\n    typeof argument === 'object' &&\n    argument !== null\n      ? `${argument.locale ?? ''}|${getDictionarySelectorCacheKey(argument)}`\n      : argument;\n\n  return useMemo(\n    () => getDictionary<T, A>(dictionary, argument as A),\n    [dictionary.key, argumentIdentity]\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;;;AA2BA,MAAa,iBAKX,YACA,qBACG;CACH,MAAM,EAAE,QAAQ,eAAe,SAAS,mBACtC,WAAW,qBAAqB,KAAK,CAAC;CAMxC,MAAM,WACJ,QAAQ,IAAI,iCAAiC,UACzC,0BAA0B;EACxB;EACA,eAAe;EACf;EACA,eAAe,WAAW;CAC5B,CAAC,IACA,oBAAoB;CAK3B,MAAM,mBACJ,QAAQ,IAAI,iCAAiC,WAC7C,OAAO,aAAa,YACpB,aAAa,OACT,GAAG,SAAS,UAAU,GAAG,GAAG,8BAA8B,QAAQ,MAClE;CAEN,OAAO,cACC,cAAoB,YAAY,QAAa,GACnD,CAAC,WAAW,KAAK,gBAAgB,CACnC;AACF"}