import { __assign, __rest } from "tslib";
import React, { useState, useEffect, useContext } from 'react';
import classNames from 'classnames';
import Icon from '../icon';
import { ConfigContext } from '../config-provider/config-context';
import { getPrefixCls } from '../_utils/general';
import RateItem from './rate-item';
var Rate = React.forwardRef(function (props, ref) {
    var _a = props.color, color = _a === void 0 ? '#FADB14' : _a, _b = props.character, character = _b === void 0 ? React.createElement(Icon, { name: "star-fill", size: 20 }) : _b, _c = props.clearable, clearable = _c === void 0 ? true : _c, _d = props.half, half = _d === void 0 ? false : _d, _e = props.count, count = _e === void 0 ? 5 : _e, _f = props.defaultValue, defaultValue = _f === void 0 ? 0 : _f, _g = props.disabled, disabled = _g === void 0 ? false : _g, onChange = props.onChange, className = props.className, style = props.style, customisedCls = props.prefixCls, otherProps = __rest(props, ["color", "character", "clearable", "half", "count", "defaultValue", "disabled", "onChange", "className", "style", "prefixCls"]);
    var configContext = useContext(ConfigContext);
    var prefixCls = getPrefixCls('rate', configContext.prefixCls, customisedCls);
    var cls = classNames(prefixCls, className);
    var _h = useState('value' in props ? props.value : defaultValue), value = _h[0], setValue = _h[1];
    var _j = useState('value' in props ? props.value : defaultValue), tmpValue = _j[0], setTmpValue = _j[1];
    var onMouseEnter = function (index) {
        !disabled && setTmpValue(index);
    };
    var onClick = function () {
        if (!disabled && clearable) {
            var val = tmpValue === value ? 0 : tmpValue;
            setTmpValue(val);
            !('value' in props) && setValue(val);
            onChange && onChange(val);
        }
        else {
            !('value' in props) && setValue(tmpValue);
            onChange && onChange(tmpValue);
        }
    };
    var onMouseLeave = function () {
        setTmpValue(value);
    };
    useEffect(function () {
        'value' in props && setValue(props.value);
    }, [props]);
    return (React.createElement("ul", __assign({}, otherProps, { ref: ref, className: cls, style: style, onMouseLeave: onMouseLeave }), Array(count)
        .fill(0)
        .map(function (_, idx) { return (React.createElement(RateItem, { key: idx, index: idx + 1, half: half, character: character, prefixCls: prefixCls, onMouseEnter: onMouseEnter, onClick: onClick, value: half ? tmpValue : Math.round(tmpValue), color: color })); })));
});
Rate.displayName = 'Rate';
export default Rate;
//# sourceMappingURL=index.js.map