'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 _dec,
    _class,
    _class2,
    _temp2,
    _default_pl_number = Intl.NumberFormat('pl', { style: 'decimal' }).format;

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 _first = require('lodash/first');

var _first2 = _interopRequireDefault(_first);

var _IconBank = require('@instructure/ui-icons/lib/Line/IconBank');

var _IconBank2 = _interopRequireDefault(_IconBank);

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

var _Button2 = _interopRequireDefault(_Button);

var _View = require('@instructure/ui-layout/lib/components/View');

var _View2 = _interopRequireDefault(_View);

var _Grid = require('@instructure/ui-layout/lib/components/Grid');

var _Grid2 = _interopRequireDefault(_Grid);

var _Text = require('@instructure/ui-elements/lib/components/Text');

var _Text2 = _interopRequireDefault(_Text);

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

var _lib2 = _interopRequireDefault(_lib);

var _InteractionTypes = require('../../../../shared/InteractionTypes');

var _InteractionTypes2 = _interopRequireDefault(_InteractionTypes);

var _formatMessage = require('@instructure/quiz-common/lib/format-message');

var _formatMessage2 = _interopRequireDefault(_formatMessage);

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

var _theme = require('./theme');

var _theme2 = _interopRequireDefault(_theme);

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

var styles = {
  template: function template(theme) {
    return '/*  imported from styles.css  */\n\n.Body__styles__popoverHeaderBox {\n  border-bottom: solid ' + theme.popoverHeaderBoxBorderWidth + ' ' + theme.popoverHeaderBoxBorderColor + ';\n}\n\n.Body__styles__popoverHeader {\n  margin: 1rem;\n  margin-bottom: 0.25rem;\n}\n\n.Body__styles__popoverContent {\n  margin-left: ' + theme.popoverContentMargin + ';\n  margin-right: ' + theme.popoverContentMargin + ';\n}\n\n.Body__styles__piggyBank {\n  font-size: x-large;\n}\n\n/* IE11 only */\n\n@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {\n  .Body__styles__popoverContent {\n    width: 22rem;\n  }\n}\n\n/* IE11 only for small screens */\n\n@media screen and (--phoneBreakPoint) and (-ms-high-contrast: active),\n  screen and (--phoneBreakPoint) and (-ms-high-contrast: none) {\n  .Body__styles__popoverContent {\n    width: 11rem;\n  }\n}\n';
  },
  'popoverHeaderBox': 'Body__styles__popoverHeaderBox',
  'popoverHeader': 'Body__styles__popoverHeader',
  'popoverContent': 'Body__styles__popoverContent',
  'piggyBank': 'Body__styles__piggyBank'
};
var AddContentBody = (_dec = (0, _lib2.default)(_theme2.default, styles), _dec(_class = (_temp2 = _class2 = function (_Component) {
  (0, _inherits3.default)(AddContentBody, _Component);

  function AddContentBody() {
    var _ref;

    var _temp, _this, _ret;

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

    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 = AddContentBody.__proto__ || Object.getPrototypeOf(AddContentBody)).call.apply(_ref, [this].concat(args))), _this), _this.openBankTray = function () {
      _this.props.onOpenBankTray();
      _this.props.setUi(_constants.BUILD_TRAY_OPEN, true);
      _this.props.setUi(_constants.BUILD_TRAY_POSITION, _this.props.position);
      _this.props.setUi(_constants.BUILD_TRAY_STIMULUS_ID, _this.props.parentEntryId);
    }, _this.addStimulus = function () {
      if (_this.props.editingQuizEntry) {
        _this.props.setNextQuizEntry(null, _this.props.quizId, _this.props.position);
      } else {
        _this.props.createStimulus(_this.props.quizId, _this.props.position);
      }
      _this.props.onAddItem();
    }, _this.addItem = function (interactionTypeData) {
      if (_this.props.quizEntryWithErrorsShowing) {
        _this.props.screenreaderNotification('Elementu ' + _default_pl_number(Math.ceil(_this.props.quizEntryWithErrorsShowing.position)) + ' nie mo\u017Cna zapisa\u0107 z powodu nieprawid\u0142owych danych wej\u015Bcia.');
      } else {
        var _this$props = _this.props,
            quizId = _this$props.quizId,
            position = _this$props.position,
            stimulusId = _this$props.stimulusId,
            parentEntryId = _this$props.parentEntryId;

        if (_this.props.editingQuizEntry) {
          _this.props.setNextQuizEntry(interactionTypeData, quizId, position, stimulusId, parentEntryId);
        } else {
          var defaultInteractionData = interactionTypeData.getDefaultInteractionData();
          var scoringData = interactionTypeData.getDefaultScoringData(defaultInteractionData);
          _this.props.createItem(quizId, position, {
            id: interactionTypeData.id,
            interactionData: defaultInteractionData,
            itemBody: interactionTypeData.itemBody,
            properties: interactionTypeData.defaultProperties,
            scoringAlgorithm: (0, _first2.default)(interactionTypeData.scoringAlgorithmOptions),
            scoringData: scoringData,
            userResponseType: (0, _first2.default)(interactionTypeData.userResponseTypeOptions)
          }, parentEntryId, stimulusId);
        }
      }
      _this.props.onAddItem();
    }, _this.renderBankButton = function () {
      return _react2.default.createElement(
        _Button2.default,
        { onClick: _this.openBankTray, 'data-automation': 'sdk-add-banked-content-button' },
        _react2.default.createElement(
          'div',
          { className: styles.piggyBank },
          _react2.default.createElement(_IconBank2.default, { title: 'Dodaj zawarto\u015B\u0107 z banku' })
        )
      );
    }, _this.renderPopoverHeader = function () {
      var text = _this.props.showHeaderText ? 'Wstaw zawarto\u015B\u0107' : '';

      return _react2.default.createElement(
        'div',
        { className: styles.popoverHeaderBox },
        _react2.default.createElement(
          'div',
          { className: styles.popoverHeader },
          _react2.default.createElement(
            _Grid2.default,
            { vAlign: 'middle', colSpacing: 'none', rowSpacing: 'none' },
            _react2.default.createElement(
              _Grid.GridRow,
              null,
              _react2.default.createElement(
                _Grid.GridCol,
                null,
                _react2.default.createElement(
                  _Text2.default,
                  null,
                  text
                )
              ),
              _react2.default.createElement(
                _Grid.GridCol,
                { width: 'auto' },
                _this.renderBankButton()
              )
            )
          )
        )
      );
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  // ==============================
  // ACTIONS
  // ==============================

  // ==============================
  // RENDERING
  // ==============================

  (0, _createClass3.default)(AddContentBody, [{
    key: 'render',
    value: function render() {
      return _react2.default.createElement(
        'div',
        { className: styles.popoverContent },
        this.renderPopoverHeader(),
        _react2.default.createElement(
          _View2.default,
          { margin: 'small' },
          _react2.default.createElement(_InteractionTypes2.default, {
            columns: 2,
            addItem: this.addItem,
            addStimulus: this.addStimulus,
            allowStimulusCreation: this.props.allowStimulusCreation,
            types: this.props.interactionTypes
          })
        )
      );
    }
  }]);
  AddContentBody.displayName = 'AddContentBody'
  ;
  return AddContentBody;
}(_react.Component), _class2.propTypes = {
  allowStimulusCreation: _propTypes2.default.bool.isRequired,
  createItem: _propTypes2.default.func.isRequired,
  createStimulus: _propTypes2.default.func.isRequired,
  editingQuizEntry: _propTypes2.default.bool,
  interactionTypes: _reactImmutableProptypes2.default.list,
  onOpenBankTray: _propTypes2.default.func,
  onAddItem: _propTypes2.default.func,
  parentEntryId: _propTypes2.default.string,
  position: _propTypes2.default.number.isRequired,
  quizEntryWithErrorsShowing: _reactImmutableProptypes2.default.record,
  quizId: _propTypes2.default.string.isRequired,
  screenreaderNotification: _propTypes2.default.func.isRequired,
  setNextQuizEntry: _propTypes2.default.func.isRequired,
  setUi: _propTypes2.default.func.isRequired,
  showHeaderText: _propTypes2.default.bool,
  stimulusId: _propTypes2.default.string
}, _class2.defaultProps = {
  onAddItem: function onAddItem() {},
  onOpenBankTray: function onOpenBankTray() {},
  showHeaderText: true }, _temp2)) || _class);
exports.default = AddContentBody;