import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
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';
var UserInputForm = function UserInputForm(props) {
  var selectedRule = props.selectedRule,
    clearSelectedRule = props.clearSelectedRule,
    invokeRule = props.invokeRule,
    onInputsModalOpened = props.onInputsModalOpened,
    onInputsModalClosed = props.onInputsModalClosed;
  var rule = selectedRule.rule,
    objects = selectedRule.objects;
  var userInputPrompts = rule.userInputPrompts;
  var getTypeOfInput = function getTypeOfInput(key, prompts) {
    var _prompts$find;
    return (_prompts$find = prompts.find(function (_ref) {
      var variableName = _ref.variableName;
      return variableName === key;
    })) === null || _prompts$find === void 0 ? void 0 : _prompts$find.inputType;
  };
  var generateInputValue = function generateInputValue(inputType, data) {
    switch (inputType) {
      case UserInputType.DROPDOWN:
        return {
          inputType: inputType,
          value: data ? data.value : ''
        };
      case UserInputType.NUMBER:
        return {
          inputType: inputType,
          value: Number(data)
        };
      default:
        return {
          inputType: inputType,
          value: data
        };
    }
  };
  var handleSubmit = function handleSubmit(data) {
    var output = Object.entries(data).reduce(function (acc, _ref2) {
      var _ref3 = _slicedToArray(_ref2, 2),
        key = _ref3[0],
        value = _ref3[1];
      var inputType = getTypeOfInput(key, userInputPrompts);
      if (!inputType) {
        return acc;
      }
      acc[key] = generateInputValue(inputType, value);
      return acc;
    }, {});
    return invokeRule(rule.id, objects, output);
  };
  var handleOpen = function handleOpen() {
    if (onInputsModalOpened) {
      onInputsModalOpened();
    }
  };
  var handleClose = function handleClose() {
    clearSelectedRule();
    if (onInputsModalClosed) {
      onInputsModalClosed();
    }
  };
  var generateUserInputForm = function 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
  }, function (_ref4) {
    var formProps = _ref4.formProps,
      submitting = _ref4.submitting;
    return /*#__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;