"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WindowPopups = exports.CUSTOM_WINDOW_FACTORY_ID = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_redux_1 = require("react-redux");
const Dialog_1 = tslib_1.__importDefault(require("../../../../components/Dialog"));
const PopupRedux_1 = require("../../../../Redux/ActionsReducers/PopupRedux");
const AdaptableContext_1 = require("../../../AdaptableContext");
const ExternalRenderer_1 = require("../../ExternalRenderer");
const PanelWithImage_1 = require("../../Panels/PanelWithImage");
const Utilities_1 = require("../Utilities");
const windowFactory_1 = require("./windowFactory");
const NoopComponent = () => {
    return React.createElement(React.Fragment, null);
};
exports.CUSTOM_WINDOW_FACTORY_ID = 'CUSTOM_WINDOW_FACTORY_ID';
const WindowPopups = () => {
    const [windowModalSettings, setWindowModalSettings] = React.useState({});
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const dispatch = (0, react_redux_1.useDispatch)();
    const windowItems = (0, react_redux_1.useSelector)((state) => state.Popup.WindowPopup.PopupList);
    return (React.createElement(React.Fragment, null, windowItems?.map((windowItem) => {
        let Component = windowFactory_1.windowFactory[windowItem.FactoryId] ?? null;
        const { windowModalProps, ...restPopupProps } = windowItem?.PopupProps ?? {};
        const handleDismiss = () => {
            dispatch((0, PopupRedux_1.PopupHideWindow)(windowItem.Id));
        };
        const size = windowModalSettings?.[windowItem.Id]?.size ??
            windowItem?.PopupProps?.size ??
            (0, Utilities_1.getWindowPopupSize)();
        const position = windowModalSettings?.[windowItem.Id]?.position ??
            windowItem?.PopupProps?.position ??
            (0, Utilities_1.getMiddlePosition)(size);
        let componentNode = null;
        if (!Component &&
            (windowItem.PopupProps.render || windowItem.PopupProps.frameworkComponent)) {
            componentNode = (React.createElement(ExternalRenderer_1.ExternalRenderer
            // TODO AFL: add configurable width&height for custom popups
            , { 
                // TODO AFL: add configurable width&height for custom popups
                style: { height: '100%' }, render: windowItem.PopupProps.render, frameworkComponent: windowItem.PopupProps.frameworkComponent, onDestroy: windowItem.PopupProps.onFrameworkComponentDestroyed }));
        }
        else {
            Component = Component ?? NoopComponent;
            componentNode = (React.createElement(Component, { api: adaptable.api, onDismiss: handleDismiss, popupProps: restPopupProps }));
        }
        return (React.createElement(Dialog_1.default, { "data-name": windowItem.Id, style: { height: '100%' }, className: "ab-Window-Modal", key: windowItem.Id, windowModal: true, windowModalProps: {
                ...windowModalProps,
                onChange: (settings) => {
                    setWindowModalSettings((settingsMap) => ({
                        ...settingsMap,
                        [windowItem.Id]: settings,
                    }));
                },
                handleSelector: '.ab-Window-Modal .ab-Panel__header',
                size: size,
                position: position,
            }, fixed: false, padding: 0, onDismiss: handleDismiss, isOpen: true, showCloseButton: true },
            React.createElement(PanelWithImage_1.PanelWithImage, { style: { height: '100%' }, bodyProps: { padding: 0, height: '100%' }, header: windowItem.Title, glyphicon: windowItem.Icon, variant: "primary" }, componentNode)));
    })));
};
exports.WindowPopups = WindowPopups;
