var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
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;
};
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  default: () => stdin_default
});
module.exports = __toCommonJS(stdin_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_use_merged_state = __toESM(require("../hooks/use-merged-state"));
var import_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
var import_utils = require("../utils");
var import_use_touch = require("../hooks/use-touch");
var import_use_refs = __toESM(require("../hooks/use-refs"));
var import_usePassiveHandler = __toESM(require("../hooks/usePassiveHandler"));
var import_icon = __toESM(require("../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
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const [bem] = createNamespace("rate", prefixCls);
  const [value, setValue] = (0, import_use_merged_state.default)({
    value: props.value,
    defaultValue: props.defaultValue
  });
  const root = (0, import_react.useRef)(null);
  const touch = (0, import_use_touch.useTouch)();
  const [itemRefs, setItemRefs] = (0, import_use_refs.default)();
  const untouchable = () => props.readonly || props.disabled || !touchable;
  const starList = (0, import_react.useMemo)(() => Array(count).fill("").map((_, i) => getRateStatus(value, i + 1, props.allowHalf, props.readonly)), [value, count]);
  const ranges = (0, import_react.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));
    }
  };
  (0, import_usePassiveHandler.default)();
  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: (0, import_utils.addUnit)(gutter)
      };
    }
    const onClickItem = (event) => {
      updateRanges();
      select(allowHalf ? getScoreByPosition(event.clientX) : score);
    };
    return /* @__PURE__ */ import_react.default.createElement("div", {
      key: index,
      ref: setItemRefs(index),
      role: "radio",
      style,
      className: (0, import_classnames.default)(bem("item")),
      tabIndex: 0,
      "aria-setsize": parseInt(count == null ? void 0 : count.toString(), 10),
      "aria-posinset": score,
      "aria-checked": !isVoid,
      onClick: onClickItem
    }, /* @__PURE__ */ import_react.default.createElement(import_icon.default, {
      size,
      name: isFull ? icon : voidIcon,
      className: (0, import_classnames.default)(bem("icon", {
        disabled,
        full: isFull
      })),
      color: disabled ? disabledColor : isFull ? color : voidColor,
      classPrefix: iconPrefix
    }), renderHalf && /* @__PURE__ */ import_react.default.createElement(import_icon.default, {
      size,
      style: {
        width: `${item.value}em`
      },
      name: isVoid ? voidIcon : icon,
      className: (0, import_classnames.default)(bem("icon", ["half", {
        disabled,
        full: !isVoid
      }])),
      color: disabled ? disabledColor : isVoid ? voidColor : color,
      classPrefix: iconPrefix
    }));
  };
  return /* @__PURE__ */ import_react.default.createElement("div", {
    ref: root,
    role: "radiogroup",
    className: (0, import_classnames.default)(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;
