var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { MelodicComponent } from '@melodicdev/core';
import { radioGroupTemplate } from './radio-group.template.js';
import { radioGroupStyles } from './radio-group.styles.js';
/**
 * ml-radio-group - Container for radio buttons
 *
 * @example
 * ```html
 * <ml-radio-group label="Select option" name="options">
 *   <ml-radio value="a" label="Option A"></ml-radio>
 *   <ml-radio value="b" label="Option B"></ml-radio>
 * </ml-radio-group>
 * ```
 *
 * @fires ml:change - Emitted when selection changes
 */
let RadioGroupComponent = class RadioGroupComponent {
    constructor() {
        /** Group label */
        this.label = '';
        /** Form field name */
        this.name = '';
        /** Current selected value */
        this.value = '';
        /** Hint text */
        this.hint = '';
        /** Error message */
        this.error = '';
        /** Layout orientation */
        this.orientation = 'vertical';
        /** Disabled state */
        this.disabled = false;
        /** Required state */
        this.required = false;
        this.handleChildChange = (event) => {
            if (event.target === this.elementRef) {
                return;
            }
            const detail = event.detail;
            this.value = detail.value;
            // Update checked state on all child radios
            this.updateChildRadios();
            // Re-emit the event
            this.elementRef.dispatchEvent(new CustomEvent('ml:change', {
                bubbles: true,
                composed: true,
                detail: { value: this.value }
            }));
        };
    }
    onInit() {
        // Listen for changes from child radios
        this.elementRef.addEventListener('ml:change', this.handleChildChange);
        // Set initial name on child radios
        this.updateChildRadios();
    }
    updateChildRadios() {
        const radios = this.elementRef.querySelectorAll('ml-radio');
        if (this.value === '') {
            for (const radio of radios) {
                const isChecked = radio.checked === true || radio.hasAttribute('checked');
                if (isChecked) {
                    this.value = radio.value ?? radio.getAttribute('value') ?? '';
                    break;
                }
            }
        }
        radios.forEach((radio) => {
            if (this.name) {
                radio.name = this.name;
            }
            radio.disabled = this.disabled;
            const radioValue = radio.value ?? radio.getAttribute('value') ?? '';
            radio.checked = this.value !== '' && radioValue === this.value;
        });
    }
};
RadioGroupComponent = __decorate([
    MelodicComponent({
        selector: 'ml-radio-group',
        template: radioGroupTemplate,
        styles: radioGroupStyles,
        attributes: ['label', 'name', 'value', 'hint', 'error', 'orientation', 'disabled', 'required']
    })
], RadioGroupComponent);
export { RadioGroupComponent };
