"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: 'fBhoODpnKo',
  template: function template(theme) {
    return "/*  imported from styles.css  */\n\n.Timer__styles__timer {\n  line-height: 1rem;\n  font-size: ".concat(theme.timerFontSize || 'none', ";\n  display: flex;\n  flex-direction: row;\n  align-items: center;\n  justify-content: center;\n}\n\n.Timer__styles__timerLeft {\n  display: flex;\n  flex-direction: column-reverse;\n  align-items: center;\n}\n\n.Timer__styles__timerRight {\n  padding: ").concat(theme.timerRightPadding || 'none', ";\n}\n\n.Timer__styles__timeRemainingLabel {\n  font-size: ").concat(theme.timeRemainingLabelFontSize || 'none', ";\n  width: 100%;\n  text-align: center;\n}\n\n[dir=\"ltr\"] .Timer__styles__timeRemainingLabel {\n  text-align: center;\n}\n\n[dir=\"rtl\"] .Timer__styles__timeRemainingLabel {\n  text-align: center;\n}\n");
  },
  'timer': 'Timer__styles__timer',
  'timerLeft': 'Timer__styles__timerLeft',
  'timerRight': 'Timer__styles__timerRight',
  'timeRemainingLabel': 'Timer__styles__timeRemainingLabel'
};
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 = "\u0627\u0644\u0648\u0642\u062A \u0627\u0644\u0645\u062A\u0628\u0642\u064A \u0647\u0648: " + timeString;
        } else {
          message = "\u0644\u0645 \u064A\u0639\u062F \u0647\u0646\u0627\u0643 \u0648\u0642\u062A\u064B\u0627 \u0645\u062A\u0628\u0642\u064A\u064B\u0627";
        }

        _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 ? "\u062A\u0645 \u062A\u0635\u063A\u064A\u0631 \u0627\u0644\u0645\u0624\u0642\u062A" : "\u0625\u0638\u0647\u0627\u0631 \u0627\u0644\u0645\u0624\u0642\u062A";
        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, "\u0623\u062B\u0646\u0627\u0621 \u0627\u0644\u0627\u062E\u062A\u0628\u0627\u0631 \u0627\u0644\u0645\u0648\u062C\u0632\u060C \u0627\u0636\u063A\u0637 \u0639\u0644\u0649 Alt+Shift+T \u0644\u0642\u0631\u0627\u0621\u0629 \u0627\u0644\u0648\u0642\u062A \u0627\u0644\u0645\u062A\u0628\u0642\u064A");
    }
  }, {
    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, "\u062A\u0628\u062F\u064A\u0644 \u0631\u0624\u064A\u0629 \u0627\u0644\u0645\u0624\u0642\u062A")));
    }
  }, {
    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
      }, "\u0627\u0644\u0648\u0642\u062A \u0627\u0644\u0645\u062A\u0628\u0642\u064A"), _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, "\u062A\u0628\u062F\u064A\u0644 \u0631\u0624\u064A\u0629 \u0627\u0644\u0645\u0624\u0642\u062A"))));
    }
  }, {
    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;