'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 _class, _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 _generateDisplayPositions = require('../../../../common/util/generateDisplayPositions');

var _generateDisplayPositions2 = _interopRequireDefault(_generateDisplayPositions);

var _QuizEntry = require('../../resources/quizEntry/QuizEntry');

var _QuizEntry2 = _interopRequireDefault(_QuizEntry);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var QuizEntriesSection = (_temp2 = _class = function (_Component) {
  (0, _inherits3.default)(QuizEntriesSection, _Component);

  function QuizEntriesSection() {
    var _ref;

    var _temp, _this, _ret;

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

    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 = QuizEntriesSection.__proto__ || Object.getPrototypeOf(QuizEntriesSection)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
      editQuizEntryToggle: -1
    }, _this.propsForQuizEntry = function (quizEntry, displayPosition) {
      return {
        displayPosition: displayPosition,
        draggingOnBuild: _this.props.isOver,
        editable: true,
        propsForQuizEntry: _this.propsForQuizEntry,
        quizEntries: _this.props.quizEntries,
        quizEntry: quizEntry,
        quizId: _this.props.quizId,
        ref: _this.props.setQuizEntryRef(quizEntry.id),
        scrollKey: quizEntry.id,
        editQuizEntryToggle: _this.state.editQuizEntryToggle === quizEntry.id,
        onEditQuizEntryToggle: _this.onEditQuizEntryToggle
      };
    }, _this.renderQuizEntry = function (quizEntry, displayPosition) {
      var ref = _this.props.setQuizEntryWrapperRef(quizEntry.id);
      var nextPosition = quizEntry.position + 1;
      nextPosition += quizEntry.hasNestedRecords ? quizEntry.positionSlots : 0;

      return _react2.default.createElement(
        'div',
        { ref: ref, key: quizEntry.id + '_build' },
        _react2.default.createElement(_QuizEntry2.default, _this.propsForQuizEntry(quizEntry, displayPosition)),
        _this.props.renderAddContentPopover(_this.props.isOver, nextPosition)
      );
    }, _this.onEditQuizEntryToggle = function (value) {
      _this.setState({ editQuizEntryToggle: value });
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(QuizEntriesSection, [{
    key: 'shouldComponentUpdate',
    value: function shouldComponentUpdate(nextProps, nextState) {
      var quizEntriesEqual = this.props.quizEntries.equals(nextProps.quizEntries);

      return !quizEntriesEqual || this.props.isOver !== nextProps.isOver || this.props.quizId !== nextProps.quizId || this.props.renderAddContentPopover !== nextProps.renderAddContentPopover || this.props.setQuizEntryRef !== nextProps.setQuizEntryRef || this.props.setQuizEntryWrapperRef !== nextProps.setQuizEntryWrapperRef || this.state.editQuizEntryToggle !== nextState.editQuizEntryToggle;
    }
  }, {
    key: 'renderQuizEntries',
    value: function renderQuizEntries() {
      var _this2 = this;

      return (0, _generateDisplayPositions2.default)(this.props.quizEntries).map(function (quizEntry) {
        if (!quizEntry.record.entryType) return null;
        return _this2.renderQuizEntry(quizEntry.record, quizEntry.displayPosition);
      });
    }
  }, {
    key: 'render',
    value: function render() {
      return _react2.default.createElement(
        'div',
        null,
        this.renderQuizEntries()
      );
    }
  }]);
  QuizEntriesSection.displayName = 'QuizEntriesSection'
  ;
  return QuizEntriesSection;
}(_react.Component), _class.propTypes = {
  isOver: _propTypes2.default.bool.isRequired,
  quizId: _propTypes2.default.string.isRequired,
  quizEntries: _reactImmutableProptypes2.default.listOf(_reactImmutableProptypes2.default.record).isRequired,
  renderAddContentPopover: _propTypes2.default.func.isRequired,
  setQuizEntryRef: _propTypes2.default.func.isRequired,
  setQuizEntryWrapperRef: _propTypes2.default.func.isRequired
}, _temp2);
exports.default = QuizEntriesSection;