"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 platform = _interopRequireWildcard(require("platform"));

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

var _first = _interopRequireDefault(require("lodash/first"));

var _Button = _interopRequireDefault(require("@instructure/ui-buttons/lib/components/Button"));

var _CloseButton = _interopRequireDefault(require("@instructure/ui-buttons/lib/components/CloseButton"));

var _Heading = _interopRequireDefault(require("@instructure/ui-elements/lib/components/Heading"));

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

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

var _InteractionTypes = _interopRequireDefault(require("../../../common/components/shared/InteractionTypes"));

var _Modal = _interopRequireWildcard(require("@instructure/ui-overlays/lib/components/Modal"));

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

  function AddBankEntryModal() {
    var _getPrototypeOf2;

    var _this;

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

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

    _this.addItem = function (interactionTypeData) {
      var defaultInteractionData = interactionTypeData.getDefaultInteractionData();
      var scoringData = interactionTypeData.getDefaultScoringData(defaultInteractionData);
      var itemProps = {
        id: interactionTypeData.id,
        interactionData: defaultInteractionData,
        itemBody: interactionTypeData.itemBody,
        properties: interactionTypeData.defaultProperties,
        scoringAlgorithm: (0, _first.default)(interactionTypeData.scoringAlgorithmOptions),
        scoringData: scoringData,
        userResponseType: (0, _first.default)(interactionTypeData.userResponseTypeOptions)
      };

      _this.props.createBankItem(_this.props.bankId, 'Item', itemProps);

      _this.closeAction();

      _this.props.navigateToBankEntry('new', _this.props.bankId);
    };

    _this.addStimulus = function () {
      _this.props.createBankItem(_this.props.bankId, 'Stimulus');

      _this.closeAction();

      _this.props.navigateToBankEntry('new', _this.props.bankId);
    };

    _this.closeAction = function () {
      if (_this.props.modalTrigger) {
        _this.props.modalTrigger.focus();
      }

      _this.props.closeModal();
    };

    return _this;
  }

  (0, _createClass2.default)(AddBankEntryModal, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.props.getInteractionTypes();
    }
  }, {
    key: "cancelButton",
    value: function cancelButton() {
      return _react.default.createElement(_Button.default, {
        onClick: this.closeAction,
        margin: _constants.XSMALL_SIDE_MARGIN
      }, "Fermer");
    }
  }, {
    key: "render",
    value: function render() {
      // For some reason, there's obscure behavior on IE11 that makes Modal size='auto'
      // behave poorly with instui Grid; see https://instructure.atlassian.net/browse/QUIZ-4100.
      // This is just a simple workaround.
      var size = platform.name === 'IE' && platform.version === '11.0' ? 'medium' : 'auto';
      return _react.default.createElement(_Modal.default, {
        open: this.props.modalOpen,
        onDismiss: this.closeAction,
        size: size,
        label: "Nouvel \xE9l\xE9ment"
      }, _react.default.createElement(_Modal.ModalHeader, null, _react.default.createElement(_Heading.default, {
        level: "reset",
        as: "h2"
      }, "Nouvel \xE9l\xE9ment"), _react.default.createElement(_CloseButton.default, {
        onClick: this.closeAction,
        placement: "end",
        offset: "medium",
        variant: "icon"
      })), _react.default.createElement(_Modal.ModalBody, null, _react.default.createElement(_InteractionTypes.default, {
        columns: 3,
        addItem: this.addItem,
        addStimulus: this.addStimulus,
        allowStimulusCreation: this.props.stimulusBankingOn,
        types: this.props.interactionTypes
      })), _react.default.createElement(_Modal.ModalFooter, null, this.cancelButton()));
    }
  }]);
  AddBankEntryModal.displayName = "AddBankEntryModal";
  return AddBankEntryModal;
}(_react.Component);

exports.default = AddBankEntryModal;
AddBankEntryModal.propTypes = {
  modalOpen: _propTypes.default.bool.isRequired,
  closeModal: _propTypes.default.func.isRequired,
  modalTrigger: _propTypes.default.instanceOf(Element),
  createBankItem: _propTypes.default.func.isRequired,
  navigateToBankEntry: _propTypes.default.func.isRequired,
  bankId: _propTypes.default.string.isRequired,
  getInteractionTypes: _propTypes.default.func.isRequired,
  interactionTypes: _reactImmutableProptypes.default.list.isRequired,
  stimulusBankingOn: _propTypes.default.bool.isRequired
};
AddBankEntryModal.defaultProps = {
  modalTrigger: null
};