"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) => { var _a; return (_a = state === null || state === void 0 ? void 0 : state.Grid) === null || _a === void 0 ? void 0 : _a.SettingPanelModuleMenuItems; });
    return React.useMemo(() => {
        var _a;
        let navigationItems = (_a = settingsPanelOptions === null || settingsPanelOptions === void 0 ? void 0 : settingsPanelOptions.navigation) === null || _a === void 0 ? void 0 : _a.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.module !== 'General' &&
            item.module !== 'CustomSettingsPanel' &&
            item.module !== 'Group' &&
            !GeneralConstants_1.STANDALONE_MODULE_POPUPS.includes(item.module) &&
            item.isVisible);
        let navItems = navigationItems
            .map((moduleName) => {
            var _a, _b;
            if (moduleName === '-') {
                return moduleName;
            }
            const customSettingsPanel = (_b = (_a = settingsPanelOptions === null || settingsPanelOptions === void 0 ? void 0 : settingsPanelOptions.customSettingsPanels) === null || _a === void 0 ? void 0 : _a.find) === null || _b === void 0 ? void 0 : _b.call(_a, (panel) => panel.name === moduleName);
            let customPanelMenuItem = null;
            if (customSettingsPanel) {
                // @ts-ignore
                customPanelMenuItem = {
                    module: 'CustomSettingsPanel',
                    label: customSettingsPanel.name,
                    isVisible: true,
                    reduxAction: PopupRedux.PopupShowScreen(null, customSettingsPanel.name),
                };
            }
            return customPanelMenuItem || visibleItems.find((item) => item.module === 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;
