var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
import React, { useRef, useContext, useMemo } from "react";
import classnames from "classnames";
import useMergedState from "../hooks/use-merged-state";
import ConfigProviderContext from "../config-provider/ConfigProviderContext";
import { addUnit } from "../utils";
import { useTouch } from "../hooks/use-touch";
import useRefs from "../hooks/use-refs";
import usePassiveHandler from "../hooks/usePassiveHandler";
import Icon from "../icon";
const getRateStatus = (value, index, allowHalf, readonly) => {
  if (value >= index) {
    return {
      status: "full",
      value: 1
    };
  }
  if (value + 0.5 >= index && allowHalf && !readonly) {
    return {
      status: "half",
      value: 0.5
    };
  }
  if (value + 1 >= index && allowHalf && readonly) {
    const cardinal = 10 ** 10;
    return {
      status: "half",
      value: Math.round((value - index + 1) * cardinal) / cardinal
    };
  }
  return {
    status: "void",
    value: 0
  };
};
const Rate = (_a) => {
  var _b = _a, {
    count,
    touchable,
    onChange
  } = _b, props = __objRest(_b, [
    "count",
    "touchable",
    "onChange"
  ]);
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace("rate", prefixCls);
  const [value, setValue] = useMergedState({
    value: props.value,
    defaultValue: props.defaultValue
  });
  const root = useRef(null);
  const touch = useTouch();
  const [itemRefs, setItemRefs] = useRefs();
  const untouchable = () => props.readonly || props.disabled || !touchable;
  const starList = useMemo(() => Array(count).fill("").map((_, i) => getRateStatus(value, i + 1, props.allowHalf, props.readonly)), [value, count]);
  const ranges = useRef();
  const updateRanges = () => {
    const rects = itemRefs.map((item) => item.getBoundingClientRect());
    ranges.current = [];
    rects.forEach((rect, index) => {
      if (props.allowHalf) {
        ranges.current.push({
          score: index + 0.5,
          left: rect.left
        }, {
          score: index + 1,
          left: rect.left + rect.width / 2
        });
      } else {
        ranges.current.push({
          score: index + 1,
          left: rect.left
        });
      }
    });
  };
  const getScoreByPosition = (x) => {
    for (let i = ranges.current.length - 1; i > 0; i--) {
      if (x > ranges.current[i].left) {
        return ranges.current[i].score;
      }
    }
    return props.allowHalf ? 0.5 : 1;
  };
  const select = (index) => {
    if (!props.disabled && !props.readonly && index !== value) {
      setValue(index);
      onChange == null ? void 0 : onChange(index);
    }
  };
  const onTouchStart = (event) => {
    if (untouchable()) {
      return;
    }
    touch.start(event);
    updateRanges();
  };
  const onTouchMove = (event) => {
    if (untouchable()) {
      return;
    }
    touch.move(event);
    if (touch.isHorizontal()) {
      const {
        clientX
      } = event.touches[0];
      select(getScoreByPosition(clientX));
    }
  };
  usePassiveHandler();
  const renderStar = (item, index) => {
    const {
      icon,
      size,
      color,
      gutter,
      voidIcon,
      voidColor,
      disabled,
      disabledColor,
      allowHalf,
      iconPrefix
    } = props;
    const score = index + 1;
    const isFull = item.status === "full";
    const isVoid = item.status === "void";
    const renderHalf = allowHalf && item.value > 0 && item.value < 1;
    let style;
    if (gutter && score !== +count) {
      style = {
        paddingRight: addUnit(gutter)
      };
    }
    const onClickItem = (event) => {
      updateRanges();
      select(allowHalf ? getScoreByPosition(event.clientX) : score);
    };
    return /* @__PURE__ */ React.createElement("div", {
      key: index,
      ref: setItemRefs(index),
      role: "radio",
      style,
      className: classnames(bem("item")),
      tabIndex: 0,
      "aria-setsize": parseInt(count == null ? void 0 : count.toString(), 10),
      "aria-posinset": score,
      "aria-checked": !isVoid,
      onClick: onClickItem
    }, /* @__PURE__ */ React.createElement(Icon, {
      size,
      name: isFull ? icon : voidIcon,
      className: classnames(bem("icon", {
        disabled,
        full: isFull
      })),
      color: disabled ? disabledColor : isFull ? color : voidColor,
      classPrefix: iconPrefix
    }), renderHalf && /* @__PURE__ */ React.createElement(Icon, {
      size,
      style: {
        width: `${item.value}em`
      },
      name: isVoid ? voidIcon : icon,
      className: classnames(bem("icon", ["half", {
        disabled,
        full: !isVoid
      }])),
      color: disabled ? disabledColor : isVoid ? voidColor : color,
      classPrefix: iconPrefix
    }));
  };
  return /* @__PURE__ */ React.createElement("div", {
    ref: root,
    role: "radiogroup",
    className: classnames(bem({
      readonly: props.readonly,
      disabled: props.disabled
    })),
    tabIndex: 0,
    onTouchStart: (e) => onTouchStart(e),
    onTouchMove: (e) => onTouchMove(e)
  }, starList.map(renderStar));
};
Rate.defaultProps = {
  size: 20,
  count: 5,
  gutter: 4,
  icon: "star",
  voidIcon: "star-o",
  touchable: true
};
var stdin_default = Rate;
export {
  stdin_default as default
};
