import * as i0 from '@angular/core';
import { EventEmitter, Directive, Input, Output } from '@angular/core';
import { FormControl } from '@angular/forms';
import { distinctUntilChanged } from 'rxjs/operators';
import { DropdownBaseComponent } from '@ironsource/fusion-ui/components/dropdown/common/base';

// eslint-disable-next-line @angular-eslint/no-conflicting-lifecycle
class AddboxDropdownBaseComponent extends DropdownBaseComponent {
    constructor() {
        super(...arguments);
        this.selected = [];
        this.autoComplete = true;
        this.itemSelected = new EventEmitter();
        this.searchValue = new FormControl();
        this.searchError = '';
        this.lastSearchValue = '';
        this.placeholderText = '';
        this.oldIsLoading = false;
        this.commaSeparatedSearch = false;
    }
    set options(value) {
        this.optionsState = Array.isArray(value) ? value.filter(this.isOptionShowed.bind(this)) : [];
        this.displayedOptions$.next(this.parseOptions(this.optionsState));
    }
    get options() {
        return this.optionsState;
    }
    // eslint-disable-next-line @angular-eslint/no-conflicting-lifecycle
    ngOnChanges(changes) {
        // showing noResults option only if options is empty
        // setting showNoResults to true only if search is finished and options = []
        if (changes.isLoading && changes.isLoading.currentValue === false && changes.options && !changes.options.firstChange) {
            if (!changes.options.currentValue || !changes.options.currentValue.length) {
                // this.showNoResults = true;
            }
            else if (this.commaSeparatedSearch) {
                this.options = [];
                // this.showNoResults = false;
                this.commaSeparatedSearch = false;
            }
        }
        else {
            // this.showNoResults = false;
        }
    }
    initListeners() {
        this.searchValue.valueChanges.pipe(distinctUntilChanged()).subscribe(value => {
            this.commaSeparatedSearch = /,/g.test(value);
            if (!this.commaSeparatedSearch) {
                this.lastSearchValue = value;
                super.onSearchChange();
                if (value) {
                    this.isOpen$.next(true);
                    setTimeout(this.calcOptionHolderPosition.bind(this), 0);
                }
            }
            else {
                this.optionsState = [];
            }
        });
    }
    // eslint-disable-next-line @angular-eslint/no-conflicting-lifecycle
    ngDoCheck() {
        if (this.loading !== this.oldIsLoading) {
            if (!this.loading && !this.isSearchByKeyword()) {
                this.onAddItemsDone();
            }
            this.oldIsLoading = this.loading;
        }
    }
    isActionButtonDisabled() {
        return this.isSearchByKeyword();
    }
    /**
     * Check if option already in selected
     */
    isOptionShowed(option) {
        return this.selected.findIndex(item => item.id === option.id) === -1;
    }
    /**
     * If in search input has ',' comma, do not made search by input value (keyword)
     */
    isSearchByKeyword() {
        return !/,/g.test(this.searchValue.value);
    }
    /**
     * Add Bulk done
     */
    onAddItemsDone() {
        this.searchValue.setValue('', { emitEvent: false });
    }
    /**
     * on key enter in input pressed - if mode for bulk search do it
     */
    addList() {
        this.commaSeparatedSearch = !this.isSearchByKeyword();
        if (this.commaSeparatedSearch) {
            this.searchChange.emit(this.searchValue.value);
        }
    }
    changeSelected({ option, $event }) {
        if (this.selected.findIndex(item => item === option) === -1) {
            this.selected = [...this.selected, option];
            const idx = this.optionsState.findIndex(item => item === option);
            this.optionsState.splice(idx, 1);
            this.options = [...this.optionsState];
            this.itemSelected.emit(option);
            this.propagateChange(this.selected);
            // suppress click event Propagation (fix for onOutsideClick)
            if ($event) {
                $event.stopPropagation();
            }
        }
    }
    /**
     * override parent function
     */
    setLabel() { }
    static { this.ɵfac = /*@__PURE__*/ (() => { let ɵAddboxDropdownBaseComponent_BaseFactory; return function AddboxDropdownBaseComponent_Factory(t) { return (ɵAddboxDropdownBaseComponent_BaseFactory || (ɵAddboxDropdownBaseComponent_BaseFactory = i0.ɵɵgetInheritedFactory(AddboxDropdownBaseComponent)))(t || AddboxDropdownBaseComponent); }; })(); }
    static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: AddboxDropdownBaseComponent, inputs: { selected: "selected", autoComplete: "autoComplete", actionButtonText: "actionButtonText", options: "options" }, outputs: { itemSelected: "itemSelected" }, features: [i0.ɵɵInheritDefinitionFeature, i0.ɵɵNgOnChangesFeature] }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(AddboxDropdownBaseComponent, [{
        type: Directive
    }], null, { selected: [{
            type: Input
        }], autoComplete: [{
            type: Input
        }], actionButtonText: [{
            type: Input
        }], itemSelected: [{
            type: Output
        }], options: [{
            type: Input
        }] }); })();

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

export { AddboxDropdownBaseComponent };
//# sourceMappingURL=ironsource-fusion-ui-components-addbox-dropdown-common-base.mjs.map
