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

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 _Text = require('@instructure/ui-elements/lib/components/Text');

var _Text2 = _interopRequireDefault(_Text);

var _List = require('@instructure/ui-core/lib/components/List');

var _List2 = _interopRequireDefault(_List);

var _Card = require('../../../../common/components/shared/Card');

var _Card2 = _interopRequireDefault(_Card);

var _CardContent = require('../../../../common/components/shared/Card/CardContent');

var _CardContent2 = _interopRequireDefault(_CardContent);

var _CardWrapper = require('../../../../common/components/shared/Card/CardWrapper');

var _CardWrapper2 = _interopRequireDefault(_CardWrapper);

var _Page = require('../../../../common/components/layout/Page');

var _Page2 = _interopRequireDefault(_Page);

var _Spinner = require('../../../../common/components/shared/spinner/Spinner');

var _Spinner2 = _interopRequireDefault(_Spinner);

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

var _generateDisplayPositions2 = _interopRequireDefault(_generateDisplayPositions);

var _formatMessage = require('../../../../common/format-message');

var _formatMessage2 = _interopRequireDefault(_formatMessage);

var _BanksTray = require('../../resources/BanksTray');

var _BanksTray2 = _interopRequireDefault(_BanksTray);

var _Sidebar = require('../../layout/Sidebar');

var _Sidebar2 = _interopRequireDefault(_Sidebar);

var _DeleteStimulusModal = require('../../resources/DeleteStimulusModal');

var _DeleteStimulusModal2 = _interopRequireDefault(_DeleteStimulusModal);

var _ItemFeedbackModal = require('../../resources/ItemFeedbackModal');

var _ItemFeedbackModal2 = _interopRequireDefault(_ItemFeedbackModal);

var _MoveImmutableModal = require('../../resources/MoveImmutableModal');

var _MoveImmutableModal2 = _interopRequireDefault(_MoveImmutableModal);

var _Modal = require('../../../../common/components/resources/quiz/AddContent/Modal');

var _Modal2 = _interopRequireDefault(_Modal);

var _QTIImportModal = require('../../QTIImportModal');

var _QTIImportModal2 = _interopRequireDefault(_QTIImportModal);

var _Header = require('./components/Header');

var _Header2 = _interopRequireDefault(_Header);

var _BuildContent = require('./components/BuildContent');

var _BuildContent2 = _interopRequireDefault(_BuildContent);

var _theme = require('./theme');

var _theme2 = _interopRequireDefault(_theme);

var _constants = require('../../../../constants');

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

var styles = {
  template: function template(theme) {
    return '/*  imported from styles.css  */\n\n.hT2DbTqP96ZCoVYpbQRo2 {\n  height: 100%;\n}\n\n._2mSuqtctonznzzXmJ4nSs5 {\n  background: ' + theme.headerBackground + ';\n  padding-top: ' + theme.headerPadding + ';\n  max-width: 79.5rem;\n  margin: auto;\n}\n';
  },
  'builder': 'hT2DbTqP96ZCoVYpbQRo2',
  'header': '_2mSuqtctonznzzXmJ4nSs5'
};

var _ref = _react2.default.createElement(
  _Card2.default,
  { wrapped: true },
  _react2.default.createElement(_Spinner2.default, null)
);

var _ref2 = _react2.default.createElement(_DeleteStimulusModal2.default, null);

var _ref3 = _react2.default.createElement(_MoveImmutableModal2.default, null);

var _ref4 = _react2.default.createElement(_Modal2.default, null);

