"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.numberValidate = exports.default = exports.Errors = void 0;
var _react = _interopRequireDefault(require("react"));
var _reactMagneticDi = require("react-magnetic-di");
var _form = require("@atlaskit/form");
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _textfield = _interopRequireDefault(require("@atlaskit/textfield"));
var _messages = _interopRequireDefault(require("../../common/messages"));
var Errors = exports.Errors = /*#__PURE__*/function (Errors) {
  Errors["EMPTY"] = "EMPTY";
  Errors["INVALID_NUMBER"] = "INVALID_NUMBER";
  return Errors;
}({});
var numberValidate = exports.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.default.createElement(_form.ErrorMessage, null, _messages.default.errorInputMustNotBeEmpty.defaultMessage);
      case Errors.INVALID_NUMBER:
        return /*#__PURE__*/_react.default.createElement(_form.ErrorMessage, null, _messages.default.errorValueIsNotValidNumber.defaultMessage);
      default:
        return null;
    }
  };
  return /*#__PURE__*/_react.default.createElement(_form.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.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_textfield.default, fieldProps), (0, _platformFeatureFlags.fg)('platform_navx_3298_message_wrapper') ? /*#__PURE__*/_react.default.createElement(_form.MessageWrapper, null, generateErrorMessage(error)) : generateErrorMessage(error));
  });
};
var _default = exports.default = NumberInputPrompt;