/**
 * @license MIT
 * author: Cube Dev Team
 * @cube-dev/ui-kit v0.0.0-canary-9f08182
 * Released under the MIT license.
 */

import { jsx as _jsx } from "react/jsx-runtime";
import { useDOMRef } from '@react-spectrum/utils';
import { forwardRef } from 'react';
import { useRadioGroup } from 'react-aria';
import { useRadioGroupState } from 'react-stately';
import { useProviderProps } from '../../../provider.js';
import { CONTAINER_STYLES, extractStyles, tasty, } from '../../../tasty/index.js';
import { mergeProps } from '../../../utils/react/index.js';
import { castNullableStringValue, } from '../../../utils/react/nullableValue.js';
import { FormContext, useFieldProps, useFormProps, wrapWithField, } from '../../form/index.js';
import { RadioContext } from './context.js';
const RadioGroupElement = tasty({
    qa: 'RadioGroup',
    styles: {
        display: 'flex',
        placeItems: 'stretch',
        placeContent: 'stretch',
        flow: {
            '': 'column',
            horizontal: 'row wrap',
            'horizontal & tabs': 'row',
        },
        padding: {
            '': '0',
            tabs: '.5x',
        },
        radius: '1cr',
        fill: {
            '': '#clear',
            'tabs | disabled': '#dark.06',
        },
        width: 'max-content max-content initial',
        flexShrink: 0,
        gap: {
            '': '1x',
            tabs: '.5x',
        },
        whiteSpace: 'nowrap',
    },
});
function RadioGroup(props, ref) {
    let orientation = props.orientation;
    props = castNullableStringValue(props);
    props = useProviderProps(props);
    props = useFormProps(props);
    props = useFieldProps(props, { defaultValidationTrigger: 'onChange' });
    let { qa, id, isDisabled, isRequired, labelPosition = 'top', isInvalid, children, styles, groupStyles, insideForm, 
    // orientation, // ignore orientation since it came from the form context
    labelProps: baseLabelProps, size, buttonType, type, form, ...otherProps } = props;
    let domRef = useDOMRef(ref);
    styles = extractStyles(otherProps, CONTAINER_STYLES, styles);
    let state = useRadioGroupState(props);
    // Set default orientation based on type
    if (orientation == null) {
        orientation =
            type === 'button' || type === 'tabs' ? 'horizontal' : 'vertical';
    }
    let { radioGroupProps: fieldProps, labelProps } = useRadioGroup({ ...props, orientation }, state);
    let radioGroup = (_jsx(RadioGroupElement, { id: id, qa: qa || 'RadioGroup', styles: styles, "data-input-type": "radiogroup", mods: {
            horizontal: orientation === 'horizontal',
            'inside-form': insideForm,
            'side-label': labelPosition === 'side',
            tabs: type === 'tabs',
        }, children: _jsx(FormContext.Provider, { value: {
                isRequired,
                isInvalid,
                isDisabled,
            }, children: _jsx(RadioContext.Provider, { value: {
                    state,
                    name: props.name,
                    size,
                    buttonType,
                    type,
                    isDisabled,
                }, children: children }) }) }));
    return wrapWithField(radioGroup, domRef, {
        ...props,
        children: null,
        fieldProps,
        labelProps: mergeProps(baseLabelProps, labelProps),
    });
}
/**
 * Radio groups allow users to select a single option from a list of mutually exclusive options.
 * All possible options are exposed up front for users to compare.
 */
const _RadioGroup = forwardRef(RadioGroup);
_RadioGroup.cubeInputType = 'RadioGroup';
_RadioGroup.displayName = 'RadioGroup';
export { _RadioGroup as RadioGroup };

        