'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;

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

var _partial2 = _interopRequireDefault(_partial);

var _immutable = require('immutable');

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

var _lib2 = _interopRequireDefault(_lib);

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

var _Button2 = _interopRequireDefault(_Button);

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

var _Text2 = _interopRequireDefault(_Text);

var _IconPlusLine = require('instructure-icons/lib/Line/IconPlusLine');

var _IconPlusLine2 = _interopRequireDefault(_IconPlusLine);

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

var _Grid2 = _interopRequireDefault(_Grid);

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

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

var _formatMessage2 = _interopRequireDefault(_formatMessage);

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.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: ' + theme.stimulusDividerBorderWidth + ' solid ' + theme.stimulusDividerBorderColor + ';\n  margin-top: ' + theme.stimulusDividerMarginTop + ';\n  margin-bottom: ' + theme.stimulusDividerMarginBottom + ';\n}\n';
  },
  'iconWrapper': 'InteractionTypes__styles__iconWrapper',
  'textWrapper': 'InteractionTypes__styles__textWrapper',
  'stimulusDivider': 'InteractionTypes__styles__stimulusDivider'
};
var InteractionTypes = (_dec = (0, _lib2.default)(_theme2.default, styles), _dec(_class = (_temp2 = _class2 = function (_Component) {
  (0, _inherits3.default)(InteractionTypes, _Component);

  function InteractionTypes() {
    var _ref;

    var _temp, _this, _ret;

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

    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 = InteractionTypes.__proto__ || Object.getPrototypeOf(InteractionTypes)).call.apply(_ref, [this].concat(args))), _this), _this.renderInteractionTypeButton = function (intType, i) {
      var onClick = void 0,
          Icon = void 0,
          name = void 0,
          title = void 0;

      if (intType.name === 'Stimulus') {
        onClick = _this.props.addStimulus;
        Icon = _IconPlusLine2.default;
        name = 'Ysgogiad';
        title = 'Ychwanegu Ysgogiad';
      } else {
        onClick = (0, _partial2.default)(_this.props.addItem, intType);
        var metadata = (0, _interactionTypeMetadata.interactionTypeMetadata)(intType);
        Icon = metadata.icon;
        name = metadata.name;
        title = 'Ychwanegu Eitem ' + name;
      }

      return _react2.default.createElement(
        _Button2.default,
        {
          fluidWidth: true,
          key: intType.slug,
          margin: '0 0 xx-small',
          onClick: onClick,
          size: 'small'
        },
        _react2.default.createElement(
          _Grid2.default,
          { colSpacing: 'none', vAlign: 'middle' },
          _react2.default.createElement(
            _Grid.GridRow,
            { key: 'iconrow' },
            _react2.default.createElement(
              _Grid.GridCol,
              { width: 'auto' },
              _react2.default.createElement(
                'div',
                { className: styles.iconWrapper },
                _react2.default.createElement(Icon, { title: title })
              )
            ),
            _react2.default.createElement(
              _Grid.GridCol,
              null,
              _react2.default.createElement(
                'div',
                { className: styles.textWrapper },
                _react2.default.createElement(
                  _Text2.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(_react2.default.createElement(
          _Grid.GridCol,
          { width: 12 / _this.props.columns, key: i },
          button
        ));
      });

      return buttons.map(function (intTypes, i) {
        return _react2.default.createElement(
          _Grid.GridRow,
          { key: i },
          intTypes
        );
      });
    }, _this.renderStimulusTypeButton = function () {
      if (_this.props.allowStimulusCreation && _this.props.stimulusOn) {
        var dividerRow = _react2.default.createElement(
          _Grid.GridRow,
          { key: 'divider' },
          _react2.default.createElement(
            _Grid.GridCol,
            { key: 'divider' },
            _react2.default.createElement('div', { className: styles.stimulusDivider })
          )
        );

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

        return [dividerRow, stimulusRow];
      }
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

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