"use strict";

exports.__esModule = true;
exports.default = exports.calculateHoveredSelectedBgColor = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _classnames = _interopRequireDefault(require("classnames"));
var _get = _interopRequireDefault(require("lodash/fp/get"));
var _cssColorFunction = require("css-color-function");
var _reactFontawesome = require("@fortawesome/react-fontawesome");
var _provider = _interopRequireDefault(require("../provider"));
var _colors = require("../../variables/colors");
var _style = _interopRequireDefault(require("./style.css"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
const LUMINOSITY_DELTA = 0.08;
const {
  cm_primary_blue: DEFAULT_BACKGROUND_COLOR
} = _colors.COLORS;
const ICON_SIZE = '12px';
const calculateHoveredSelectedBgColor = (selectedBgColor, luminosityDelta = LUMINOSITY_DELTA) => (0, _cssColorFunction.convert)(`hsl(from ${selectedBgColor} h s calc(l*(1 - ${luminosityDelta})))`);
// note: luminosity decrease is relative to the selected color
exports.calculateHoveredSelectedBgColor = calculateHoveredSelectedBgColor;
const Chip = (props, context) => {
  const {
    text,
    subText,
    selected = false,
    customIcon,
    onClick,
    leftIcon,
    backgroundColor = DEFAULT_BACKGROUND_COLOR
  } = props;
  const {
    skin
  } = context;
  const skinColor = (0, _get.default)('common.primary', skin);
  const selectedBgColor = backgroundColor === 'skin' && skinColor ? skinColor : backgroundColor;
  const hoveredSelectedBgColor = calculateHoveredSelectedBgColor(selectedBgColor);
  const [isHovered, setIsHovered] = (0, _react.useState)(false);
  const handleClick = (0, _react.useMemo)(() => onClick, [onClick]);
  const handleMouseEnter = (0, _react.useCallback)(() => {
    setIsHovered(true);
  }, []);
  const handleMouseLeave = (0, _react.useCallback)(() => {
    setIsHovered(false);
  }, []);
  const hoverStyle = isHovered ? {
    backgroundColor: hoveredSelectedBgColor
  } : {};
  const defaultIcon = selected ? 'fa-check' : 'fa-plus';
  const color = selected ? _colors.COLORS.white : _colors.COLORS.cm_grey_400;
  return /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)(_style.default.container, !selected && _style.default.unselected),
    style: {
      ...(selected && selectedBgColor ? {
        backgroundColor: selectedBgColor
      } : {}),
      ...(selected && hoverStyle)
    },
    onClick: handleClick,
    onMouseEnter: handleMouseEnter,
    onMouseLeave: handleMouseLeave,
    "aria-label": text,
    "data-name": `chip-${text}`
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.left
  }, leftIcon ? /*#__PURE__*/_react.default.createElement(_reactFontawesome.FontAwesomeIcon, {
    style: {
      color
    },
    icon: leftIcon,
    fontSize: ICON_SIZE
  }) : null, /*#__PURE__*/_react.default.createElement("div", {
    title: text
  }, /*#__PURE__*/_react.default.createElement("span", {
    className: _style.default.text,
    style: {
      color
    }
  }, text), subText ? /*#__PURE__*/_react.default.createElement("span", {
    className: _style.default.subText,
    style: {
      color
    }
  }, subText) : null)), /*#__PURE__*/_react.default.createElement(_reactFontawesome.FontAwesomeIcon, {
    style: {
      color
    },
    icon: customIcon ? `fa-${customIcon}` : defaultIcon,
    fontSize: ICON_SIZE
  }));
};
Chip.contextTypes = {
  skin: _provider.default.childContextTypes.skin
};
Chip.propTypes = process.env.NODE_ENV !== "production" ? {
  text: _propTypes.default.string,
  subText: _propTypes.default.string,
  selected: _propTypes.default.bool,
  customIcon: _propTypes.default.string,
  backgroundColor: _propTypes.default.string,
  onClick: _propTypes.default.func,
  leftIcon: _propTypes.default.string
} : {};
var _default = exports.default = Chip;
//# sourceMappingURL=index.js.map