"use strict";

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

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getHoursMinutesSeconds = getHoursMinutesSeconds;
exports.default = exports.MINUTE_IN_SECONDS = exports.HOUR_IN_SECONDS = 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 _getPrototypeOf2 = _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 _withI18nSupport2 = _interopRequireDefault(require("@instructure/quiz-common/lib/with-i18n-support"));

var _uiA11y = require("@instructure/ui-a11y");

var _formatTimespan = require("../../../util/formatTimespan");

var HOUR_IN_SECONDS = 3600;
exports.HOUR_IN_SECONDS = HOUR_IN_SECONDS;
var MINUTE_IN_SECONDS = 60;
/**
 * @param  {integer} seconds Number of seconds
 * @return {object}
 */

exports.MINUTE_IN_SECONDS = MINUTE_IN_SECONDS;

function getHoursMinutesSeconds(seconds) {
  if (seconds <= 0 || seconds === null || seconds === void 0) {
    return {
      hours: 0,
      minutes: 0,
      seconds: 0
    };
  }

  return {
    seconds: seconds % MINUTE_IN_SECONDS,
    minutes: Math.floor(seconds / MINUTE_IN_SECONDS) % MINUTE_IN_SECONDS,
    hours: Math.floor(seconds / HOUR_IN_SECONDS)
  };
}

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

  function FormattedTimer() {
    (0, _classCallCheck2.default)(this, FormattedTimer);
    return (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(FormattedTimer).apply(this, arguments));
  }

  (0, _createClass2.default)(FormattedTimer, [{
    key: "render",
    value: function render() {
      var _getHoursMinutesSecon = getHoursMinutesSeconds(this.props.seconds),
          hours = _getHoursMinutesSecon.hours,
          minutes = _getHoursMinutesSecon.minutes,
          seconds = _getHoursMinutesSecon.seconds;

      var altText = (0, _formatTimespan.formatTime)(this.props.seconds);
      var display = hours > 0 ? this.formatDecimal2MinIntegerNoFraction(hours) + ":" + this.formatDecimal2MinIntegerNoFraction(minutes) + ":" + this.formatDecimal2MinIntegerNoFraction(seconds) : this.formatDecimal2MinIntegerNoFraction(minutes) + ":" + this.formatDecimal2MinIntegerNoFraction(seconds);
      return _react.default.createElement(_uiA11y.AccessibleContent, {
        alt: altText
      }, display);
    }
  }]);
  FormattedTimer.displayName = "FormattedTimer";
  return FormattedTimer;
}((0, _withI18nSupport2.default)(_react.Component));

exports.default = FormattedTimer;
FormattedTimer.propTypes = {
  seconds: _propTypes.default.number
};
FormattedTimer.defaultProps = {
  seconds: 0
};