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

class RadioGroupBaseComponent {
    constructor(uniqueIdService, logService, cdr) {
        this.uniqueIdService = uniqueIdService;
        this.logService = logService;
        this.cdr = cdr;
        /** @internal */
        this.selectedChange = new EventEmitter();
        /**
         * Method to call when value has changes.
         * @internal
         */
        this.propagateChange = (_) => { };
        /**
         * Method to call when the component is touched (when it was is clicked).
         * @internal
         */
        this.propagateTouched = () => { };
    }
    ngOnInit() {
        const grUniq = this.uniqueIdService.getUniqueId();
        this.name = 'is-rbgroup-' + (this.name || grUniq);
        this.id = 'is-rbgroup-' + grUniq;
        this.inline = this.inline || false;
        // check and prepere options for render
        this.options = this.options || [];
        // in case selected not arrived with options
        if (!this.selected) {
            this.selected = this.options.length !== 0 ? this.options[0] : null;
            this.setSelected(this.selected);
        }
        else {
            // check that selected in options
            const selFound = this.options.find(item => {
                return item.id === this.selected.id;
            });
            if (!selFound) {
                // set err message
                this.logService.error(new Error('Selected option not part of options'));
            }
        }
    }
    /** @internal */
    isSelected(option) {
        return option === this.selected || (option && this.selected && option.id === this.selected.id);
    }
    /** @internal */
    setSelected(selectedValue) {
        this.selected = this.options.find(item => item.id === selectedValue);
        this.selectedChange.emit(this.selected);
        this.propagateChange(this.selected);
    }
    /**
     * update value from model to the component
     * @internal
     */
    writeValue(value) {
        this.selected = value;
        this.cdr.markForCheck();
    }
    /**
     * Informs the outside world about changes.
     * see method propagateChange call - this.propagateChange(this.model);
     * @internal
     */
    registerOnChange(fn) {
        this.propagateChange = fn;
    }
    /**
     * on click
     * @internal
     */
    registerOnTouched(fn) {
        this.propagateTouched = fn;
    }
    /**
     * on set form controll enabled / disabled
     * also do UI Component enabled / disabled
     * @internal
     */
    setDisabledState(isDisabled) {
        this.isDisabled = isDisabled;
    }
    static { this.ɵfac = function RadioGroupBaseComponent_Factory(t) { return new (t || RadioGroupBaseComponent)(i0.ɵɵdirectiveInject(i1.UniqueIdService), i0.ɵɵdirectiveInject(i2.LogService), i0.ɵɵdirectiveInject(i0.ChangeDetectorRef)); }; }
    static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: RadioGroupBaseComponent, inputs: { name: "name", inline: "inline", isDisabled: "isDisabled", options: "options", selected: "selected" }, outputs: { selectedChange: "selectedChange" } }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(RadioGroupBaseComponent, [{
        type: Directive
    }], () => [{ type: i1.UniqueIdService }, { type: i2.LogService }, { type: i0.ChangeDetectorRef }], { name: [{
            type: Input
        }], inline: [{
            type: Input
        }], isDisabled: [{
            type: Input
        }], options: [{
            type: Input
        }], selected: [{
            type: Input
        }], selectedChange: [{
            type: Output
        }] }); })();

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

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