const require_index = require('../config/dist/esm/defaultValues/index.cjs');
const require_built = require('../config/dist/esm/built.cjs');
const require_checkIsURLAbsolute = require('../utils/checkIsURLAbsolute.cjs');
const require_getPathWithoutLocale = require('./getPathWithoutLocale.cjs');

//#region ../packages/@intlayer/core/dist/esm/localization/getMultilingualUrls.mjs
/**
* Generates multilingual URLs by prefixing the given URL with each supported locale
* or adding search parameters based on the routing mode.
* Handles both absolute and relative URLs appropriately.
*
* This function gets the locales, default locale, and routing mode from the configuration if not provided.
*
* Example:
*
* ```ts
*  // prefix-no-default mode
*  getMultilingualUrls('/dashboard', ['en', 'fr'], 'en', 'prefix-no-default')
*     // Returns { en: '/dashboard', fr: '/fr/dashboard' }
*
*  // prefix-all mode
*  getMultilingualUrls('/dashboard', ['en', 'fr'], 'en', 'prefix-all')
*     // Returns { en: '/en/dashboard', fr: '/fr/dashboard' }
*
*  // search-params mode
*  getMultilingualUrls('/dashboard', ['en', 'fr'], 'en', 'search-params')
*     // Returns { en: '/dashboard?locale=en', fr: '/dashboard?locale=fr' }
*
*  // no-prefix mode
*  getMultilingualUrls('/dashboard', ['en', 'fr'], 'en', 'no-prefix')
*     // Returns { en: '/dashboard', fr: '/dashboard' }
* ```
*
* @param url - The original URL string to be processed.
* @param locales - Optional array of supported locales. Defaults to configured locales.
* @param defaultLocale - The default locale. Defaults to configured default locale.
* @param mode - URL routing mode for locale handling. Defaults to configured mode.
* @returns An object mapping each locale to its corresponding multilingual URL.
*/
const getMultilingualUrls = (url, locales = require_built.built_default?.internationalization?.locales, defaultLocale = require_built.built_default?.internationalization?.defaultLocale, mode = require_built.built_default?.routing?.mode) => {
	const urlWithoutLocale = require_getPathWithoutLocale.getPathWithoutLocale(url, locales);
	const isAbsoluteUrl = require_checkIsURLAbsolute.checkIsURLAbsolute(urlWithoutLocale);
	const parsedUrl = isAbsoluteUrl ? new URL(urlWithoutLocale) : new URL(urlWithoutLocale, "http://example.com");
	let pathname = parsedUrl.pathname;
	if (!pathname.startsWith("/")) pathname = `/${pathname}`;
	const baseUrl = isAbsoluteUrl ? `${parsedUrl.protocol}//${parsedUrl.host}` : "";
	const routingMode = mode ?? require_index.defaultValues_exports.Routing.ROUTING_MODE;
	return (locales ?? []).reduce((acc, locale) => {
		const isDefaultLocale = locale?.toString() === defaultLocale?.toString();
		let localizedUrl;
		if (routingMode === "search-params") {
			const searchParams = new URLSearchParams(parsedUrl.search);
			searchParams.set("locale", locale.toString());
			const queryString = searchParams.toString();
			const pathWithQuery = queryString ? `${pathname}?${queryString}` : pathname;
			localizedUrl = isAbsoluteUrl ? `${baseUrl}${pathWithQuery}${parsedUrl.hash}` : `${pathWithQuery}${parsedUrl.hash}`;
		} else if (routingMode === "no-prefix") localizedUrl = isAbsoluteUrl ? `${baseUrl}${pathname}${parsedUrl.search}${parsedUrl.hash}` : `${pathname}${parsedUrl.search}${parsedUrl.hash}`;
		else {
			let localizedPath = routingMode === "prefix-all" || routingMode === "prefix-no-default" && !isDefaultLocale ? `/${locale}${pathname}` : pathname;
			if (localizedPath.length > 1 && localizedPath.endsWith("/")) localizedPath = localizedPath.slice(0, -1);
			localizedUrl = isAbsoluteUrl ? `${baseUrl}${localizedPath}${parsedUrl.search}${parsedUrl.hash}` : `${localizedPath}${parsedUrl.search}${parsedUrl.hash}`;
		}
		acc[locale] = localizedUrl;
		return acc;
	}, {});
};

//#endregion
exports.getMultilingualUrls = getMultilingualUrls;
//# sourceMappingURL=getMultilingualUrls.cjs.map