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

var _reactImmutableProptypes = require('react-immutable-proptypes');

var _reactImmutableProptypes2 = _interopRequireDefault(_reactImmutableProptypes);

var _pick = require('lodash/pick');

var _pick2 = _interopRequireDefault(_pick);

var _partial = require('lodash/partial');

var _partial2 = _interopRequireDefault(_partial);

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

var _formatMessage2 = _interopRequireDefault(_formatMessage);

var _WarningWrapper = require('../../../../../common/components/resources/WarningWrapper');

var _WarningWrapper2 = _interopRequireDefault(_WarningWrapper);

var _warningHelpers = require('../../../../../common/util/warningHelpers');

var _QuizEntryBankShow = require('../QuizEntryBank/QuizEntryBankShow');

var _QuizEntryBankShow2 = _interopRequireDefault(_QuizEntryBankShow);

var _QuizEntryBankEdit = require('../QuizEntryBank/QuizEntryBankEdit');

var _QuizEntryBankEdit2 = _interopRequireDefault(_QuizEntryBankEdit);

var _ItemEdit = require('../../../../../common/components/resources/item/ItemEdit');

var _ItemEdit2 = _interopRequireDefault(_ItemEdit);

var _ItemShow = require('../../../../../common/components/resources/item/ItemShow');

var _ItemShow2 = _interopRequireDefault(_ItemShow);

var _ItemOverride = require('../../../../../common/components/resources/item/ItemOverride');

var _ItemOverride2 = _interopRequireDefault(_ItemOverride);

var _AddToBankOptions = require('../../AddToBankOptions');

var _AddToBankOptions2 = _interopRequireDefault(_AddToBankOptions);

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

var _QuizEntryShow = require('../QuizEntryShow');

var _QuizEntryShow2 = _interopRequireDefault(_QuizEntryShow);

var _QuizEntryEdit = require('../QuizEntryEdit');

var _QuizEntryEdit2 = _interopRequireDefault(_QuizEntryEdit);

var _Stimulus = require('../../../../../common/components/resources/stimulus/Stimulus');

