import { __assign, __read, __rest } from "tslib";
import React, { Children } from "react";
import { useStateProps } from "../../hooks/useStateProps";
import { RadioGroupStyled, RadioOptionStyled } from "../styles";
import { RadioGroupContext } from "./RadioGroupContext";
var RadioGroup = function (props) {
    var value = props.value, onChange = props.onChange, onClick = props.onClick, children = props.children, classNameButton = props.classNameButton, options = props.options, className = props.className, name = props.name, isDisabled = props.isDisabled, _a = props.direction, direction = _a === void 0 ? "column" : _a, otherProps = __rest(props, ["value", "onChange", "onClick", "children", "classNameButton", "options", "className", "name", "isDisabled", "direction"]);
    var _b = __read(useStateProps(value), 2), groupValue = _b[0], setGroupValue = _b[1];
    var handleClick = function (event, _value) {
        onClick && onClick(event, _value);
    };
    var handleChange = function (event, _value) {
        if (groupValue !== _value) {
            onChange && onChange(event, _value);
            setGroupValue(_value || null);
        }
    };
    return (React.createElement(RadioGroupContext.Provider, { value: {
            groupValue: groupValue,
            onChange: handleChange,
            onClick: handleClick
        } },
        React.createElement(RadioGroupStyled, __assign({ className: className, direction: direction }, otherProps), options
            ? options.map(function (option) { return (React.createElement(RadioOptionStyled, { key: option.value, value: option.value, label: option.label, className: classNameButton, isDisabled: option.isDisabled || isDisabled, name: name })); })
            : Children.map(children, function (child) { return (React.createElement(RadioOptionStyled, { name: name, isDisabled: isDisabled, className: classNameButton }, child)); }))));
};
export default RadioGroup;
