"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

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

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

var _context = require("../config-provider/context");

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

var _tooltip = _interopRequireDefault(require("../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.default.useContext(_context.ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

  var rateCls = getPrefixClassName('rate');
  var classNames = (0, _react.useMemo)(function () {
    var _classnames;

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

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

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

var _default = Star;
exports.default = _default;