"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 _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

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

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

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

var _KeyCode = _interopRequireDefault(require("../components/rc-util/KeyCode"));

var _icon = _interopRequireDefault(require("../icon"));

var _star = _interopRequireDefault(require("./star"));

var StarFill = _icon.default.StarFill;

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.default.useContext(_context.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 = (0, _react.useRef)();
  (0, _react.useEffect)(function () {
    if (autoFocus && rateRef.current) {
      rateRef.current.focus();
    }
  }, []);
  var count = (0, _react.useMemo)(function () {
    return getValidValue(customCount, 5);
  }, [customCount]); // 当前值

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


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


  var prevClearedValue = (0, _react.useRef)();
  (0, _react.useEffect)(function () {
    if (Reflect.has(props, 'value')) {
      var nextValue = getValidValue(props.value, 0);
      setValue(nextValue);
    }
  }, [props]);
  var getValue = (0, _react.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 = (0, _react.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 = (0, _react.useCallback)(function () {
    setHoverValue(undefined);
  }, []);
  var changeValue = (0, _react.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 = (0, _react.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 = (0, _react.useMemo)(function () {
    return (0, _classnames2.default)(rateCls, customClassName, (0, _defineProperty2.default)({}, "".concat(rateCls, "-disabled"), disabled));
  }, [customClassName, disabled, rateCls]);
  var currentValue = (0, _react.useMemo)(function () {
    return typeof hoverValue === 'number' ? hoverValue : value;
  }, [hoverValue, value]);
  (0, _react.useEffect)(function () {
    if (disabled) {
      return;
    }

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

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

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

    var keyCode = event.keyCode;

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

        event.preventDefault();
        break;

      case _KeyCode.default.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.default.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.default.createElement(_star.default, {
      key: index,
      value: currentValue,
      character: character || /*#__PURE__*/_react.default.createElement(StarFill, null),
      index: index,
      allowHalf: allowHalf,
      onMouseMove: handleMouseMove,
      onClick: handleClick,
      tooltips: tooltips
    });
  }));
};

Rate.defaultProps = {
  count: 5,
  allowHalf: false
};
var _default = Rate;
exports.default = _default;