import * as React from 'react';
import { useSelector } from 'react-redux';
import { DEFAULT_SETTINGS_PANEL_NAVIGATION_ITEMS } from '../../../../Utilities/Defaults/DefaultSettingsPanel';
import { useAdaptable } from '../../../AdaptableContext';
import * as PopupRedux from '../../../../Redux/ActionsReducers/PopupRedux';
import { STANDALONE_MODULE_POPUPS } from '../../../../Utilities/Constants/GeneralConstants';
export const useMenuItems = () => {
    const adaptable = useAdaptable();
    const settingsPanelOptions = adaptable.adaptableOptions.settingsPanelOptions;
    const allMenuItems = useSelector((state) => state?.Internal?.SettingsPanelModuleEntries);
    return React.useMemo(() => {
        let navigationItems = settingsPanelOptions?.navigation?.items;
        if (!navigationItems) {
            navigationItems = [...DEFAULT_SETTINGS_PANEL_NAVIGATION_ITEMS];
            if (settingsPanelOptions.customSettingsPanels) {
                navigationItems.push(...settingsPanelOptions.customSettingsPanels.map((panel) => panel.name));
            }
        }
        const visibleItems = allMenuItems.filter((item) => item.category !== 'General' &&
            item.category !== 'CustomSettingsPanel' &&
            item.category !== 'Group' &&
            !STANDALONE_MODULE_POPUPS.includes(item.category) &&
            item.isVisible);
        let navItems = navigationItems
            .map((moduleName) => {
            if (moduleName === '-') {
                return moduleName;
            }
            const customSettingsPanel = settingsPanelOptions?.customSettingsPanels?.find?.((panel) => panel.name === moduleName);
            let customPanelMenuItem = null;
            if (customSettingsPanel) {
                // @ts-ignore
                customPanelMenuItem = {
                    category: 'CustomSettingsPanel',
                    label: customSettingsPanel.name,
                    isVisible: true,
                    reduxAction: PopupRedux.PopupShowScreen(null, customSettingsPanel.name),
                };
            }
            return customPanelMenuItem || visibleItems.find((item) => item.category === moduleName);
        })
            .filter(Boolean);
        // remove sequential '-' items
        navItems = navItems.filter((item, index) => {
            if (item === '-' && navItems[index - 1] === '-') {
                return false;
            }
            return true;
        });
        return navItems;
    }, [allMenuItems]);
};
