'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

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 _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _styles = require('@material-ui/core/styles');

var _Chip = require('@material-ui/core/Chip');

var _Chip2 = _interopRequireDefault(_Chip);

var _Select = require('./Select');

var _Select2 = _interopRequireDefault(_Select);

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

var styleSheet = {
  row: {
    display: 'flex',
    flexWrap: 'wrap',
    marginTop: -3
  },
  chip: {
    margin: '3px 3px 0 0',
    height: 30
  },
  select: {
    minWidth: 220
  },
  highlight: {
    backgroundColor: '#ff9696'
  }
};

var MultiSelect = function (_Component) {
  (0, _inherits3.default)(MultiSelect, _Component);

  function MultiSelect() {
    var _ref;

    var _temp, _this, _ret;

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

    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 = MultiSelect.__proto__ || (0, _getPrototypeOf2.default)(MultiSelect)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
      selectValue: '',
      suggestions: _this.props.suggestions.filter(function (s) {
        return _this.props.value.indexOf(s) === -1;
      })
    }, _this.handleSelectChange = function (e, d) {
      _this.setState({ selectValue: d.newValue });
    }, _this.handleKeyDown = function (e) {
      if (_this.justAdded || e.keyCode !== 13) {
        _this.justAdded = false;
        return;
      }
      var newValue = _this.state.selectValue;
      var index = _this.props.value.indexOf(newValue);
      if (index > -1) {
        _this.highlightChip(index);
        _this.setState({ selectValue: '' });
      } else {
        _this.handleSuggestionSelected(e, { suggestionValue: newValue });
      }
    }, _this.handleSuggestionSelected = function (e, d) {
      var newValue = _this.props.value.concat(d.suggestionValue);
      _this.justAdded = true;
      _this.setState({ selectValue: '' });
      _this.props.onChange(e, { newValue: newValue, type: 'add' });
    }, _this.handleRequestDelete = function (i) {
      return function (e) {
        var value = _this.props.value;

        var newValue = value.slice(0, i).concat(value.slice(i + 1));
        _this.props.onChange(e, { newValue: newValue, type: 'delete' });
      };
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(MultiSelect, [{
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      if (nextProps.value !== this.props.value) {
        var suggestions = nextProps.suggestions.filter(function (s) {
          return nextProps.value.indexOf(s) === -1;
        });
        this.setState({ suggestions: suggestions });
      }
    }
  }, {
    key: 'highlightChip',
    value: function highlightChip(i) {
      var _this2 = this;

      this.setState({ highlight: i });
      setTimeout(function () {
        return _this2.setState({ highlight: null });
      }, 500);
    }
  }, {
    key: 'render',
    value: function render() {
      var _this3 = this;

      var _props = this.props,
          classes = _props.classes,
          value = _props.value;

      return _react2.default.createElement(
        'div',
        { className: classes.row },
        value.map(function (d, i) {
          return _react2.default.createElement(_Chip2.default, {
            label: d,
            key: i,
            onRequestDelete: _this3.handleRequestDelete(i),
            className: classes.chip + (_this3.state.highlight === i ? ' ' + classes.highlight : '')
          });
        }),
        _react2.default.createElement(_Select2.default, {
          value: this.state.selectValue,
          suggestions: this.state.suggestions,
          onChange: this.handleSelectChange,
          onKeyDown: this.handleKeyDown,
          onSuggestionSelected: this.handleSuggestionSelected,
          className: classes.select
        })
      );
    }
  }]);
  return MultiSelect;
}(_react.Component);

MultiSelect.propTypes = {
  classes: _propTypes2.default.object.isRequired,
  className: _propTypes2.default.string,
  value: _propTypes2.default.array
};

MultiSelect.defaultProps = {
  value: []
};

exports.default = (0, _styles.withStyles)(styleSheet)(MultiSelect);
//# sourceMappingURL=MultiSelect.js.map