'use strict';

exports.__esModule = true;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _ButtonTag = require('../../atoms/buttons/ButtonTag');

var _ButtonTag2 = _interopRequireDefault(_ButtonTag);

require('./style.css');

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 Tags = function (_Component) {
  _inherits(Tags, _Component);

  function Tags(props) {
    _classCallCheck(this, Tags);

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

    _this.state = {
      tags: _this.props.tags ? _this.props.tags : null
    };
    return _this;
  }

  Tags.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    this.setState({ tags: nextProps.tags });
  };

  Tags.prototype.handleClearAll = function handleClearAll() {
    if (typeof this.props.onClearCallback === 'function') {
      this.props.onClearCallback();
    }
    this.setState({ tags: null });
  };

  Tags.prototype.handleClearThis = function handleClearThis(event) {
    if (typeof this.props.onClearThisCallback === 'function') {
      this.props.onClearThisCallback(event.target, event);
    }
  };

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

    var tags = this.state.tags;

    return tags && _react2.default.createElement(
      'div',
      { className: 'ma__tags' },
      tags.map(function (tag, tagIndex) {
        return _react2.default.createElement(_ButtonTag2.default, {
          type: tag.type,
          value: tag.value,
          text: tag.text,
          handleClick: function handleClick(e) {
            return _this2.handleClearThis(e);
          }
          /* eslint-disable-next-line react/no-array-index-key */
          , key: 'resultsHeading.tag.' + tagIndex
        });
      }),
      tags.length > 1 && _react2.default.createElement(
        'button',
        { type: 'button', className: 'ma__tags-clear js-results-heading-clear', onClick: function onClick() {
            return _this2.handleClearAll();
          } },
        'Clear all'
      )
    );
  };

  return Tags;
}(_react.Component);

Tags.propTypes = process.env.NODE_ENV !== "production" ? {
  /** The tags applied to the search list
        type: The type of tag
        text: The text displayed by the tag (required)
        value: The value of the tag  */
  tags: _propTypes2.default.arrayOf(_propTypes2.default.shape({
    type: _propTypes2.default.string,
    text: _propTypes2.default.string.isRequired,
    value: _propTypes2.default.string
  })),
  /** Custom onClick function that triggers when 'Clear all' button is clicked */
  onClearCallback: _propTypes2.default.func,
  /** Custom onClick function that triggers when a tag is clicked */
  onClearThisCallback: _propTypes2.default.func
} : {};

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