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 { SwitcherMode } from '@ironsource/fusion-ui/components/switcher/common/entities';
import { isNullOrUndefined } from '@ironsource/fusion-ui/utils';

class SwitcherBaseComponent {
    set disabled(value) {
        this.setDisabledState(value);
    }
    constructor(uniqueService, changeDetectorRef) {
        this.uniqueService = uniqueService;
        this.changeDetectorRef = changeDetectorRef;
        this.options = [];
        this.error = '';
        this.mode = SwitcherMode.Circle;
        this.selectedChange = new EventEmitter();
        this.isDisabled = false;
        // Implement ControlValueAccessor methods
        this.propagateChange = (_) => { };
        this.propagateTouched = () => { };
    }
    ngOnInit() {
        const uniq = this.uniqueService.getUniqueId();
        this.name = this.name || `fui-switcher-${uniq}`;
        this.id = `fuiSwitcher${uniq}`;
    }
    isSelected(item) {
        if (isNullOrUndefined(this.selected) || isNullOrUndefined(this.selected.id)) {
            return false;
        }
        return item.id === this.selected.id;
    }
    setSelected(selected) {
        this.propagateTouched();
        if (!isNullOrUndefined(this.selected) && !isNullOrUndefined(this.selected.id) && selected.id === this.selected.id) {
            return;
        }
        this.selected = selected;
        this.propagateChange({ ...selected });
        this.selectedChange.emit({ ...selected });
    }
    writeValue(value) {
        if (isNullOrUndefined(value)) {
            this.selected = { id: '', title: '' };
        }
        else {
            this.selected = value;
        }
        this.changeDetectorRef.markForCheck();
    }
    registerOnChange(fn) {
        this.propagateChange = fn;
    }
    registerOnTouched(fn) {
        this.propagateTouched = fn;
    }
    setDisabledState(isDisabled) {
        this.isDisabled = isDisabled;
    }
    static { this.ɵfac = function SwitcherBaseComponent_Factory(t) { return new (t || SwitcherBaseComponent)(i0.ɵɵdirectiveInject(i1.UniqueIdService), i0.ɵɵdirectiveInject(i0.ChangeDetectorRef)); }; }
    static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: SwitcherBaseComponent, inputs: { name: "name", options: "options", error: "error", mode: "mode", disabled: "disabled" }, outputs: { selectedChange: "selectedChange" } }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(SwitcherBaseComponent, [{
        type: Directive
    }], () => [{ type: i1.UniqueIdService }, { type: i0.ChangeDetectorRef }], { name: [{
            type: Input
        }], options: [{
            type: Input
        }], error: [{
            type: Input
        }], mode: [{
            type: Input
        }], disabled: [{
            type: Input
        }], selectedChange: [{
            type: Output
        }] }); })();

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

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