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
 */
export 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.
 */
export 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.
 */
export 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
 */
export 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.
 */
export 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.
 */
export function buildMenuItemsForMode(structure, mode) {
    const groups = getMenuGroupsForMode(structure, mode);
    return [
        ...groups.primaryItems,
        ...groups.sharedItems,
    ];
}
//# sourceMappingURL=header-toggle-menu.js.map
