'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.MINUTE_IN_SECONDS = exports.HOUR_IN_SECONDS = undefined;

var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _default_pl_number = Intl.NumberFormat('pl', { style: 'decimal' }).format,
    _dec,
    _class,
    _class2,
    _temp;

exports.pad = pad;
exports.getHoursMinutesSeconds = getHoursMinutesSeconds;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames3 = require('classnames');

var _classnames4 = _interopRequireDefault(_classnames3);

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

var _AccessibleContent2 = _interopRequireDefault(_AccessibleContent);

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

var _PresentationContent2 = _interopRequireDefault(_PresentationContent);

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

var _lib2 = _interopRequireDefault(_lib);

var _formatMessage = require('../../../format-message');

var _formatMessage2 = _interopRequireDefault(_formatMessage);

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

var _theme = require('./theme');

var _theme2 = _interopRequireDefault(_theme);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var styles = {
  template: function template(theme) {
    return '/*  imported from styles.css  */\n\n.FormattedDuration__styles__dark {\n  color: ' + theme.darkColor + ';\n}\n';
  },
  'dark': 'FormattedDuration__styles__dark'
};
var HOUR_IN_SECONDS = exports.HOUR_IN_SECONDS = 3600;
var MINUTE_IN_SECONDS = exports.MINUTE_IN_SECONDS = 60;

function pad(val) {
  var str = val.toString();
  var zero = _default_pl_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 FormattedTimer = (_dec = (0, _lib2.default)(_theme2.default, styles), _dec(_class = (_temp = _class2 = function (_Component) {
  (0, _inherits3.default)(FormattedTimer, _Component);

  function FormattedTimer() {
    (0, _classCallCheck3.default)(this, FormattedTimer);
    return (0, _possibleConstructorReturn3.default)(this, (FormattedTimer.__proto__ || Object.getPrototypeOf(FormattedTimer)).apply(this, arguments));
  }

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

      return _react2.default.createElement(
        'span',
        { className: styles.dark },
        pad(_default_pl_number(pad(hours))),
        _react2.default.createElement(
          _PresentationContent2.default,
          null,
          ':'
        )
      );
    }
  }, {
    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 _react2.default.createElement(
        _AccessibleContent2.default,
        { alt: altText },
        this.renderHours(hours),
        _react2.default.createElement(
          'span',
          { className: (0, _classnames4.default)((0, _defineProperty3.default)({}, styles.dark, hours > 0 || minutes > 0)) },
          pad(_default_pl_number(minutes)),
          _react2.default.createElement(
            _PresentationContent2.default,
            null,
            ':'
          )
        ),
        _react2.default.createElement(
          'span',
          { className: (0, _classnames4.default)((0, _defineProperty3.default)({}, styles.dark, hours > 0 || minutes > 0 || seconds > 0)) },
          pad(_default_pl_number(pad(seconds)))
        )
      );
    }
  }]);
  FormattedTimer.displayName = 'FormattedTimer'
  ;
  return FormattedTimer;
}(_react.Component), _class2.propTypes = {
  timeLimitInSeconds: _propTypes2.default.number // TODO: rename to seconds
}, _class2.defaultProps = {
  timeLimitInSeconds: 0
}, _temp)) || _class);
exports.default = FormattedTimer;