'use strict';

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

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 _dec, _class, _class2, _temp;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactImmutableProptypes = require('react-immutable-proptypes');

var _reactImmutableProptypes2 = _interopRequireDefault(_reactImmutableProptypes);

var _difference_in_calendar_days = require('date-fns/difference_in_calendar_days');

var _difference_in_calendar_days2 = _interopRequireDefault(_difference_in_calendar_days);

var _is_after = require('date-fns/is_after');

var _is_after2 = _interopRequireDefault(_is_after);

var _IconWarningLine = require('instructure-icons/lib/Line/IconWarningLine');

var _IconWarningLine2 = _interopRequireDefault(_IconWarningLine);

var _ScreenReaderContent = require('@instructure/ui-a11y/lib/components/ScreenReaderContent');

var _ScreenReaderContent2 = _interopRequireDefault(_ScreenReaderContent);

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('../../../../../common/format-message');

var _formatMessage2 = _interopRequireDefault(_formatMessage);

var _FormattedDuration = require('../../../../../common/components/shared/FormattedDuration');

var _FormattedDuration2 = _interopRequireDefault(_FormattedDuration);

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

var _InfoBox = require('./InfoBox');

var _InfoBox2 = _interopRequireDefault(_InfoBox);

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._33QS9ndcQ7gQFup8682nic {\n  display: flex;\n  justify-content: flex-start;\n  padding-bottom: ' + theme.containerPadding + ';\n  border-bottom: ' + theme.containerBorderColor + ' solid 1px;\n}\n\n._2uDRhGdp6fzOdmUQVRvIXs {\n  color: ' + theme.pastDueColor + ';\n  display: flex;\n}\n\n.mg2wncoRyFzYxBCj1sPrp {\n  margin-right: ' + theme.iconWrapper + ';\n}\n';
  },
  'container': '_33QS9ndcQ7gQFup8682nic',
  'pastDue': '_2uDRhGdp6fzOdmUQVRvIXs',
  'iconWrapper': 'mg2wncoRyFzYxBCj1sPrp'
};

var _ref = _react2.default.createElement(
  _PresentationContent2.default,
  null,
  '--:--'
);

var _ref2 = _react2.default.createElement(
  _PresentationContent2.default,
  null,
  '--'
);

var _ref3 = _react2.default.createElement(_IconWarningLine2.default, null);

