import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { Icon } from '../Icon';
var COLORS = ['#ff4c57', '#00bcff', '#8b7cff', '#19adcf', '#ff7c00'];

var getColorForString = function getColorForString(text) {
  var numberHash = text.split('').reduce(function (acc, char) {
    return char.charCodeAt(0);
  }, 0);
  return COLORS[numberHash % COLORS.length];
};

export var CatalogItemIcon =
/*#__PURE__*/
function (_React$PureComponent) {
  _inheritsLoose(CatalogItemIcon, _React$PureComponent);

  function CatalogItemIcon() {
    return _React$PureComponent.apply(this, arguments) || this;
  }

  var _proto = CatalogItemIcon.prototype;

  _proto.renderIcon = function renderIcon() {
    var iconName = this.props.iconName;

    if (!iconName) {
      return React.createElement("div", {
        className: "rc-catalog-item-icon__icon--close"
      }, React.createElement(Icon, {
        name: "close"
      }));
    }

    return React.createElement("div", {
      className: "rc-catalog-item-icon__icon"
    }, React.createElement(Icon, {
      name: iconName
    }));
  };

  _proto.render = function render() {
    var _this$props = this.props,
        testAutomationId = _this$props.testAutomationId,
        iconName = _this$props.iconName,
        backgroundColor = _this$props.backgroundColor,
        size = _this$props.size,
        isRound = _this$props.isRound;
    var className = classnames('rc-catalog-item-icon', {
      'rc-catalog-item-icon--round': isRound,
      'rc-catalog-item-icon--placeholder': !iconName
    });
    var style = {
      fontSize: size + "px"
    };

    if (iconName) {
      style.backgroundColor = backgroundColor || getColorForString(iconName);
    }

    return React.createElement("div", {
      style: style,
      className: className,
      "data-test-automation-id": testAutomationId
    }, this.renderIcon());
  };

  return CatalogItemIcon;
}(React.PureComponent);

_defineProperty(CatalogItemIcon, "propTypes", {
  iconName: PropTypes.string,
  backgroundColor: PropTypes.string,
  size: PropTypes.number,
  isRound: PropTypes.bool,
  testAutomationId: PropTypes.string
});

_defineProperty(CatalogItemIcon, "defaultProps", {
  iconName: null,
  backgroundColor: null,
  size: 60,
  isRound: null,
  testAutomationId: 'catalogItemIcon'
});