"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 _partial = _interopRequireDefault(require("lodash/partial"));

var _immutable = require("immutable");

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

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

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

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

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

var _interactionTypeMetadata = require("../../../util/interactionTypeMetadata");

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

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

var _dec, _class, _class2, _temp;

var styles = {
  componentId: 'bLlES',
  template: function template(theme) {
    return "\n\n.InteractionTypes__styles__iconWrapper {\n  padding-right: 0.25rem;\n}\n\n.InteractionTypes__styles__textWrapper {\n  width: 10rem;\n}\n\n.InteractionTypes__styles__stimulusDivider {\n  border-top: ".concat(theme.stimulusDividerBorderWidth || 'inherit', " solid ").concat(theme.stimulusDividerBorderColor || 'inherit', ";\n  margin-top: ").concat(theme.stimulusDividerMarginTop || 'inherit', ";\n  margin-bottom: ").concat(theme.stimulusDividerMarginBottom || 'inherit', ";\n}\n");
  },
  'iconWrapper': 'InteractionTypes__styles__iconWrapper',
  'textWrapper': 'InteractionTypes__styles__textWrapper',
  'stimulusDivider': 'InteractionTypes__styles__stimulusDivider'
};
var InteractionTypes = (_dec = (0, _lib.default)(_theme.default, styles), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(InteractionTypes, _Component);

  function InteractionTypes() {
    var _getPrototypeOf2;

    var _this;

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

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

    _this.renderInteractionTypeButton = function (intType, i) {
      var onClick, Icon, name, title;

      if (intType.name === 'Stimulus') {
        onClick = _this.props.addStimulus;
        Icon = _IconPlus.default;
        name = "Stimulus";
        title = "Tilf\xF8j stimulus";
      } else {
        onClick = (0, _partial.default)(_this.props.addItem, intType);
        var metadata = (0, _interactionTypeMetadata.interactionTypeMetadata)(intType);
        Icon = metadata.icon;
        name = metadata.name;
        title = "Tilf\xF8j " + name + " element";
      }

      return _react.default.createElement(_Button.default, {
        fluidWidth: true,
        key: intType.slug,
        margin: "0 0 xx-small",
        onClick: onClick,
        size: "small"
      }, _react.default.createElement(_Grid.default, {
        colSpacing: "none",
        vAlign: "middle"
      }, _react.default.createElement(_Grid.GridRow, {
        key: "iconrow"
      }, _react.default.createElement(_Grid.GridCol, {
        width: "auto"
      }, _react.default.createElement("div", {
        className: styles.iconWrapper
      }, _react.default.createElement(Icon, {
        title: title
      }))), _react.default.createElement(_Grid.GridCol, null, _react.default.createElement("div", {
        className: styles.textWrapper
      }, _react.default.createElement(_Text.default, null, name))))));
    };

    _this.renderInteractionTypeRows = function () {
      var buttons = [];
      var types = _this.props.types; // The interaction types are in alphabetical order

      types.sortBy(function (intType) {
        return (0, _interactionTypeMetadata.interactionTypeMetadata)(intType).name;
      }).forEach(function (intType, i) {
        var button = _this.renderInteractionTypeButton(intType, i);

        if (i % _this.props.columns === 0) {
          buttons[Math.floor(i / _this.props.columns)] = [];
        }

        buttons[Math.floor(i / _this.props.columns)].push(_react.default.createElement(_Grid.GridCol, {
          width: 12 / _this.props.columns,
          key: i
        }, button));
      });
      return buttons.map(function (intTypes, i) {
        return _react.default.createElement(_Grid.GridRow, {
          key: i
        }, intTypes);
      });
    };

    _this.renderStimulusTypeButton = function () {
      if (_this.props.allowStimulusCreation && _this.props.stimulusOn) {
        var dividerRow = _react.default.createElement(_Grid.GridRow, {
          key: "divider"
        }, _react.default.createElement(_Grid.GridCol, {
          key: "divider"
        }, _react.default.createElement("div", {
          className: styles.stimulusDivider
        })));

        var stimulusRow = _react.default.createElement(_Grid.GridRow, {
          key: "stimulus"
        }, _react.default.createElement(_Grid.GridCol, {
          width: 12 / _this.props.columns,
          key: "stimulus"
        }, _this.renderInteractionTypeButton({
          name: 'Stimulus',
          slug: 'stimulus'
        })));

        return [dividerRow, stimulusRow];
      }
    };

    return _this;
  }

  (0, _createClass2.default)(InteractionTypes, [{
    key: "render",
    value: function render() {
      return _react.default.createElement(_Grid.default, {
        rowSpacing: "none",
        colSpacing: "small",
        startAt: "medium"
      }, this.renderInteractionTypeRows(), this.renderStimulusTypeButton());
    }
  }]);
  InteractionTypes.displayName = "InteractionTypes";
  return InteractionTypes;
}(_react.Component), _class2.propTypes = {
  columns: _propTypes.default.number.isRequired,
  addItem: _propTypes.default.func.isRequired,
  addStimulus: _propTypes.default.func.isRequired,
  allowStimulusCreation: _propTypes.default.bool,
  types: _reactImmutableProptypes.default.list,
  stimulusOn: _propTypes.default.bool.isRequired
}, _class2.defaultProps = {
  allowStimulusCreation: true,
  types: (0, _immutable.List)()
}, _temp)) || _class);
exports.default = InteractionTypes;