import * as React from 'react';
import { useState } from 'react';
import useProperty from '../utils/useProperty';
import { Box, Flex } from '../Flex';
import { twMerge } from '../../twMerge';
import clsx from 'clsx';
import { targetChildren } from '../twUtils';
const RadioContext = React.createContext({
    value: null,
    name: '',
    onChange: (v) => { },
    variant: 'default',
});
export const useRadioContext = () => {
    return React.useContext(RadioContext);
};
const Radio = ({ children, checked, onChange, value, name, gapDistance = 10, childrenPosition = 'end', as = 'label', id, tabIndex, disabled, ...props }) => {
    const context = useRadioContext();
    const { value: contextValue, onChange: contextOnChange, name: contextName, variant } = context;
    const [stateChecked, setStateChecked] = useState(false);
    const computedChecked = checked !== undefined
        ? checked
        : contextValue !== undefined
            ? contextValue === value
            : stateChecked;
    const onInputChange = (event) => {
        const newChecked = event.target.checked;
        if (checked === undefined) {
            setStateChecked(newChecked);
        }
        if (onChange) {
            onChange(newChecked, event);
        }
        if (newChecked) {
            contextOnChange(value);
        }
    };
    const gap = React.createElement("div", { style: { marginLeft: gapDistance, display: 'inline-block' } });
    const before = childrenPosition === 'start' ? children : null;
    const beforeGap = childrenPosition === 'start' ? gap : null;
    const after = childrenPosition === 'end' ? children : null;
    const afterGap = childrenPosition === 'end' ? gap : null;
    return (React.createElement(Box, { ...props, className: twMerge(`ab-Radio twa:my-2 twa:inline-flex twa:flex-row twa:items-center twa:cursor-pointer twa:relative`, props.className), "data-checked": computedChecked, as: as },
        before,
        beforeGap,
        React.createElement("input", { disabled: disabled, className: clsx('ab-Radio-input', 'twa:align-middle', 'twa:m-0', 'twa:rounded-full', 'twa:cursor-pointer', 'twa:position-relative', 'twa:w-[1rem]', 'twa:h-[1rem]', 'twa:min-w-[1rem]', 'twa:min-h-[1rem]', variant === 'default' && 'ab-Radio-input--default', variant === 'text-only' && 'ab-Radio-input--text-only twa:size-0 twa:opacity-0'), id: id, checked: computedChecked, type: "radio", name: name ?? contextName, value: value, tabIndex: tabIndex, onChange: onInputChange }),
        afterGap,
        after));
};
export const RadioGroup = (props) => {
    const { orientation, value: _value, name, onRadioChange, children, variant = 'default' } = props;
    const [value, setValue] = useProperty(props, 'value', undefined, {
        onChange: (value) => {
            onRadioChange(value);
        },
    });
    return (React.createElement(RadioContext.Provider, { value: { value, onChange: setValue, name, variant } },
        React.createElement(Flex, { className: clsx('ab-RadioGroup', props.className), flexDirection: orientation == 'horizontal' ? 'row' : 'column', alignItems: orientation == 'horizontal' ? 'center' : 'flex-start' }, children)));
};
export const radioGroupStyling = {
    horizontalTextOnly: clsx('twa:bg-primarylight twa:rounded-standard twa:p-1 twa:gap-5', {
        'twa:*:flex-1 twa:*:rounded-standard twa:*:p-1 twa:*:m-0': 'default styles for all the radio buttons',
        'twa:*:data-[checked=true]:bg-accent twa:*:data-[checked=true]:text-accent-foreground': 'style the checked state',
        [targetChildren.focusWithinOutline]: 'styles for when a radio is currently focused - show a ring, so people know they can do keyboard nav with left/right arrow keys',
    }),
};
export default Radio;
