'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 _class, _temp2;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactImmutableProptypes = require('react-immutable-proptypes');

var _reactImmutableProptypes2 = _interopRequireDefault(_reactImmutableProptypes);

var _debounce = require('lodash/debounce');

var _debounce2 = _interopRequireDefault(_debounce);

var _TagSelect = require('../TagSelect');

var _TagSelect2 = _interopRequireDefault(_TagSelect);

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

var tagShape = _propTypes2.default.shape({
  id: _propTypes2.default.string,
  value: _propTypes2.default.string.isRequired
});

var TagSuggest = (_temp2 = _class = function (_Component) {
  (0, _inherits3.default)(TagSuggest, _Component);

  function TagSuggest() {
    var _ref;

    var _temp, _this, _ret;

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

    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 = TagSuggest.__proto__ || Object.getPrototypeOf(TagSuggest)).call.apply(_ref, [this].concat(args))), _this), _this.debouncedSuggestions = (0, _debounce2.default)(function (parentType, parentId, tagText) {
      return _this.props.getTagSuggestions(parentType, parentId, tagText);
    }, 200), _this.handleTagInputChange = function (tagText) {
      var _this$props = _this.props,
          parentType = _this$props.parentType,
          parentId = _this$props.parentId;

      _this.props.setTagInput(parentType, parentId, tagText);
      if (tagText != null) {
        _this.debouncedSuggestions(parentType, parentId, tagText);
      } else {
        _this.props.setTagSuggestions(parentType, parentId, null, tagText);
      }
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(TagSuggest, [{
    key: 'getTagSuggestions',
    value: function getTagSuggestions() {
      var _props = this.props,
          defaultSuggestions = _props.defaultSuggestions,
          tagSuggestions = _props.tagSuggestions,
          tags = _props.tags;

      var suggestions = tagSuggestions || _immutable2.default.fromJS(defaultSuggestions);
      var visibleTagIds = tags.map(function (tag) {
        return tag.id;
      });
      // Exclude suggestions for tags already selected
      return suggestions.filterNot(function (tag) {
        return visibleTagIds.includes(tag.get('id'));
      });
    }
  }, {
    key: 'render',
    value: function render() {
      return _react2.default.createElement(_TagSelect2.default, {
        allowNewTags: this.props.allowNewTags,
        noMatchText: this.props.noMatchText,
        tags: this.props.tags,
        tagInput: this.props.tagInput,
        tagSuggestions: this.getTagSuggestions().toJS(),
        newTagSuggest: this.props.newTagSuggest,
        onChange: this.props.onChange,
        onInputChange: this.handleTagInputChange
      });
    }
  }]);
  TagSuggest.displayName = 'TagSuggest'
  ;
  return TagSuggest;
}(_react.Component), _class.propTypes = {
  allowNewTags: _propTypes2.default.bool,
  parentType: _propTypes2.default.string.isRequired,
  parentId: _propTypes2.default.string.isRequired,
  noMatchText: _propTypes2.default.string.isRequired,
  onChange: _propTypes2.default.func.isRequired,
  defaultSuggestions: _propTypes2.default.arrayOf(tagShape),
  tagSuggestions: _reactImmutableProptypes2.default.listOf(_reactImmutableProptypes2.default.map),
  setTagInput: _propTypes2.default.func.isRequired,
  newTagSuggest: _propTypes2.default.string,
  getTagSuggestions: _propTypes2.default.func.isRequired,
  setTagSuggestions: _propTypes2.default.func.isRequired,
  tagInput: _propTypes2.default.string,
  tags: _propTypes2.default.arrayOf(tagShape)
}, _class.defaultProps = {
  allowNewTags: false,
  newTagSuggest: null,
  tagInput: null,
  tags: [],
  defaultSuggestions: [],
  tagSuggestions: null
}, _temp2);
exports.default = TagSuggest;