{"version":3,"file":"getPathWithoutLocale.cjs","names":["internationalization","checkIsURLAbsolute"],"sources":["../../../src/localization/getPathWithoutLocale.ts"],"sourcesContent":["import { internationalization } from '@intlayer/config/built';\n\n// ── 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 a prefix-based\n * mode (neither 'prefix-all' nor 'prefix-no-default').\n */\nconst TREE_SHAKE_PREFIX_MODES =\n  process.env['INTLAYER_ROUTING_MODE'] &&\n  process.env['INTLAYER_ROUTING_MODE'] !== 'prefix-all' &&\n  process.env['INTLAYER_ROUTING_MODE'] !== 'prefix-no-default';\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 {\n  DeclaredLocales,\n  LocalesValues,\n  PathWithoutLocale,\n} from '@intlayer/types/module_augmentation';\nimport { checkIsURLAbsolute } from '../utils/checkIsURLAbsolute';\n\n/**\n * Removes the locale segment from the given URL or pathname if present.\n * Also removes locale from search parameters if present.\n *\n * This function get the locales from the configuration if not provided.\n *\n * Example:\n *\n * ```ts\n * getPathWithoutLocale('/en/dashboard') // Returns '/dashboard'\n * getPathWithoutLocale('/fr/dashboard') // Returns '/dashboard'\n * getPathWithoutLocale('/dashboard') // Returns '/dashboard'\n * getPathWithoutLocale('dashboard') // Returns 'dashboard'\n * getPathWithoutLocale('/dashboard?locale=fr') // Returns '/dashboard'\n * getPathWithoutLocale('https://example.com/en/dashboard') // Returns 'https://example.com/dashboard'\n * getPathWithoutLocale('https://example.com/fr/dashboard') // Returns 'https://example.com/dashboard'\n * getPathWithoutLocale('https://example.com/dashboard') // Returns 'https://example.com/dashboard'\n * getPathWithoutLocale('https://example.com/dashboard?locale=fr') // Returns 'https://example.com/dashboard'\n * ```\n *\n * @param inputUrl - The complete URL string or pathname to process.\n * @param locales - Optional array of supported locales. Defaults to `localesDefault`.\n * @returns The URL string or pathname without the locale segment or locale search parameter.\n */\n/**\n * The return type is narrowed to a template-literal type when both `inputUrl`\n * and `locales` are string literals known at compile time. Absolute URLs fall\n * back to `string`.\n */\nexport const getPathWithoutLocale = <\n  const T extends string,\n  const L extends LocalesValues = DeclaredLocales,\n>(\n  inputUrl: T,\n  locales: L[] = internationalization?.locales as L[]\n): PathWithoutLocale<T, L> => {\n  // Determine if the original URL is absolute (includes protocol)\n  const isAbsoluteUrl = checkIsURLAbsolute(inputUrl);\n\n  let fixedInputUrl = inputUrl;\n\n  if (inputUrl?.endsWith('/')) {\n    fixedInputUrl = inputUrl.slice(0, -1);\n  }\n\n  // Initialize a URL object if the URL is absolute\n  // For relative URLs, use a dummy base to leverage the URL API\n  const url = isAbsoluteUrl\n    ? new URL(fixedInputUrl)\n    : new URL(fixedInputUrl, 'http://example.com');\n\n  const pathname = url.pathname;\n\n  // Ensure the pathname starts with '/'\n  if (!pathname.startsWith('/')) {\n    // If not, return the URL as is\n    url.pathname = `/${pathname}`;\n  }\n\n  // Only strip locale path prefix in prefix-based routing modes\n  if (!TREE_SHAKE_PREFIX_MODES) {\n    // Split the pathname to extract the first segment\n    const pathSegments = pathname.split('/');\n    const firstSegment = pathSegments[1]; // The segment after the first '/'\n\n    // Check if the first segment is a supported locale\n    if (locales?.includes(firstSegment as L)) {\n      // Remove the locale segment from the pathname\n      pathSegments.splice(1, 1); // Remove the first segment\n\n      // Reconstruct the pathname\n      const newPathname = pathSegments.join('/') ?? '/';\n      url.pathname = newPathname;\n    }\n  }\n\n  // Only strip locale from search parameters in search-params routing mode\n  if (!TREE_SHAKE_SEARCH_PARAMS) {\n    const searchParams = new URLSearchParams(url.search);\n    if (searchParams.has('locale')) {\n      searchParams.delete('locale');\n      url.search = searchParams.toString();\n    }\n  }\n\n  if (isAbsoluteUrl) {\n    return url.toString() as PathWithoutLocale<T, L>;\n  }\n\n  return url.toString().replace('http://example.com', '') as PathWithoutLocale<\n    T,\n    L\n  >;\n};\n"],"mappings":";;;;;;;;;;AAUA,MAAM,0BACJ,QAAQ,IAAI,4BACZ,QAAQ,IAAI,6BAA6B,gBACzC,QAAQ,IAAI,6BAA6B;;;;AAK3C,MAAM,2BACJ,QAAQ,IAAI,4BACZ,QAAQ,IAAI,6BAA6B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsC3C,MAAa,wBAIX,UACA,UAAeA,6CAAsB,YACT;CAE5B,MAAM,gBAAgBC,oDAAmB,SAAS;CAElD,IAAI,gBAAgB;AAEpB,KAAI,UAAU,SAAS,IAAI,CACzB,iBAAgB,SAAS,MAAM,GAAG,GAAG;CAKvC,MAAM,MAAM,gBACR,IAAI,IAAI,cAAc,GACtB,IAAI,IAAI,eAAe,qBAAqB;CAEhD,MAAM,WAAW,IAAI;AAGrB,KAAI,CAAC,SAAS,WAAW,IAAI,CAE3B,KAAI,WAAW,IAAI;AAIrB,KAAI,CAAC,yBAAyB;EAE5B,MAAM,eAAe,SAAS,MAAM,IAAI;EACxC,MAAM,eAAe,aAAa;AAGlC,MAAI,SAAS,SAAS,aAAkB,EAAE;AAExC,gBAAa,OAAO,GAAG,EAAE;AAIzB,OAAI,WADgB,aAAa,KAAK,IAAI,IAAI;;;AAMlD,KAAI,CAAC,0BAA0B;EAC7B,MAAM,eAAe,IAAI,gBAAgB,IAAI,OAAO;AACpD,MAAI,aAAa,IAAI,SAAS,EAAE;AAC9B,gBAAa,OAAO,SAAS;AAC7B,OAAI,SAAS,aAAa,UAAU;;;AAIxC,KAAI,cACF,QAAO,IAAI,UAAU;AAGvB,QAAO,IAAI,UAAU,CAAC,QAAQ,sBAAsB,GAAG"}