"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf3 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _immutable = require("immutable");

var _reactImmutableProptypes = _interopRequireDefault(require("react-immutable-proptypes"));

var _pick = _interopRequireDefault(require("lodash/pick"));

var _partial = _interopRequireDefault(require("lodash/partial"));

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

var _formatMessage = _interopRequireDefault(require("@instructure/quiz-i18n/lib/format-message"));

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

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

var _QuizEntryBankShow = _interopRequireDefault(require("../QuizEntryBank/QuizEntryBankShow"));

var _QuizEntryBankEdit = _interopRequireDefault(require("../QuizEntryBank/QuizEntryBankEdit"));

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

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

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

var _AddToBankOptions = _interopRequireDefault(require("../../AddToBankOptions"));

var _TagDisplay = _interopRequireDefault(require("../../../../../common/components/shared/TagDisplay"));

var _constants = require("@instructure/quiz-common/lib/constants");

var _QuizEntryShow = _interopRequireDefault(require("../QuizEntryShow"));

var _QuizEntryEdit = _interopRequireDefault(require("../QuizEntryEdit"));

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

var _theme = _interopRequireDefault(require("./theme"));

var _dec, _class, _class2, _temp;

var styles = {
  componentId: 'bEUOz',
  template: function template(theme) {
    return "\n\n.bEUOz_cjWK{border-top:".concat(theme.footerBorderWidth || 'inherit', " solid ").concat(theme.footerBorderColor || 'inherit', ";font-size:").concat(theme.footerFontSize || 'inherit', ";overflow:auto;padding:").concat(theme.footerPadding || 'inherit', "}");
  },
  'footer': 'bEUOz_cjWK'
};
var QuizEntry = (_dec = (0, _lib.default)(_theme.default, styles), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(QuizEntry, _Component);

  function QuizEntry() {
    var _getPrototypeOf2;

    var _this;

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

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

    _this = (0, _possibleConstructorReturn2.default)(this, (_getPrototypeOf2 = (0, _getPrototypeOf3.default)(QuizEntry)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _this.openRegradeModal = function () {
      if (_this.props.regradingOn) {
        _this.props.openModal(_constants.REGRADE_MODAL);
      }
    };

    _this.renderItemBankingOptions = function () {
      return _react.default.createElement(_AddToBankOptions.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());
    };

    return _this;
  }

  (0, _createClass2.default)(QuizEntry, [{
    key: "additionalEditOptions",
    value: function additionalEditOptions(item) {
      var options = [];

      if (this.props.quizEntry.entryType === 'Item') {
        options.push({
          key: 'banks',
          title: "Legge til elementer i banken",
          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, _pick.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 _this$props = this.props,
          entry = _this$props.entry,
          shouldClone = _this$props.shouldClone,
          quizEntry = _this$props.quizEntry;
      var QuizEntryComponent = isEditing ? _QuizEntryEdit.default : _QuizEntryShow.default;
      var ItemComponent = isEditing && (!entry.isImmutable() || shouldClone) ? _ItemEdit.default : _ItemShow.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, _partial.default)(this.props.setUi, _constants.SHOULD_CLONE_ITEM), this.props.switchOffEditing, isEditing, quizEntry.isArchivedBankEntry ? 'info' : 'warning');
      return _react.default.createElement(QuizEntryComponent, quizEntryProps, _react.default.createElement(_WarningWrapper.default, propsForItemVersioning, _react.default.createElement(ItemComponent, itemProps)));
    }
  }, {
    key: "renderBank",
    value: function renderBank() {
      var QuizEntryComponent = this.props.isEditing ? _QuizEntryEdit.default : _QuizEntryShow.default;
      var QuizEntryBankComponent = this.props.isEditing ? _QuizEntryBankEdit.default : _QuizEntryBankShow.default;
      var bank = this.props.entry;
      var bankProps = {
        title: bank.title,
        entry: bank,
        name: bank.title
      };
      return _react.default.createElement(QuizEntryComponent, Object.assign({}, this.props, bankProps), _react.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() ? _QuizEntryEdit.default : _QuizEntryShow.default;
      var itemProps = this.propsForItem();
      var mergedItem = this.isEditing() ? this.props.quizEntry.getWorkingInstance().getItem() : itemProps.entry;
      var ItemComponent = this.isOverriding() ? _ItemOverride.default : _ItemShow.default;
      return _react.default.createElement(QuizEntryComponent, quizEntryProps, _react.default.createElement(_WarningWrapper.default, Object.assign({}, bankEntryWarningWrapperProps, {
        disableChildren: this.isEditing() && !this.isOverridable()
      }), _react.default.createElement(ItemComponent, Object.assign({}, itemProps, {
        entry: mergedItem,
        quizEntry: this.props.quizEntry
      }))), this.renderFooter());
    }
  }, {
    key: "renderStimulus",
    value: function renderStimulus(bankEntryWarningWrapperProps) {
      return _react.default.createElement(_Stimulus.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: "renderTags",
    value: function renderTags() {
      return _react.default.createElement(_TagDisplay.default, {
        tagAssociations: this.props.tagAssociations
      });
    }
  }, {
    key: "renderFooter",
    value: function renderFooter() {
      if (!this.props.useBankTags) {
        return null;
      } // Footer currently only contains tags, so don't render if no tags


      if (this.props.tagAssociations == null || this.props.tagAssociations.isEmpty()) {
        return null;
      }

      return _react.default.createElement("div", {
        className: styles.footer
      }, this.renderTags());
    }
  }, {
    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(_react.default.createElement("div", {
        tabIndex: "-1"
      }, this.renderQuizEntryByType()));
    }
  }]);
  QuizEntry.displayName = "QuizEntry";
  return QuizEntry;
}(_react.Component), _class2.propTypes = {
  changeQuizEntryPoints: _propTypes.default.func.isRequired,
  childEntries: _reactImmutableProptypes.default.listOf(_reactImmutableProptypes.default.record),
  connectDropTarget: _propTypes.default.func.isRequired,
  // injected by DnD
  displayPosition: _propTypes.default.number.isRequired,
  entry: _reactImmutableProptypes.default.record.isRequired,
  guid: _propTypes.default.string.isRequired,
  initialFocusId: _propTypes.default.string,
  isEditing: _propTypes.default.bool,
  draggingOnBuild: _propTypes.default.bool,
  newActiveBank: _propTypes.default.func.isRequired,
  newActiveBankEntry: _propTypes.default.func.isRequired,
  newHoverPosition: _propTypes.default.func.isRequired,
  // used for DnD
  openModal: _propTypes.default.func.isRequired,
  propsForQuizEntry: _propTypes.default.func.isRequired,
  quizEntries: _reactImmutableProptypes.default.listOf(_reactImmutableProptypes.default.record).isRequired,
  quizEntry: _reactImmutableProptypes.default.record.isRequired,
  quizId: _propTypes.default.string.isRequired,
  regradingOn: _propTypes.default.bool.isRequired,
  screenreaderNotification: _propTypes.default.func.isRequired,
  scrollKey: _propTypes.default.string,
  setUi: _propTypes.default.func.isRequired,
  shouldClone: _propTypes.default.bool,
  showError: _propTypes.default.func.isRequired,
  switchOffEditing: _propTypes.default.func.isRequired,
  switchOnEditing: _propTypes.default.func.isRequired,
  tagAssociations: _reactImmutableProptypes.default.listOf(_reactImmutableProptypes.default.map),
  useBankTags: _propTypes.default.bool,
  workingEntry: _reactImmutableProptypes.default.record.isRequired,
  editQuizEntryToggle: _propTypes.default.bool.isRequired,
  onEditQuizEntryToggle: _propTypes.default.func.isRequired
}, _class2.defaultProps = {
  childEntries: (0, _immutable.List)(),
  draggingAboveHalf: false,
  draggingOnBuild: false,
  draggingOver: false,
  initialFocusId: null,
  isEditing: false,
  scrollKey: void 0,
  shouldClone: false,
  tagAssociations: (0, _immutable.List)(),
  useBankTags: false
}, _class2.QUIZ_ENTRY_ITEM_OVERRIDE_TYPES = ['choice'], _temp)) || _class);
exports.default = QuizEntry;