"use strict";

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

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

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

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

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 _classnames3 = _interopRequireDefault(require("classnames"));

var _AccessibleContent = _interopRequireDefault(require("@instructure/ui-a11y/lib/components/AccessibleContent"));

var _PresentationContent = _interopRequireDefault(require("@instructure/ui-a11y/lib/components/PresentationContent"));

var _lib = _interopRequireDefault(require("@instructure/ui-themeable/lib"));

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

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

var _theme = _interopRequireDefault(require("./theme"));

var _default_frCA_number = Intl.NumberFormat("fr-CA", { style: "decimal" }).format,
    _dec,
    _class,
    _class2,
    _temp;

var styles = {
  componentId: 'cYuau',
  template: function template(theme) {
    return "\n\n.cYuau_bqjS{color:".concat(theme.darkColor || 'inherit', "}");
  },
  'dark': 'cYuau_bqjS'
};
var HOUR_IN_SECONDS = 3600;
exports.HOUR_IN_SECONDS = HOUR_IN_SECONDS;
var MINUTE_IN_SECONDS = 60;
exports.MINUTE_IN_SECONDS = MINUTE_IN_SECONDS;

function pad(val) {
  var str = val.toString();

  var zero = _default_frCA_number(0);

  return str.length === 1 ? zero + str : str;
}
/**
 * Converts the seconds into a string of HH:MM:SS,
 * and reduces the output to include only the hours if relevant
 * @param  {integer} seconds Number of seconds
 * @return {object}
 */


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 _ref =
/*#__PURE__*/
_react.default.createElement(_PresentationContent.default, null, ":");

var _ref2 =
/*#__PURE__*/
_react.default.createElement(_PresentationContent.default, null, ":");

var FormattedTimer = (_dec = (0, _lib.default)(_theme.default, styles), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(FormattedTimer, _Component);

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

  (0, _createClass2.default)(FormattedTimer, [{
    key: "renderHours",
    value: function renderHours(hours) {
      if (hours <= 0) {
        return null;
      }

      return _react.default.createElement("span", {
        className: styles.dark
      }, pad(_default_frCA_number(pad(hours))), _ref);
    }
  }, {
    key: "render",
    value: function render() {
      var _getHoursMinutesSecon = getHoursMinutesSeconds(this.props.timeLimitInSeconds),
          hours = _getHoursMinutesSecon.hours,
          minutes = _getHoursMinutesSecon.minutes,
          seconds = _getHoursMinutesSecon.seconds;

      var altText = (0, _formatTimespan.formatTime)(this.props.timeLimitInSeconds);
      return _react.default.createElement(_AccessibleContent.default, {
        alt: altText
      }, this.renderHours(hours), _react.default.createElement("span", {
        className: (0, _classnames3.default)((0, _defineProperty2.default)({}, styles.dark, hours > 0 || minutes > 0))
      }, pad(_default_frCA_number(minutes)), _ref2), _react.default.createElement("span", {
        className: (0, _classnames3.default)((0, _defineProperty2.default)({}, styles.dark, hours > 0 || minutes > 0 || seconds > 0))
      }, pad(_default_frCA_number(pad(seconds)))));
    }
  }]);
  FormattedTimer.displayName = "FormattedTimer";
  return FormattedTimer;
}(_react.Component), _class2.propTypes = {
  timeLimitInSeconds: _propTypes.default.number // TODO: rename to seconds

}, _class2.defaultProps = {
  timeLimitInSeconds: 0
}, _temp)) || _class);
exports.default = FormattedTimer;