import { ButtonStyleType } from "../Button/Button";
import ErrorMessage from "../ErrorMessage/ErrorMessage";
import ConfirmModal from "../Modal/ConfirmModal";
import SideOver from "../SideOver/SideOver";
import RunForm from "./RunForm";
import JSONFunctions from "../../../Types/JSONFunctions";
import { ComponentInputType, NodeType, } from "../../../Types/Workflow/Component";
import React, { useState } from "react";
const RunModal = (props) => {
    const [component, setComponent] = useState(props.trigger);
    const [hasFormValidationErrors, setHasFormValidationErrors] = useState(false);
    const [showRunConfirmation, setShowRunConfirmation] = useState(false);
    const [showFormValidationErrors, setShowFormValidationErrors] = useState(false);
    return (React.createElement(SideOver, { title: "Run Workflow", description: "You can run this workflow manually. This can be helpful to test the workflow.", onClose: props.onClose, submitButtonDisabled: component.nodeType === NodeType.PlaceholderNode, submitButtonText: "Run Workflow Manually", onSubmit: () => {
            if (hasFormValidationErrors) {
                setShowFormValidationErrors(true);
            }
            else {
                setShowRunConfirmation(true);
            }
        } },
        React.createElement(React.Fragment, null,
            showRunConfirmation && (React.createElement(ConfirmModal, { title: `Run Workflow Manually`, description: `Are you sure you want to run this workflow manually?`, onClose: () => {
                    setShowRunConfirmation(false);
                }, submitButtonText: "Run", onSubmit: () => {
                    setShowRunConfirmation(false);
                    // parse things as JSON if args in JSON
                    for (const args of component.metadata.returnValues) {
                        if ((args.type === ComponentInputType.JSON ||
                            args.type === ComponentInputType.JSONArray ||
                            args.type === ComponentInputType.BaseModel ||
                            args.type === ComponentInputType.BaseModelArray ||
                            args.type === ComponentInputType.Query ||
                            args.type === ComponentInputType.Select ||
                            args.type === ComponentInputType.StringDictionary) &&
                            component.returnValues &&
                            component.returnValues[args.id] &&
                            typeof component.returnValues[args.id] === "string") {
                            component.returnValues[args.id] = JSONFunctions.parse(component.returnValues[args.id]);
                        }
                    }
                    props.onRun(component);
                    props.onClose();
                }, submitButtonType: ButtonStyleType.SUCCESS })),
            showFormValidationErrors && (React.createElement(ConfirmModal, { title: `Please fix errors`, description: `There are some validation errors on the form. Please fix them before you run the workflow.`, submitButtonText: "Close", onSubmit: () => {
                    setShowFormValidationErrors(false);
                }, submitButtonType: ButtonStyleType.NORMAL })),
            component.nodeType === NodeType.Node && (React.createElement(RunForm, { component: component, onFormChange: (component) => {
                    setComponent(Object.assign({}, component));
                }, onHasFormValidationErrors: (value) => {
                    setHasFormValidationErrors(value);
                } })),
            component.nodeType === NodeType.PlaceholderNode && (React.createElement(ErrorMessage, { message: "No trigger added. Please add a trigger in order to run this workflow" })))));
};
export default RunModal;
//# sourceMappingURL=RunModal.js.map