'use strict';

exports.__esModule = true;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _semanticUiReact = require('semantic-ui-react');

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

var _propTypes2 = _interopRequireDefault(_propTypes);

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var propTypes = {
  tags: _propTypes2.default.array.isRequired,
  onTagRemove: _propTypes2.default.func,
  onTagAdd: _propTypes2.default.func,
  onChange: _propTypes2.default.func,
  label: _propTypes2.default.string,
  labelWidth: _propTypes2.default.number,
  placeholder: _propTypes2.default.string
};

var defaultProps = {
  label: 'Tags',
  labelWidth: 100,
  placeholder: 'Add a tag...',
  onChange: function onChange() {}
};

var TagsInput = function (_Component) {
  _inherits(TagsInput, _Component);

  // TODO add 2 the same items - make it impossible
  function TagsInput(props) {
    _classCallCheck(this, TagsInput);

    var _this = _possibleConstructorReturn(this, _Component.call(this, props));

    _this.deleteTag = function (tag) {
      var newTags = _this.state.tags.filter(function (t) {
        return tag !== t;
      });
      _this.setState(Object.assign({}, _this.state, { tags: newTags }), function () {
        _this.props.onTagRemove && _this.props.onTagRemove(tag);
        _this.props.onChange(_this.state.tags);
      });
    };

    _this.catchTagSubmit = function (e) {
      return e.key === 'Enter' ? function () {
        var newTag = _this.state.inputValue;
        var newTags = [].concat(_this.state.tags, [newTag]);
        _this.setState(Object.assign({}, _this.state, { inputValue: '', tags: newTags }), function () {
          _this.props.onTagAdd && _this.props.onTagAdd(newTag);
          _this.props.onChange(_this.state.tags);
        });
      }() : null;
    };

    _this.state = {
      tags: props.tags,
      inputValue: ''
    };
    return _this;
  }

  TagsInput.prototype.updateInputValue = function updateInputValue(e) {
    var newValue = e.target.value;
    this.setState(Object.assign({}, this.state, { inputValue: newValue }));
  };

  TagsInput.prototype.render = function render() {
    var _this2 = this;

    var props = this.props;

    return _react2.default.createElement(
      'div',
      null,
      _react2.default.createElement(
        _semanticUiReact.Form.Group,
        null,
        _react2.default.createElement(
          _semanticUiReact.Form.Field,
          null,
          _react2.default.createElement(
            'label',
            { style: { width: props.labelWidth + 'px' } },
            props.label
          )
        ),
        this.state.tags.map(function (tag, idx) {
          return _react2.default.createElement(
            _semanticUiReact.Form.Field,
            { key: idx },
            _react2.default.createElement(
              _semanticUiReact.Label,
              { size: 'small' },
              tag,
              _react2.default.createElement(_semanticUiReact.Icon, { name: 'delete', link: true, onClick: _this2.deleteTag.bind(_this2, tag) })
            )
          );
        }),
        _react2.default.createElement(
          _semanticUiReact.Form.Field,
          null,
          _react2.default.createElement(_semanticUiReact.Input, { transparent: true, placeholder: props.placeholder,
            value: this.state.inputValue,
            onChange: this.updateInputValue.bind(this),
            onKeyPress: this.catchTagSubmit
          })
        )
      )
    );
  };

  return TagsInput;
}(_react.Component);

TagsInput.propTypes = process.env.NODE_ENV !== "production" ? propTypes : {};
TagsInput.defaultProps = defaultProps;

exports.default = TagsInput;
module.exports = exports['default'];