"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireDefault(require("react"));
var _reactMagneticDi = require("react-magnetic-di");
var _new = _interopRequireDefault(require("@atlaskit/button/new"));
var _form = _interopRequireWildcard(require("@atlaskit/form"));
var _modalDialog = _interopRequireWildcard(require("@atlaskit/modal-dialog"));
var _messages = _interopRequireDefault(require("../common/messages"));
var _types = require("../common/types");
var _main = _interopRequireDefault(require("./boolean-prompt/main"));
var _main2 = _interopRequireDefault(require("./number-prompt/main"));
var _main3 = _interopRequireDefault(require("./paragraph-prompt/main"));
var _main4 = _interopRequireDefault(require("./select-prompt/main"));
var _main5 = _interopRequireDefault(require("./text-prompt/main"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
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 _types.UserInputType.DROPDOWN:
        return {
          inputType: inputType,
          value: data ? data.value : ''
        };
      case _types.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 = (0, _slicedToArray2.default)(_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 _types.UserInputType.TEXT:
        return /*#__PURE__*/_react.default.createElement(_main5.default, {
          key: inputPrompt.variableName,
          userInputPrompt: inputPrompt
        });
      case _types.UserInputType.NUMBER:
        return /*#__PURE__*/_react.default.createElement(_main2.default, {
          key: inputPrompt.variableName,
          userInputPrompt: inputPrompt
        });
      case _types.UserInputType.DROPDOWN:
        return /*#__PURE__*/_react.default.createElement(_main4.default, {
          key: inputPrompt.variableName,
          userInputPrompt: inputPrompt
        });
      case _types.UserInputType.BOOLEAN:
        return /*#__PURE__*/_react.default.createElement(_main.default, {
          key: inputPrompt.variableName,
          userInputPrompt: inputPrompt
        });
      case _types.UserInputType.PARAGRAPH:
        return /*#__PURE__*/_react.default.createElement(_main3.default, {
          key: inputPrompt.variableName,
          userInputPrompt: inputPrompt
        });
      default:
        return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("br", null), /*#__PURE__*/_react.default.createElement(_form.ErrorMessage, null, _messages.default.errorInputTypeIsNotSupported.defaultMessage), /*#__PURE__*/_react.default.createElement("br", null));
    }
  };
  return /*#__PURE__*/_react.default.createElement(_modalDialog.default, {
    "data-testid": "automation-platform.ui.manual-rules-container.manual-triggers-form.modal",
    onClose: handleClose,
    onOpenComplete: handleOpen
  }, /*#__PURE__*/_react.default.createElement(_form.default, {
    onSubmit: handleSubmit
  }, function (_ref4) {
    var formProps = _ref4.formProps,
      submitting = _ref4.submitting;
    return /*#__PURE__*/_react.default.createElement("form", formProps, /*#__PURE__*/_react.default.createElement(_modalDialog.ModalHeader, {
      hasCloseButton: true
    }, /*#__PURE__*/_react.default.createElement(_modalDialog.ModalTitle, null, rule.name)), /*#__PURE__*/_react.default.createElement(_modalDialog.ModalBody, null, _messages.default.inputRequiredForAction.defaultMessage, /*#__PURE__*/_react.default.createElement(_form.FormSection, null, userInputPrompts.map(generateUserInputForm))), /*#__PURE__*/_react.default.createElement(_modalDialog.ModalFooter, null, /*#__PURE__*/_react.default.createElement(_new.default, {
      appearance: "subtle",
      onClick: handleClose,
      isDisabled: submitting
    }, _messages.default.cancelButtonTitle.defaultMessage), /*#__PURE__*/_react.default.createElement(_new.default, {
      appearance: "primary",
      type: "submit",
      isLoading: submitting,
      autoFocus: true
    }, _messages.default.continueButtonTitle.defaultMessage)));
  }));
};
var _default = exports.default = UserInputForm;