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

class IconSelectListBaseComponent {
    constructor(_uniqueService) {
        this._uniqueService = _uniqueService;
        this.isMultiSelect = true;
        this.selectionChanged = new EventEmitter();
        this.propagateChange = (_) => { };
        this.propagateTouched = () => { };
        this.uid = this._uniqueService.getUniqueId().toString();
    }
    ngOnInit() {
        this.id = this.id || 'iconSelect_' + this.uid;
        this.options = this.options || [];
        this.selected = this.selected || [];
    }
    isItemChecked(item) {
        return (this.selected &&
            this.selected.some(selected => {
                return selected.id === item.id;
            }));
    }
    onItemSelectionChanged(evt, item) {
        this.propagateTouched();
        if (!this.isMultiSelect) {
            this.selected = [item];
        }
        else {
            const idx = this.selected.findIndex(option => option.id === item.id);
            if (evt && idx === -1) {
                this.selected.push(item);
            }
            else {
                this.selected.splice(idx, 1);
            }
        }
        this.propagateChange(this.selected);
        this.selectionChanged.emit(this.selected);
    }
    writeValue(value) {
        if (value === undefined || value === null) {
            this.selected = [];
        }
        else {
            this.selected = [...value];
        }
    }
    registerOnChange(fn) {
        this.propagateChange = fn;
    }
    registerOnTouched(fn) {
        this.propagateTouched = fn;
    }
    static { this.ɵfac = function IconSelectListBaseComponent_Factory(t) { return new (t || IconSelectListBaseComponent)(i0.ɵɵdirectiveInject(i1.UniqueIdService)); }; }
    static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: IconSelectListBaseComponent, inputs: { id: "id", error: "error", options: "options", selected: "selected", isMultiSelect: "isMultiSelect" }, outputs: { selectionChanged: "selectionChanged" } }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(IconSelectListBaseComponent, [{
        type: Directive
    }], () => [{ type: i1.UniqueIdService }], { id: [{
            type: Input
        }], error: [{
            type: Input
        }], options: [{
            type: Input
        }], selected: [{
            type: Input
        }], isMultiSelect: [{
            type: Input
        }], selectionChanged: [{
            type: Output
        }] }); })();

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

export { IconSelectListBaseComponent };
//# sourceMappingURL=ironsource-fusion-ui-components-icon-select-list-common-base.mjs.map
