"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 _propTypes = _interopRequireDefault(require("prop-types"));

var _partial = _interopRequireDefault(require("lodash/partial"));

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

var _IconClock = _interopRequireDefault(require("@instructure/ui-icons/lib/Line/IconClock"));

var _IconArrowOpenStart = _interopRequireDefault(require("@instructure/ui-icons/lib/Line/IconArrowOpenStart"));

var _Button = _interopRequireDefault(require("@instructure/ui-buttons/lib/components/Button"));

var _View = _interopRequireDefault(require("@instructure/ui-layout/lib/components/View"));

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

var _FormattedDuration = _interopRequireDefault(require("../../../shared/FormattedDuration"));

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

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

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

var _dec, _class, _class2, _temp;

var styles = {
  componentId: 'fBhoO',
  template: function template(theme) {
    return "\n\n.fBhoO_dAhe{flex-direction:row;font-size:".concat(theme.timerFontSize || 'inherit', ";justify-content:center;line-height:1rem}\n\n.fBhoO_dAhe,.fBhoO_Swlx{align-items:center;display:flex}\n\n.fBhoO_Swlx{flex-direction:column-reverse}\n\n.fBhoO_dkDW{padding:").concat(theme.timerRightPadding || 'inherit', "}\n\n.fBhoO_bYJj{font-size:").concat(theme.timeRemainingLabelFontSize || 'inherit', ";text-align:center;width:100%}\n\n[dir=ltr] .fBhoO_bYJj,[dir=rtl] .fBhoO_bYJj{text-align:center}");
  },
  'timer': 'fBhoO_dAhe',
  'timerLeft': 'fBhoO_Swlx',
  'timerRight': 'fBhoO_dkDW',
  'timeRemainingLabel': 'fBhoO_bYJj'
};
var Timer = (_dec = (0, _lib.default)(_theme.default, styles), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(Timer, _Component);

  function Timer() {
    var _getPrototypeOf2;

    var _this;

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

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

    _this.handleKeyDown = function (event) {
      // Check for Alt+Shift+T and nothing else
      if (event.keyCode === 84 && event.altKey && event.shiftKey && !event.ctrlKey) {
        var message;
        event.preventDefault();

        if (_this.props.timeLeftInSeconds >= 1) {
          var timeString = (0, _formatTimespan.formatTime)(_this.props.timeLeftInSeconds);
          message = "Le temps restant est : " + timeString;
        } else {
          message = "Il ne reste plus de temps";
        }

        _this.props.screenreaderNotification(message);
      }
    };

    _this.showTimerRef = function (node) {
      _this.showTimerBtn = node;
    };

    _this.hideTimerRef = function (node) {
      _this.hideTimerBtn = node;
    };

    _this.focusOnShow = function () {
      _this.showTimerBtn && _this.showTimerBtn.focus();
    };

    _this.focusOnHide = function () {
      _this.hideTimerBtn && _this.hideTimerBtn.focus();
    };

    return _this;
  }

  (0, _createClass2.default)(Timer, [{
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps, prevState) {
      var _this$props = this.props,
          timerMinimized = _this$props.timerMinimized,
          screenreaderNotification = _this$props.screenreaderNotification,
          timerRunning = _this$props.timerRunning;

      if (timerMinimized !== prevProps.timerMinimized) {
        var message = timerMinimized ? "pendule minimis\xE9" : "pendule visible";
        screenreaderNotification(message);
        var focusFn = timerMinimized ? this.focusOnShow : this.focusOnHide;
        focusFn();
      }

      if (timerRunning && !prevProps.timerRunning) {
        document.body.addEventListener('keydown', this.handleKeyDown);
      } else if (!timerRunning && prevProps.timerRunning) {
        document.body.removeEventListener('keydown', this.handleKeyDown);
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      document.body.removeEventListener('keydown', this.handleKeyDown);
    }
  }, {
    key: "renderHelpScreenreaderText",
    value: function renderHelpScreenreaderText() {
      return _react.default.createElement(_ScreenReaderContent.default, null, "Pendant le quiz, appuyez sur Alt + Maj + T pour que le temps restant soit visible");
    }
  }, {
    key: "renderMinimizedTimer",
    value: function renderMinimizedTimer() {
      return _react.default.createElement("div", {
        className: styles.timer
      }, this.renderHelpScreenreaderText(), _react.default.createElement(_Button.default, {
        ref: this.showTimerRef,
        icon: _IconClock.default,
        onClick: (0, _partial.default)(this.props.updateTimerMinimized, false)
      }, _react.default.createElement(_ScreenReaderContent.default, null, "Activer/D\xE9sactiver la visibilit\xE9 de la pendule")));
    }
  }, {
    key: "renderMaximizedTimer",
    value: function renderMaximizedTimer() {
      return _react.default.createElement("div", {
        className: styles.timer
      }, this.renderHelpScreenreaderText(), _react.default.createElement(_View.default, {
        as: "div"
      }, _react.default.createElement("div", {
        className: styles.timerLeft
      }, _react.default.createElement("div", {
        className: styles.timeRemainingLabel
      }, "Temps restant"), _react.default.createElement(_FormattedDuration.default, {
        timeLimitInSeconds: this.props.timeLeftInSeconds
      }))), _react.default.createElement("div", {
        className: styles.timerRight
      }, _react.default.createElement(_Button.default, {
        ref: this.hideTimerRef,
        icon: _IconArrowOpenStart.default,
        onClick: (0, _partial.default)(this.props.updateTimerMinimized, true)
      }, _react.default.createElement(_ScreenReaderContent.default, null, "Activer/D\xE9sactiver la visibilit\xE9 de la pendule"))));
    }
  }, {
    key: "render",
    value: function render() {
      if (!this.props.timerRunning) {
        return null;
      } else if (this.props.timerMinimized) {
        return this.renderMinimizedTimer();
      } else {
        return this.renderMaximizedTimer();
      }
    }
  }]);
  Timer.displayName = "Timer";
  return Timer;
}(_react.Component), _class2.propTypes = {
  screenreaderNotification: _propTypes.default.func.isRequired,
  timeLeftInSeconds: _propTypes.default.number,
  timerMinimized: _propTypes.default.bool,
  timerRunning: _propTypes.default.bool,
  updateTimerMinimized: _propTypes.default.func.isRequired
}, _class2.defaultProps = {
  timerRunning: false,
  timeLeftInSeconds: 0,
  timerMinimized: false
}, _temp)) || _class);
exports.default = Timer;