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

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

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

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

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

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

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

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

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

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

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

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

var _dec,
    _class,
    _class2,
    _temp,
    _default_nb_number = Intl.NumberFormat("nb", { style: "decimal" }).format;

var styles = {
  componentId: 'cHBBh',
  template: function template(theme) {
    return "\n\n.quiz-core__Body--popoverHeaderBox {\n  border-bottom: solid ".concat(theme.popoverHeaderBoxBorderWidth || 'inherit', " ").concat(theme.popoverHeaderBoxBorderColor || 'inherit', ";\n}\n\n.quiz-core__Body--popoverHeader {\n  margin: 1rem;\n  margin-bottom: 0.25rem;\n}\n\n.quiz-core__Body--popoverContent {\n  margin-left: ").concat(theme.popoverContentMargin || 'inherit', ";\n  margin-right: ").concat(theme.popoverContentMargin || 'inherit', ";\n}\n\n.quiz-core__Body--piggyBank {\n  font-size: x-large;\n}\n\n\n\n@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {\n  .quiz-core__Body--popoverContent {\n    width: 22rem;\n  }\n}\n\n\n\n@media screen and (--phoneBreakPoint) and (-ms-high-contrast: active),\n  screen and (--phoneBreakPoint) and (-ms-high-contrast: none) {\n  .quiz-core__Body--popoverContent {\n    width: 11rem;\n  }\n}\n");
  },
  'popoverHeaderBox': 'quiz-core__Body--popoverHeaderBox',
  'popoverHeader': 'quiz-core__Body--popoverHeader',
  'popoverContent': 'quiz-core__Body--popoverContent',
  'piggyBank': 'quiz-core__Body--piggyBank'
};
var AddContentBody = (_dec = (0, _lib.default)(_theme.default, styles), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(AddContentBody, _Component);

  function AddContentBody() {
    var _getPrototypeOf2;

    var _this;

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

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

    _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("Elementet " + _default_nb_number(Math.ceil(_this.props.quizEntryWithErrorsShowing.position)) + " kan ikke lagres p\xE5 grunn av ugyldige inngangsdata");
      } 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, _first.default)(interactionTypeData.scoringAlgorithmOptions),
            scoringData: scoringData,
            userResponseType: (0, _first.default)(interactionTypeData.userResponseTypeOptions)
          }, parentEntryId, stimulusId);
        }
      }

      _this.props.onAddItem();
    };

    _this.renderBankButton = function () {
      return _react.default.createElement(_Button.default, {
        onClick: _this.openBankTray,
        "data-automation": "sdk-add-banked-content-button"
      }, _react.default.createElement("div", {
        className: styles.piggyBank
      }, _react.default.createElement(_IconBank.default, {
        title: "Legg til bankinnhold"
      })));
    };

    _this.renderPopoverHeader = function () {
      var text = _this.props.showHeaderText ? "Sett inn innhold" : '';
      return _react.default.createElement("div", {
        className: styles.popoverHeaderBox
      }, _react.default.createElement("div", {
        className: styles.popoverHeader
      }, _react.default.createElement(_Grid.default, {
        vAlign: "middle",
        colSpacing: "none",
        rowSpacing: "none"
      }, _react.default.createElement(_Grid.GridRow, null, _react.default.createElement(_Grid.GridCol, null, _react.default.createElement(_Text.default, null, text)), _react.default.createElement(_Grid.GridCol, {
        width: "auto"
      }, _this.renderBankButton())))));
    };

    return _this;
  }

  (0, _createClass2.default)(AddContentBody, [{
    key: "render",
    value: function render() {
      return _react.default.createElement("div", {
        className: styles.popoverContent
      }, this.renderPopoverHeader(), _react.default.createElement(_View.default, {
        margin: "small"
      }, _react.default.createElement(_InteractionTypes.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: _propTypes.default.bool.isRequired,
  createItem: _propTypes.default.func.isRequired,
  createStimulus: _propTypes.default.func.isRequired,
  editingQuizEntry: _propTypes.default.bool,
  interactionTypes: _reactImmutableProptypes.default.list,
  onOpenBankTray: _propTypes.default.func,
  onAddItem: _propTypes.default.func,
  parentEntryId: _propTypes.default.string,
  position: _propTypes.default.number.isRequired,
  quizEntryWithErrorsShowing: _reactImmutableProptypes.default.record,
  quizId: _propTypes.default.string.isRequired,
  screenreaderNotification: _propTypes.default.func.isRequired,
  setNextQuizEntry: _propTypes.default.func.isRequired,
  setUi: _propTypes.default.func.isRequired,
  showHeaderText: _propTypes.default.bool,
  stimulusId: _propTypes.default.string
}, _class2.defaultProps = {
  onAddItem: function onAddItem() {},
  onOpenBankTray: function onOpenBankTray() {},
  showHeaderText: true // ==============================
  // ACTIONS
  // ==============================

}, _temp)) || _class);
exports.default = AddContentBody;