"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_redux_1 = require("react-redux");
const rebass_1 = require("rebass");
const StatusBarRedux_1 = require("../../Redux/ActionsReducers/StatusBarRedux");
const AdaptableContext_1 = require("../AdaptableContext");
const StatusBarPanel_1 = require("./StatusBarPanel");
const AdaptableStatusBar = (props) => {
    var _a;
    /**
     * All state is selected so the Status Bar is re-rendered for all adaptable state changes
     */
    const state = (0, react_redux_1.useSelector)((state) => state);
    const statusPanels = (0, StatusBarRedux_1.getStatusPanelsSelector)(state);
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    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) => { var _a; return (_a = state === null || state === void 0 ? void 0 : state.Grid) === null || _a === void 0 ? void 0 : _a.SettingPanelModuleMenuItems; });
    return (React.createElement(rebass_1.Flex, { className: "ab-StatusBar-Panel", "data-id": props.context.Key }, (_a = statusSubPanels === null || statusSubPanels === void 0 ? void 0 : statusSubPanels.StatusBarPanels) === null || _a === void 0 ? void 0 : _a.map((subPanel) => {
        var _a, _b, _c, _d, _e, _f;
        const module = adaptable.ModuleService.getModuleById(subPanel);
        if (!module) {
            return React.createElement(React.Fragment, { key: subPanel });
        }
        if (!module.isModuleAvailable()) {
            return React.createElement(React.Fragment, { key: subPanel });
        }
        /**
         * Both view properties and status panel are optional.
         */
        const statusBarPanelProps = (_d = (_c = (_b = (_a = module.getViewProperties) === null || _a === void 0 ? void 0 : _a.call(module)) === null || _b === void 0 ? void 0 : _b.getStatusBarPanelProps) === null || _c === void 0 ? void 0 : _c.call(_b)) !== null && _d !== void 0 ? _d : {};
        const moduleInfo = module.moduleInfo;
        const onAction = (_e = statusBarPanelProps.onAction) !== null && _e !== void 0 ? _e : (() => {
            const menuItem = allMenuItems.find((menuItem) => menuItem.module === subPanel);
            if (menuItem === null || menuItem === void 0 ? void 0 : menuItem.reduxAction) {
                dispatch(menuItem.reduxAction);
            }
        });
        return (React.createElement(StatusBarPanel_1.StatusBarPanel, Object.assign({ "data-name": subPanel }, statusBarPanelProps, { tooltip: moduleInfo.FriendlyName, onAction: onAction, icon: (_f = statusBarPanelProps.icon) !== null && _f !== void 0 ? _f : moduleInfo.Glyph, key: subPanel })));
    })));
};
exports.AdaptableStatusBar = AdaptableStatusBar;
