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;

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; }

import React from 'react';
import PropTypes from 'prop-types';
import { Input, Label, Form, Icon } from 'semantic-ui-react';
import Tag from './tag';
import { relative } from 'path';

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 React.createElement(
            'div',
            { style: { position: relative } },
            this.state.tags.map(function (tag, idx) {
                return React.createElement(Tag, { key: idx, text: tag, removable: true, color: 'blue', onRemoveClick: _this2.deleteTag, className: 'tagitem' });
            }),
            React.createElement(Input, { transparent: true, placeholder: this.props.placeholder,
                style: { float: 'left', marginLeft: '0.3em' },
                value: this.state.inputValue,
                onChange: this.updateInputValue.bind(this),
                onKeyPress: this.catchTagSubmit
            })
        );
    };

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

export { _default as default };