import * as React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import Dialog from '../../../../components/Dialog';
import { PopupHideWindow } from '../../../../Redux/ActionsReducers/PopupRedux';
import { useAdaptable } from '../../../AdaptableContext';
import { ExternalRenderer } from '../../ExternalRenderer';
import { PanelWithImage } from '../../Panels/PanelWithImage';
import { getMiddlePosition, getWindowPopupSize } from '../Utilities';
import { windowFactory } from './windowFactory';
const NoopComponent = () => {
    return React.createElement(React.Fragment, null);
};
export const CUSTOM_WINDOW_FACTORY_ID = 'CUSTOM_WINDOW_FACTORY_ID';
export const WindowPopups = () => {
    const [windowModalSettings, setWindowModalSettings] = React.useState({});
    const adaptable = useAdaptable();
    const dispatch = useDispatch();
    const windowItems = useSelector((state) => state.Popup.WindowPopup.PopupList);
    return (React.createElement(React.Fragment, null, windowItems?.map((windowItem) => {
        let Component = windowFactory[windowItem.FactoryId] ?? null;
        const { windowModalProps, ...restPopupProps } = windowItem?.PopupProps ?? {};
        const handleDismiss = () => {
            dispatch(PopupHideWindow(windowItem.Id));
        };
        const size = windowModalSettings?.[windowItem.Id]?.size ??
            windowItem?.PopupProps?.size ??
            getWindowPopupSize();
        const position = windowModalSettings?.[windowItem.Id]?.position ??
            windowItem?.PopupProps?.position ??
            getMiddlePosition(size);
        let componentNode = null;
        if (!Component &&
            (windowItem.PopupProps.render || windowItem.PopupProps.frameworkComponent)) {
            componentNode = (React.createElement(ExternalRenderer, { 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, { "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, { style: { height: '100%' }, bodyProps: { padding: 0, height: '100%' }, header: windowItem.Title, glyphicon: windowItem.Icon, variant: "primary" }, componentNode)));
    })));
};
