import { jsx as _jsx } from "react/jsx-runtime";
import { useCallback, useState } from 'react';
import { Star, StarFill } from '@gravity-ui/icons';
import { Button, Icon } from '@gravity-ui/uikit';
import { block } from "../../../utils/cn.js";
import { ButtonGroup } from "../ButtonGroup/index.js";
import { i18n } from "./i18n/index.js";
import "./StarRating.css";
const b = block('star-rating');
const STAR_COUNT = 5;
const getButtonSize = (size) => {
    if (size === 's')
        return 'xs';
    if (size === 'm')
        return 's';
    return 'm';
};
const getIconSize = (size) => {
    if (size === 's')
        return 16;
    if (size === 'm')
        return 20;
    return 24;
};
export function StarRating({ value, onChange, disabled = false, size = 'l', 'aria-label': ariaLabel, 'aria-label-star': ariaLabelStar, className, qa, }) {
    var _a;
    const [hoverValue, setHoverValue] = useState(null);
    const handleClick = useCallback((rating) => {
        if (!disabled && onChange) {
            onChange(rating);
        }
    }, [disabled, onChange]);
    const handleMouseEnter = useCallback((rating) => {
        if (!disabled) {
            setHoverValue(rating);
        }
    }, [disabled]);
    const handleMouseLeave = useCallback(() => {
        setHoverValue(null);
    }, []);
    const displayValue = (_a = hoverValue !== null && hoverValue !== void 0 ? hoverValue : value) !== null && _a !== void 0 ? _a : 0;
    const buttonSize = getButtonSize(size);
    const iconSize = getIconSize(size);
    return (_jsx("div", { className: b({ size, disabled }, className), role: "radiogroup", "aria-label": ariaLabel || i18n('aria-label-group'), "data-qa": qa, onMouseLeave: handleMouseLeave, children: _jsx(ButtonGroup, { children: Array.from({ length: STAR_COUNT }, (_, index) => {
                const rating = index + 1;
                const isFilled = rating <= displayValue;
                return (_jsx(Button, { view: "flat-secondary", size: buttonSize, onClick: () => handleClick(rating), onMouseEnter: () => handleMouseEnter(rating), disabled: disabled, role: "radio", "aria-checked": rating === value, "aria-label": ariaLabelStar
                        ? ariaLabelStar(rating)
                        : i18n('aria-label-star', { rating }), className: b('star', { filled: isFilled }), qa: `star-rating-button-${rating}`, children: _jsx(Icon, { data: isFilled ? StarFill : Star, size: iconSize }) }, rating));
            }) }) }));
}
//# sourceMappingURL=StarRating.js.map
