'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, _temp2;

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 _immutable = require('immutable');

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

var _lib2 = _interopRequireDefault(_lib);

var _featureCheck = require('../../../../../common/util/featureCheck');

var _CustomPropTypes = require('../../../../../common/util/CustomPropTypes');

var _CustomPropTypes2 = _interopRequireDefault(_CustomPropTypes);

var _GradingBar = require('../GradingBar');

var _GradingBar2 = _interopRequireDefault(_GradingBar);

var _SessionItemResult = require('../../../../../common/components/resources/sessionItemResult/SessionItemResult');

var _SessionItemResult2 = _interopRequireDefault(_SessionItemResult);

var _RegradeOverride = require('../../quizEntry/RegradeOverride');

var _RegradeOverride2 = _interopRequireDefault(_RegradeOverride);

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.SessionItemResultWrapper__styles__root {\n  padding-bottom: ' + theme.rootPadding + ';\n  flex: 1 1 auto;\n\n  display: flex;\n  flex-direction: column;\n  justify-content: center;\n  align-items: center;\n}\n\n.SessionItemResultWrapper__styles__result,\n.SessionItemResultWrapper__styles__grading_bar {\n  width: 100%;\n}\n';
  },
  'root': 'SessionItemResultWrapper__styles__root',
  'result': 'SessionItemResultWrapper__styles__result',
  'grading_bar': 'SessionItemResultWrapper__styles__grading_bar'
};
var SessionItemResultWrapper = (_dec = (0, _lib2.default)(_theme2.default, styles), _dec(_class = (_temp2 = _class2 = function (_Component) {
  (0, _inherits3.default)(SessionItemResultWrapper, _Component);

  function SessionItemResultWrapper() {
    var _ref;

    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, SessionItemResultWrapper);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = SessionItemResultWrapper.__proto__ || Object.getPrototypeOf(SessionItemResultWrapper)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
      graderFeedback: _this.props.graderFeedback
    }, _this.gradingBar = null, _this._timeouts = [], _this.startFeedbackEdit = function () {
      if (!_this.props.isEditing) {
        _this.props.switchOnEditing();
        _this._timeouts = _this._timeouts.concat(setTimeout(function () {
          return _this.gradingBar.focusRce();
        }));
      }
    }, _this.closeFeedbackEdit = function () {
      _this.props.switchOffEditing();
      _this.gradingBar.focusFeedbackButton();
    }, _this.submitFeedbackEdit = function () {
      _this.props.handleChangeFeedback(_this.props.sessionItem.id, _this.props.item.id, {
        graderFeedback: _this.state.graderFeedback.toJS()
      });
      _this.closeFeedbackEdit();
    }, _this.updateFeedbackEdit = function (e, _ref2) {
      var editorContent = _ref2.editorContent;

      _this.setState({
        graderFeedback: (0, _immutable.Map)({ content: editorContent })
      });
    }, _this.handleRegradingEditModeToggle = function () {
      _this.props.toggleRegradingEditMode(_this.props.regradingKey);
    }, _this.handleRegradingOnCancel = function (sessionItemId) {
      _this._timeouts = _this._timeouts.concat(setTimeout(function () {
        return _this.gradingBar.focusRegradeButton();
      }));
    }, _this.handleGradingBarRef = function (node) {
      _this.gradingBar = node;
    }, _this.renderRegradeOverride = function (sessionItem, item) {
      return _react2.default.createElement(_RegradeOverride2.default, {
        displayPosition: sessionItem.position,
        guid: sessionItem.id,
        onCancel: _this.handleRegradingOnCancel,
        itemId: item.id,
        quizSessionId: _this.props.quizSessionId,
        sessionItem: sessionItem
      });
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(SessionItemResultWrapper, [{
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      this._timeouts.forEach(clearTimeout);
    }
  }, {
    key: 'isAutogradeable',
    value: function isAutogradeable() {
      var notAutogradeableTypes = ['essay', 'file-upload'];
      return !notAutogradeableTypes.includes(this.props.item.getInteractionType().slug);
    }
  }, {
    key: 'isSubjective',
    value: function isSubjective() {
      return this.props.item.getInteractionType().isSubjective;
    }

    // =============
    //   HANDLERS
    // =============

    // =============
    //   RENDERING
    // =============

  }, {
    key: 'renderResult',
    value: function renderResult(sessionItem, item) {
      var _props = this.props,
          handleChangeGradeStatus = _props.handleChangeGradeStatus,
          handleChangePoints = _props.handleChangePoints,
          itemResultsModifications = _props.itemResultsModifications,
          sessionItemResult = _props.sessionItemResult,
          shouldRenderTopBorder = _props.shouldRenderTopBorder;


      return _react2.default.createElement(
        'div',
        { className: styles.root, 'data-automation': 'sdk-grading-result-wrapper' },
        _react2.default.createElement(
          'div',
          { className: styles.result },
          _react2.default.createElement(_SessionItemResult2.default, {
            appContainer: this.props.appContainer,
            editGraderFeedback: this.startFeedbackEdit,
            hideGraderFeedback: this.props.isEditing,
            item: item,
            sessionItem: sessionItem,
            sessionItemResult: sessionItemResult,
            shouldRenderTopBorder: shouldRenderTopBorder
          })
        ),
        _react2.default.createElement(
          'div',
          { className: styles.grading_bar },
          _react2.default.createElement(_GradingBar2.default, {
            appContainer: this.props.appContainer,
            ref: this.handleGradingBarRef,
            activeQuizSessionId: this.props.activeQuizSessionId,
            cancelFeedbackEdit: this.closeFeedbackEdit,
            graderFeedback: this.state.graderFeedback,
            handleChangeGradeStatus: handleChangeGradeStatus,
            handleChangePoints: handleChangePoints,
            isAutogradeable: this.isAutogradeable(),
            isEditing: this.props.isEditing,
            isSubjective: this.isSubjective(),
            itemResultsModifications: itemResultsModifications,
            regradeToggleAction: this.handleRegradingEditModeToggle,
            sessionItem: sessionItem,
            sessionItemResult: sessionItemResult,
            startFeedbackEdit: this.startFeedbackEdit,
            submitFeedbackEdit: this.submitFeedbackEdit,
            updateFeedbackEdit: this.updateFeedbackEdit
          })
        )
      );
    }
  }, {
    key: 'render',
    value: function render() {
      var _props2 = this.props,
          sessionItem = _props2.sessionItem,
          item = _props2.item;

      if (this.props.isRegrading && (0, _featureCheck.featureOn)('regrading_from_speedgrader')) {
        return this.renderRegradeOverride(sessionItem, item);
      } else {
        return this.renderResult(sessionItem, item);
      }
    }
  }]);
  SessionItemResultWrapper.displayName = 'SessionItemResultWrapper'
  ;
  return SessionItemResultWrapper;
}(_react.Component), _class2.propTypes = {
  activeQuizSessionId: _propTypes2.default.string.isRequired,
  appContainer: _CustomPropTypes2.default.selectors.isRequired,
  handleChangeFeedback: _propTypes2.default.func.isRequired,
  handleChangeGradeStatus: _propTypes2.default.func.isRequired,
  handleChangePoints: _propTypes2.default.func.isRequired,
  graderFeedback: _reactImmutableProptypes2.default.map.isRequired,
  isEditing: _propTypes2.default.bool.isRequired,
  isRegrading: _propTypes2.default.bool.isRequired,
  item: _reactImmutableProptypes2.default.record.isRequired,
  itemResultsModifications: _reactImmutableProptypes2.default.map.isRequired,
  quizSessionId: _propTypes2.default.string.isRequired,
  regradingKey: _propTypes2.default.string.isRequired,
  sessionItem: _reactImmutableProptypes2.default.record.isRequired,
  sessionItemResult: _reactImmutableProptypes2.default.record.isRequired,
  shouldRenderTopBorder: _propTypes2.default.bool.isRequired,
  switchOffEditing: _propTypes2.default.func.isRequired,
  switchOnEditing: _propTypes2.default.func.isRequired,
  toggleRegradingEditMode: _propTypes2.default.func.isRequired
}, _temp2)) || _class);
exports.default = SessionItemResultWrapper;