import { h } from '@stencil/core/internal/client';

/**
 * 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
 */
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
 */
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;
}

function getLastMenuItem(menuItems) {
    return menuItems.length > 0
        ? menuItems[menuItems.length - 1]
        : null;
}
function getModeRoots(menuItems) {
    if (menuItems.length <= 1) {
        return [];
    }
    return menuItems.slice(0, -1);
}
/**
 * Header menu mode utilities.
 *
 * Purpose
 * -------
 * Interprets the backend top-level menu structure used for header menu
 * switching.
 *
 * Expected top-level order:
 * - all items except the last = switchable root menus
 * - last item = shared/common root
 *
 * Responsibilities
 * ----------------
 * - extract a structured representation of the switchable menu roots
 * - resolve the active root for a selected mode
 * - return grouped or flat visible items for rendering
 */
function extractToggleableMenuStructure(menuItems) {
    const sharedRoot = getLastMenuItem(menuItems);
    const modeRoots = getModeRoots(menuItems);
    const menuModeOptions = modeRoots.map((root, index) => ({
        index,
        label: root.name,
        href: root.href || undefined,
        root,
    }));
    return {
        hasMenuSwitching: menuModeOptions.length > 0,
        modeRoots,
        menuModeOptions,
        sharedRoot,
        sharedItems: sharedRoot?.children ?? [],
    };
}
/**
 * Returns the active root item for the selected mode.
 *
 * Falls back to the first available mode root when the requested mode
 * is out of bounds.
 */
function getToggleRootForMode(structure, mode) {
    return structure.modeRoots[mode] ?? structure.modeRoots[0] ?? null;
}
/**
 * Returns the active root item for the selected mode directly from raw menu items.
 */
function getToggleRootForMenuItems(menuItems, mode) {
    const structure = extractToggleableMenuStructure(menuItems);
    return getToggleRootForMode(structure, mode);
}
/**
 * Returns grouped visible items for rendering.
 *
 * - primaryItems = children of the active root
 * - sharedItems = children of the shared/common root
 */
function getMenuGroupsForMode(structure, mode) {
    const activeRoot = getToggleRootForMode(structure, mode);
    if (!activeRoot) {
        return {
            primaryItems: structure.sharedItems,
            sharedItems: [],
        };
    }
    return {
        primaryItems: activeRoot?.children ?? [],
        sharedItems: structure.sharedItems,
    };
}
/**
 * Returns grouped visible items directly from raw menu items.
 */
function getVisibleMenuGroupsForMode(menuItems, mode) {
    const structure = extractToggleableMenuStructure(menuItems);
    return getMenuGroupsForMode(structure, mode);
}
/**
 * Builds a flat list of visible menu items for the selected mode.
 *
 * Useful for render paths that do not need grouped rendering.
 */
function buildMenuItemsForMode(structure, mode) {
    const groups = getMenuGroupsForMode(structure, mode);
    return [
        ...groups.primaryItems,
        ...groups.sharedItems,
    ];
}

