'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 _OutcomeAlignments = require('outcomes-react/lib/containers/OutcomeAlignments');

var _OutcomeAlignments2 = _interopRequireDefault(_OutcomeAlignments);

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

var _OutcomeCount2 = _interopRequireDefault(_OutcomeCount);

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

var _FormFieldGroup2 = _interopRequireDefault(_FormFieldGroup);

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

var _ScreenReaderContent2 = _interopRequireDefault(_ScreenReaderContent);

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

var _CustomPropTypes2 = _interopRequireDefault(_CustomPropTypes);

var _componentForItem = require('../../../../util/componentForItem');

var _componentForItem2 = _interopRequireDefault(_componentForItem);

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

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

var _formatMessage2 = _interopRequireDefault(_formatMessage);

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

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

var _RceImageImportModal2 = _interopRequireDefault(_RceImageImportModal);

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

var _ElementsForSelectors2 = _interopRequireDefault(_ElementsForSelectors);

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

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

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

  function ItemEdit() {
    var _ref;

    var _temp, _this, _ret;

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

    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 = ItemEdit.__proto__ || Object.getPrototypeOf(ItemEdit)).call.apply(_ref, [this].concat(args))), _this), _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, _ElementsForSelectors2.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 _react2.default.createElement(_RceImageImportModal2.default, {
        importModalId: importModalId,
        onClose: onClose,
        onSuccess: onSuccess,
        parentId: _this.props.parentId,
        parentType: _this.props.parentType
      });
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(ItemEdit, [{
    key: 'additionalEditOptions',
    value: function additionalEditOptions() {
      var _props = this.props,
          guid = _props.guid,
          workingItem = _props.workingItem;

      var itemId = guid || workingItem.get('id');
      var outcomeAlignmentSetGuid = workingItem.get('outcomeAlignmentSetGuid');
      var options = [];
      if (this.props.outcomesEndpoint) {
        options.push({
          key: 'outcomes',
          title: _react2.default.createElement(
            'span',
            null,
            'Knytt til l\xE6ringsm\xE5l',
            ' ',
            _react2.default.createElement(_OutcomeCount2.default, {
              alignmentSetId: outcomeAlignmentSetGuid,
              artifactType: 'quizEntry',
              artifactId: itemId,
              contextUuid: this.props.contextUuid,
              host: this.props.outcomesEndpoint,
              jwt: this.props.outcomesToken
            })
          ),
          component: _react2.default.createElement(_OutcomeAlignments2.default, {
            artifactType: 'quizEntry',
            artifactId: itemId,
            applicationElement: this.applicationElement,
            alignmentSetId: outcomeAlignmentSetGuid,
            contextUuid: this.props.contextUuid,
            emptySetHeading: 'Knytt l\xE6ringsm\xE5l til dette sp\xF8rsm\xE5let.',
            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, _componentForItem2.default)(interactionType, _constants.COMPONENT_EDIT);
      var propsForInteraction = (0, _interactionTypePropsHelper.propsForInteractionEdit)(this.props, this.additionalEditOptions());

      return _react2.default.createElement(
        _FormFieldGroup2.default,
        {
          messages: this.getError('onSelf'),
          description: _react2.default.createElement(
            _ScreenReaderContent2.default,
            null,
            'Rediger gjenstand:'
          ),
          name: 'edit_item_' + this.props.guid
        },
        _react2.default.createElement(Interaction, Object.assign({}, propsForInteraction, {
          itemId: this.props.guid,
          renderImportModal: this.renderImportModal
        }))
      );
    }
  }]);
  ItemEdit.displayName = 'ItemEdit'
  ;
  return ItemEdit;
}(_react.Component), _class.propTypes = {
  additionalOptions: _propTypes2.default.arrayOf(_propTypes2.default.shape({
    key: _propTypes2.default.string.isRequired,
    title: _propTypes2.default.string.isRequired,
    component: _propTypes2.default.node.isRequired
  })),
  appContainer: _CustomPropTypes2.default.selectors.isRequired,
  changeOutcomeAlignmentSetGuid: _propTypes2.default.func.isRequired,
  entry: _reactImmutableProptypes2.default.record.isRequired,
  guid: _propTypes2.default.string.isRequired,
  occludeQuizEntries: _propTypes2.default.func.isRequired,
  contextUuid: _propTypes2.default.string,
  outcomesEndpoint: _propTypes2.default.string,
  outcomesToken: _propTypes2.default.string,
  workingItem: _reactImmutableProptypes2.default.record.isRequired,
  // required by propsForInteractionEdit
  addError: _propTypes2.default.func.isRequired,
  changeItemState: _propTypes2.default.func.isRequired,
  errorsAreShowing: _propTypes2.default.bool,
  handleQuizEntryPointsChange: _propTypes2.default.func.isRequired,
  interactionFileUpload: _propTypes2.default.func.isRequired,
  openImportModal: _propTypes2.default.func.isRequired,
  parentId: _propTypes2.default.string.isRequired,
  parentType: _propTypes2.default.string.isRequired,
  screenreaderNotification: _propTypes2.default.func,
  setUploadStatus: _propTypes2.default.func.isRequired,
  initialFocusId: _propTypes2.default.string
}, _class.defaultProps = {
  additionalOptions: [],
  contextUuid: null,
  errorsAreShowing: false,
  outcomesEndpoint: null,
  outcomesToken: null,
  screenreaderNotification: false,
  initialFocusId: null
}, _temp2);
exports.default = ItemEdit;