import Alert, { AlertType } from "../Alerts/Alert";
import ButtonType from "../Button/ButtonTypes";
import ModelForm from "../Forms/ModelForm";
import Modal, { ModalWidth } from "../Modal/Modal";
import BaseModel from "../../../Models/DatabaseModels/DatabaseBaseModel/DatabaseBaseModel";
import React, { useRef, useState } from "react";
const ModelFormModal = (props) => {
    const [isFormLoading, setIsFormLoading] = useState(false);
    const [submitButtonText, setSubmitButtonText] = useState(props.submitButtonText || "Save");
    const formRef = props.formRef ||
        useRef(null);
    const [error, setError] = useState("");
    let modalWidth = props.modalWidth || ModalWidth.Normal;
    if (props.formProps.steps && props.formProps.steps.length > 0) {
        modalWidth = props.modalWidth || ModalWidth.Medium;
    }
    return (React.createElement(Modal, Object.assign({}, props, { submitButtonText: submitButtonText, modalWidth: modalWidth, submitButtonType: ButtonType.Submit, isLoading: isFormLoading, description: props.description, disableSubmitButton: isFormLoading, onSubmit: async () => {
            var _a;
            await ((_a = formRef.current) === null || _a === void 0 ? void 0 : _a.submitForm());
        }, error: error }),
        !error ? (React.createElement(React.Fragment, null,
            React.createElement(ModelForm, Object.assign({}, props.formProps, { name: props.name, modelAPI: props.modelAPI, modelType: props.modelType, onIsLastFormStep: (isLastFormStep) => {
                    if (isLastFormStep) {
                        setSubmitButtonText(props.submitButtonText || "Save");
                    }
                    else {
                        setSubmitButtonText("Next");
                    }
                }, modelIdToEdit: props.modelIdToEdit, hideSubmitButton: true, formRef: formRef, onLoadingChange: (isFormLoading) => {
                    setIsFormLoading(isFormLoading);
                }, initialValues: props.initialValues, onSuccess: (data) => {
                    if (props.onSuccess) {
                        props.onSuccess(BaseModel.fromJSONObject(data, props.modelType));
                    }
                }, onError: (error) => {
                    setError(error);
                }, onBeforeCreate: props.onBeforeCreate })),
            props.footer)) : (React.createElement(React.Fragment, null)),
        error ? React.createElement(Alert, { title: error, type: AlertType.DANGER }) : React.createElement(React.Fragment, null)));
};
export default ModelFormModal;
//# sourceMappingURL=ModelFormModal.js.map