'use strict';

exports.__esModule = true;
exports.default = undefined;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _class, _temp2;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

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

var _tag = require('./tag');

var _tag2 = _interopRequireDefault(_tag);

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 _default = (_temp2 = _class = function (_React$Component) {
    _inherits(_default, _React$Component);

    function _default() {
        var _temp, _this, _ret;

        _classCallCheck(this, _default);

        for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
            args[_key] = arguments[_key];
        }

        return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.state = {
            inputValue: '',
            tags: _this.props.tags
        }, _this.addTagFromInput = function (newTag) {
            var tags = [].concat(_this.state.tags, [newTag]);
            _this.setState(_extends({}, _this.state, { tags: tags, inputValue: '' }), function () {
                return _this.props.onTagAdd(newTag);
            });
        }, _this.catchTagSubmit = function (_ref) {
            var key = _ref.key;

            if (key !== 'Enter') return;
            var newTag = _this.state.inputValue;
            var notDuplication = !_this.state.tags.some(function (item) {
                return item === newTag;
            });
            var isNotEmpty = newTag !== '';
            if (notDuplication && isNotEmpty) _this.addTagFromInput(newTag);
        }, _this.deleteTag = function (tag) {
            var newTags = _this.state.tags.filter(function (t) {
                return tag !== t;
            });
            _this.setState(Object.assign({}, _this.state, { tags: newTags }), function () {
                return _this.props.onTagRemove(tag);
            });
        }, _temp), _possibleConstructorReturn(_this, _ret);
    }

    /*
    item: PropTypes.oneOfType([
            PropTypes.string,
            PropTypes.shape({
                text: PropTypes.string.isRequired,
                color: PropTypes.string,
                removable: PropTypes.bool
            })
        ]).isRequired
    */

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

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

        return _react2.default.createElement(
            'div',
            { className: 'tagsinput container' },
            this.state.tags.map(function (tag, idx) {
                return _react2.default.createElement(_tag2.default, { key: idx, text: tag, removable: true, onRemoveClick: _this2.deleteTag, className: 'tagitem' });
            }),
            _react2.default.createElement(_semanticUiReact.Input, { transparent: true, placeholder: this.props.placeholder, className: 'taginput',
                value: this.state.inputValue,
                onChange: this.updateInputValue.bind(this),
                onKeyPress: this.catchTagSubmit
            })
        );
    };

    return _default;
}(_react2.default.Component), _class.propTypes = {
    tags: _propTypes2.default.array.isRequired,
    placeholder: _propTypes2.default.string,
    onTagAdd: _propTypes2.default.func,
    onTagRemove: _propTypes2.default.func
}, _class.defaultProps = {
    placeholder: '',
    onTagAdd: function onTagAdd() {},
    onTagRemove: function onTagRemove() {} }, _temp2);

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