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

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

var _react = require("@use-gesture/react");

var _bem = require("@zarm-design/bem");

var _icons = require("@zarm-design/icons");

var _ahooks = require("ahooks");

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

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

var _dom = require("../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 = (0, _ahooks.useControllableValue)(props),
      _useControllableValue2 = (0, _slicedToArray2.default)(_useControllableValue, 2),
      value = _useControllableValue2[0],
      setValue = _useControllableValue2[1];

  var itemRefs = React.useRef([]);

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

  var bem = (0, _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 = (0, _dom.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 = (0, _react.useDrag)(function (_ref) {
    var _ref$values = (0, _slicedToArray2.default)(_ref.values, 1),
        x = _ref$values[0];

    if (readonly) return;
    setValue(getScoreByPosition(x));
  }, {
    pointer: {
      touch: true
    },
    axis: 'x'
  });
  return /*#__PURE__*/React.createElement("div", (0, _extends2.default)({
    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(_icons.StarFill, null)
};
var _default = Rate;
exports.default = _default;