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 { radioCardGroupTemplate } from './radio-card-group.template.js';
import { radioCardGroupStyles } from './radio-card-group.styles.js';
/**
 * ml-radio-card-group - A group of selectable card-style radio options
 *
 * @example
 * ```html
 * <ml-radio-card-group value="basic" label="Select a plan">
 *   <ml-radio-card value="basic" label="Basic" description="Up to 5 users" detail="$10/mo"></ml-radio-card>
 *   <ml-radio-card value="pro" label="Business" description="Up to 50 users" detail="$25/mo"></ml-radio-card>
 * </ml-radio-card-group>
 * ```
 *
 * @fires ml:change - Emitted when selection changes. Detail: { value: string }
 */
let RadioCardGroupComponent = class RadioCardGroupComponent {
    constructor() {
        /** Currently selected value */
        this.value = '';
        /** Group label */
        this.label = '';
        /** Hint text */
        this.hint = '';
        /** Error message */
        this.error = '';
        /** Layout orientation */
        this.orientation = 'vertical';
        /** Disabled state */
        this.disabled = false;
        /** Required state */
        this.required = false;
        this.handleSlotChange = () => {
            this.syncCards();
        };
        this._handleCardSelect = (event) => {
            event.stopPropagation();
            this.value = event.detail.value;
            this.syncCards();
            this.elementRef.dispatchEvent(new CustomEvent('ml:change', {
                bubbles: true,
                composed: true,
                detail: { value: this.value }
            }));
        };
    }
    onCreate() {
        this.elementRef.addEventListener('ml:card-select', this._handleCardSelect);
        this.syncCards();
    }
    onDestroy() {
        this.elementRef.removeEventListener('ml:card-select', this._handleCardSelect);
    }
    syncCards() {
        const cards = this.elementRef.querySelectorAll('ml-radio-card');
        // If no value set, check for initially-selected card
        if (this.value === '') {
            for (const card of cards) {
                if (card.hasAttribute('selected')) {
                    this.value = card.getAttribute('value') ?? '';
                    break;
                }
            }
        }
        cards.forEach((card) => {
            const cardValue = card.getAttribute('value') ?? '';
            const isSelected = cardValue === this.value;
            card.toggleAttribute('selected', isSelected);
            card.toggleAttribute('group-disabled', this.disabled);
        });
    }
};
RadioCardGroupComponent = __decorate([
    MelodicComponent({
        selector: 'ml-radio-card-group',
        template: radioCardGroupTemplate,
        styles: radioCardGroupStyles,
        attributes: ['value', 'label', 'hint', 'error', 'orientation', 'disabled', 'required']
    })
], RadioCardGroupComponent);
export { RadioCardGroupComponent };
