// #region Imports
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;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Emit } from '../../../../Events/Decorators/EmitDecorator';
import { HTMLElementEventEmitter } from '@breadstone/mosaik-elements';
import { Appearanceable } from '../../../Behaviors/Appearanceable';
import { Disableable } from '../../../Behaviors/Disableable';
import { Invalidable } from '../../../Behaviors/Invalidable';
import { Orientable } from '../../../Behaviors/Orientable';
import { Valueable } from '../../../Behaviors/Valueable';
import { Variantable } from '../../../Behaviors/Variantable';
import { Attribute } from '../../../Decorators/AttributeDecorator';
import { Component } from '../../../Decorators/ComponentDecorator';
import { Watch } from '../../../Decorators/WatchDecorator';
import { CustomElement } from '../../Abstracts/CustomElement';
import { RadioElement } from './RadioElement';
import { radioGroupElementTemplate } from './RadioGroupElementTemplate';
import { radioGroupElementJoyStyle } from './Themes/RadioGroupElement.Joy';
import { radioGroupElementRetroStyle } from './Themes/RadioGroupElement.Retro';
import { radioGroupElementCosmopolitanStyle } from './Themes/RadioGroupElement.Cosmopolitan';
import { Themeable } from '../../../Behaviors/Themeable';
import { Slottable } from '../../../Behaviors/Slottable';
// #endregion
/**
 * Radio Group - A container for grouping multiple radio buttons.
 *
 * @element mosaik-radio-group
 *
 * @description
 * The Radio Group component is used to organize and manage a set of radio buttons, allowing users to select only one option from the group.
 * It typically contains multiple Radio components, each representing an individual option.
 * Radio Groups are commonly used when users need to choose one option from a list or set of choices.
 *
 * @category Forms
 * @fires radioChanged - Called when the selection in the group changes.
 *
 * @example
 * ```html
 * <mosaik-radio-group>
 *   <mosaik-radio label="Option 1"></mosaik-radio>
 *   <mosaik-radio label="Option 2"></mosaik-radio>
 *   <mosaik-radio label="Option 3"></mosaik-radio>
 * </mosaik-radio-group>
 * ```
 *
 * @public
 */
