"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 _CustomPropTypes = _interopRequireDefault(require("../../../../../common/util/CustomPropTypes"));

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 _withI18nSupport2 = _interopRequireDefault(require("@instructure/quiz-common/lib/with-i18n-support"));

var _default_svXK12_number = Intl.NumberFormat("sv-x-k12", { style: "decimal" }).format;

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

  function ExtraTimeMultiplier() {
    var _getPrototypeOf2;

    var _this;

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

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

    _this.onChangeMultiplier = function (e, multiplierString, normalizedValue) {
      var fromButtonInput = e.nativeEvent.target && e.nativeEvent.target.tagName !== 'INPUT';
      var fromArrowKeys = e.key === 'ArrowUp' || e.key === 'ArrowDown';
      var normalizeOnChange = fromButtonInput || fromArrowKeys;
      var shouldUpdateMessage = e.type !== 'blur';
      var messages = shouldUpdateMessage ? null : _this.state.multiplierMessages;
      var aboveMax = normalizedValue > _this.props.maxMultiplier;
      var belowMin = normalizedValue < _this.props.minMultiplier;
      var outOfRange = aboveMax || belowMin;

      if (normalizeOnChange && outOfRange) {
        // Don't allow the up-button or down-button to take us out of range
        _this.onBlurMultiplier();

        return;
      }

      if (multiplierString !== '') {
        if (normalizedValue === null) {
          var _params;

          var messageText = _formatMessage.default.custom(["userInput", "string"], "sv-x-k12", multiplierString, _params) + " is not a valid number.";
          messages = [{
            type: 'error',
            text: messageText
          }];
        } else if (normalizedValue > _this.props.maxMultiplier) {
          var _params2;

          var _messageText = _formatMessage.default.custom(["userInput", "string"], "sv-x-k12", multiplierString, _params2) + " is above the maximum of " + _default_svXK12_number(_this.props.maxMultiplier);

          messages = [{
            type: 'error',
            text: _messageText
          }];
        } else if (normalizedValue < _this.props.minMultiplier) {
          var _params3;

          var _messageText2 = _formatMessage.default.custom(["userInput", "string"], "sv-x-k12", multiplierString, _params3) + " is below the minimum of " + _default_svXK12_number(_this.props.minMultiplier);

          messages = [{
            type: 'error',
            text: _messageText2
          }];
        }
      }

      var update = {
        rawMultiplierValue: multiplierString,
        multiplierMessages: messages
      };

      if (normalizedValue !== null) {
        var valueToUpdate = normalizedValue;
        valueToUpdate = aboveMax ? _this.props.maxMultiplier : valueToUpdate;
        valueToUpdate = belowMin ? _this.props.minMultiplier : valueToUpdate;
        update.normalizedMultiplierValue = valueToUpdate;
      }

      _this.setState(update);
    };

    _this.onBlurMultiplier = function () {
      _this.setState({
        rawMultiplierValue: _this.formatDecimal2FractionDigits(_this.state.normalizedMultiplierValue)
      });

      _this.props.onChange(Number(_this.state.normalizedMultiplierValue));
    };

    return _this;
  }

  (0, _createClass2.default)(ExtraTimeMultiplier, [{
    key: "componentWillMount",
    value: function componentWillMount() {
      var multiplierValue = Math.max(this.props.timerMultiplierValue, this.props.minMultiplier);
      multiplierValue = this.formatDecimal2FractionDigits(multiplierValue);
      this.setState({
        rawMultiplierValue: multiplierValue,
        normalizedMultiplierValue: multiplierValue
      });
    }
  }, {
    key: "render",
    value: function render() {
      return _react.default.createElement(_uiLayout.View, {
        display: "block",
        margin: "medium none"
      }, _react.default.createElement(_quizNumberInput.default, {
        label: "Multiplikator",
        step: 0.01,
        value: this.state.rawMultiplierValue,
        onChange: this.onChangeMultiplier,
        onBlur: this.onBlurMultiplier,
        decimalPrecision: 2,
        messages: this.state.multiplierMessages || [{
          text: "\xC3 \u2013 tidsgr\xE4ns",
          type: 'hint'
        }]
      }));
    }
  }]);
  ExtraTimeMultiplier.displayName = "ExtraTimeMultiplier";
  return ExtraTimeMultiplier;
}((0, _withI18nSupport2.default)(_react.Component));

ExtraTimeMultiplier.propTypes = {
  onChange: _propTypes.default.func.isRequired,
  timerMultiplierValue: _propTypes.default.number.isRequired,
  maxMultiplier: _propTypes.default.number.isRequired,
  minMultiplier: _CustomPropTypes.default.numberInRange(1, Infinity),
  locale: _propTypes.default.string
};
ExtraTimeMultiplier.defaultProps = {
  minMultiplier: 1,
  locale: null
};
ExtraTimeMultiplier.contextTypes = {
  locale: _propTypes.default.string
};
var _default = ExtraTimeMultiplier;
exports.default = _default;