"use strict";

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

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

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

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

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

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 _formatMessage = _interopRequireDefault(require("@instructure/quiz-i18n/lib/format-message"));

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

var _Heading = _interopRequireDefault(require("@instructure/ui-elements/lib/components/Heading"));

var ResetQuiz =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(ResetQuiz, _Component);

  function ResetQuiz() {
    var _getPrototypeOf2;

    var _this;

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

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

    _this.focusReopenButton = function () {
      _this.reopenButtonRef && _this.reopenButtonRef.focus();
    };

    _this.reopenQuizSession = function () {
      _this.props.withConfirm(
      /*#__PURE__*/
      (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee() {
        return _regenerator.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return _this.props.reopenQuizSession(_this.props.sessionId);

              case 2:
                _this.props.trayResetFocus();

                _this.props.screenreaderNotification("\u6B64\u5B78\u751F\u7684\u905E\u4EA4\u6A5F\u6703\u5DF2\u91CD\u65B0\u958B\u555F");

              case 4:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      })), {
        onCancel: _this.focusReopenButton,
        shouldReturnFocus: false,
        // manually handling return focus instead
        title: "\u91CD\u555F\u905E\u4EA4\u6A5F\u6703",
        continueText: "\u91CD\u555F",
        text: "\u91CD\u555F\u6B64\u905E\u4EA4\u6A5F\u6703\u5C07\u5141\u8A31\u5B78\u751F\u7E7C\u7E8C\u9032\u884C\u6B64\u8A55\u4F30\u3002\n        \u4E4B\u524D\u6240\u8A18\u9304\u7684\u56DE\u61C9\u5C07\u7372\u4FDD\u7559\u3002\u5982\u679C\u6B64\u70BA\u5373\u6642\u8A55\u4F30\uFF0C\n        \u8A08\u6642\u5668\u6839\u64DA\u7B2C\u4E00\u6B21\u63D0\u4EA4\u6642\u5269\u9918\u7684\u6642\u9593\u7E7C\u7E8C\u8A08\u6642\u3002"
      });
    };

    _this.reopenButtonRef = function (node) {
      _this.reopenButtonRef = node;
    };

    return _this;
  }

  (0, _createClass2.default)(ResetQuiz, [{
    key: "render",
    value: function render() {
      if (!this.props.reopenOn) return null;

      if (!this.props.sessionId) {
        return _react.default.createElement("span", null, "\u5C1A\u672A\u8A18\u9304\u4EFB\u4F55\u905E\u4EA4\u5617\u8A66\u3002");
      }

      return _react.default.createElement("div", null, _react.default.createElement(_Heading.default, {
        level: "h3"
      }, "\u7BA1\u7406\u76EE\u524D\u7684\u905E\u4EA4\u6A5F\u6703"), this.props.reopenOn ? _react.default.createElement(_Button.default, {
        ref: this.reopenButtonRef,
        onClick: this.reopenQuizSession,
        disabled: !this.props.isComplete,
        margin: "medium none"
      }, "\u91CD\u555F") : null);
    }
  }]);
  ResetQuiz.displayName = "ResetQuiz";
  return ResetQuiz;
}(_react.Component);

exports.default = ResetQuiz;
ResetQuiz.propTypes = {
  sessionId: _propTypes.default.string.isRequired,
  isComplete: _propTypes.default.bool.isRequired,
  trayResetFocus: _propTypes.default.func.isRequired,
  reopenQuizSession: _propTypes.default.func.isRequired,
  reopenOn: _propTypes.default.bool.isRequired,
  screenreaderNotification: _propTypes.default.func.isRequired,
  withConfirm: _propTypes.default.func.isRequired
};