import { type NavConfig, type NavEntry, config } from './config' function _extendConfig(config1: NavConfig, config2: NavConfig): NavConfig { // Helper function to merge two NavEntry objects function mergeEntries(entry1: NavEntry, entry2: NavEntry): NavEntry { if ('items' in entry1 && 'items' in entry2) { // If both entries are NavParent objects, merge their items return { label: entry1.label, items: _extendConfig(entry1.items, entry2.items), } } // Otherwise, override with the new entry return entry2 } // Create a record to merge configs by label const newConfig: Record = {} // Start with the original config for (const entry of config1) { newConfig[entry.label] = { ...entry } } // Merge entries from the second config for (const entry of config2) { if (newConfig[entry.label]) { const mergedEntry = mergeEntries(newConfig[entry.label] ?? entry, entry) newConfig[entry.label] = { ...newConfig[entry.label], ...mergedEntry } } else { newConfig[entry.label] = { ...newConfig[entry.label], ...entry } } } // Convert the map back to an array return Object.values(newConfig) } /** * Allows developers to manipulate the contents of the VerticalNavBar. * * When the entry is a NavParent: * - if a NavParent with the same label exists, their items will be merged recursively. * - otherwise, a new parent category will be added. * * When the entry is a NavItem: * - if a NavItem with the same label exists, its config is overriden. * - otherwise, a new item will be added to the parent. * @param extraNavConfig a list with new or updated entries */ export function extendConfig(extraNavConfig: NavConfig) { return _extendConfig(config, extraNavConfig) }