'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 _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _Select = require('@instructure/ui-forms/lib/components/Select');

var _Select2 = _interopRequireDefault(_Select);

var _formatMessage = require('../../../format-message');

var _formatMessage2 = _interopRequireDefault(_formatMessage);

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

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

  function TagSelect() {
    var _ref;

    var _temp, _this, _ret;

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

    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 = TagSelect.__proto__ || Object.getPrototypeOf(TagSelect)).call.apply(_ref, [this].concat(args))), _this), _this.handleChange = function (event, options) {
      var onChange = _this.props.onChange;

      var tags = options.map(function (option) {
        return {
          id: option.tag_id != null ? option.value : null,
          tag_value: option.tag_id != null ? option.label : option.value,
          tag_id: option.tag_id
        };
      });
      onChange(tags);
    }, _this.handleInputChange = function (e, value) {
      var text = value.trim();
      _this.props.onInputChange(text === '' ? null : text);
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(TagSelect, [{
    key: 'options',
    value: function options() {
      var tags = this.props.tags;

      return tags.map(function (ta) {
        return {
          value: ta.id == null ? ta.tag_value : ta.id,
          label: ta.tag_value,
          tag_id: ta.tag_id
        };
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var newTagSuggest = this.props.newTagSuggest;

      return _react2.default.createElement(
        _Select2.default,
        {
          label: 'Tags and Metadata',
          onChange: this.handleChange,
          emptyOption: 'Enter a tag to search or create',
          onInputChange: this.handleInputChange,
          selectedOption: this.options(),
          filter: function filter(options) {
            return options;
          },
          multiple: true,
          editable: true },
        newTagSuggest && _react2.default.createElement(
          'option',
          { key: newTagSuggest, value: newTagSuggest },
          'Create new tag: ' + newTagSuggest
        )
      );
    }
  }]);
  TagSelect.displayName = 'TagSelect'
  ;
  return TagSelect;
}(_react.Component), _class.propTypes = {
  onChange: _propTypes2.default.func.isRequired,
  onInputChange: _propTypes2.default.func.isRequired,
  newTagSuggest: _propTypes2.default.string,
  tags: _propTypes2.default.arrayOf(_propTypes2.default.shape({
    id: _propTypes2.default.string,
    tag_value: _propTypes2.default.string.isRequired,
    tag_id: _propTypes2.default.string
  }))
}, _class.defaultProps = {
  newTagSuggest: null,
  tags: []
}, _temp2);
exports.default = TagSelect;