import * as i0 from '@angular/core';
import { EventEmitter, Directive, Input, Output } from '@angular/core';
import * as i1 from '@ironsource/fusion-ui/services/unique-id';
import { isNullOrUndefined } from '@ironsource/fusion-ui/utils';

class RadioBaseComponent {
    constructor(uidService, elRef, changeDetectorRef) {
        this.uidService = uidService;
        this.elRef = elRef;
        this.changeDetectorRef = changeDetectorRef;
        this.changed = new EventEmitter();
        // Implement ControlValueAccessor methods
        /**
         * Method to call when value has changes.
         */
        this.propagateChange = (_) => { };
        /**
         * Method to call when the component is touched (when it was is clicked).
         */
        this.propagateTouched = () => { };
    }
    ngOnInit() {
        const grUniq = this.uidService.getUniqueId();
        this.name = this.name || '';
        this.id = 'fu-rb-' + grUniq;
        this.label = this.label || '';
        this.value = this.value ?? this.id;
    }
    genElementId() {
        return this.name + '_' + this.value;
    }
    onChanged() {
        this.propagateTouched();
        this.propagateChange(this.value);
        this.changed.emit(this.value);
    }
    /**
     * update value from model to the component
     */
    writeValue(value) {
        if (value === undefined || value === null) {
            this.selectedValue = '';
        }
        else {
            this.selectedValue = value;
        }
        if (isNullOrUndefined(this.checked)) {
            this.checked = this.value === this.selectedValue;
        }
        this.elRef.nativeElement.querySelector('input').checked = this.checked;
        this.changeDetectorRef.markForCheck();
    }
    /**
     * Informs the outside world about changes.
     * see method propagateChange call - this.propagateChange(this.model);
     */
    registerOnChange(fn) {
        this.propagateChange = fn;
    }
    /**
     * on click
     */
    registerOnTouched(fn) {
        this.propagateTouched = fn;
    }
    /**
     * on set form controll enabled / disabled
     * also do UI Component enabled / disabled
     */
    setDisabledState(isDisabled) {
        this.isDisabled = isDisabled;
    }
    static { this.ɵfac = function RadioBaseComponent_Factory(t) { return new (t || RadioBaseComponent)(i0.ɵɵdirectiveInject(i1.UniqueIdService), i0.ɵɵdirectiveInject(i0.ElementRef), i0.ɵɵdirectiveInject(i0.ChangeDetectorRef)); }; }
    static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: RadioBaseComponent, inputs: { name: "name", isDisabled: "isDisabled", checked: "checked", value: "value", label: "label", icon: "icon", tooltip: "tooltip" }, outputs: { changed: "changed" } }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(RadioBaseComponent, [{
        type: Directive
    }], () => [{ type: i1.UniqueIdService }, { type: i0.ElementRef }, { type: i0.ChangeDetectorRef }], { name: [{
            type: Input
        }], isDisabled: [{
            type: Input
        }], checked: [{
            type: Input
        }], value: [{
            type: Input
        }], label: [{
            type: Input
        }], icon: [{
            type: Input
        }], tooltip: [{
            type: Input
        }], changed: [{
            type: Output
        }] }); })();

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

export { RadioBaseComponent };
//# sourceMappingURL=ironsource-fusion-ui-components-radio-common-base.mjs.map