function renderHeaderMenuSwitcher(props) {
    const structure = extractToggleableMenuStructure(props.vm.rawMenuItems);
    if (!structure.hasMenuSwitching) {
        return null;
    }
    const options = structure.menuModeOptions.map(option => ({
        ...option,
        href: props.navigateOnMenuToggle ? option.href : undefined,
    }));
    return (h("pn-marketweb-header-toggle", { value: props.activeMenuMode, options: options, onToggleChange: props.onMenuToggleChange }));
}
function renderHeaderLogo(props) {
    const homePageLink = props.homePageLink ?? '/';
    return (h("div", { class: "siteheader-logocontainer" },
        h("a", { href: homePageLink, title: props.homeLabel, "aria-label": props.homeLabel, class: "siteheader-logolink" },
            h("slot", { name: "logo" },
                h("svg", { class: "siteheader-logo", xmlns: "http://www.w3.org/2000/svg", width: "12.7rem", height: "2.4rem", viewBox: "0 0 141.73 26.65", role: "img", "aria-label": "PostNord" },
                    h("path", { d: "M108.84,13.35c0,6.34-5.17,9-10,9S89,19.76,89,13.63c0-6.28,5.08-9,10-9S108.84,7.25,108.84,13.35Zm-6.56.18a3.4,3.4,0,1,0-6.8,0,3.29,3.29,0,0,0,3.42,3.41A3.24,3.24,0,0,0,102.28,13.54Zm-83,0c0,4.83-3.32,8.82-8.49,8.82a6.54,6.54,0,0,1-4.65-1.57v5.86H0V5.16H6.17V6.8a6.43,6.43,0,0,1,5.11-2.18C16.41,4.62,19.31,8.58,19.31,13.54Zm-6.53,0A3.27,3.27,0,0,0,9.4,10,3.28,3.28,0,0,0,6,13.54,3.29,3.29,0,0,0,9.4,16.95,3.24,3.24,0,0,0,12.78,13.54Zm103.69,0.27c0-2.54,1.54-3.32,3.6-3.32a6.56,6.56,0,0,1,2.08.3L122.3,5.1a3.92,3.92,0,0,0-1.45-.18c-3.45,0-4.38,2.18-4.38,2.18V5.16H110.3V21.84h6.17v-8ZM141.73,0V21.84h-6.16V20.21a6.43,6.43,0,0,1-5.11,2.18c-5.14,0-8-4-8-8.91s3.32-8.82,8.49-8.82a6.54,6.54,0,0,1,4.65,1.57v-5Zm-6,13.48a3.28,3.28,0,0,0-3.41-3.41A3.24,3.24,0,0,0,129,13.47,3.4,3.4,0,1,0,135.75,13.47ZM81.35,4.62c-3.6,0-4.9,2.18-4.9,2.18V5.16H70.29V21.84h6.16V13.29c0-2.11.79-3.08,2.69-3.08s2.18,1.57,2.18,3.57v8.07h6.22V11.6C87.55,7,85.34,4.62,81.35,4.62ZM64.07,1.42H57.9V21.84h6.16V10.36H67l2.81-5.2H64.07V1.42ZM50.26,11.21c-2-.3-2.87-0.3-2.87-1.09s0.81-.94,2.42-0.94a16.15,16.15,0,0,1,5.11.91L56,5.41a24.56,24.56,0,0,0-6.13-.75c-5.89,0-9,2.27-9,5.89,0,2.81,1.66,4.47,6.53,5.11,2.06,0.27,2.81.39,2.81,1.15s-0.82,1-2.24,1a16.67,16.67,0,0,1-6-1.3L40.77,21.3a22.14,22.14,0,0,0,6.68,1c6.35,0,9.28-2.24,9.28-5.89C56.72,13.54,55.15,12,50.26,11.21ZM40.19,13.35c0,6.34-5.17,9-10,9s-9.82-2.6-9.82-8.73c0-6.28,5.08-9,10-9S40.19,7.25,40.19,13.35Zm-6.56.18a3.4,3.4,0,1,0-6.8,0,3.28,3.28,0,0,0,3.41,3.41A3.24,3.24,0,0,0,33.63,13.54Z", fill: "currentColor", transform: "translate(0 0)" }))))));
}
function renderHeaderLogin(props) {
    if (!props.vm.loginDialog || props.hideLogin) {
        return null;
    }
    return (h("pn-marketweb-siteheader-login", { token: props.userToken, siteUrl: props.vm.siteDefinition?.url, fullname: props.userFullname, loggedin: props.userLoggedin, loginDialog: props.vm.loginDialog, endpoint: props.endpoint ?? '', i18n: props.i18n, showProfileSelection: props.showProfileSelection, showUnifiedLogin: props.showUnifiedLogin, "check-user-logged-in-state-interval": props.checkUserLoggedInStateInterval, useMarketwebLogin: props.useMarketwebLogin, environment: props.environment, appearance: props.appearance }));
}
function renderHeaderLanguageSelector(props) {
    if (!props.vm.languageOptions.length || props.hideLanguageSelector) {
        return null;
    }
    return (h("pn-language-selector", { value: props.language, appearance: props.appearance }, props.vm.languageOptions.map(language => (h("pn-language-selector-option", { name: language.nativeName, code: language.twoLetterISOLanguageName, selected: language.isCurrent, url: getLanguageVersionUrl(language, props.vm.siteDefinition, props.spaMode) })))));
}
function renderHeaderSiteSelector(props) {
    if (!props.vm.siteSelector || props.hideSiteSelector) {
        return null;
    }
    return (h("pn-site-selector", { language: props.language, buttontext: props.buttonText, appearance: props.appearance },
        props.vm.siteSelector.currentSiteTitle && (h("pn-site-selector-item", { heading: props.vm.siteSelector.currentSiteTitle, description: props.vm.siteSelector.currentSiteDescription })),
        (props.vm.siteSelector.siteSelections ?? []).map(site => (h("pn-site-selector-item", { heading: site.linkText, description: site.linkDescription, url: props.adjustSiteSelectorUrl(site.href ?? site.pageLink ?? ''), newwindow: site.openInNewWindow })))));
}

export { getToggleRootForMenuItems as a, buildMenuItemsForMode as b, getVisibleMenuGroupsForMode as c, renderHeaderLanguageSelector as d, extractToggleableMenuStructure as e, renderHeaderSiteSelector as f, getLanguageOptions as g, renderHeaderLogo as h, renderHeaderMenuSwitcher as i, renderHeaderLogin as r };

//# sourceMappingURL=pn-marketweb-header-shared.js.map