import * as i0 from '@angular/core';
import { inject, ElementRef, ChangeDetectorRef, forwardRef, Directive } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';

class GenericControlValueAccessor {
    constructor() {
        this.elementRef = inject(ElementRef);
        this.cdr = inject(ChangeDetectorRef);
        this._value = null;
        this._initialized = false;
        this.setDisabledState = (isDisabled) => {
            const element = this.elementRef.nativeElement;
            if (element && this._initialized) {
                element.disabled = isDisabled;
                // Manually trigger change detection for zoneless compatibility
                this.cdr.markForCheck();
            }
        };
    }
    ngAfterViewInit() {
        // Initialize after the UI5 web component is ready
        // Use requestAnimationFrame for zoneless compatibility instead of setTimeout
        requestAnimationFrame(() => {
            this.initializeComponent();
        });
    }
    registerOnChange(fn) {
        this.onChange = fn;
    }
    registerOnTouched(fn) {
        this.onTouched = fn;
    }
    writeValue(val) {
        this._value = val;
        if (this._initialized) {
            this.updateElementValue(val);
        }
    }
    initializeComponent() {
        const element = this.elementRef.nativeElement;
        this._initialized = true;
        // Set up event listeners on the UI5 web component
        if (element && element.addEventListener) {
            element.addEventListener('input', (e) => {
                if (this.onChange) {
                    this.onChange(e.target.value);
                    // Manually trigger change detection for zoneless compatibility
                    this.cdr.markForCheck();
                }
            });
            element.addEventListener('change', (e) => {
                if (this.onChange) {
                    this.onChange(e.target.value);
                    // Manually trigger change detection for zoneless compatibility
                    this.cdr.markForCheck();
                }
            });
            element.addEventListener('focusout', () => {
                if (this.onTouched) {
                    this.onTouched();
                    // Manually trigger change detection for zoneless compatibility
                    this.cdr.markForCheck();
                }
            });
        }
        // Set initial value if we have one pending
        if (this._value !== null) {
            this.updateElementValue(this._value);
        }
    }
    updateElementValue(value) {
        const element = this.elementRef.nativeElement;
        if (element && this._initialized) {
            element.value = value || '';
            // Manually trigger change detection for zoneless compatibility
            this.cdr.markForCheck();
        }
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.4", ngImport: i0, type: GenericControlValueAccessor, deps: [], target: i0.ɵɵFactoryTarget.Directive }); }
    static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "20.1.4", type: GenericControlValueAccessor, isStandalone: true, selector: "[noop]", providers: [
            {
                provide: NG_VALUE_ACCESSOR,
                useExisting: forwardRef(() => GenericControlValueAccessor),
                multi: true
            }
        ], ngImport: i0 }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.4", ngImport: i0, type: GenericControlValueAccessor, decorators: [{
            type: Directive,
            args: [{
                    selector: '[noop]',
                    standalone: true,
                    providers: [
                        {
                            provide: NG_VALUE_ACCESSOR,
                            useExisting: forwardRef(() => GenericControlValueAccessor),
                            multi: true
                        }
                    ]
                }]
        }] });

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

export { GenericControlValueAccessor };
//# sourceMappingURL=fundamental-ngx-ui5-webcomponents-utils.mjs.map
