"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdaptablePopup = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const rebass_1 = require("rebass");
const GeneralConstants = tslib_1.__importStar(require("../../../../Utilities/Constants/GeneralConstants"));
const TopBar_1 = tslib_1.__importDefault(require("./TopBar"));
const join_1 = tslib_1.__importDefault(require("../../../../components/utils/join"));
const Navigation_1 = require("./Navigation");
const PopupContext_1 = tslib_1.__importDefault(require("./PopupContext"));
const AdaptablePopupBody_1 = require("./AdaptablePopupBody");
const AdaptableContext_1 = require("../../../AdaptableContext");
const CustomSettingsPanelView_1 = require("./CustomSettingsPanelView");
const useMenuItems_1 = require("./useMenuItems");
const AdaptablePopupDialog_1 = require("./AdaptablePopupDialog");
const AdaptablePopup = (props) => {
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const settingsPanelOptions = adaptable.adaptableOptions.settingsPanelOptions;
    const settingsPanelTitle = adaptable.ModuleService.getModuleFriendlyName('SettingsPanel');
    const menuItems = (0, useMenuItems_1.useMenuItems)();
    const isWindowModal = settingsPanelOptions.popupType === 'window';
    const modalContainer = adaptable.adaptableOptions?.containerOptions?.modalContainer;
    let friendlyName = null;
    /**
     * This means that it is not rendered in the context of Settings Panel
     * - no topbar
     * - no navigation
     */
    let isStandalone = false;
    let moduleViewContent = null;
    let activeItem = null;
    let accessLevel = 'Full';
    const firstMenuItem = menuItems.find((menuItem) => menuItem !== '-');
    const firstMenuModule = firstMenuItem?.category;
    let componentModule = props.componentModule;
    let componentName = props.componentName;
    // ts forces to check for General, there is no menu item General
    const useFirstItem = !props.componentName;
    if (useFirstItem &&
        firstMenuModule !== 'General' &&
        firstMenuModule !== 'CustomSettingsPanel' &&
        firstMenuModule !== 'Group') {
        componentModule = firstMenuModule;
    }
    const customSettingsPanel = settingsPanelOptions?.customSettingsPanels?.find((customPanel) => useFirstItem ? customPanel.name === firstMenuItem.label : customPanel.name === componentName);
    if (customSettingsPanel) {
        friendlyName = customSettingsPanel.name;
        activeItem = customSettingsPanel.name;
        moduleViewContent = React.createElement(CustomSettingsPanelView_1.CustomSettingsPanelView, { settingsPanel: customSettingsPanel });
    }
    else {
        const module = props.api.internalApi.getModuleService().getModuleById(componentModule);
        const moduleInfo = module?.moduleInfo;
        accessLevel = props.api.entitlementApi.getEntitlementAccessLevelForModule(componentModule);
        friendlyName = moduleInfo.FriendlyName;
        isStandalone =
            moduleInfo &&
                (GeneralConstants.STANDALONE_MODULE_POPUPS.includes(moduleInfo?.ModuleName) ||
                    (props.componentName && props.componentName !== moduleInfo?.Popup));
        activeItem = moduleInfo.ModuleName;
        moduleViewContent = (React.createElement(AdaptablePopupBody_1.AdaptablePopupBody, { api: props.api, module: module, componentName: props.componentName, moduleParams: props.moduleParams, onClearParams: props.onClearParams, onHide: props.onHide }));
    }
    const baseClassName = 'ab-Adaptable-Popup';
    const className = (0, join_1.default)(baseClassName, `${baseClassName}--${isWindowModal ? 'window' : 'modal'}`, isStandalone ? `${baseClassName}--action-popup` : `${baseClassName}--settings-popup`);
    return (React.createElement(PopupContext_1.default.Provider, { value: {
            hidePopup: () => {
                if (props.onHide) {
                    props.onHide();
                }
            },
        } },
        React.createElement(AdaptablePopupDialog_1.AdaptablePopupDialog, { dataName: componentModule, baseClassName: baseClassName, className: className, friendlyName: friendlyName, isActionModule: isStandalone, isWindowModal: isWindowModal, onHide: props.onHide, modalContainer: modalContainer },
            !isStandalone && React.createElement(TopBar_1.default, { icon: settingsPanelOptions.icon }, settingsPanelTitle),
            React.createElement(rebass_1.Flex, { flexDirection: "row", flex: "1 1 0", style: { minHeight: 0 }, className: accessLevel == 'ReadOnly' ? GeneralConstants.READ_ONLY_STYLE : '' },
                !isStandalone && (React.createElement(Navigation_1.Navigation, { menuItems: menuItems, api: props.api, activeItem: activeItem, customSettingsPanels: settingsPanelOptions.customSettingsPanels })),
                React.createElement(rebass_1.Flex, { flex: 1, className: "ab-Adaptable-Popup__Body" }, moduleViewContent)))));
};
exports.AdaptablePopup = AdaptablePopup;
