import { r as registerInstance, d as createEvent } from './core-3f33e631.js';
import { N as NativeValidationStrategy } from './native-validation-strategy-a4373eb1.js';

let radioGroupId = 0;
const RadioGroup = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.inputId = `anj-rg-${radioGroupId++}`;
        this.radios = [];
        /**
         * Make the control read only
         */
        this.disabled = false;
        /**
         * The input name name
         */
        this.name = this.inputId;
        /**
         * @internal
         * The validation strategy to use
         */
        this.validationStrategy = new NativeValidationStrategy();
        this.anjChange = createEvent(this, "anjChange", 7);
    }
    disabledChanged() {
        this.radios.forEach(control => {
            control.disabled = this.disabled;
        });
    }
    onInvalidChange() {
        this.radios.forEach(r => r.invalid = this.invalid);
    }
    valueChanged(value) {
        this.updateRadios();
        this.checkValidity();
        this.anjChange.emit({ value });
    }
    onRadioDidLoad(event) {
        const target = event.target;
        target.name = this.name;
        target.required = this.required;
        this.radios.push(target);
        if (!this.value && target.checked) {
            this.value = target.value;
        }
        else {
            this.updateRadios();
        }
    }
    onRadioDidUnload(event) {
        const target = event.target;
        this.radios = this.radios.filter((radio) => {
            return radio.value !== target.value;
        });
    }
    onRadioSelect(event) {
        const target = event.target;
        this.value = target.value;
    }
    onValidChange() {
        this.checkValidity();
    }
    /**
     * Checks the native element validity use native validators
     */
    async checkValidity() {
        let valid = false;
        await new Promise(resolve => {
            this.radios.forEach(async (radio) => {
                valid = await radio.checkValidity();
            });
            resolve();
        });
        this.invalid = !valid;
        return valid;
    }
    updateRadios() {
        this.radios.forEach((radio) => {
            radio.checked = radio.value === this.value;
        });
    }
    static get watchers() { return {
        "disabled": ["disabledChanged"],
        "invalid": ["onInvalidChange"],
        "value": ["valueChanged"]
    }; }
    static get style() { return "anj-radio-group {\n  display: block;\n  font-size: 0.875rem;\n  font-family: var(--anj-font-family);\n  font-weight: normal;\n  line-height: 1.5;\n  outline: none;\n  margin-bottom: 1rem;\n  padding-left: 0;\n  position: relative;\n}\nanj-radio-group, anj-radio-group::before, anj-radio-group::after {\n  -webkit-box-sizing: border-box;\n  box-sizing: border-box;\n  outline: none;\n}\nanj-radio-group:focus, anj-radio-group:active, anj-radio-group *:focus, anj-radio-group *:active {\n  outline: none;\n}"; }
};

export { RadioGroup as anj_radio_group };
