{"version":3,"file":"index.mjs","sources":["../../../src/common/locale/index.ts"],"sourcesContent":["import { IntlShape } from 'react-intl/src/types';\nimport { formatCurrencyCode } from '@transferwise/formatting';\nimport { Direction } from '../direction';\n\nexport const DEFAULT_LANG = 'en';\nexport const DEFAULT_LOCALE = 'en-GB';\n\n/**\n * Languages written right-to-left.\n */\nexport const RTL_LANGUAGES = ['ar', 'he'];\n\n/**\n * @deprecated The source of truth for supported languages lives in Crab.\n */\nexport const SUPPORTED_LANGUAGES = [\n  DEFAULT_LANG,\n  'cs',\n  'de',\n  'es',\n  'fr',\n  'hu',\n  'id',\n  'it',\n  'ja',\n  'nl',\n  'pl',\n  'pt',\n  'ro',\n  'ru',\n  'th',\n  'tr',\n  'uk',\n  'zh',\n];\n\n/**\n * Verifies and adjusts locale, replacing `_` with `-`.\n *\n * @param locale `es`, `es_ES`, `en-GB`, `en`, `ja`, `ja-JP`, etc.\n * @returns `null` if locale is unrecognized by `Intl.Locale`.\n */\nexport function adjustLocale(locale: string) {\n  const localeTrimmed = locale?.trim();\n  if (localeTrimmed) {\n    try {\n      return new Intl.Locale(localeTrimmed.replace('_', '-')).baseName;\n    } catch (error) {\n      // eslint-disable-next-line no-console\n      console.error(error);\n    }\n  }\n  return null;\n}\n\n/**\n * Provides corresponding lang (iso2) for provided locale.\n *\n * @deprecated The use of this function almost always breaks language variants\n * e.g. Simplified and Traditional Chinese.\n * There should be no use case for this function.\n * To select the correct translations from a translations object, pass the\n * locale directly into Crab's getLocalisedMessages.\n * @param locale `es`, `es-ES`, `en-GB`, `en`, `ja`, `ja-JP`, etc.\n * @returns Two-letter ISO 639-1 language code, falling back to `null` if locale is invalid or language is unsupported.\n */\nexport function getLangFromLocale(locale: string) {\n  const adjustedLocale = adjustLocale(locale);\n  if (adjustedLocale != null) {\n    const { language } = new Intl.Locale(adjustedLocale);\n    if (SUPPORTED_LANGUAGES.includes(language)) {\n      return language;\n    }\n  }\n  return null;\n}\n\n/**\n * Provides corresponding country code (iso2) for locales code with explicit region value.\n *\n * @param locale `es-ES`, `en-GB`, `ja-JP`, etc.\n * @returns `null` if the locale is invalid or the region can‘t be identified.\n */\nexport function getCountryFromLocale(locale: string) {\n  const adjustedLocale = adjustLocale(locale);\n  return adjustedLocale != null ? (new Intl.Locale(adjustedLocale).region ?? null) : null;\n}\n\n/**\n * Provides the layout direction for a given locale.\n *\n * @param locale `es`, `es-ES`, `en-GB`, `en`, `ja`, `ja-JP`, etc.\n * @returns The layout direction based on the locale, falling back to `Direction.LTR` if the locale is invalid or unsupported.\n */\nexport function getDirectionFromLocale(locale: string) {\n  const adjustedLocale = adjustLocale(locale);\n  return adjustedLocale != null && RTL_LANGUAGES.includes(new Intl.Locale(adjustedLocale).language)\n    ? Direction.RTL\n    : Direction.LTR;\n}\n\n/**\n * Provides the localized currency name for a given currency code.\n * @param intl The `intl` object from `react-intl` used for localization.\n * @param currencyCode The ISO 4217 currency code (e.g., `USD`, `EUR`, `JPY`).\n * @returns The localized currency name if Intl.DisplayNames is supported supported, otherwise returns the original currency code.\n */\nexport function getLocaleCurrencyName(intl: IntlShape, currencyCode: string) {\n  if (typeof Intl.DisplayNames === 'function') {\n    const localisedName = intl.formatDisplayName(currencyCode, {\n      type: 'currency',\n      fallback: 'none',\n    });\n    return localisedName != null ? localisedName : formatCurrencyCode(currencyCode);\n  }\n  return formatCurrencyCode(currencyCode);\n}\n"],"names":["DEFAULT_LANG","DEFAULT_LOCALE","RTL_LANGUAGES","SUPPORTED_LANGUAGES","adjustLocale","locale","localeTrimmed","trim","Intl","Locale","replace","baseName","error","console","getLangFromLocale","adjustedLocale","language","includes","getCountryFromLocale","region","getDirectionFromLocale","Direction","RTL","LTR","getLocaleCurrencyName","intl","currencyCode","DisplayNames","localisedName","formatDisplayName","type","fallback","formatCurrencyCode"],"mappings":";;;AAIO,MAAMA,YAAY,GAAG;AACrB,MAAMC,cAAc,GAAG;AAE9B;;AAEG;MACUC,aAAa,GAAG,CAAC,IAAI,EAAE,IAAI;AAEx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