var Build = (_dec = (0, _lib2.default)(_theme2.default, styles), _dec(_class = (_temp = _class2 = function (_Component) {
  (0, _inherits3.default)(Build, _Component);

  function Build() {
    (0, _classCallCheck3.default)(this, Build);
    return (0, _possibleConstructorReturn3.default)(this, (Build.__proto__ || Object.getPrototypeOf(Build)).apply(this, arguments));
  }

  (0, _createClass3.default)(Build, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      this.loadQuiz();
      this.checkForInvalidQuizEntries();
    }
  }, {
    key: 'componentDidUpdate',
    value: function componentDidUpdate(prevProps) {
      if (this.props.activeQuizId !== prevProps.activeQuizId) {
        this.loadQuiz();
      }
      if (!this.props.quizEntries.equals(prevProps.quizEntries)) {
        this.checkForInvalidQuizEntries();
      }
    }

    // ===========
    //   HELPERS
    // ===========

  }, {
    key: 'loadQuiz',
    value: function loadQuiz() {
      var _props = this.props,
          activeQuizId = _props.activeQuizId,
          clearErrorsShowing = _props.clearErrorsShowing,
          contextId = _props.contextId,
          quizBuildingSession = _props.quizBuildingSession;

      clearErrorsShowing();
      quizBuildingSession({
        activeQuizId: activeQuizId,
        contextId: contextId,
        createNew: true
      });
    }
  }, {
    key: 'getDisplayPositionsForInvalidQuizEntries',
    value: function getDisplayPositionsForInvalidQuizEntries() {
      var quizEntries = this.props.quizEntries;

      var invalidQuizEntries = quizEntries.filter(function (qe) {
        return !qe.isValid();
      });
      if (invalidQuizEntries.size > 0) {
        var positionMap = (0, _generateDisplayPositions2.default)(quizEntries, true);
        return invalidQuizEntries.map(function (qe) {
          return positionMap[qe.id];
        }).toJS();
      }
    }
  }, {
    key: 'getInvalidMessage',
    value: function getInvalidMessage(invalidPositions) {
      return _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(
          _Text2.default,
          { weight: 'bold' },
          (0, _formatMessage2.default)('Invalid Items.')
        ),
        '\xA0',
        (0, _formatMessage2.default)('Students will not be able to take this assessment until they are resolved.'),
        _react2.default.createElement(
          _List2.default,
          null,
          invalidPositions.map(function (position) {
            return _react2.default.createElement(
              _List.ListItem,
              { key: position },
              (0, _formatMessage2.default)('question { position, number }', { position: position })
            );
          })
        )
      );
    }
  }, {
    key: 'checkForInvalidQuizEntries',
    value: function checkForInvalidQuizEntries() {
      var invalidPositions = this.getDisplayPositionsForInvalidQuizEntries();
      if (invalidPositions) {
        this.props.addError(this.getInvalidMessage(invalidPositions), false, 'invalid_quiz_alert');
      } else {
        this.props.removeAlert('invalid_quiz_alert');
      }
    }

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

  }, {
    key: 'render',
    value: function render() {
      var _props2 = this.props,
          stimulusOn = _props2.stimulusOn,
          quiz = _props2.quiz;


      if (!this.props.quiz.isLoaded()) {
        return _ref;
      }

      return _react2.default.createElement(
        'div',
        { className: styles.builder },
        _react2.default.createElement(
          _Page2.default,
          {
            contentRole: this.props.contentRole,
            sidebarComponent: _Sidebar2.default,
            sidebarOpen: this.props.sidebarOpen,
            sidebarRole: this.props.sidebarRole,
            toggleSidebar: this.props.toggleSidebar
          },
          _react2.default.createElement(
            _CardWrapper2.default,
            null,
            _react2.default.createElement(_QTIImportModal2.default, {
              qtiImport: quiz.getLatestImport(),
              parentId: quiz.id,
              parentType: _constants.PARENT_TYPE_QUIZ
            }),
            _react2.default.createElement(_ItemFeedbackModal2.default, { parentId: quiz.id, parentType: _constants.PARENT_TYPE_QUIZ }),
            stimulusOn && _ref2,
            _ref3,
            _ref4,
            _react2.default.createElement(
              'div',
              { className: styles.header },
              _react2.default.createElement(_Header2.default, Object.assign({}, this.props, {
                scrollKey: 'header'
              }))
            ),
            _react2.default.createElement(
              _CardContent2.default,
              null,
              _react2.default.createElement(_BuildContent2.default, { onQuizUpdated: this.props.onQuizUpdated })
            ),
            _react2.default.createElement(_BanksTray2.default, {
              getUserInfo: this.props.getUserInfo,
              getShareableUsers: this.props.getShareableUsers
            })
          )
        )
      );
    }
  }]);
  Build.displayName = 'Build'
  ;
  return Build;
}(_react.Component), _class2.propTypes = {
  activeQuizId: _propTypes2.default.string.isRequired, // passed in by consumer
  addError: _propTypes2.default.func.isRequired,
  clearErrorsShowing: _propTypes2.default.func.isRequired,
  contentRole: _propTypes2.default.string,
  contextId: _propTypes2.default.string, // passed in by consumer
  getShareableUsers: _propTypes2.default.func, // passed in by consumer
  getUserInfo: _propTypes2.default.func, // passed in by consumer
  onQuizUpdated: _propTypes2.default.func, // passed in by consumer, optional
  quiz: _reactImmutableProptypes2.default.record.isRequired,
  quizBuildingSession: _propTypes2.default.func.isRequired,
  quizEntries: _reactImmutableProptypes2.default.listOf(_reactImmutableProptypes2.default.record).isRequired,
  removeAlert: _propTypes2.default.func.isRequired,
  sidebarOpen: _propTypes2.default.bool,
  sidebarRole: _propTypes2.default.string,
  stimulusOn: _propTypes2.default.bool.isRequired,
  toggleSidebar: _propTypes2.default.func
}, _class2.defaultProps = {
  contentRole: void 0,
  contextId: null,
  errorsShowing: (0, _immutable.Map)(),
  getShareableUsers: null,
  getUserInfo: null,
  onQuizUpdated: function onQuizUpdated() {},

  sidebarOpen: void 0,
  sidebarRole: void 0,
  toggleSidebar: void 0
}, _temp)) || _class);
exports.default = Build;