/**
 * Removes duplicated language segments from generated URLs.
 *
 * Example:
 * /sv/sv/page -> /sv/page
 *
 * This protects against incorrectly concatenated site-domain-based URLs.
 */
export function removeDoubleLanguage(url, language) {
    if (!language) {
        return url;
    }
    const languageSegment = language.toLowerCase() + '/';
    const doubleLanguageSegment = '/' + languageSegment + languageSegment;
    if (url.includes(doubleLanguageSegment)) {
        return url.replace(doubleLanguageSegment, '/' + languageSegment);
    }
    return url;
}
/**
 * Resolves a menu/login href to an absolute site-relative URL when needed.
 *
 * What it does:
 * - Leaves fully-qualified http/https URLs unchanged
 * - Prefixes relative URLs with the current site definition domain
 * - Cleans up accidental double language segments
 */
export function adjustHref(href, siteDefinition, language) {
    if (!href) {
        return href;
    }
    if (href.indexOf('http') === 0) {
        return href;
    }
    const siteUrl = siteDefinition?.url ?? '';
    const domainUrl = siteUrl.lastIndexOf('/') === siteUrl.length - 1
        ? siteUrl.substring(0, siteUrl.length - 1)
        : siteUrl;
    return removeDoubleLanguage(domainUrl + href, language);
}
/**
 * Recursively adjusts a menu item and all child menu items so that
 * relative URLs become site-domain-aware absolute URLs.
 *
 * This returns a new object rather than mutating the original API item.
 */
export function adjustMenuItemUrls(item, siteDefinition, language) {
    return {
        ...item,
        href: adjustHref(item.href, siteDefinition, language),
        children: (item.children ?? []).map(child => adjustMenuItemUrls(child, siteDefinition, language)),
    };
}
/**
 * Adjusts login dialog links so that relative URLs are resolved against
 * the active site domain.
 *
 * This is used when siteDomainInUrls is enabled.
 */
export function adjustLoginDialogUrls(loginDialog, siteDefinition, language) {
    if (!loginDialog) {
        return null;
    }
    return {
        ...loginDialog,
        loggedInLinks: (loginDialog.loggedInLinks ?? []).map(item => ({
            ...item,
            href: adjustHref(item.href, siteDefinition, language),
        })),
        loginMenuLinks: (loginDialog.loginMenuLinks ?? []).map(item => ({
            ...item,
            href: adjustHref(item.href, siteDefinition, language),
        })),
    };
}
/**
 * Navigates to the provided href when available.
 */
export function navigateToHref(href) {
    if (!href) {
        return;
    }
    window.location.href = href;
}
/**
 * Navigates to a menu item's href when available.
 */
export function navigateToMenuItem(item) {
    navigateToHref(item?.href);
}
//# sourceMappingURL=header-url.js.map
