"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _reactImmutableProptypes = _interopRequireDefault(require("react-immutable-proptypes"));

var _debounce = _interopRequireDefault(require("lodash/debounce"));

var _TagSelect = _interopRequireDefault(require("../TagSelect"));

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

var TagSuggest =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(TagSuggest, _Component);

  function TagSuggest() {
    var _getPrototypeOf2;

    var _this;

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

    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)(TagSuggest)).call.apply(_getPrototypeOf2, [this].concat(args)));
    _this.debouncedSuggestions = (0, _debounce.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);
      }
    };

    return _this;
  }

  (0, _createClass2.default)(TagSuggest, [{
    key: "getTagSuggestions",
    value: function getTagSuggestions() {
      var _this$props2 = this.props,
          defaultSuggestions = _this$props2.defaultSuggestions,
          tagSuggestions = _this$props2.tagSuggestions,
          tags = _this$props2.tags;

      var suggestions = tagSuggestions || _immutable.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 _react.default.createElement(_TagSelect.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);

exports.default = TagSuggest;
TagSuggest.propTypes = {
  allowNewTags: _propTypes.default.bool,
  parentType: _propTypes.default.string.isRequired,
  parentId: _propTypes.default.string.isRequired,
  noMatchText: _propTypes.default.string.isRequired,
  onChange: _propTypes.default.func.isRequired,
  defaultSuggestions: _propTypes.default.arrayOf(tagShape),
  tagSuggestions: _reactImmutableProptypes.default.listOf(_reactImmutableProptypes.default.map),
  setTagInput: _propTypes.default.func.isRequired,
  newTagSuggest: _propTypes.default.string,
  getTagSuggestions: _propTypes.default.func.isRequired,
  setTagSuggestions: _propTypes.default.func.isRequired,
  tagInput: _propTypes.default.string,
  tags: _propTypes.default.arrayOf(tagShape)
};
TagSuggest.defaultProps = {
  allowNewTags: false,
  newTagSuggest: null,
  tagInput: null,
  tags: [],
  defaultSuggestions: [],
  tagSuggestions: null
};