import * as i0 from '@angular/core';
import { inject, ElementRef, Renderer2, HostListener, Input, HostBinding, Component, NgModule } from '@angular/core';
import { NgControl } from '@angular/forms';
import { InputDirective } from '@eui/components/shared';
import { coerceBooleanProperty } from '@angular/cdk/coercion';

/**
 * A radio input component that implements form control functionality and custom styling.
 * This component extends InputDirective and implements form control interfaces for seamless
 * integration with Angular's form controls.
 */
class EuiInputRadioComponent extends InputDirective {
    /**
     * Gets or sets whether the radio input is in an invalid state.
     * This can be set manually or will be automatically set when used with form validation.
     *
     * @property {boolean} isInvalid - The invalid state of the radio input
     */
    get isInvalid() {
        return this._isInvalid || null;
    }
    set isInvalid(state) {
        this.setInvalid(state);
    }
    /**
     * Gets the CSS classes for the radio input component.
     * Combines base classes with invalid state modifier if applicable.
     *
     * @returns {string} Space-separated list of CSS classes
     */
    get class() {
        return [super.getCssClasses('eui-input-radio'), this._isInvalid ? 'eui-input-radio--invalid' : ''].join(' ').trim();
    }
    /**
     * Gets or sets the default checked state of the radio input.
     * This is different from the current checked state and represents the initial value.
     *
     * @property {any} defaultChecked - The default checked state
     */
    get defaultChecked() {
        return this._defaultChecked ? '' : null;
    }
    set defaultChecked(value) {
        this._defaultChecked = coerceBooleanProperty(value);
        this._renderer.setProperty(this._elementRef.nativeElement, 'checked', this._defaultChecked);
    }
    /**
     * Gets whether the radio input is currently selected.
     *
     * @returns {boolean} True if the radio input is selected, false otherwise
     */
    get selected() {
        return this._elementRef?.nativeElement.checked;
    }
    /**
     * Gets or sets the value of the radio input.
     * The value can be of any type and will be used when the radio is selected in a form group.
     *
     * @property {any} value - The value associated with this radio input
     */
    get value() {
        return this._value;
    }
    set value(value) {
        this._value = value;
        if (typeof value === 'number' || typeof value === 'boolean' || !!value) {
            this._elementRef.nativeElement.value = this._value;
        }
    }
    constructor() {
        super();
        this.type = 'radio';
        this.ngControl = inject(NgControl, { optional: true, self: true });
        this._elementRef = inject(ElementRef);
        this._renderer = inject(Renderer2);
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
        this.onChange = (_) => {
            /* Nothing to be Done so far */
        };
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
        this.onTouched = (_) => {
            /* Nothing to be Done so far */
        };
        // Firefox fix: set type to radio before first ngDoCheck runs
        this._elementRef.nativeElement.type = 'radio';
        // if there's no id attribute set one
        if (!this._elementRef.nativeElement.id) {
            this.setIdAttribute();
        }
        // register control valueAccessor
        if (this.ngControl) {
            this.ngControl.valueAccessor = this;
        }
    }
    /**
     * Initializes the component.
     * Sets up form control validation status subscription and handles initial state.
     */
    ngOnInit() {
        super.ngOnInit();
        // in case control value is null set the default one (isChecked) and sync Control State
        // if (this.ngControl?.control?.value === null) {
        // this.ngControl.control.setValue('', { emitModelToViewChange: false });
        // changing Model Expression after view checked, so detect changes
        // TODO: check why although it's checked .checked returns false
        // this.ngControl.viewToModelUpdate(this._checked);
        // this._cd.detectChanges();
        // }
        if (this.ngControl) {
            this.ngControl.statusChanges.subscribe((status) => {
                this.isInvalid = status === 'INVALID';
                this.euiDanger = this.isInvalid;
            });
        }
    }
    /**
     * Performs change detection and updates invalid state based on form control status.
     */
    ngDoCheck() {
        if (this.ngControl) {
            this.isInvalid = this.ngControl.invalid && this.ngControl.touched;
        }
    }
    /**
     * Handles changes to component inputs. Specifically, handles changes to
     * readonly and invalid states.
     *
     * @param {SimpleChanges} changes - Object containing changed properties
     */
    ngOnChanges(changes) {
        // when readonly changes hide other radio (input+label)
        if (changes['readonly']) {
            const readonly = coerceBooleanProperty(changes['readonly']?.currentValue);
            if (readonly) {
                this._renderer.setAttribute(this._elementRef.nativeElement, 'readonly', null);
            }
            else {
                this._renderer.removeAttribute(this._elementRef.nativeElement, 'readonly');
            }
        }
        if (changes['isInvalid']) {
            if (changes['isInvalid'].currentValue) {
                this._renderer.addClass(this._elementRef.nativeElement, 'eui-input-radio--invalid');
            }
            else {
                this._renderer.removeClass(this._elementRef.nativeElement, 'eui-input-radio--invalid');
            }
        }
    }
    /**
     * Implements ControlValueAccessor.writeValue.
     * Updates the checked state based on the form control value.
     *
     * @param {string} obj - The value to write
     */
    writeValue(obj) {
        // set checked state based if the radio value matches the control's one
        this._elementRef.nativeElement.checked = this._value === obj;
    }
    /**
     * Registers a callback function that is called when the control's value changes.
     *
     * @param {Function} fn - The callback function
     */
    // TODO: find the correct type or turn into a generic, https://www.typescriptlang.org/docs/handbook/2/generics.html
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    registerOnChange(fn) {
        this.onChange = fn;
    }
    /**
     * Registers a callback function that is called when the control is touched.
     *
     * @param {Function} fn - The callback function
     */
    // TODO: find the correct type or turn into a generic, https://www.typescriptlang.org/docs/handbook/2/generics.html
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    registerOnTouched(fn) {
        this.onTouched = fn;
    }
    /**
     * Sets the disabled state of the radio input.
     *
     * @param {boolean} isDisabled - Whether the radio input should be disabled
     */
    setDisabledState(isDisabled) {
        this.disabled = isDisabled;
    }
    /**
     * Handles change events on the radio input.
     * Updates the model value when the radio selection changes.
     *
     * @param {Event} event - The change event
     */
    onCheckedChanged(event) {
        const target = event.target;
        this._defaultChecked = target.checked;
        this.onChange(target.value === 'on' ? null : target.value);
        this.onTouched(target.value);
    }
    /**
     * Handles space key press events. Prevents selection changes when the input
     * is readonly.
     *
     * @param {KeyboardEvent} event - The keyboard event
     */
    onSpacePressed(event) {
        if (this.readonly) {
            event.preventDefault();
            event.stopPropagation();
        }
    }
    /**
     * Sets the invalid state of the radio input.
     * Updates both the internal state and the visual appearance.
     *
     * @param {boolean} state - The invalid state to set
     */
    setInvalid(state) {
        // in case it's controlled by NgControl override
        this._isInvalid = this.control ? this.control.invalid && this.control.touched : coerceBooleanProperty(state);
        // set BaseDirective Attribute
        this.euiDanger = this._isInvalid;
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: EuiInputRadioComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "19.2.17", type: EuiInputRadioComponent, isStandalone: true, selector: "input[euiInputRadio]", inputs: { isInvalid: "isInvalid", defaultChecked: ["checked", "defaultChecked"], value: "value" }, host: { listeners: { "change": "onCheckedChanged($event)", "keydown.space": "onSpacePressed($event)" }, properties: { "class": "this.class", "attr.type": "this.type", "attr.checked": "this.defaultChecked" } }, usesInheritance: true, usesOnChanges: true, ngImport: i0, template: '', isInline: true, styles: [".eui-19 :host.eui-input-radio:not(.cdk-visually-hidden){appearance:inherit;align-items:center;border-radius:var(--eui-br-max);cursor:pointer;display:inline-flex;flex-shrink:0;height:var(--eui-s-xl);justify-content:center;margin-right:var(--eui-s-s);position:relative;vertical-align:middle;width:var(--eui-s-xl);accent-color:var(--eui-c-white);background:var(--eui-c-surface-container);border:var(--eui-bw-xs) solid var(--eui-c-s-secondary-border)}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden)+::ng-deep label,.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden)+::ng-deep .eui-label{display:inline-flex;cursor:pointer;margin-right:var(--eui-s-m);white-space:inherit}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden):checked{background:var(--eui-c-surface-container)}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden):checked:before{height:1rem;width:1rem;content:\"\";mask-repeat:no-repeat;mask-position:50% 50%;mask-size:cover;mask-image:var(--eui-internal-icon-ellipse-path);background-color:var(--eui-c-s-primary);height:var(--eui-s-l);width:var(--eui-s-l)}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden):disabled:not([readonly]){background:var(--eui-c-disabled-bg);opacity:.25;pointer-events:none;-webkit-user-select:none;user-select:none}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden):disabled:not([readonly]):checked:before{height:1rem;width:1rem;content:\"\";mask-repeat:no-repeat;mask-position:50% 50%;mask-size:cover;mask-image:var(--eui-internal-icon-ellipse-path);background-color:var(--eui-c-s-primary);height:var(--eui-s-l);width:var(--eui-s-l)}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden):disabled:not([readonly])+::ng-deep label{pointer-events:none}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden)[readonly]{display:none;pointer-events:none;-webkit-user-select:none;user-select:none}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden)[readonly]+::ng-deep label,.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden)[readonly]+::ng-deep .eui-label{display:none}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden)[readonly]:checked{display:none}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden)[readonly]:checked+::ng-deep label,.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden)[readonly]:checked+::ng-deep .eui-label{align-items:center;display:inline-flex;pointer-events:none;position:initial}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden).eui-input-radio--invalid,.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden).eui-input-radio--danger{border:var(--eui-bw-xs) solid var(--eui-c-s-danger)!important}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden).eui-input-radio--invalid:checked:before,.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden).eui-input-radio--danger:checked:before{height:1rem;width:1rem;content:\"\";mask-repeat:no-repeat;mask-position:50% 50%;mask-size:cover;mask-image:var(--eui-internal-icon-ellipse-path);background-color:var(--eui-c-s-danger);height:var(--eui-s-l);width:var(--eui-s-l)}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden):focus:not([readonly]){outline:var(--eui-bw-s) solid var(--eui-c-s-primary-border-light)!important;outline-offset:1px!important;border-radius:var(--eui-br-max)!important}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden).eui-input-radio--invalid:not([readonly]),.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden).eui-input-radio--danger:not([readonly]){outline-color:var(--eui-c-s-danger-border-light)!important}\n"] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: EuiInputRadioComponent, decorators: [{
            type: Component,
            args: [{ selector: 'input[euiInputRadio]', template: '', styles: [".eui-19 :host.eui-input-radio:not(.cdk-visually-hidden){appearance:inherit;align-items:center;border-radius:var(--eui-br-max);cursor:pointer;display:inline-flex;flex-shrink:0;height:var(--eui-s-xl);justify-content:center;margin-right:var(--eui-s-s);position:relative;vertical-align:middle;width:var(--eui-s-xl);accent-color:var(--eui-c-white);background:var(--eui-c-surface-container);border:var(--eui-bw-xs) solid var(--eui-c-s-secondary-border)}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden)+::ng-deep label,.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden)+::ng-deep .eui-label{display:inline-flex;cursor:pointer;margin-right:var(--eui-s-m);white-space:inherit}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden):checked{background:var(--eui-c-surface-container)}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden):checked:before{height:1rem;width:1rem;content:\"\";mask-repeat:no-repeat;mask-position:50% 50%;mask-size:cover;mask-image:var(--eui-internal-icon-ellipse-path);background-color:var(--eui-c-s-primary);height:var(--eui-s-l);width:var(--eui-s-l)}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden):disabled:not([readonly]){background:var(--eui-c-disabled-bg);opacity:.25;pointer-events:none;-webkit-user-select:none;user-select:none}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden):disabled:not([readonly]):checked:before{height:1rem;width:1rem;content:\"\";mask-repeat:no-repeat;mask-position:50% 50%;mask-size:cover;mask-image:var(--eui-internal-icon-ellipse-path);background-color:var(--eui-c-s-primary);height:var(--eui-s-l);width:var(--eui-s-l)}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden):disabled:not([readonly])+::ng-deep label{pointer-events:none}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden)[readonly]{display:none;pointer-events:none;-webkit-user-select:none;user-select:none}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden)[readonly]+::ng-deep label,.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden)[readonly]+::ng-deep .eui-label{display:none}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden)[readonly]:checked{display:none}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden)[readonly]:checked+::ng-deep label,.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden)[readonly]:checked+::ng-deep .eui-label{align-items:center;display:inline-flex;pointer-events:none;position:initial}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden).eui-input-radio--invalid,.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden).eui-input-radio--danger{border:var(--eui-bw-xs) solid var(--eui-c-s-danger)!important}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden).eui-input-radio--invalid:checked:before,.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden).eui-input-radio--danger:checked:before{height:1rem;width:1rem;content:\"\";mask-repeat:no-repeat;mask-position:50% 50%;mask-size:cover;mask-image:var(--eui-internal-icon-ellipse-path);background-color:var(--eui-c-s-danger);height:var(--eui-s-l);width:var(--eui-s-l)}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden):focus:not([readonly]){outline:var(--eui-bw-s) solid var(--eui-c-s-primary-border-light)!important;outline-offset:1px!important;border-radius:var(--eui-br-max)!important}.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden).eui-input-radio--invalid:not([readonly]),.eui-19 :host.eui-input-radio:not(.cdk-visually-hidden).eui-input-radio--danger:not([readonly]){outline-color:var(--eui-c-s-danger-border-light)!important}\n"] }]
        }], ctorParameters: () => [], propDecorators: { isInvalid: [{
                type: Input
            }], class: [{
                type: HostBinding,
                args: ['class']
            }], type: [{
                type: HostBinding,
                args: ['attr.type']
            }], defaultChecked: [{
                type: HostBinding,
                args: ['attr.checked']
            }, {
                type: Input,
                args: ['checked']
            }], value: [{
                type: Input
            }], onCheckedChanged: [{
                type: HostListener,
                args: ['change', ['$event']]
            }], onSpacePressed: [{
                type: HostListener,
                args: ['keydown.space', ['$event']]
            }] } });

/**
 * Angular module that provides radio input functionality.
 *
 * @deprecated Use {@link `EUI_INPUT_RADIO`} instead.
 */
class EuiInputRadioModule {
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: EuiInputRadioModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
    static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.17", ngImport: i0, type: EuiInputRadioModule, imports: [EuiInputRadioComponent], exports: [EuiInputRadioComponent] }); }
    static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: EuiInputRadioModule }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: EuiInputRadioModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [EuiInputRadioComponent],
                    exports: [EuiInputRadioComponent],
                }]
        }] });

const EUI_INPUT_RADIO = [
    EuiInputRadioComponent,
];
// export { EuiInputRadioComponent as EuiInputRadio } from './eui-input-radio.component';

/**
 * Generated bundle index. Do not edit.
 */

export { EUI_INPUT_RADIO, EuiInputRadioComponent, EuiInputRadioModule };
//# sourceMappingURL=eui-components-eui-input-radio.mjs.map
