{"version":3,"file":"localize-utils.mjs","names":[],"sources":["../../src/link/localize-utils.ts"],"sourcesContent":["import type NextLink from 'next/link';\nimport type { ComponentProps } from 'react';\nimport { getI18nConfig } from 'gt-i18n/internal';\nimport { createLocalePrefixedHrefWarning } from '../errors/link';\nimport { resolveMiddlewareConfig } from '../middleware-dir/resolveMiddlewareConfig';\nimport { compilePathRegex, pathnameMatchesRegex } from '../utils/pathRegex';\n\n/** A locale to prefix, `undefined` for the current locale, or `false` for none. */\nexport type LinkLocale = ComponentProps<typeof NextLink>['locale'];\n\nlet routingConfig:\n  | (ReturnType<typeof resolveMiddlewareConfig> & { pathRegex?: RegExp })\n  | undefined;\n\n/**\n * Reads the routing options passed to withGTConfig() on first use, so\n * importing gt-next/link does no work.\n */\nfunction getRoutingConfig() {\n  routingConfig ??= {\n    ...resolveMiddlewareConfig(),\n    pathRegex: compilePathRegex(process.env._GENERALTRANSLATION_PATH_REGEX),\n  };\n  return routingConfig;\n}\n\n/**\n * Returns the locale to prefix, or `false` when the middleware would not\n * redirect the unprefixed path. Unprefixed default-locale paths are rewritten\n * internally unless `prefixDefaultLocale` is set. An explicit default-locale\n * switch keeps its prefix while smart routing would otherwise route to the\n * locale cookie.\n */\nfunction getPrefixLocale(\n  locale: LinkLocale,\n  currentLocale: string,\n  defaultLocale: string\n): string | false {\n  if (locale === false) return false;\n  const targetLocale = locale ?? currentLocale;\n  const { prefixDefaultLocale, enableSmartRouting } = getRoutingConfig();\n  if (prefixDefaultLocale || targetLocale !== defaultLocale)\n    return targetLocale;\n  return locale !== undefined && enableSmartRouting ? targetLocale : false;\n}\n\nlet warnedLocalePrefixedHref = false;\n\n/** Warns once when an href already starts with a configured locale. */\nfunction warnLocalePrefixedHref(href: string, pathname: string) {\n  if (warnedLocalePrefixedHref) return;\n  const pathLocale = pathname.split('/')[1];\n  if (!getI18nConfig().getLocales().includes(pathLocale)) return;\n  warnedLocalePrefixedHref = true;\n  console.warn(createLocalePrefixedHrefWarning(href, pathLocale));\n}\n\nfunction prefixPath(path: string, locale: string): string {\n  if (!path.startsWith('/') || path.startsWith('//')) return path;\n\n  const url = new URL(path, 'http://localhost');\n  if (!pathnameMatchesRegex(url.pathname, getRoutingConfig().pathRegex))\n    return path;\n  if (process.env.NODE_ENV === 'development')\n    warnLocalePrefixedHref(path, url.pathname);\n  url.pathname =\n    url.pathname === '/'\n      ? `/${encodeURIComponent(locale)}`\n      : `/${encodeURIComponent(locale)}${url.pathname}`;\n\n  return `${url.pathname}${url.search}${url.hash}`;\n}\n\n/**\n * Localizes an href the way `gt-next/link` does.\n *\n * Internal paths are prefixed with the target locale, except default-locale\n * paths when `prefixDefaultLocale` is `false` (an explicit default `locale`\n * keeps its prefix while `enableSmartRouting` is on). Localized path aliases\n * and locale availability are still resolved by the middleware. External URLs\n * and paths that don't match `pathRegex` are never prefixed.\n */\nexport function localizeHref(\n  href: string,\n  locale: LinkLocale,\n  currentLocale: string,\n  defaultLocale: string\n): string {\n  const prefixLocale = getPrefixLocale(locale, currentLocale, defaultLocale);\n  return prefixLocale ? prefixPath(href, prefixLocale) : href;\n}\n"],"mappings":";;;;;AAUA,IAAI;;;;;AAQJ,SAAS,mBAAmB;AAC1B,mBAAkB;EAChB,GAAG,yBAAyB;EAC5B,WAAW,iBAAiB,QAAQ,IAAI,+BAA+B;EACxE;AACD,QAAO;;;;;;;;;AAUT,SAAS,gBACP,QACA,eACA,eACgB;AAChB,KAAI,WAAW,MAAO,QAAO;CAC7B,MAAM,eAAe,UAAU;CAC/B,MAAM,EAAE,qBAAqB,uBAAuB,kBAAkB;AACtE,KAAI,uBAAuB,iBAAiB,cAC1C,QAAO;AACT,QAAO,WAAW,KAAA,KAAa,qBAAqB,eAAe;;AAGrE,IAAI,2BAA2B;;AAG/B,SAAS,uBAAuB,MAAc,UAAkB;AAC9D,KAAI,yBAA0B;CAC9B,MAAM,aAAa,SAAS,MAAM,IAAI,CAAC;AACvC,KAAI,CAAC,eAAe,CAAC,YAAY,CAAC,SAAS,W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