var QuizInfo = (_dec = (0, _lib2.default)(_theme2.default, styles), _dec(_class = (_temp = _class2 = function (_Component) {
  (0, _inherits3.default)(QuizInfo, _Component);

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

  (0, _createClass3.default)(QuizInfo, [{
    key: 'hasDueAt',
    value: function hasDueAt() {
      return this.dueAt !== null;
    }
  }, {
    key: 'renderTimeLimitPrimary',
    value: function renderTimeLimitPrimary() {
      if (!this.hasTimeLimit) {
        return _react2.default.createElement(
          'span',
          null,
          _react2.default.createElement(
            _ScreenReaderContent2.default,
            null,
            (0, _formatMessage2.default)('No Time Limit')
          ),
          _ref
        );
      }

      return _react2.default.createElement(
        'span',
        null,
        _react2.default.createElement(
          _ScreenReaderContent2.default,
          null,
          (0, _formatMessage2.default)('During the quiz, press Alt+Shift+T to have the remaining time read')
        ),
        _react2.default.createElement(
          _ScreenReaderContent2.default,
          null,
          (0, _formatMessage2.default)('Time Limit')
        ),
        _react2.default.createElement(_FormattedDuration2.default, { timeLimitInSeconds: this.timeLimitInSeconds })
      );
    }
  }, {
    key: 'renderTimeLimitSecondary',
    value: function renderTimeLimitSecondary() {
      var text = this.hasTimeLimit ? (0, _formatMessage2.default)('Time Limit') : (0, _formatMessage2.default)('No Time Limit');
      return _react2.default.createElement(
        _PresentationContent2.default,
        null,
        text
      );
    }
  }, {
    key: 'renderDueDatePrimary',
    value: function renderDueDatePrimary(d) {
      if (!this.hasDueAt()) {
        return _react2.default.createElement(
          'span',
          null,
          _react2.default.createElement(
            _ScreenReaderContent2.default,
            null,
            (0, _formatMessage2.default)('No Due Date')
          ),
          _ref2
        );
      }

      var display = (0, _formatMessage2.default)('{d, date, default}', { d: d }).split(',')[0]; // TODO: figure out if this assumption works

      return _react2.default.createElement(
        'span',
        null,
        _react2.default.createElement(
          _ScreenReaderContent2.default,
          null,
          (0, _formatMessage2.default)('Due on')
        ),
        display
      );
    }
  }, {
    key: 'renderDueDateSecondary',
    value: function renderDueDateSecondary(d) {
      if (!this.hasDueAt()) {
        return _react2.default.createElement(
          _PresentationContent2.default,
          null,
          (0, _formatMessage2.default)('No Due Date')
        );
      }

      var daysUntil = (0, _difference_in_calendar_days2.default)(d, Date.now());

      if ((0, _is_after2.default)(Date.now(), d)) {
        return _react2.default.createElement(
          'div',
          { className: styles.pastDue },
          _react2.default.createElement(
            'div',
            { className: styles.iconWrapper },
            _ref3
          ),
          (0, _formatMessage2.default)('Past Due: {\n              daysUntil, plural,\n              one {# day}\n              other {# days}\n            }', { daysUntil: Math.abs(daysUntil) })
        );
      }

      return (0, _formatMessage2.default)('Due in {\n      daysUntil, plural,\n      one {# day}\n      other {# days}\n    }', { daysUntil: daysUntil });
    }
  }, {
    key: 'renderTimeLimit',
    value: function renderTimeLimit() {
      var accessibilityText = void 0;

      if (this.hasTimeLimit) {
        accessibilityText = (0, _formatMessage2.default)('Time Limit {time}', { time: (0, _formatTimespan.formatTime)(this.timeLimitInSeconds) });
      }

      return _react2.default.createElement(_InfoBox2.default, {
        accessibilityText: accessibilityText,
        mainText: this.renderTimeLimitPrimary(),
        subText: this.renderTimeLimitSecondary()
      });
    }
  }, {
    key: 'renderDueDate',
    value: function renderDueDate() {
      var d = new Date(Date.parse(this.dueAt));

      return _react2.default.createElement(_InfoBox2.default, {
        mainText: this.renderDueDatePrimary(d),
        subText: this.renderDueDateSecondary(d)
      });
    }
  }, {
    key: 'renderExtraInfoBoxes',
    value: function renderExtraInfoBoxes() {
      return this.props.extraInfoBoxes.map(function (boxObject, index) {
        return _react2.default.createElement(_InfoBox2.default, Object.assign({}, boxObject, { key: index }));
      });
    }
  }, {
    key: 'render',
    value: function render() {
      return _react2.default.createElement(
        'div',
        { className: styles.container },
        this.renderTimeLimit(),
        this.renderDueDate(),
        this.props.extraInfoBoxes.length > 0 && this.renderExtraInfoBoxes()
      );
    }
  }, {
    key: 'dueAt',
    get: function get() {
      return this.props.quiz.dueAt;
    }
  }, {
    key: 'timeLimitInSeconds',
    get: function get() {
      var _props = this.props,
          quiz = _props.quiz,
          quizSession = _props.quizSession;

      if (quizSession.get('disableTimer')) return 0;

      var quizTimeLimit = quiz.getTimeLimit();
      if (quizTimeLimit === 0) return 0;

      var timerMultiplier = quizSession.get('timerMultiplier');
      if (timerMultiplier) return quizTimeLimit * timerMultiplier;

      var extraTimeInSeconds = quizSession.get('extraTimeInSeconds');
      if (extraTimeInSeconds) return quizTimeLimit + extraTimeInSeconds;

      return quizTimeLimit;
    }
  }, {
    key: 'hasTimeLimit',
    get: function get() {
      return this.timeLimitInSeconds > 0;
    }
  }]);
  QuizInfo.displayName = 'QuizInfo'
  ;
  return QuizInfo;
}(_react.Component), _class2.propTypes = {
  extraInfoBoxes: _propTypes2.default.arrayOf(_propTypes2.default.shape({
    mainText: _propTypes2.default.string.isRequired,
    subText: _propTypes2.default.string.isRequired
  })),
  quiz: _reactImmutableProptypes2.default.record.isRequired,
  quizSession: _reactImmutableProptypes2.default.record.isRequired
}, _class2.defaultProps = {
  extraInfoBoxes: []
}, _temp)) || _class);
exports.default = QuizInfo;