import React from 'react';
import { di } from 'react-magnetic-di';
import { ErrorMessage, Field } from '@atlaskit/form';
import Textfield from '@atlaskit/textfield';
import messages from '../../common/messages';
export var Errors = /*#__PURE__*/function (Errors) {
  Errors["EMPTY"] = "EMPTY";
  Errors["INVALID_NUMBER"] = "INVALID_NUMBER";
  return Errors;
}({});
export var numberValidate = function numberValidate(isRequired, value) {
  if (isRequired && !value) {
    return Errors.EMPTY;
  }
  if (value && Number.isNaN(Number(value))) {
    return Errors.INVALID_NUMBER;
  }
  return undefined;
};
var NumberInputPrompt = function NumberInputPrompt(_ref) {
  var userInputPrompt = _ref.userInputPrompt;
  var variableName = userInputPrompt.variableName,
    required = userInputPrompt.required,
    displayName = userInputPrompt.displayName,
    defaultValue = userInputPrompt.defaultValue;
  var generateErrorMessage = function generateErrorMessage(error) {
    switch (error) {
      case Errors.EMPTY:
        return /*#__PURE__*/React.createElement(ErrorMessage, null, messages.errorInputMustNotBeEmpty.defaultMessage);
      case Errors.INVALID_NUMBER:
        return /*#__PURE__*/React.createElement(ErrorMessage, null, messages.errorValueIsNotValidNumber.defaultMessage);
      default:
        return null;
    }
  };
  return /*#__PURE__*/React.createElement(Field, {
    key: variableName,
    name: variableName,
    label: displayName,
    defaultValue: defaultValue,
    isRequired: required,
    validate: function validate(value) {
      return numberValidate(required, value);
    }
  }, function (_ref2) {
    var fieldProps = _ref2.fieldProps,
      error = _ref2.error;
    return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Textfield, fieldProps), generateErrorMessage(error));
  });
};
export default NumberInputPrompt;