"use strict";

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

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

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 _formatMessage = _interopRequireDefault(require("@instructure/quiz-i18n/lib/format-message"));

var _ExtraAttemptsInput = _interopRequireDefault(require("./ExtraAttemptsInput"));

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

  function ExtraAttemptSettings() {
    var _getPrototypeOf2;

    var _this;

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

    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)(ExtraAttemptSettings)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _this.clamp = function (value, min, max) {
      if (value > max) {
        return max;
      }

      if (value < min) {
        return min;
      }

      return value;
    };

    _this.state = {
      rawValue: _this.props.extraAttempts,
      // what the user has typed in
      parsedRawValue: _this.props.extraAttempts,
      // rawValue parsed to a number, or null if invalid
      normalizedValue: _this.clamp(_this.props.extraAttempts, 0, _this.props.maxExtraAttempts),
      // the most recent valid, in-range number
      revert: false // display normalizedValue instead of rawValue when true

    };

    _this.shouldNormalizeOnChange = function (e) {
      // Input events from arrow keys or arrow buttons should be normalized immediately
      var fromButtonInput = e.nativeEvent.target && e.nativeEvent.target.tagName !== 'INPUT';
      var fromArrowKeys = e.key === 'ArrowUp' || e.key === 'ArrowDown' || e.key === 'up' || e.key === 'down';
      return fromButtonInput || fromArrowKeys;
    };

    _this.onChange = function (e, raw, parsed) {
      var normalizeImmediately = _this.shouldNormalizeOnChange(e);

      var normalizedValue = _this.clamp(parsed, 0, _this.props.maxExtraAttempts);

      var update = {
        rawValue: normalizeImmediately ? _formatMessage.default.number(normalizedValue) : raw,
        parsedRawValue: normalizeImmediately ? normalizedValue : parsed,
        revert: false
      };

      if (parsed !== null) {
        update.normalizedValue = normalizedValue;
      }

      _this.setState(update);
    };

    _this.onBlur = function () {
      _this.setState({
        revert: true
      });

      _this.props.onChange(_this.state.normalizedValue);
    };

    return _this;
  }

  (0, _createClass2.default)(ExtraAttemptSettings, [{
    key: "render",
    value: function render() {
      return _react.default.createElement(_ExtraAttemptsInput.default, {
        label: "\u0414\u043E\u043F\u043E\u043B\u043D\u0438\u0442\u0435\u043B\u044C\u043D\u044B\u0435 \u043F\u043E\u043F\u044B\u0442\u043A\u0438",
        max: this.props.maxExtraAttempts,
        value: this.state.rawValue,
        parsedValue: this.state.parsedRawValue,
        revertValue: this.state.normalizedValue,
        revert: this.state.revert,
        onChange: this.onChange,
        onBlur: this.onBlur,
        messages: this.state.messages
      });
    }
  }]);
  ExtraAttemptSettings.displayName = "ExtraAttemptSettings";
  return ExtraAttemptSettings;
}(_react.Component);

ExtraAttemptSettings.propTypes = {
  onChange: _propTypes.default.func.isRequired,
  extraAttempts: _propTypes.default.number.isRequired,
  maxExtraAttempts: _propTypes.default.number
};
ExtraAttemptSettings.defaultProps = {
  maxExtraAttempts: 999
};
var _default = ExtraAttemptSettings;
exports.default = _default;