"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StarRating = StarRating;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const icons_1 = require("@gravity-ui/icons");
const uikit_1 = require("@gravity-ui/uikit");
const cn_1 = require("../../../utils/cn.js");
const ButtonGroup_1 = require("../ButtonGroup/index.js");
const i18n_1 = require("./i18n/index.js");
require("./StarRating.css");
const b = (0, cn_1.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;
};
function StarRating({ value, onChange, disabled = false, size = 'l', 'aria-label': ariaLabel, 'aria-label-star': ariaLabelStar, className, qa, }) {
    var _a;
    const [hoverValue, setHoverValue] = (0, react_1.useState)(null);
    const handleClick = (0, react_1.useCallback)((rating) => {
        if (!disabled && onChange) {
            onChange(rating);
        }
    }, [disabled, onChange]);
    const handleMouseEnter = (0, react_1.useCallback)((rating) => {
        if (!disabled) {
            setHoverValue(rating);
        }
    }, [disabled]);
    const handleMouseLeave = (0, react_1.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 ((0, jsx_runtime_1.jsx)("div", { className: b({ size, disabled }, className), role: "radiogroup", "aria-label": ariaLabel || (0, i18n_1.i18n)('aria-label-group'), "data-qa": qa, onMouseLeave: handleMouseLeave, children: (0, jsx_runtime_1.jsx)(ButtonGroup_1.ButtonGroup, { children: Array.from({ length: STAR_COUNT }, (_, index) => {
                const rating = index + 1;
                const isFilled = rating <= displayValue;
                return ((0, jsx_runtime_1.jsx)(uikit_1.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)
                        : (0, i18n_1.i18n)('aria-label-star', { rating }), className: b('star', { filled: isFilled }), qa: `star-rating-button-${rating}`, children: (0, jsx_runtime_1.jsx)(uikit_1.Icon, { data: isFilled ? icons_1.StarFill : icons_1.Star, size: iconSize }) }, rating));
            }) }) }));
}
//# sourceMappingURL=StarRating.js.map
