"use strict";

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

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

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 _reactImmutableProptypes = _interopRequireDefault(require("react-immutable-proptypes"));

var _OutcomeAlignments = _interopRequireDefault(require("outcomes-react/lib/containers/OutcomeAlignments"));

var _OutcomeCount = _interopRequireDefault(require("outcomes-react/lib/containers/OutcomeCount"));

var _FormFieldGroup = _interopRequireDefault(require("@instructure/ui-forms/lib/components/FormFieldGroup"));

var _ScreenReaderContent = _interopRequireDefault(require("@instructure/ui-a11y/lib/components/ScreenReaderContent"));

var _CustomPropTypes = _interopRequireDefault(require("../../../../../common/util/CustomPropTypes"));

var _componentForItem = _interopRequireDefault(require("../../../../util/componentForItem"));

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

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

var _interactionTypePropsHelper = require("../../../../util/interactionTypePropsHelper");

var _RceImageImportModal = _interopRequireDefault(require("../../../../components/RichContentInput/RceImageImportModal"));

var _ElementsForSelectors = _interopRequireDefault(require("../../../../../common/util/ElementsForSelectors"));

var _instUIMessages = require("../../../../util/instUIMessages");

var ItemEdit =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(ItemEdit, _Component);

  function ItemEdit() {
    var _getPrototypeOf2;

    var _this;

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

    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)(ItemEdit)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _this.onOutcomeAlignmentSetChange = function (outcomeAlignmentSet) {
      var item = _this.props.entry;

      if (item.outcomeAlignmentSetGuid !== outcomeAlignmentSet.guid) {
        return _this.props.changeOutcomeAlignmentSetGuid(item.id, outcomeAlignmentSet.guid);
      }

      return null;
    };

    _this.applicationElement = function () {
      return (0, _ElementsForSelectors.default)(_this.props.appContainer);
    };

    _this.onModalOpen = function () {
      _this.props.occludeQuizEntries(true);
    };

    _this.onModalClose = function () {
      _this.props.occludeQuizEntries(false);
    };

    _this.getError = function (field) {
      if (_this.props.errorsAreShowing) {
        var errors = _this.props.workingItem.getErrors().toJS();

        return (0, _instUIMessages.toErrors)(errors[field] || []);
      }
    };

    _this.renderImportModal = function (importModalId, onSuccess, onClose) {
      return _react.default.createElement(_RceImageImportModal.default, {
        importModalId: importModalId,
        onClose: onClose,
        onSuccess: onSuccess,
        parentId: _this.props.parentId,
        parentType: _this.props.parentType
      });
    };

    return _this;
  }

  (0, _createClass2.default)(ItemEdit, [{
    key: "additionalEditOptions",
    value: function additionalEditOptions() {
      var _this$props = this.props,
          guid = _this$props.guid,
          workingItem = _this$props.workingItem;
      var itemId = guid || workingItem.get('id');
      var outcomeAlignmentSetGuid = workingItem.get('outcomeAlignmentSetGuid');
      var options = [];

      if (this.props.outcomesEndpoint) {
        options.push({
          key: 'outcomes',
          title: _react.default.createElement("span", null, "\u5BF9\u9F50\u5230\u7ED3\u679C", " ", _react.default.createElement(_OutcomeCount.default, {
            alignmentSetId: outcomeAlignmentSetGuid,
            artifactType: "quizEntry",
            artifactId: itemId,
            contextUuid: this.props.contextUuid,
            host: this.props.outcomesEndpoint,
            jwt: this.props.outcomesToken
          })),
          component: _react.default.createElement(_OutcomeAlignments.default, {
            artifactType: "quizEntry",
            artifactId: itemId,
            applicationElement: this.applicationElement,
            alignmentSetId: outcomeAlignmentSetGuid,
            contextUuid: this.props.contextUuid,
            emptySetHeading: "\u5C06\u673A\u6784\u7ED3\u679C\u5BF9\u9F50\u5230\u6B64\u95EE\u9898\u3002",
            host: this.props.outcomesEndpoint,
            jwt: this.props.outcomesToken,
            onModalOpen: this.onModalOpen,
            onModalClose: this.onModalClose,
            onUpdate: this.onOutcomeAlignmentSetChange
          })
        });
      }

      if (this.props.additionalOptions) {
        return options.concat(this.props.additionalOptions);
      }

      return options;
    }
  }, {
    key: "render",
    value: function render() {
      var item = this.props.entry;
      var interactionType = item.getInteractionType().slug;
      var Interaction = (0, _componentForItem.default)(interactionType, _constants.COMPONENT_EDIT);
      var propsForInteraction = (0, _interactionTypePropsHelper.propsForInteractionEdit)(this.props, this.additionalEditOptions());
      return _react.default.createElement(_FormFieldGroup.default, {
        messages: this.getError('onSelf'),
        description: _react.default.createElement(_ScreenReaderContent.default, null, "\u7F16\u8F91\u9879\u76EE\uFF1A"),
        name: "edit_item_".concat(this.props.guid) // eslint-disable-line react/jsx-no-literals

      }, _react.default.createElement(Interaction, Object.assign({}, propsForInteraction, {
        itemId: this.props.guid,
        renderImportModal: this.renderImportModal
      })));
    }
  }]);
  ItemEdit.displayName = "ItemEdit";
  return ItemEdit;
}(_react.Component);

exports.default = ItemEdit;
ItemEdit.propTypes = {
  additionalOptions: _propTypes.default.arrayOf(_propTypes.default.shape({
    key: _propTypes.default.string.isRequired,
    title: _propTypes.default.string.isRequired,
    component: _propTypes.default.node.isRequired
  })),
  appContainer: _CustomPropTypes.default.selectors.isRequired,
  changeOutcomeAlignmentSetGuid: _propTypes.default.func.isRequired,
  entry: _reactImmutableProptypes.default.record.isRequired,
  guid: _propTypes.default.string.isRequired,
  occludeQuizEntries: _propTypes.default.func.isRequired,
  contextUuid: _propTypes.default.string,
  outcomesEndpoint: _propTypes.default.string,
  outcomesToken: _propTypes.default.string,
  workingItem: _reactImmutableProptypes.default.record.isRequired,
  // required by propsForInteractionEdit
  addError: _propTypes.default.func.isRequired,
  changeItemState: _propTypes.default.func.isRequired,
  errorsAreShowing: _propTypes.default.bool,
  handleQuizEntryPointsChange: _propTypes.default.func.isRequired,
  interactionFileUpload: _propTypes.default.func.isRequired,
  oneQuestionAtATime: _propTypes.default.bool.isRequired,
  openImportModal: _propTypes.default.func.isRequired,
  parentId: _propTypes.default.string.isRequired,
  parentType: _propTypes.default.string.isRequired,
  quizId: _propTypes.default.string.isRequired,
  screenreaderNotification: _propTypes.default.func,
  setOneQuestionAtATime: _propTypes.default.func.isRequired,
  setUploadStatus: _propTypes.default.func.isRequired,
  initialFocusId: _propTypes.default.string,
  showCalculator: _propTypes.default.bool
};
ItemEdit.defaultProps = {
  additionalOptions: [],
  contextUuid: null,
  errorsAreShowing: false,
  outcomesEndpoint: null,
  outcomesToken: null,
  screenreaderNotification: false,
  initialFocusId: null,
  showCalculator: false
};