import React from 'react';
import { di } from 'react-magnetic-di';
import Button from '@atlaskit/button/new';
import Form, { ErrorMessage, FormSection } from '@atlaskit/form';
import Modal, { ModalBody, ModalFooter, ModalHeader, ModalTitle } from '@atlaskit/modal-dialog';
import messages from '../common/messages';
import { UserInputType } from '../common/types';
import BooleanInputPrompt from './boolean-prompt/main';
import NumberInputPrompt from './number-prompt/main';
import ParagraphInputPrompt from './paragraph-prompt/main';
import SelectInputPrompt from './select-prompt/main';
import TextInputPrompt from './text-prompt/main';
const UserInputForm = props => {
  const {
    selectedRule,
    clearSelectedRule,
    invokeRule,
    onInputsModalOpened,
    onInputsModalClosed
  } = props;
  const {
    rule,
    objects
  } = selectedRule;
  const {
    userInputPrompts
  } = rule;
  const getTypeOfInput = (key, prompts) => {
    var _prompts$find;
    return (_prompts$find = prompts.find(({
      variableName
    }) => variableName === key)) === null || _prompts$find === void 0 ? void 0 : _prompts$find.inputType;
  };
  const generateInputValue = (inputType, data) => {
    switch (inputType) {
      case UserInputType.DROPDOWN:
        return {
          inputType,
          value: data ? data.value : ''
        };
      case UserInputType.NUMBER:
        return {
          inputType,
          value: Number(data)
        };
      default:
        return {
          inputType,
          value: data
        };
    }
  };
  const handleSubmit = data => {
    const output = Object.entries(data).reduce((acc, [key, value]) => {
      const inputType = getTypeOfInput(key, userInputPrompts);
      if (!inputType) {
        return acc;
      }
      acc[key] = generateInputValue(inputType, value);
      return acc;
    }, {});
    return invokeRule(rule.id, objects, output);
  };
  const handleOpen = () => {
    if (onInputsModalOpened) {
      onInputsModalOpened();
    }
  };
  const handleClose = () => {
    clearSelectedRule();
    if (onInputsModalClosed) {
      onInputsModalClosed();
    }
  };
  const generateUserInputForm = inputPrompt => {
    switch (inputPrompt.inputType) {
      case UserInputType.TEXT:
        return /*#__PURE__*/React.createElement(TextInputPrompt, {
          key: inputPrompt.variableName,
          userInputPrompt: inputPrompt
        });
      case UserInputType.NUMBER:
        return /*#__PURE__*/React.createElement(NumberInputPrompt, {
          key: inputPrompt.variableName,
          userInputPrompt: inputPrompt
        });
      case UserInputType.DROPDOWN:
        return /*#__PURE__*/React.createElement(SelectInputPrompt, {
          key: inputPrompt.variableName,
          userInputPrompt: inputPrompt
        });
      case UserInputType.BOOLEAN:
        return /*#__PURE__*/React.createElement(BooleanInputPrompt, {
          key: inputPrompt.variableName,
          userInputPrompt: inputPrompt
        });
      case UserInputType.PARAGRAPH:
        return /*#__PURE__*/React.createElement(ParagraphInputPrompt, {
          key: inputPrompt.variableName,
          userInputPrompt: inputPrompt
        });
      default:
        return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("br", null), /*#__PURE__*/React.createElement(ErrorMessage, null, messages.errorInputTypeIsNotSupported.defaultMessage), /*#__PURE__*/React.createElement("br", null));
    }
  };
  return /*#__PURE__*/React.createElement(Modal, {
    "data-testid": "automation-platform.ui.manual-rules-container.manual-triggers-form.modal",
    onClose: handleClose,
    onOpenComplete: handleOpen
  }, /*#__PURE__*/React.createElement(Form, {
    onSubmit: handleSubmit
  }, ({
    formProps,
    submitting
  }) => /*#__PURE__*/React.createElement("form", formProps, /*#__PURE__*/React.createElement(ModalHeader, {
    hasCloseButton: true
  }, /*#__PURE__*/React.createElement(ModalTitle, null, rule.name)), /*#__PURE__*/React.createElement(ModalBody, null, messages.inputRequiredForAction.defaultMessage, /*#__PURE__*/React.createElement(FormSection, null, userInputPrompts.map(generateUserInputForm))), /*#__PURE__*/React.createElement(ModalFooter, null, /*#__PURE__*/React.createElement(Button, {
    appearance: "subtle",
    onClick: handleClose,
    isDisabled: submitting
  }, messages.cancelButtonTitle.defaultMessage), /*#__PURE__*/React.createElement(Button, {
    appearance: "primary",
    type: "submit",
    isLoading: submitting,
    autoFocus: true
  }, messages.continueButtonTitle.defaultMessage)))));
};
export default UserInputForm;