"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.CatalogItemIcon = void 0;

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames = _interopRequireDefault(require("classnames"));

var _Icon = require("../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];
};

var CatalogItemIcon =
/*#__PURE__*/
function (_React$PureComponent) {
  (0, _inheritsLoose2.default)(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.default.createElement("div", {
        className: "rc-catalog-item-icon__icon--close"
      }, _react.default.createElement(_Icon.Icon, {
        name: "close"
      }));
    }

    return _react.default.createElement("div", {
      className: "rc-catalog-item-icon__icon"
    }, _react.default.createElement(_Icon.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 = (0, _classnames.default)('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.default.createElement("div", {
      style: style,
      className: className,
      "data-test-automation-id": testAutomationId
    }, this.renderIcon());
  };

  return CatalogItemIcon;
}(_react.default.PureComponent);

exports.CatalogItemIcon = CatalogItemIcon;
(0, _defineProperty2.default)(CatalogItemIcon, "propTypes", {
  iconName: _propTypes.default.string,
  backgroundColor: _propTypes.default.string,
  size: _propTypes.default.number,
  isRound: _propTypes.default.bool,
  testAutomationId: _propTypes.default.string
});
(0, _defineProperty2.default)(CatalogItemIcon, "defaultProps", {
  iconName: null,
  backgroundColor: null,
  size: 60,
  isRound: null,
  testAutomationId: 'catalogItemIcon'
});