import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import React, { useRef, useEffect, useState, useMemo, useCallback } from 'react';
var StarFill = Icon.StarFill;
import classnames from 'classnames';
import { ConfigContext } from '../config-provider/context';
import KeyCode from '../components/rc-util/KeyCode';
import Icon from '../icon';
import Star from './star';

var getValidValue = function getValidValue(value, defaultValue) {
  if (typeof value === 'number' && value > 0) {
    return value;
  }

  return defaultValue;
};

var Rate = function Rate(props) {
  var _React$useContext = React.useContext(ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

  var allowClear = props.allowClear,
      allowHalf = props.allowHalf,
      autoFocus = props.autoFocus,
      character = props.character,
      customClassName = props.className,
      customCount = props.count,
      defaultValue = props.defaultValue,
      disabled = props.disabled,
      style = props.style,
      tooltips = props.tooltips,
      onBlur = props.onBlur,
      onChange = props.onChange,
      onFocus = props.onFocus,
      onHoverChange = props.onHoverChange,
      onShowChange = props.onShowChange,
      onKeyDown = props.onKeyDown;
  var rateCls = getPrefixClassName('rate');
  var rateRef = useRef();
  useEffect(function () {
    if (autoFocus && rateRef.current) {
      rateRef.current.focus();
    }
  }, []);
  var count = useMemo(function () {
    return getValidValue(customCount, 5);
  }, [customCount]); // 当前值

  var _useState = useState(getValidValue(defaultValue, 0)),
      _useState2 = _slicedToArray(_useState, 2),
      value = _useState2[0],
      setValue = _useState2[1]; // 当前回显的值


  var _useState3 = useState(),
      _useState4 = _slicedToArray(_useState3, 2),
      hoverValue = _useState4[0],
      setHoverValue = _useState4[1]; // 允许清除会有一个特殊的情况: 再次点击当前值被清楚，但是hover的值没有变化的时候，应该是0而不是当前hover的值


  var prevClearedValue = useRef();
  useEffect(function () {
    if (Reflect.has(props, 'value')) {
      var nextValue = getValidValue(props.value, 0);
      setValue(nextValue);
    }
  }, [props]);
  var getValue = useCallback(function (event, index) {
    var nextValue = index + 1;

    if (allowHalf) {
      var target = event.currentTarget;
      var rect = target.getBoundingClientRect();
      var width = target.clientWidth;
      var isLeft = event.pageX - rect.left < width / 2;

      if (isLeft) {
        nextValue -= 0.5;
      }
    }

    return nextValue;
  }, [allowHalf]);
  var handleMouseMove = useCallback(function (event, index) {
    if (disabled) {
      return;
    }

    var nextHoverValue = getValue(event, index);

    if (nextHoverValue === prevClearedValue.current) {
      nextHoverValue = 0;
    } else {
      prevClearedValue.current = undefined;
    }

    setHoverValue(nextHoverValue);

    if (onHoverChange) {
      onHoverChange(nextHoverValue);
    }
  }, [disabled, getValue, onHoverChange]);
  var handleMouseLeave = useCallback(function () {
    setHoverValue(undefined);
  }, []);
  var changeValue = useCallback(function (nextValue) {
    var changedValue = nextValue;

    if (nextValue < 0) {
      changedValue = 0;
    } else if (nextValue > count) {
      changedValue = count;
    }

    if (!Reflect.has(props, 'value')) {
      setValue(changedValue);
    }

    if (onChange) {
      onChange(changedValue);
    }
  }, [count, onChange, props]);
  var handleClick = useCallback(function (event, index) {
    if (disabled) {
      return;
    }

    var nextValue = getValue(event, index);

    if (allowClear && nextValue === value) {
      prevClearedValue.current = nextValue;
      nextValue = 0;
      setHoverValue(undefined);
    }

    changeValue(nextValue);
  }, [allowClear, changeValue, disabled, getValue, value]);
  var className = useMemo(function () {
    return classnames(rateCls, customClassName, _defineProperty({}, "".concat(rateCls, "-disabled"), disabled));
  }, [customClassName, disabled, rateCls]);
  var currentValue = useMemo(function () {
    return typeof hoverValue === 'number' ? hoverValue : value;
  }, [hoverValue, value]);
  useEffect(function () {
    if (disabled) {
      return;
    }

    if (onShowChange) {
      onShowChange(currentValue);
    }
  }, [disabled, onShowChange, currentValue]);
  var handleBlur = useCallback(function (event) {
    if (disabled) {
      return;
    }

    if (onBlur) {
      onBlur(event);
    }
  }, [disabled, onBlur]);
  var handleFocus = useCallback(function (event) {
    if (disabled) {
      return;
    }

    if (onFocus) {
      onFocus(event);
    }
  }, [disabled, onFocus]);
  var handleKeyDown = useCallback(function (event) {
    if (disabled) {
      return disabled;
    }

    var keyCode = event.keyCode;

    switch (keyCode) {
      case KeyCode.LEFT:
        if (allowHalf) {
          changeValue(value - 0.5);
        } else {
          changeValue(value - 1);
        }

        event.preventDefault();
        break;

      case KeyCode.RIGHT:
        if (allowHalf) {
          changeValue(value + 0.5);
        } else {
          changeValue(value + 1);
        }

        event.preventDefault();
        break;
    }

    if (onKeyDown) {
      onKeyDown(event);
    }
  }, [allowHalf, changeValue, disabled, onKeyDown, value]);
  return /*#__PURE__*/React.createElement("div", {
    ref: rateRef,
    className: className,
    style: style,
    tabIndex: disabled ? -1 : 0,
    onMouseLeave: handleMouseLeave,
    onBlur: handleBlur,
    onFocus: handleFocus,
    onKeyDown: handleKeyDown
  }, Array.from({
    length: count
  }).map(function (_, index) {
    return /*#__PURE__*/React.createElement(Star, {
      key: index,
      value: currentValue,
      character: character || /*#__PURE__*/React.createElement(StarFill, null),
      index: index,
      allowHalf: allowHalf,
      onMouseMove: handleMouseMove,
      onClick: handleClick,
      tooltips: tooltips
    });
  }));
};

Rate.defaultProps = {
  count: 5,
  allowHalf: false
};
export default Rate;