function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

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

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import React from 'react';
import PropTypes from 'prop-types';
import cls from 'classnames';
import ConfigProvider from '../../config-provider';
import Balloon from '../../balloon';
import Icon from '../../icon';
var Tooltip = Balloon.Tooltip;

var ClusterItem =
/*#__PURE__*/
function (_React$Component) {
  _inherits(ClusterItem, _React$Component);

  function ClusterItem(props) {
    var _this;

    _classCallCheck(this, ClusterItem);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(ClusterItem).call(this, props));

    _defineProperty(_assertThisInitialized(_this), "getBgColor", function () {
      var _this$props = _this.props,
          data = _this$props.data,
          colorSets = _this$props.colorSets;
      var text = data.text;

      if (typeof text !== 'string' || colorSets.length === 0 || colorSets.length === 1) {
        return '#289ED3';
      }

      var charCode = text.charCodeAt(0);
      var len = colorSets.length;
      var index = charCode % len;
      return colorSets[index];
    });

    _defineProperty(_assertThisInitialized(_this), "onHoverClose", function () {
      _this.setState({
        hoverClose: true
      });
    });

    _defineProperty(_assertThisInitialized(_this), "onLeaveClose", function () {
      _this.setState({
        hoverClose: false
      });
    });

    _defineProperty(_assertThisInitialized(_this), "onClose", function (e) {
      var _this$props2 = _this.props,
          onClose = _this$props2.onClose,
          data = _this$props2.data;
      e.stopPropagation();
      onClose(data);
    });

    _defineProperty(_assertThisInitialized(_this), "onClick", function () {
      var _this$props3 = _this.props,
          onClick = _this$props3.onClick,
          data = _this$props3.data;
      onClick(data);
    });

    _this.state = {
      hoverClose: false
    };
    return _this;
  }

  _createClass(ClusterItem, [{
    key: "render",
    value: function render() {
      var _cls;

      var _this$props4 = this.props,
          prefix = _this$props4.prefix,
          data = _this$props4.data;
      var img = data.img,
          text = data.text;
      var hoverClose = this.state.hoverClose;
      return React.createElement(Tooltip, {
        trigger: React.createElement("div", {
          className: cls((_cls = {}, _defineProperty(_cls, "".concat(prefix, "avatar-cluster-item"), true), _defineProperty(_cls, "".concat(prefix, "avatar-cluster-item-hover-close"), hoverClose), _cls)),
          onClick: this.onClick
        }, img ? React.createElement("img", {
          className: "".concat(prefix, "avatar-cluster-item-img"),
          src: img
        }) : React.createElement("span", {
          className: "".concat(prefix, "avatar-cluster-item-text"),
          style: {
            background: this.getBgColor()
          }
        }, React.createElement("span", {
          className: "scale"
        }, text.slice(0, 2))), React.createElement(Icon, {
          type: "remove-o-fill",
          size: "small",
          className: "".concat(prefix, "avatar-cluster-item-close"),
          onClick: this.onClose,
          onMouseEnter: this.onHoverClose,
          onMouseLeave: this.onLeaveClose
        }))
      }, text);
    }
  }]);

  return ClusterItem;
}(React.Component);

_defineProperty(ClusterItem, "propTypes", {
  prefix: PropTypes.string,
  data: PropTypes.shape({
    img: PropTypes.string,
    text: PropTypes.string.isRequired,
    id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired
  }).isRequired,
  colorSets: PropTypes.arrayOf(PropTypes.string),
  onClose: PropTypes.func,
  onClick: PropTypes.func
});

_defineProperty(ClusterItem, "defaultProps", {
  prefix: 'next-',
  onClose: function onClose() {},
  onClick: function onClick() {}
});

export default ConfigProvider.config(ClusterItem);