"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToolPanelWrapper = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const AdaptableContext_1 = require("../../AdaptableContext");
const AdaptableViewFactory_1 = require("../../AdaptableViewFactory");
const CustomToolPanelContent_1 = require("./CustomToolPanelContent");
const rebass_1 = require("rebass");
const ToolPanelRedux = tslib_1.__importStar(require("../../../Redux/ActionsReducers/ToolPanelRedux"));
const PanelToolPanel_1 = require("../Panels/PanelToolPanel");
const PopupRedux = tslib_1.__importStar(require("../../../Redux/ActionsReducers/PopupRedux"));
const ModuleConstants_1 = require("../../../Utilities/Constants/ModuleConstants");
const ToolPanelWrapper = (props) => {
    const { adaptableToolPanel, customToolPanel, visibilityMode: visibilityMode, onVisibilityModeChange: onVisibilityModeChange, } = props;
    const { api } = (0, AdaptableContext_1.useAdaptable)();
    const getToolPanelConfiguration = () => {
        if (adaptableToolPanel) {
            const accessLevel = api.entitlementApi.getEntitlementAccessLevelForModule(adaptableToolPanel);
            const moduleInfo = api.internalApi
                .getModuleService()
                .getModuleInfoByModule(adaptableToolPanel);
            const ModuleToolPanel = AdaptableViewFactory_1.AdaptableViewPanelFactory.get(adaptableToolPanel);
            return [
                adaptableToolPanel,
                moduleInfo.FriendlyName,
                ModuleToolPanel ? (React.createElement(ModuleToolPanel, { viewType: "ToolPanel", api: api, accessLevel: accessLevel, moduleInfo: moduleInfo })) : null,
                moduleInfo,
            ];
        }
        else {
            // custom toolPanel
            return [
                customToolPanel.name,
                customToolPanel?.title ?? customToolPanel.name,
                React.createElement(CustomToolPanelContent_1.CustomToolPanelContent, { customToolPanel: customToolPanel }),
            ];
        }
    };
    const [toolPanelName, toolPanelHeader, toolPanelContent, moduleInfo] = getToolPanelConfiguration();
    const isMinimised = visibilityMode === 'collapsed';
    const isConfigurable = 
    // only module toolPanels are configurable
    !!moduleInfo && toolPanelName !== 'OpenFin' && toolPanelName !== 'IPushPull';
    const isToolPanelModuleConfigurable = api.internalApi
        .getModuleService()
        .getModuleById(ModuleConstants_1.ToolPanelModuleId)
        .isModuleEditable();
    return (React.createElement(React.Fragment, null, toolPanelContent && (React.createElement(rebass_1.Box, { marginTop: 1, className: `ab-ToolPanel__container ab-ToolPanel__container--${toolPanelName}` },
        React.createElement(PanelToolPanel_1.PanelToolPanel, { className: `ab-ToolPanel__${toolPanelName}`, "data-name": `ab-ToolPanel__${toolPanelName}`, headerText: toolPanelHeader, disabled: !isToolPanelModuleConfigurable, onMinimiseChanged: () => {
                onVisibilityModeChange(isMinimised ? 'expanded' : 'collapsed');
            }, isMinimised: isMinimised, onConfigure: isConfigurable
                ? () => {
                    const maxWidth = api.internalApi
                        .getModuleService()
                        .getPopupMaxWidth(moduleInfo.ModuleName);
                    const popupParams = maxWidth
                        ? { maxWidth, source: 'Toolbar' }
                        : { source: 'Toolbar' };
                    api.internalApi.dispatchReduxAction(PopupRedux.PopupShowScreen(moduleInfo.ModuleName, moduleInfo.Popup, popupParams));
                }
                : null, onClose: () => api.internalApi.dispatchReduxAction(ToolPanelRedux.ToolPanelHideToolPanel(toolPanelName)) }, isMinimised ? null : toolPanelContent)))));
};
exports.ToolPanelWrapper = ToolPanelWrapper;
