{"version":3,"file":"validatePrefix.mjs","names":[],"sources":["../../../src/localization/validatePrefix.ts"],"sourcesContent":["// ── Tree-shake constants ──────────────────────────────────────────────────────\n// When these env vars are injected at build time, bundlers eliminate the\n// branches guarded by these constants.\n\n/**\n * True when the build-time routing mode is known and is NOT 'no-prefix'.\n */\nconst TREE_SHAKE_NO_PREFIX =\n  process.env['INTLAYER_ROUTING_MODE'] &&\n  process.env['INTLAYER_ROUTING_MODE'] !== 'no-prefix';\n\n/**\n * True when the build-time routing mode is known and is NOT 'search-params'.\n */\nconst TREE_SHAKE_SEARCH_PARAMS =\n  process.env['INTLAYER_ROUTING_MODE'] &&\n  process.env['INTLAYER_ROUTING_MODE'] !== 'search-params';\n\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport {\n  getPrefix,\n  type RoutingOptions,\n  resolveRoutingConfig,\n} from './getPrefix';\n\nexport type ValidatePrefixResult = {\n  isValid: boolean;\n  localePrefix: string | undefined;\n};\n\n/**\n * Checks whether a given locale is valid based on the configured locales.\n *\n * @param locale - The locale value to validate. Can be `undefined` or `null`.\n * @param options - Optional configuration to override default settings.\n * @param options.locales - Array of valid locales. Defaults to the configured internationalization locales.\n * @param options.defaultLocale - The default locale to use as fallback. Defaults to the configured default locale.\n * @param options.mode - The routing mode (`'prefix'`, `'prefix-all'`, or `'no-prefix'`). Defaults to the configured routing mode.\n * @returns An object containing the validation result and the locale prefix.\n *\n * @example\n * // Check if 'en' is a valid locale\n * const { isValid, localePrefix } = validatePrefix('en');\n *\n * @example\n * // Check with custom options\n * const { isValid, localePrefix } = validatePrefix('fr', {\n *   locales: ['en', 'fr', 'es'],\n *   defaultLocale: 'en',\n *   mode: 'prefix-all',\n * });\n */\nexport const validatePrefix = (\n  locale: LocalesValues | undefined | null,\n  options?: RoutingOptions\n): ValidatePrefixResult => {\n  const { defaultLocale, mode, locales } = resolveRoutingConfig(options);\n\n  if (\n    (!TREE_SHAKE_NO_PREFIX && mode === 'no-prefix') ||\n    (!TREE_SHAKE_SEARCH_PARAMS && mode === 'search-params')\n  ) {\n    return { isValid: true, localePrefix: undefined };\n  }\n\n  const { localePrefix } = getPrefix(locale || defaultLocale, {\n    mode,\n    locales,\n    defaultLocale,\n  });\n\n  if (localePrefix === locale && locale === undefined) {\n    return { isValid: true, localePrefix: undefined };\n  }\n\n  // A segment that doesn't match a locale code pattern (e.g. 'concept', 'compiler')\n  // is a content slug from a default-locale URL, not an invalid locale attempt.\n  if (locale && !/^[a-z]{2,3}(-[a-zA-Z]{2,4})?$/.test(locale)) {\n    return { isValid: true, localePrefix: undefined };\n  }\n\n  const isValid = locales.some((localeEl) => localeEl === locale);\n\n  return { isValid, localePrefix };\n};\n"],"mappings":";;;;;;AAOA,MAAM,uBACJ,QAAQ,IAAI,4BACZ,QAAQ,IAAI,6BAA6B;;;;AAK3C,MAAM,2BACJ,QAAQ,IAAI,4BACZ,QAAQ,IAAI,6BAA6B;;;;;;;;;;;;;;;;;;;;;;;AAoC3C,MAAa,kBACX,QACA,YACyB;CACzB,MAAM,EAAE,eAAe,MAAM,YAAY,qBAAqB,QAAQ;AAEtE,KACG,CAAC,wBAAwB,SAAS,eAClC,CAAC,4BAA4B,SAAS,gBAEvC,QAAO;EAAE,SAAS;EAAM,cAAc;EAAW;CAGnD,MAAM,EAAE,iBAAiB,UAAU,UAAU,eAAe;EAC1D;EACA;EACA;EACD,CAAC;AAEF,KAAI,iBAAiB,UAAU,WAAW,OACxC,QAAO;EAAE,SAAS;EAAM,cAAc;EAAW;AAKnD,KAAI,UAAU,CAAC,gCAAgC,KAAK,OAAO,CACzD,QAAO;EAAE,SAAS;EAAM,cAAc;EAAW;AAKnD,QAAO;EAAE,SAFO,QAAQ,MAAM,aAAa,aAAa,OAExC;EAAE;EAAc"}