let RadioGroupElement = class RadioGroupElement extends Themeable(Invalidable(Disableable(Orientable(Valueable(Appearanceable(Variantable(Slottable(CustomElement)))))))) {
    // #region Fields
    _radioChanged;
    _name;
    _required;
    _eventSubscriptions;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._name = '';
        this._required = false;
        this._radioChanged = new HTMLElementEventEmitter(this, 'radioChanged');
        this._eventSubscriptions = new Array();
    }
    // #endregion
    // #region Properties
    /**
     * Returns the `{@link is}` property.
     * The `{@link is}` property represents natural name of this element.
     *
     * @public
     * @static
     * @readonly
     */
    static get is() {
        return 'mosaik-radio-group';
    }
    /**
     * Gets or sets the `name` property.
     *
     * @public
     * @attr
     */
    get name() {
        return this._name;
    }
    set name(value) {
        if (this._name !== value) {
            this._name = value;
            this.requestUpdate('name');
        }
    }
    /**
     * Gets or sets the `required` property.
     *
     * @public
     * @attr
     */
    get required() {
        return this._required;
    }
    set required(value) {
        if (this._required !== value) {
            this._required = value;
            this.requestUpdate('required');
        }
    }
    /**
     * Called when the selection in the group changes.
     * Provides reference to `{@link IRadioGroupChangedEventDetail}` as event detail.
     *
     * @public
     * @readonly
     * @eventProperty
     */
    get radioChanged() {
        return this._radioChanged;
    }
    // #endregion
    // #region Methods
    /**
     * @public
     * @override
     */
    onSlotChanges() {
        this.clearEvents();
        const elements = this.getSlotAssignments('')
            .filter((x) => x instanceof RadioElement);
        for (const [i, e] of elements.entries()) {
            e.disabled = this.disabled;
            e.appearance = this.appearance;
            e.variant = this.variant;
            e.name = this.name;
            e.dir = this.dir;
            e.isChecked = this.value === e.value;
            e.classList.add('radio-group-radio');
            e.classList.toggle('first', i === 0);
            e.classList.toggle('inner', i > 0 && i < elements.length - 1);
            e.classList.toggle('last', i === elements.length - 1);
            this._eventSubscriptions.push(e.on('checked', (x) => this.onRadioCheckChanged(x), { passive: true }));
        }
    }
    /**
     * Returns whether a form will validate when it is submitted, without having to submit it.
     *
     * @public
     * @override
     */
    checkValidity() {
        const elements = this.getSlotAssignments('')
            .filter((x) => x instanceof RadioElement)
            .filter((x) => x.isChecked ?? false);
        return elements.length > 0;
    }
    /**
     * Resets the element to its initial state.
     *
     * @public
     */
    reset() {
        const elements = this.getSlotAssignments('')
            .filter((x) => x instanceof RadioElement);
        elements.forEach((x) => x.uncheck());
        this.invalid = false;
    }
    /**
     * Emits the {@link radioChanged} event.
     *
     * @protected
     */
    onRadioChanged(args) {
        this._radioChanged.emit(args);
    }
    /**
     * @protected
     */
    onDisabledPropertyChange(_prev, next) {
        const elements = this.getSlotAssignments('')
            .filter((x) => x instanceof RadioElement);
        if (next !== undefined) {
            elements.forEach((x) => x.disabled = next);
        }
    }
    /**
     * @protected
     */
    onAppearancePropertyChange(_prev, next) {
        if (next) {
            const elements = this.getSlotAssignments('')
                .filter((x) => x instanceof RadioElement);
            elements.forEach((x) => x.appearance = next);
        }
    }
    /**
     * @protected
     */
    onVariantPropertyChange(_prev, next) {
        if (next) {
            const elements = this.getSlotAssignments('')
                .filter((x) => x instanceof RadioElement);
            elements.forEach((x) => x.variant = next);
        }
    }
    /**
     * @protected
     */
    onNamePropertyChange(_prev, next) {
        if (next) {
            const elements = this.getSlotAssignments('')
                .filter((x) => x instanceof RadioElement);
            elements.forEach((x) => x.name = next);
        }
    }
    /**
     * @protected
     */
    onDirPropertyChange(_prev, next) {
        if (next) {
            const elements = this.getSlotAssignments('')
                .filter((x) => x instanceof RadioElement);
            elements.forEach((x) => x.dir = next);
        }
    }
    /**
     * @protected
     */
    onValuePropertyChange(_prev, next) {
        const elements = this.getSlotAssignments('')
            .filter((x) => x instanceof RadioElement);
        elements.forEach((x) => x.isChecked = next?.includes(x.value) ?? false);
    }
    /**
     * @private
     */
    onRadioCheckChanged(event) {
        const elements = this.getSlotAssignments('')
            .filter((x) => x instanceof RadioElement);
        // exclude the current element and uncheck all other elements
        elements.filter((x) => x !== event.target)
            .forEach((x) => x.uncheck());
        this.value = event.target.value;
        this.onRadioChanged({ value: event.target.value });
    }
    /**
     * @private
     */
    clearEvents() {
        this._eventSubscriptions.forEach((x) => x.dispose());
        this._eventSubscriptions = [];
    }
};
__decorate([
    Attribute({ type: String }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], RadioGroupElement.prototype, "name", null);
__decorate([
    Attribute({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], RadioGroupElement.prototype, "required", null);
__decorate([
    Emit({ eventName: 'radioChanged' }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [])
], RadioGroupElement.prototype, "radioChanged", null);
__decorate([
    Watch('disabled'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Boolean, Boolean]),
    __metadata("design:returntype", void 0)
], RadioGroupElement.prototype, "onDisabledPropertyChange", null);
__decorate([
    Watch('appearance'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", void 0)
], RadioGroupElement.prototype, "onAppearancePropertyChange", null);
__decorate([
    Watch('variant'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", void 0)
], RadioGroupElement.prototype, "onVariantPropertyChange", null);
__decorate([
    Watch('name'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", void 0)
], RadioGroupElement.prototype, "onNamePropertyChange", null);
__decorate([
    Watch('dir'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", void 0)
], RadioGroupElement.prototype, "onDirPropertyChange", null);
__decorate([
    Watch('value'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Array, Array]),
    __metadata("design:returntype", void 0)
], RadioGroupElement.prototype, "onValuePropertyChange", null);
RadioGroupElement = __decorate([
    Component({
        selector: 'mosaik-radio-group',
        template: radioGroupElementTemplate,
        themes: {
            joy: radioGroupElementJoyStyle,
            retro: radioGroupElementRetroStyle,
            cosmopolitan: radioGroupElementCosmopolitanStyle
        },
        imports: [RadioElement]
    }),
    __metadata("design:paramtypes", [])
], RadioGroupElement);
export { RadioGroupElement };
//# sourceMappingURL=RadioGroupElement.js.map