"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FormPopups = exports.FormDialog = 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 AdaptableFormComponent_1 = require("../../../../components/AdaptableFormComponent");
const Dialog_1 = tslib_1.__importDefault(require("../../../../components/Dialog"));
const AdaptableForm_1 = require("../../../../AdaptableState/Common/AdaptableForm");
const PopupRedux_1 = require("../../../../Redux/ActionsReducers/PopupRedux");
const AdaptableContext_1 = require("../../../AdaptableContext");
const FormDialog = ({ id, formProps, form, prepareContext, }) => {
    const dispatch = (0, react_redux_1.useDispatch)();
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const [data, setData] = React.useState(() => {
        return (0, AdaptableForm_1.getDefaultAdaptableFormData)(form);
    });
    const handleDismiss = () => {
        dispatch((0, PopupRedux_1.PopupHideForm)(id));
    };
    const context = {
        adaptableApi: adaptable.api,
        formData: data,
        ...formProps,
    };
    return (React.createElement(Dialog_1.default, { isOpen: true, showCloseButton: false, style: { minHeight: 'auto', minWidth: 400, maxHeight: '80vh' }, onDismiss: handleDismiss },
        React.createElement(rebass_1.Flex, { flexDirection: 'column', "data-name": "form-dialog", padding: 4, height: '100%' },
            React.createElement(AdaptableFormComponent_1.AdaptableFormComponent, { formDef: form, data: data, context: context, onChange: setData, api: adaptable.api, displayTitle: true, onButtonClick: async (button) => {
                    handleDismiss();
                    const preparedContext = typeof prepareContext === 'function' ? await prepareContext(context) : context;
                    button.onClick?.(button, preparedContext);
                } }))));
};
exports.FormDialog = FormDialog;
const FormPopups = () => {
    const forms = (0, react_redux_1.useSelector)((state) => state.Popup.FormPopup.FormList);
    return (React.createElement(React.Fragment, null, forms.map((form) => {
        return (React.createElement(exports.FormDialog, { key: form.Id, prepareContext: form.prepareContext, id: form.Id, formProps: form.FormProps, form: form.Form }));
    })));
};
exports.FormPopups = FormPopups;
