/**
 * Builds the list of language options that should actually be shown.
 *
 * What it does:
 * - Reads the backend-provided language selector model
 * - Checks page <link rel="alternate" hreflang="..."> tags
 * - If hreflang tags exist, only languages with matching tags are shown
 * - If no hreflang tags exist, all backend-provided languages are shown
 */
export function getLanguageOptions(languageSelector) {
    if (!languageSelector?.languages?.length) {
        return [];
    }
    const hrefLangTags = Array.from(document.querySelectorAll('link[rel="alternate"][hreflang]'));
    const hrefLangs = hrefLangTags.map(tag => {
        let value = tag.getAttribute('hreflang') ?? '';
        if (value.indexOf('en-') !== -1 || value.indexOf('en_') !== -1) {
            value = value.substring(0, 2);
        }
        return value;
    });
    if (!hrefLangs.length) {
        return [...languageSelector.languages];
    }
    return languageSelector.languages.filter(option => hrefLangs.includes(option.twoLetterISOLanguageName));
}
/**
 * Resolves the URL for a given language option.
 *
 * What it does:
 * - Returns null in SPA mode so language switching can be handled by events only
 * - Uses an existing hreflang alternate URL if one exists in the document
 * - Otherwise falls back to generating a language URL from the site definition
 */
export function getLanguageVersionUrl(item, siteDefinition, spaMode) {
    if (spaMode) {
        return null;
    }
    const alternativeTag = document.querySelector(`link[rel="alternate"][hreflang="${item.twoLetterISOLanguageName}"]`);
    if (alternativeTag) {
        return alternativeTag.getAttribute('href');
    }
    const siteUrl = siteDefinition?.url ?? '/';
    return siteUrl + (siteUrl.lastIndexOf('/') !== siteUrl.length - 1 ? '/' : '') + item.twoLetterISOLanguageName;
}
//# sourceMappingURL=header-language.js.map
