{"version":3,"sources":["../../../src/localization/getMultilingualUrls.ts"],"sourcesContent":["import type { LocalesValues } from '@intlayer/config/client';\nimport configuration from '@intlayer/config/built';\n\n// @ts-ignore intlayer declared for module augmentation\nimport type { IConfigLocales } from 'intlayer';\nimport { checkIsURLAbsolute } from '../utils/checkIsURLAbsolute';\nimport { getPathWithoutLocale } from './getPathWithoutLocale';\n\n/**\n * Generates multilingual URLs by prefixing the given URL with each supported locale.\n * Handles both absolute and relative URLs appropriately.\n *\n * This function get the locales, default locale, and prefix default from the configuration if not provided.\n *\n * Example:\n *\n * ```ts\n *  getMultilingualUrls('/dashboard', ['en', 'fr'], 'en', false)\n *     // Returns { en: '/dashboard', fr: '/fr/dashboard' }\n *  getMultilingualUrls('https://example.com/dashboard', ['en', 'fr'], 'en', true)\n *     // Returns { en: 'https://example.com/en/dashboard', fr: 'https://example.com/fr/dashboard' }\n * ```\n *\n * @param url - The original URL string to be prefixed with locales.\n * @param locales - Optional array of supported locales. Defaults to `localesDefault`.\n * @param defaultLocale - The default locale. Defaults to `defaultLocaleDefault`.\n * @param prefixDefault - Whether to prefix the default locale. Defaults to `prefixDefaultDefault`.\n * @returns An object mapping each locale to its corresponding multilingual URL.\n */\nexport const getMultilingualUrls = (\n  url: string,\n  locales: LocalesValues[] = configuration.internationalization.locales,\n  defaultLocale: LocalesValues = configuration.internationalization\n    .defaultLocale,\n  prefixDefault: boolean = configuration.middleware.prefixDefault\n): IConfigLocales<string> => {\n  // Remove any existing locale segment from the URL\n  const urlWithoutLocale = getPathWithoutLocale(url, locales);\n\n  // Determine if the original URL is absolute (includes protocol)\n  const isAbsoluteUrl = checkIsURLAbsolute(urlWithoutLocale);\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 parsedUrl = isAbsoluteUrl\n    ? new URL(urlWithoutLocale)\n    : new URL(urlWithoutLocale, 'http://example.com');\n\n  // Extract the pathname from the parsed URL\n  let pathname = parsedUrl.pathname;\n\n  // Ensure the pathname starts with a '/'\n  if (!pathname.startsWith('/')) {\n    pathname = `/${pathname}`;\n  }\n\n  // Prepare the base URL (protocol + host) if it's absolute\n  const baseUrl = isAbsoluteUrl\n    ? `${parsedUrl.protocol}//${parsedUrl.host}`\n    : '';\n\n  // Generate multilingual URLs by iterating over each locale\n  const multilingualUrls = locales.reduce<IConfigLocales<string>>(\n    (acc, locale) => {\n      // Determine if the current locale is the default locale\n      const isDefaultLocale = locale.toString() === defaultLocale.toString();\n\n      // Decide whether to prefix the default locale based on `prefixDefault`\n      const shouldPrefix = prefixDefault || !isDefaultLocale;\n\n      // Construct the new pathname with or without the locale prefix\n      let localizedPath = shouldPrefix ? `/${locale}${pathname}` : pathname;\n\n      if (localizedPath.length > 1 && localizedPath.endsWith('/')) {\n        localizedPath = localizedPath.slice(0, -1);\n      }\n\n      // Combine with the base URL if the original URL was absolute\n      const localizedUrl = isAbsoluteUrl\n        ? `${baseUrl}${localizedPath}${parsedUrl.search}${parsedUrl.hash}`\n        : `${localizedPath}${parsedUrl.search}${parsedUrl.hash}`;\n\n      // Assign the constructed URL to the corresponding locale key\n      acc[locale as unknown as keyof typeof acc] = localizedUrl;\n\n      return acc;\n    },\n    {} as IConfigLocales<string> // Initialize an empty object\n  );\n\n  return multilingualUrls;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,mBAA0B;AAI1B,gCAAmC;AACnC,kCAAqC;AAuB9B,MAAM,sBAAsB,CACjC,KACA,UAA2B,aAAAA,QAAc,qBAAqB,SAC9D,gBAA+B,aAAAA,QAAc,qBAC1C,eACH,gBAAyB,aAAAA,QAAc,WAAW,kBACvB;AAE3B,QAAM,uBAAmB,kDAAqB,KAAK,OAAO;AAG1D,QAAM,oBAAgB,8CAAmB,gBAAgB;AAIzD,QAAM,YAAY,gBACd,IAAI,IAAI,gBAAgB,IACxB,IAAI,IAAI,kBAAkB,oBAAoB;AAGlD,MAAI,WAAW,UAAU;AAGzB,MAAI,CAAC,SAAS,WAAW,GAAG,GAAG;AAC7B,eAAW,IAAI,QAAQ;AAAA,EACzB;AAGA,QAAM,UAAU,gBACZ,GAAG,UAAU,QAAQ,KAAK,UAAU,IAAI,KACxC;AAGJ,QAAM,mBAAmB,QAAQ;AAAA,IAC/B,CAAC,KAAK,WAAW;AAEf,YAAM,kBAAkB,OAAO,SAAS,MAAM,cAAc,SAAS;AAGrE,YAAM,eAAe,iBAAiB,CAAC;AAGvC,UAAI,gBAAgB,eAAe,IAAI,MAAM,GAAG,QAAQ,KAAK;AAE7D,UAAI,cAAc,SAAS,KAAK,cAAc,SAAS,GAAG,GAAG;AAC3D,wBAAgB,cAAc,MAAM,GAAG,EAAE;AAAA,MAC3C;AAGA,YAAM,eAAe,gBACjB,GAAG,OAAO,GAAG,aAAa,GAAG,UAAU,MAAM,GAAG,UAAU,IAAI,KAC9D,GAAG,aAAa,GAAG,UAAU,MAAM,GAAG,UAAU,IAAI;AAGxD,UAAI,MAAqC,IAAI;AAE7C,aAAO;AAAA,IACT;AAAA,IACA,CAAC;AAAA;AAAA,EACH;AAEA,SAAO;AACT;","names":["configuration"]}