import _extends from "@babel/runtime/helpers/extends";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import { useDrag } from '@use-gesture/react';
import { createBEM } from '@zarm-design/bem';
import { StarFill } from '@zarm-design/icons';
import { useControllableValue } from 'ahooks';
import * as React from 'react';
import { ConfigContext } from '../config-provider';
import { getBoundingClientRect } from '../utils/dom';
var Rate = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var className = props.className,
      style = props.style,
      count = props.count,
      character = props.character,
      allowHalf = props.allowHalf,
      allowClear = props.allowClear,
      readonly = props.readonly;

  var _useControllableValue = useControllableValue(props),
      _useControllableValue2 = _slicedToArray(_useControllableValue, 2),
      value = _useControllableValue2[0],
      setValue = _useControllableValue2[1];

  var itemRefs = React.useRef([]);

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

  var bem = createBEM('rate', {
    prefixCls: prefixCls
  });
  var items = Array(count).fill(null);

  var getScoreRanges = function getScoreRanges() {
    var ranges = [];
    itemRefs.current.forEach(function (item, index) {
      if (!item) return;
      var rect = getBoundingClientRect(item);

      if (allowHalf) {
        ranges.push({
          score: index + 0.5,
          left: rect.left
        }, {
          score: index + 1,
          left: rect.left + rect.width / 2
        });
      } else {
        ranges.push({
          score: index + 1,
          left: rect.left
        });
      }
    });
    return ranges;
  };

  var getScoreByPosition = function getScoreByPosition(x) {
    var ranges = getScoreRanges();

    for (var i = ranges.length - 1; i > 0; i--) {
      if (x > ranges[i].left) return ranges[i].score;
    }

    return allowHalf ? 0.5 : 1;
  };

  var render = function render(_, index) {
    var score = index + 1;
    var checked = true;

    var handleClick = function handleClick(event) {
      if (readonly) return;
      var next = getScoreByPosition(event.clientX);

      if (allowClear && value === next) {
        setValue(0);
        return;
      }

      setValue(allowHalf ? next : score);
    };

    var itemCls = bem('item', [{
      active: value >= index + 1,
      half: allowHalf && value === index + 0.5
    }]);
    return /*#__PURE__*/React.createElement("div", {
      key: index,
      ref: function ref(element) {
        itemRefs.current[index] = element;
      },
      role: "radio",
      tabIndex: 0,
      className: itemCls,
      onClick: handleClick,
      "aria-setsize": count,
      "aria-posinset": score,
      "aria-checked": checked
    }, /*#__PURE__*/React.createElement("div", {
      className: bem('character__half')
    }, character), /*#__PURE__*/React.createElement("div", {
      className: bem('character__full')
    }, character));
  };

  var cls = bem([{
    readonly: readonly
  }, className]);
  var bind = useDrag(function (_ref) {
    var _ref$values = _slicedToArray(_ref.values, 1),
        x = _ref$values[0];

    if (readonly) return;
    setValue(getScoreByPosition(x));
  }, {
    pointer: {
      touch: true
    },
    axis: 'x'
  });
  return /*#__PURE__*/React.createElement("div", _extends({
    ref: ref,
    role: "radiogroup",
    tabIndex: 0,
    className: cls,
    style: style
  }, bind()), items.map(render));
});
Rate.displayName = 'Rate';
Rate.defaultProps = {
  count: 5,
  defaultValue: 0,
  allowHalf: false,
  allowClear: false,
  character: /*#__PURE__*/React.createElement(StarFill, null)
};
export default Rate;