'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; /* eslint "jsx-a11y/no-noninteractive-tabindex": "warn" */


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 _lib = require('@instructure/ui-themeable/lib');

var _lib2 = _interopRequireDefault(_lib);

var _generateDisplayPositions = require('../../../../../common/util/generateDisplayPositions');

var _generateDisplayPositions2 = _interopRequireDefault(_generateDisplayPositions);

var _TakeProgress = require('../TakeProgress');

var _TakeProgress2 = _interopRequireDefault(_TakeProgress);

var _TakeItem = require('../TakeItem');

var _TakeItem2 = _interopRequireDefault(_TakeItem);

var _TakeStimulus = require('../TakeStimulus');

var _TakeStimulus2 = _interopRequireDefault(_TakeStimulus);

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.TakeBody__styles__root {}\n\n.TakeBody__styles__root .TakeBody__styles__itemWrapper {\n  border-bottom: ' + theme.itemWrapperBorderWidth + ' solid ' + theme.itemWrapperBorderColor + ';\n  outline: none;\n}\n\n.TakeBody__styles__root .TakeBody__styles__itemWrapper:first-child {\n  border-top: ' + theme.itemWrapperBorderWidth + ' solid ' + theme.itemWrapperBorderColor + ';\n}\n';
  },
  'root': 'TakeBody__styles__root',
  'itemWrapper': 'TakeBody__styles__itemWrapper'
};
var TakeBody = (_dec = (0, _lib2.default)(_theme2.default, styles), _dec(_class = (_temp = _class2 = function (_Component) {
  (0, _inherits3.default)(TakeBody, _Component);

  function TakeBody(props) {
    (0, _classCallCheck3.default)(this, TakeBody);

    var _this = (0, _possibleConstructorReturn3.default)(this, (TakeBody.__proto__ || Object.getPrototypeOf(TakeBody)).call(this, props));

    _this.setRef = function (itemRef) {
      _this.itemRef = itemRef;
    };

    _this.renderAllItems = function () {
      return (0, _generateDisplayPositions2.default)(_this.props.sessionItems).map(function (si) {
        return _this.renderSessionItem(si.record);
      });
    };

    _this.renderCurrentItem = function () {
      var item = _this.props.sessionItems.find(function (item) {
        return item.get('position') === _this.props.currentSessionItemPosition;
      });
      return _this.renderOQAAT(item);
    };

    _this.itemRef = null;
    // TODO: refactor the access and assignment this.focus
    _this.focus = _this.props.focus || _this.focusItemRef;
    return _this;
  }

  (0, _createClass3.default)(TakeBody, [{
    key: 'componentDidUpdate',
    value: function componentDidUpdate(prevProps) {
      // Only focus if OQAAT is on and we're switching to a new item from a previous item
      if (!this.props.isOneQuestionAtATime || this.props.currentSessionItemPosition === prevProps.currentSessionItemPosition) {
        return;
      }
      this.focus();
    }
  }, {
    key: 'focusItemRef',
    value: function focusItemRef() {
      this.itemRef && this.itemRef.focus();
    }
  }, {
    key: 'renderProgressBar',
    value: function renderProgressBar() {
      if (this.props.showProgress) {
        return _react2.default.createElement(_TakeProgress2.default, {
          currentQuestionPosition: this.props.currentSessionItemPosition,
          totalQuestionCount: this.props.sessionItemsCount
        });
      }
    }
  }, {
    key: 'renderOQAAT',
    value: function renderOQAAT(sessionItem) {
      if (!sessionItem) {
        return;
      }

      return _react2.default.createElement(
        'div',
        null,
        this.renderProgressBar(),
        this.renderSessionItem(sessionItem)
      );
    }
  }, {
    key: 'renderSessionItem',
    value: function renderSessionItem(sessionItem) {
      if (sessionItem.isStimulus) {
        return _react2.default.createElement(_TakeStimulus2.default, {
          isOqaat: this.props.isOneQuestionAtATime,
          key: sessionItem.position,
          sessionItem: sessionItem,
          quizSessionId: this.props.quizSessionId
        });
      }
      return _react2.default.createElement(
        'div',
        {
          role: 'form',
          className: styles.itemWrapper,
          ref: this.setRef,
          tabIndex: '-1',
          key: sessionItem.position
        },
        _react2.default.createElement(_TakeItem2.default, {
          quizSessionId: this.props.quizSessionId,
          sessionItem: sessionItem
        })
      );
    }
  }, {
    key: 'render',
    value: function render() {
      var body = this.props.isOneQuestionAtATime ? this.renderCurrentItem() : this.renderAllItems();

      return _react2.default.createElement(
        'div',
        { className: styles.root },
        body
      );
    }
  }]);
  TakeBody.displayName = 'TakeBody'
  ;
  return TakeBody;
}(_react.Component), _class2.propTypes = {
  currentSessionItemPosition: _propTypes2.default.number.isRequired,
  focus: _propTypes2.default.func,
  isOneQuestionAtATime: _propTypes2.default.bool.isRequired,
  quizSessionId: _propTypes2.default.string.isRequired,
  sessionItems: _reactImmutableProptypes2.default.list.isRequired,
  sessionItemsCount: _propTypes2.default.number.isRequired,
  showProgress: _propTypes2.default.bool.isRequired
}, _class2.defaultProps = {
  focus: null
}, _temp)) || _class);
exports.default = TakeBody;