var _Stimulus2 = _interopRequireDefault(_Stimulus);

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

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

  function QuizEntry() {
    var _ref;

    var _temp, _this, _ret;

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

    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 = QuizEntry.__proto__ || Object.getPrototypeOf(QuizEntry)).call.apply(_ref, [this].concat(args))), _this), _this.openRegradeModal = function () {
      if (_this.props.regradingOn) {
        _this.props.openModal(_constants.REGRADE_MODAL);
      }
    }, _this.renderItemBankingOptions = function () {
      return _react2.default.createElement(_AddToBankOptions2.default, {
        displayPosition: _this.props.displayPosition,
        entryType: _this.props.quizEntry.entryType,
        guid: _this.props.guid,
        isValid: _this.props.workingEntry.isValid(),
        openModal: _this.props.openModal,
        screenreaderNotification: _this.props.screenreaderNotification,
        showError: _this.props.showError,
        'data-automation': 'sdk-item-banking-options'
      });
    }, _this.storeDndComponentRef = function (node) {
      _this.dndComponent = node;
    }, _this.handleQuizEntryPointsChange = function (points) {
      _this.props.changeQuizEntryPoints(_this.props.quizEntry.id, points);
    }, _this.bankEntryWarningWrapperProps = function () {
      if (_this.props.quizEntry.entryEditable) {
        var propsFunc = _this.props.quizEntry.hasStimulus ? _warningHelpers.propsForStimulusBankEntryEditPrompt : _warningHelpers.propsForItemBankEntryEditPrompt;
        return propsFunc(_this.isEditing(), _this.props.switchOffEditing, function () {
          var bankEntry = _this.props.quizEntry.getEntry();
          _this.props.setUi(_constants.BUILD_TRAY_OPEN, true);
          _this.props.newActiveBankEntry(bankEntry.id, bankEntry.bankId);
          _this.props.newActiveBank(bankEntry.bankId);
          _this.props.switchOffEditing();
        });
      }
      return (0, _warningHelpers.propsForBankEntryUnauthorizedEdit)(_this.isEditing());
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(QuizEntry, [{
    key: 'additionalEditOptions',
    value: function additionalEditOptions(item) {
      var options = [];
      if (this.props.quizEntry.entryType === 'Item') {
        options.push({
          key: 'banks',
          title: 'Mise en banque d\'\xE9l\xE9ment',
          component: this.renderItemBankingOptions()
        });
      }
      return options;
    }
  }, {
    key: 'propsForItem',
    value: function propsForItem() {
      var item = this.props.entry;

      return {
        additionalOptions: this.additionalEditOptions(item),
        entry: item,
        guid: this.props.guid,
        handleQuizEntryPointsChange: this.handleQuizEntryPointsChange,
        parentId: this.props.quizId,
        parentType: _constants.PARENT_TYPE_QUIZ,
        switchOffEditing: this.props.switchOffEditing,
        switchOnEditing: this.props.switchOnEditing,
        title: item.title,
        initialFocusId: this.props.initialFocusId
      };
    }
  }, {
    key: 'quizEntryProps',
    value: function quizEntryProps() {
      var entry = this.props.entry;

      return Object.assign({
        entry: entry,
        openRegradeModal: this.openRegradeModal,
        name: entry.getInteractionType().name,
        initialFocusId: this.props.initialFocusId
      }, (0, _pick2.default)(this.props, ['displayPosition', 'guid', 'draggingOnBuild', 'newHoverPosition', 'quizEntry', 'quizEntries', 'quizId', 'setUi', 'switchOffEditing', 'switchOnEditing', 'shouldClone', 'scrollKey', 'editQuizEntryToggle', 'onEditQuizEntryToggle']));
    }
  }, {
    key: 'isEditing',
    value: function isEditing() {
      var isTemporary = this.props.entry.isTemporary && this.props.entry.isTemporary();
      return this.props.isEditing || isTemporary;
    }
  }, {
    key: 'renderItem',
    value: function renderItem() {
      var isEditing = this.isEditing();
      var _props = this.props,
          entry = _props.entry,
          shouldClone = _props.shouldClone,
          quizEntry = _props.quizEntry;

      var QuizEntryComponent = isEditing ? _QuizEntryEdit2.default : _QuizEntryShow2.default;
      var ItemComponent = isEditing && (!entry.isImmutable() || shouldClone) ? _ItemEdit2.default : _ItemShow2.default;
      var itemProps = this.propsForItem();
      var quizEntryProps = this.quizEntryProps();
      var propsForWrapperFunc = quizEntry.isArchivedBankEntry ? _warningHelpers.propsForBankEntryVersioningWrapper : _warningHelpers.propsForItemVersioningWrapper;
      var propsForItemVersioning = propsForWrapperFunc(entry.isImmutable() || quizEntry.isArchivedBankEntry, shouldClone, (0, _partial2.default)(this.props.setUi, _constants.SHOULD_CLONE_ITEM), this.props.switchOffEditing, isEditing, quizEntry.isArchivedBankEntry ? 'info' : 'warning');
      return _react2.default.createElement(
        QuizEntryComponent,
        quizEntryProps,
        _react2.default.createElement(
          _WarningWrapper2.default,
          propsForItemVersioning,
          _react2.default.createElement(ItemComponent, itemProps)
        )
      );
    }
  }, {
    key: 'renderBank',
    value: function renderBank() {
      var QuizEntryComponent = this.props.isEditing ? _QuizEntryEdit2.default : _QuizEntryShow2.default;
      var QuizEntryBankComponent = this.props.isEditing ? _QuizEntryBankEdit2.default : _QuizEntryBankShow2.default;

      var bank = this.props.entry;
      var bankProps = {
        title: bank.title,
        entry: bank,
        name: bank.title
      };
      return _react2.default.createElement(
        QuizEntryComponent,
        Object.assign({}, this.props, bankProps),
        _react2.default.createElement(QuizEntryBankComponent, Object.assign({}, this.props, bankProps))
      );
    }
  }, {
    key: 'isOverridable',
    value: function isOverridable() {
      if (typeof this.props.entry.getInteractionType !== 'function') return false;
      return QuizEntry.QUIZ_ENTRY_ITEM_OVERRIDE_TYPES.includes(this.props.entry.getInteractionType().slug);
    }
  }, {
    key: 'isOverriding',
    value: function isOverriding() {
      return this.isOverridable() && this.isEditing();
    }
  }, {
    key: 'renderBankEntry',
    value: function renderBankEntry() {
      var bankEntryWarningWrapperProps = this.bankEntryWarningWrapperProps();

      if (this.props.quizEntry.hasStimulus) {
        return this.renderStimulus(this.bankEntryWarningWrapperProps);
      }

      var overrideProps = {
        title: this.props.entry.title,
        isBankedContent: true
      };
      var quizEntryProps = Object.assign({}, this.props, overrideProps);
      var QuizEntryComponent = this.isEditing() ? _QuizEntryEdit2.default : _QuizEntryShow2.default;
      var itemProps = this.propsForItem();
      var mergedItem = this.props.quizEntry.getWorkingInstance().getItem();
      var ItemComponent = this.isOverriding() ? _ItemOverride2.default : _ItemShow2.default;

      return _react2.default.createElement(
        QuizEntryComponent,
        quizEntryProps,
        _react2.default.createElement(
          _WarningWrapper2.default,
          Object.assign({}, bankEntryWarningWrapperProps, { disableChildren: this.isEditing() && !this.isOverridable() }),
          _react2.default.createElement(ItemComponent, Object.assign({}, itemProps, { entry: mergedItem, quizEntry: this.props.quizEntry }))
        )
      );
    }
  }, {
    key: 'renderStimulus',
    value: function renderStimulus(bankEntryWarningWrapperProps) {
      return _react2.default.createElement(_Stimulus2.default, {
        bankEntryWarningWrapperProps: bankEntryWarningWrapperProps,
        childEntries: this.props.childEntries,
        displayPosition: this.props.displayPosition,
        dndRef: this.storeDndComponentRef,
        guid: this.props.guid,
        isBankedContent: bankEntryWarningWrapperProps !== void 0,
        isEditing: this.isEditing(),
        draggingOnBuild: this.props.draggingOnBuild,
        newHoverPosition: this.props.newHoverPosition // used for DnD
        , propsForQuizEntry: this.props.propsForQuizEntry,
        quizEntry: this.props.quizEntry,
        quizEntries: this.props.quizEntries,
        quizId: this.props.quizId,
        scrollKey: this.props.scrollKey,
        shouldClone: this.props.shouldClone,
        stimulus: this.props.entry,
        switchOnEditing: this.props.switchOnEditing,
        switchOffEditing: this.props.switchOffEditing,
        editStimulusToggle: this.props.editQuizEntryToggle,
        onEditStimulusToggle: this.props.onEditQuizEntryToggle
      });
    }
  }, {
    key: 'renderQuizEntryByType',
    value: function renderQuizEntryByType() {
      if (this.props.quizEntry.isArchivedBankEntry) {
        if (this.props.quizEntry.hasStimulus) {
          return this.renderStimulus();
        } else {
          return this.renderItem();
        }
      }
      if (this.props.quizEntry.isItem) {
        return this.renderItem();
      } else if (this.props.quizEntry.isBankEntry) {
        return this.renderBankEntry();
      } else if (this.props.quizEntry.isBank) {
        return this.renderBank();
      } else if (this.props.quizEntry.isStimulus) {
        return this.renderStimulus();
      }
      return null;
    }
  }, {
    key: 'render',
    value: function render() {
      return this.props.connectDropTarget(_react2.default.createElement(
        'div',
        { tabIndex: '-1' },
        this.renderQuizEntryByType()
      ));
    }
  }]);
  QuizEntry.displayName = 'QuizEntry'
  ;
  return QuizEntry;
}(_react.Component), _class.propTypes = {
  changeQuizEntryPoints: _propTypes2.default.func.isRequired,
  childEntries: _reactImmutableProptypes2.default.listOf(_reactImmutableProptypes2.default.record),
  connectDropTarget: _propTypes2.default.func.isRequired, // injected by DnD
  displayPosition: _propTypes2.default.number.isRequired,
  entry: _reactImmutableProptypes2.default.record.isRequired,
  guid: _propTypes2.default.string.isRequired,
  initialFocusId: _propTypes2.default.string,
  isEditing: _propTypes2.default.bool,
  draggingOnBuild: _propTypes2.default.bool,
  newActiveBank: _propTypes2.default.func.isRequired,
  newActiveBankEntry: _propTypes2.default.func.isRequired,
  newHoverPosition: _propTypes2.default.func.isRequired, // used for DnD
  openModal: _propTypes2.default.func.isRequired,
  propsForQuizEntry: _propTypes2.default.func.isRequired,
  quizEntries: _reactImmutableProptypes2.default.listOf(_reactImmutableProptypes2.default.record).isRequired,
  quizEntry: _reactImmutableProptypes2.default.record.isRequired,
  quizId: _propTypes2.default.string.isRequired,
  regradingOn: _propTypes2.default.bool.isRequired,
  screenreaderNotification: _propTypes2.default.func.isRequired,
  scrollKey: _propTypes2.default.string,
  setUi: _propTypes2.default.func.isRequired,
  shouldClone: _propTypes2.default.bool,
  showError: _propTypes2.default.func.isRequired,
  switchOffEditing: _propTypes2.default.func.isRequired,
  switchOnEditing: _propTypes2.default.func.isRequired,
  workingEntry: _reactImmutableProptypes2.default.record.isRequired,
  editQuizEntryToggle: _propTypes2.default.bool.isRequired,
  onEditQuizEntryToggle: _propTypes2.default.func.isRequired
}, _class.defaultProps = {
  childEntries: (0, _immutable.List)(),
  draggingAboveHalf: false,
  draggingOnBuild: false,
  draggingOver: false,
  initialFocusId: null,
  isEditing: false,
  scrollKey: void 0,
  shouldClone: false
}, _class.QUIZ_ENTRY_ITEM_OVERRIDE_TYPES = ['choice'], _temp2);
exports.default = QuizEntry;