"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Rate = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _nativeProps = require("../../utils/native-props");
var _withDefaultProps = require("../../utils/with-default-props");
var _usePropsValue = require("../../utils/use-props-value");
var _star = require("./star");
var _react2 = require("@use-gesture/react");
var _bound = require("../../utils/bound");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const classPrefix = `adm-rate`;
const defaultProps = {
  count: 5,
  allowHalf: false,
  character: _react.default.createElement(_star.Star, null),
  defaultValue: 0,
  readOnly: false,
  allowClear: true
};
const Rate = p => {
  const props = (0, _withDefaultProps.mergeProps)(defaultProps, p);
  const [value, setValue] = (0, _usePropsValue.usePropsValue)(props);
  const containerRef = (0, _react.useRef)(null);
  const starList = Array(props.count).fill(null);
  function renderStar(v, half) {
    return _react.default.createElement("div", {
      className: (0, _classnames.default)(`${classPrefix}-star`, {
        [`${classPrefix}-star-active`]: value >= v,
        [`${classPrefix}-star-half`]: half,
        [`${classPrefix}-star-readonly`]: props.readOnly
      }),
      role: 'radio',
      "aria-checked": value >= v,
      "aria-label": '' + v
    }, props.character);
  }
  const bind = (0, _react2.useDrag)(state => {
    if (props.readOnly) return;
    const {
      xy: [clientX],
      tap
    } = state;
    const container = containerRef.current;
    if (!container) return;
    const rect = container.getBoundingClientRect();
    const rawValue = (clientX - rect.left) / rect.width * props.count;
    const ceiledValue = props.allowHalf ? Math.ceil(rawValue * 2) / 2 : Math.ceil(rawValue);
    const boundValue = (0, _bound.bound)(ceiledValue, 0, props.count);
    if (tap) {
      if (props.allowClear && boundValue === value) {
        setValue(0);
        return;
      }
    }
    setValue(boundValue);
  }, {
    axis: 'x',
    pointer: {
      touch: true
    },
    filterTaps: true
  });
  return (0, _nativeProps.withNativeProps)(props, _react.default.createElement("div", Object.assign({
    className: (0, _classnames.default)(classPrefix, {
      [`${classPrefix}-half`]: props.allowHalf
    }),
    role: 'radiogroup',
    "aria-readonly": props.readOnly,
    ref: containerRef
  }, bind()), starList.map((_, i) => _react.default.createElement("div", {
    key: i,
    className: (0, _classnames.default)(`${classPrefix}-box`)
  }, props.allowHalf && renderStar(i + 0.5, true), renderStar(i + 1, false)))));
};
exports.Rate = Rate;