{"version":3,"file":"useTranslations.mjs","names":[],"sources":["../../src/hooks/useTranslations.ts"],"sourcesContent":["import { useCallback, useMemo } from 'react';\nimport {\n  extractVariables,\n  renderDictionaryEntry,\n  renderDictionaryObject,\n  resolveDictionaryLookupOptions,\n} from 'gt-i18n/internal';\nimport { useLocale } from './condition-store';\nimport { useGT } from './useGT';\nimport type {\n  DictionaryObjectTranslation,\n  TranslationVariables,\n} from 'gt-i18n/types';\nimport { useDefaultLocale } from './i18n-config';\nimport { useShouldTranslate } from './utils';\nimport { useTrackedDictionaryResolver } from './external-store/useTrackedDictionaryResolver';\nimport { useTrackedDictionaryObjResolver } from './external-store/useTrackedDictionaryObjResolver';\n\n// ===== Hook ===== //\n\nexport function useTranslations(rootId?: string): UseTranslationsFunction {\n  const locale = useLocale();\n  const defaultLocale = useDefaultLocale();\n  const shouldTranslate = useShouldTranslate();\n  const gt = useGT();\n  const resolveDictionaryEntry = useTrackedDictionaryResolver();\n  const translateObject = useTranslationsObj(rootId);\n\n  const translateEntry = useCallback(\n    (suffix: string, options: TranslationVariables = {}) => {\n      const id = getId(rootId, suffix);\n\n      const sourceEntry = resolveDictionaryEntry({\n        locale: defaultLocale,\n        id,\n      });\n      if (sourceEntry === undefined) {\n        throw new Error(`Dictionary entry ${id} cannot be found`);\n      }\n      const sourceOptions = resolveDictionaryLookupOptions(sourceEntry.options);\n      if (!shouldTranslate) {\n        return gt(sourceEntry.entry, {\n          ...sourceOptions,\n          ...extractVariables(options),\n          $locale: defaultLocale,\n        });\n      }\n\n      const targetEntry = resolveDictionaryEntry({ locale, id });\n      if (targetEntry?.entry != null) {\n        return renderDictionaryEntry({\n          sourceLocale: defaultLocale,\n          targetLocale: locale,\n          sourceEntry,\n          target: targetEntry.entry,\n          dictionaryOptions: sourceOptions,\n          options,\n        });\n      }\n\n      return gt(sourceEntry.entry, {\n        ...sourceOptions,\n        ...extractVariables(options),\n        $locale: locale,\n      });\n    },\n    [defaultLocale, gt, rootId, locale, resolveDictionaryEntry, shouldTranslate]\n  );\n\n  // Wrap instead of mutating translateEntry, so the returned function changes\n  // identity when either translateEntry or translateObject changes\n  return useMemo(\n    () =>\n      Object.assign(\n        (suffix: string, options?: TranslationVariables) =>\n          translateEntry(suffix, options),\n        { obj: translateObject }\n      ),\n    [translateEntry, translateObject]\n  );\n}\n\nfunction useTranslationsObj(rootId?: string): UseTranslationsObjFunction {\n  const locale = useLocale();\n  const defaultLocale = useDefaultLocale();\n  const shouldTranslate = useShouldTranslate();\n  const gt = useGT();\n  const resolveDictionaryObject = useTrackedDictionaryObjResolver();\n\n  return useCallback(\n    (suffix: string) => {\n      const entryId = getId(rootId, suffix);\n      const sourceObject = resolveDictionaryObject({\n        locale: defaultLocale,\n        id: entryId,\n      });\n      if (sourceObject === undefined) {\n        throw new Error(`Dictionary entry ${entryId} cannot be found`);\n      }\n\n      let targetObject = undefined;\n      if (shouldTranslate) {\n        const targetLookup = { locale, id: entryId };\n        targetObject = resolveDictionaryObject(targetLookup);\n      }\n\n      return renderDictionaryObject({\n        sourceObject,\n        targetObject,\n        translate: (sourceEntry, dictionaryOptions) =>\n          gt(sourceEntry.entry, {\n            ...dictionaryOptions,\n            $locale: shouldTranslate ? locale : defaultLocale,\n          }),\n      });\n    },\n    [\n      defaultLocale,\n      rootId,\n      locale,\n      resolveDictionaryObject,\n      shouldTranslate,\n      gt,\n    ]\n  );\n}\n\n// ===== Lookup Helpers ===== //\n\nfunction getId(prefix: string | undefined, suffix: string): string {\n  return prefix ? `${prefix}.${suffix}` : suffix;\n}\n\n// ===== Types ===== //\n\nexport type UseTranslationsFunction = ((\n  id: string,\n  options?: TranslationVariables\n) => string) & {\n  obj: (id: string) => DictionaryObjectTranslation;\n};\n\ntype UseTranslationsObjFunction = (id: string) => DictionaryObjectTranslation;\n"],"mappings":"okBAoBA,SAAgB,EAAgB,EAA0C,CACxE,IAAM,EAAS,GAAW,CACpB,EAAgB,GAAkB,CAClC,EAAkB,GAAoB,CACtC,EAAK,GAAO,CACZ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