"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdaptableStatusBar = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_1 = require("react");
const react_redux_1 = require("react-redux");
const StatusBarRedux_1 = require("../../Redux/ActionsReducers/StatusBarRedux");
const AdaptableContext_1 = require("../AdaptableContext");
const StatusBarPanel_1 = require("./StatusBarPanel");
const useRerender_1 = require("../../components/utils/useRerender");
const Flex_1 = require("../../components/Flex");
const AdaptableStatusBar = (props) => {
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const rerender = (0, useRerender_1.useRerender)();
    (0, react_1.useEffect)(() => {
        return adaptable.api.eventApi.on('AdaptableStateChanged', rerender);
    }, [adaptable]);
    const statusPanels = (0, react_redux_1.useSelector)(StatusBarRedux_1.getStatusPanelsSelector);
    const dispatch = (0, react_redux_1.useDispatch)();
    const statusSubPanels = statusPanels.find((statusPanel) => statusPanel.Key === props.context.Key);
    const allMenuItems = (0, react_redux_1.useSelector)((state) => state?.Internal?.SettingsPanelModuleEntries);
    return (React.createElement(Flex_1.Flex, { className: "ab-StatusBar-Panel", "data-id": props.context.Key }, statusSubPanels?.StatusBarPanels?.map((subPanel) => {
        const module = adaptable.ModuleService.getModuleById(subPanel);
        if (!module?.isModuleVisible()) {
            return React.createElement(React.Fragment, { key: subPanel });
        }
        /**
         * Both view properties and status panel are optional.
         */
        const statusBarPanelProps = module.getViewProperties?.()?.getStatusBarPanelProps?.() ?? {};
        const moduleInfo = module.moduleInfo;
        const onAction = statusBarPanelProps.onAction ??
            (() => {
                // #by-default-open-settings-panel-for-module
                const menuItem = allMenuItems.find((menuItem) => menuItem.category === subPanel);
                if (menuItem?.reduxAction) {
                    dispatch(menuItem.reduxAction);
                }
            });
        return (React.createElement(StatusBarPanel_1.StatusBarPanel, { "data-name": subPanel, ...statusBarPanelProps, tooltip: moduleInfo.FriendlyName, onAction: onAction, icon: statusBarPanelProps.icon ?? moduleInfo.Glyph, key: subPanel }));
    })));
};
exports.AdaptableStatusBar = AdaptableStatusBar;
