import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import React, { useCallback, useMemo } from 'react';
import { ConfigContext } from '../config-provider/context';
import classnames from 'classnames';
import Tooltip from '../tooltip';

var Star = function Star(_ref) {
  var character = _ref.character,
      value = _ref.value,
      index = _ref.index,
      allowHalf = _ref.allowHalf,
      tooltips = _ref.tooltips,
      onMouseMove = _ref.onMouseMove,
      onClick = _ref.onClick;

  var _React$useContext = React.useContext(ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

  var rateCls = getPrefixClassName('rate');
  var classNames = useMemo(function () {
    var _classnames;

    return classnames("".concat(rateCls, "-star"), (_classnames = {}, _defineProperty(_classnames, "".concat(rateCls, "-star-full"), value >= index + 1), _defineProperty(_classnames, "".concat(rateCls, "-star-half"), allowHalf && value === index + 0.5), _classnames));
  }, [rateCls, value, index, allowHalf]);
  var handleMouseMove = useCallback(function (event) {
    onMouseMove(event, index);
  }, [index, onMouseMove]);
  var handleClick = useCallback(function (event) {
    onClick(event, index);
  }, [index, onClick]);
  var tooltip = useMemo(function () {
    if (Array.isArray(tooltips) && index < tooltips.length) {
      return tooltips[index];
    }

    return undefined;
  }, [tooltips, index]);
  var characterNode = useMemo(function () {
    if (typeof character === 'function') {
      return character({
        index: index
      });
    }

    return character;
  }, [character, index]);
  var starContent = useMemo(function () {
    return /*#__PURE__*/React.createElement("div", {
      className: classNames,
      onMouseMove: handleMouseMove,
      onClick: handleClick
    }, /*#__PURE__*/React.createElement("div", {
      className: "".concat(rateCls, "-star-background")
    }, characterNode), /*#__PURE__*/React.createElement("div", {
      className: "".concat(rateCls, "-star-content")
    }, characterNode));
  }, [characterNode, classNames, handleClick, handleMouseMove, rateCls]);
  return tooltip ? /*#__PURE__*/React.createElement(Tooltip, {
    overlay: tooltip
  }, starContent) : starContent;
};

export default Star;