"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importStar(require("react"));
const acui_1 = require("@Acme Corporation/acui");
const Radio = (0, react_1.forwardRef)(({ name, options, value, defaultValue, label, helperText, error: externalError, isRequired = false, isDisabled = false, isReadOnly = false, isInvalid: externalInvalid, size = 'md', orientation = 'vertical', className = '', wrapperClassName = '', labelClassName = '', radioClassName = '', errorClassName = '', helperTextClassName = '', onValueChange, validateOnChange = false, onChange, ...props }, ref) => {
    const theme = (0, acui_1.useTheme)();
    const formContext = react_1.default.useContext(FormContext);
    const [localValue, setLocalValue] = react_1.default.useState(value ?? defaultValue ?? '');
    const groupId = react_1.default.useId();
    const { errors = {}, touched = {}, setFieldValue, setFieldTouched, setFieldError } = formContext || {};
    const error = externalError || errors[name];
    const isInvalid = externalInvalid || (touched[name] && !!error);
    const isControlled = value !== undefined;
    const handleChange = (0, react_1.useCallback)((e) => {
        const newValue = e.target.value;
        if (!isControlled) {
            setLocalValue(newValue);
        }
        onChange?.(e);
        onValueChange?.(newValue);
        if (formContext && validateOnChange) {
            setFieldValue(name, newValue);
        }
    }, [isControlled, onChange, onValueChange, formContext, name, setFieldValue, validateOnChange]);
    const labelId = react_1.default.useId();
    const errorId = react_1.default.useId();
    const helperTextId = react_1.default.useId();
    // Fix ARIA attribute validation by using proper type-safe values
    const ariaProps = {
        'aria-invalid': isInvalid ? true : undefined,
        'aria-required': isRequired ? true : undefined,
        'aria-labelledby': label ? labelId : undefined,
        'aria-describedby': [
            error ? errorId : null,
            helperText ? helperTextId : null
        ].filter(Boolean).join(' ') || undefined
    };
    return ((0, jsx_runtime_1.jsxs)("div", { className: `bf-radio-group ${wrapperClassName} ${orientation}`, role: "radiogroup", "aria-labelledby": label ? `${groupId}-label` : undefined, "aria-invalid": isInvalid ? 'true' : undefined, "aria-describedby": error || helperText ? `${error ? errorId : ''} ${helperText ? helperTextId : ''}`.trim() : undefined, children: [label && ((0, jsx_runtime_1.jsxs)("div", { id: `${groupId}-label`, className: `bf-radio-group-label ${labelClassName}`, children: [label, isRequired && (0, jsx_runtime_1.jsx)("span", { className: "bf-required-mark", "aria-hidden": "true", children: "*" })] })), (0, jsx_runtime_1.jsx)("div", { className: `bf-radio-options ${className} ${orientation}`, children: options.map((option, index) => {
                    const optionId = `${groupId}-${index}`;
                    const isChecked = (isControlled ? value : localValue) === option.value;
                    const isOptionDisabled = isDisabled || option.disabled;
                    return ((0, jsx_runtime_1.jsxs)("label", { className: `bf-radio-option ${radioClassName} ${size} ${isOptionDisabled ? 'disabled' : ''} ${isReadOnly ? 'readonly' : ''}`, htmlFor: optionId, children: [(0, jsx_runtime_1.jsx)("input", { ref: (node) => {
                                    if (index === 0) {
                                        ref?.(node);
                                    }
                                }, type: "radio", id: optionId, name: name, value: option.value, checked: isChecked, onChange: handleChange, disabled: isOptionDisabled, readOnly: isReadOnly, required: isRequired, "aria-required": isRequired ? 'true' : undefined, "aria-describedby": option.description ? `${optionId}-description` : undefined, ...ariaProps, className: `bf-radio-input ${className}`, ...props }), (0, jsx_runtime_1.jsx)("span", { className: "bf-radio-custom", "aria-hidden": "true" }), (0, jsx_runtime_1.jsxs)("div", { className: "bf-radio-content", children: [(0, jsx_runtime_1.jsx)("span", { className: "bf-radio-label", children: option.label }), option.description && ((0, jsx_runtime_1.jsx)("span", { id: `${optionId}-description`, className: "bf-radio-description", children: option.description }))] })] }, optionId));
                }) }), error && ((0, jsx_runtime_1.jsx)("div", { id: errorId, className: `bf-radio-error ${errorClassName}`, role: "alert", "aria-live": "assertive", children: error })), helperText && !error && ((0, jsx_runtime_1.jsx)("div", { id: helperTextId, className: `bf-radio-helper-text ${helperTextClassName}`, role: "status", children: helperText }))] }));
});
Radio.displayName = 'Radio';
exports.default = (0, react_1.memo)(Radio);
//# sourceMappingURL=Radio.js.map