"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf3 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _uiLayout = require("@instructure/ui-layout");

var _quizNumberInput = _interopRequireDefault(require("@instructure/quiz-number-input"));

var _formatMessage = _interopRequireDefault(require("@instructure/quiz-i18n/lib/format-message"));

var _default_ar_number = Intl.NumberFormat("ar", { style: "decimal" }).format;

var ExtraAttemptsInput =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(ExtraAttemptsInput, _Component);

  function ExtraAttemptsInput() {
    var _getPrototypeOf2;

    var _this;

    (0, _classCallCheck2.default)(this, ExtraAttemptsInput);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = (0, _possibleConstructorReturn2.default)(this, (_getPrototypeOf2 = (0, _getPrototypeOf3.default)(ExtraAttemptsInput)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _this.getMessages = function (value, parsedValue, min, max) {
      var messages = null;

      if (parsedValue === null && value !== '') {
        var _params;

        messages = [{
          type: 'error',
          text: _formatMessage.default.custom(["userInput", "string"], "ar", value, _params) + " is not a valid number."
        }];
      } else if (parsedValue < min) {
        messages = [{
          type: 'error',
          text: _default_ar_number(parsedValue) + " \u0623\u062F\u0646\u0649 \u0645\u0646 \u0627\u0644\u062D\u062F \u0627\u0644\u0623\u062F\u0646\u0649 " + _default_ar_number(min) + "."
        }];
      } else if (parsedValue > max) {
        messages = [{
          type: 'error',
          text: _default_ar_number(parsedValue) + " \u0623\u0639\u0644\u0649 \u0645\u0646 \u0627\u0644\u062D\u062F \u0627\u0644\u0623\u0642\u0635\u0649 " + _default_ar_number(max) + "."
        }];
      }

      return messages;
    };

    _this.onChange = function () {
      var e = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
      var raw = arguments.length > 1 ? arguments[1] : void 0;
      var parsedNum = arguments.length > 2 ? arguments[2] : void 0;
      var parsed = parsedNum === null ? null : Number(parsedNum);

      _this.props.onChange(e, raw, parsed);
    };

    return _this;
  }

  (0, _createClass2.default)(ExtraAttemptsInput, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
          label = _this$props.label,
          max = _this$props.max,
          min = _this$props.min,
          value = _this$props.value,
          parsedValue = _this$props.parsedValue,
          revertValue = _this$props.revertValue,
          revert = _this$props.revert,
          onBlur = _this$props.onBlur;
      var step = 1;
      var precision = 0;
      return _react.default.createElement(_uiLayout.View, {
        display: "block",
        margin: "medium none"
      }, _react.default.createElement(_quizNumberInput.default, {
        label: label,
        step: step,
        value: revert ? revertValue : value,
        onChange: this.onChange,
        onBlur: onBlur,
        messages: this.getMessages(value, parsedValue, min, max),
        decimalPrecision: precision
      }));
    }
  }]);
  ExtraAttemptsInput.displayName = "ExtraAttemptsInput";
  return ExtraAttemptsInput;
}(_react.Component);

ExtraAttemptsInput.propTypes = {
  label: _propTypes.default.string.isRequired,
  max: _propTypes.default.number.isRequired,
  min: _propTypes.default.number,
  value: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string]).isRequired,
  parsedValue: _propTypes.default.number,
  onChange: _propTypes.default.func.isRequired,
  onBlur: _propTypes.default.func.isRequired,
  revertValue: _propTypes.default.number,
  revert: _propTypes.default.bool
};
ExtraAttemptsInput.defaultProps = {
  min: 0,
  revert: false,
  parsedValue: null,
  revertValue: null
};
var _default = ExtraAttemptsInput;
exports.default = _default;