import { getLanguageOptions } from "../utils/header-language";
import { prependHomeMenuItem } from "../utils/header-menu";
import { buildMenuItemsForMode, extractToggleableMenuStructure, } from "../utils/header-toggle-menu";
import { adjustLoginDialogUrls, adjustMenuItemUrls, } from "../utils/header-url";
/**
 * Maps backend header data into the internal renderable view model.
 *
 * This mapper is the normalization boundary between the backend header payload
 * and the UI-facing header view model.
 *
 * Top-level menu interpretation:
 * - all items except the last = switchable root menus
 * - last item = shared/common root
 *
 * These semantics are inferred by position, not by stable backend identifiers.
 *
 * Responsibilities
 * ----------------
 * - normalize top-level backend payload sections
 * - optionally adjust relative URLs against the site domain
 * - detect whether the backend menu supports menu switching
 * - preserve the raw normalized menu structure for the wrapper component
 * - prepare a flat `menuItems` list for simpler rendering paths
 *
 * Important
 * ---------
 * - The switchable root items themselves are not intended to be rendered as
 *   visible main navigation entries
 * - Visible navigation is derived from:
 *   - active root children
 *   - plus shared/common root children
 * - If only a shared/common root exists, its children are rendered flat
 * - The wrapper component may choose either:
 *   - `menuItems` for flat rendering
 *   - or `rawMenuItems` + menu helpers for grouped rendering
 */
export function mapHeaderDataToViewModel(data, options) {
    if (!data || typeof data !== 'object') {
        return {
            siteDefinition: null,
            menuItems: [],
            search: null,
            siteSelector: null,
            languageSelector: null,
            languageOptions: [],
            loginDialog: null,
            homePageLink: '/',
            hasMenuSwitching: false,
            menuModeOptions: [],
            defaultMenuMode: 0,
            rawMenuItems: [],
        };
    }
    const siteDefinition = data.sitedefinition ?? null;
    /**
     * Keep a normalized copy of the backend menu structure.
     *
     * This raw structure is preserved because the wrapper component may need the
     * original top-level roots in order to:
     * - switch active menu mode
     * - render grouped navigation
     * - resolve mode navigation targets
     *
     * Top-level semantics are positional:
     * - all items except the last = switchable root menus
     * - last item = shared/common root
     */
    const rawMenuItems = (data.mainMenu?.menuItems ?? []).map(item => options.siteDomainInUrls
        ? adjustMenuItemUrls(item, siteDefinition, options.language)
        : { ...item });
    const toggleStructure = extractToggleableMenuStructure(rawMenuItems);
    /**
     * `menuItems` is kept as a flat convenience list for render paths that do
     * not need to reason about root menus explicitly.
     *
     * This flat list contains:
     * - active-mode primary items when switchable roots exist
     * - plus shared/common items
     *
     * If only a shared/common root exists, its children are rendered flat.
     */
    let menuItems = buildMenuItemsForMode(toggleStructure, 0);
    menuItems = prependHomeMenuItem(menuItems, options.i18n, siteDefinition?.url ?? '/', options.hideHomeMenuItem);
    const search = {
        ...(data.search ?? {}),
        AutocompleteEndpoint: options.autocompleteEndpoint ?? '',
    };
    const siteSelector = data.siteSelectorViewModel ?? null;
    const languageSelector = data.languageSelectorViewModel ?? null;
    const languageOptions = getLanguageOptions(languageSelector);
    const loginDialog = options.siteDomainInUrls
        ? adjustLoginDialogUrls(data.loginViewModel ?? null, siteDefinition, options.language)
        : data.loginViewModel ?? null;
    return {
        siteDefinition,
        menuItems,
        search,
        siteSelector,
        languageSelector,
        languageOptions,
        loginDialog,
        homePageLink: siteDefinition?.url ?? document.location.hostname,
        hasMenuSwitching: toggleStructure.hasMenuSwitching,
        menuModeOptions: toggleStructure.menuModeOptions,
        defaultMenuMode: 0,
        rawMenuItems,
    };
}
//# sourceMappingURL=pn-marketweb-header.mapper.js.map
