"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useMenuItems = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_redux_1 = require("react-redux");
const DefaultSettingsPanel_1 = require("../../../../Utilities/Defaults/DefaultSettingsPanel");
const AdaptableContext_1 = require("../../../AdaptableContext");
const PopupRedux = tslib_1.__importStar(require("../../../../Redux/ActionsReducers/PopupRedux"));
const GeneralConstants_1 = require("../../../../Utilities/Constants/GeneralConstants");
const useMenuItems = () => {
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const settingsPanelOptions = adaptable.adaptableOptions.settingsPanelOptions;
    const allMenuItems = (0, react_redux_1.useSelector)((state) => state?.Internal?.SettingsPanelModuleEntries);
    return React.useMemo(() => {
        let navigationItems = settingsPanelOptions?.navigation?.items;
        if (!navigationItems) {
            navigationItems = [...DefaultSettingsPanel_1.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' &&
            !GeneralConstants_1.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]);
};
exports.useMenuItems = useMenuItems;
