import { AutoCompleteReadonlyState } from '../components/CollectionViewsDropdown/AutoCompleteReadonlyState';
import { DropdownSelectState } from './DropdownSelectState';
import { action, computed, makeObservable, observable } from 'mobx';
export class DropdownCollectionFilterState {
    get shouldShowPlaceholder() {
        return (this.inputState.disableEditing ||
            this.collectionFilter.collection.query.search.isEmpty);
    }
    get inputValue() {
        return this.shouldShowInputValue
            ? this.collectionFilter.collection.query.search.value
            : '';
    }
    get shouldShowInputValue() {
        return this.dropdownSelect.dropdown.isOn || !this.inputState.disableEditing;
    }
    onOptionsShow() {
        this.promotedItems = this.collectionFilter.select.selectedValues.map((item) => item.key);
        this.dropdownSelect.open();
    }
    constructor(params) {
        this.promotedItems = [];
        this.resetOnClose = params.resetOnClose;
        this.container = params.container;
        this.inputState = new AutoCompleteReadonlyState();
        this.collectionFilter = params.collectionFilter;
        const { collectionFilter } = this;
        const { select, filter } = collectionFilter;
        this.dropdownSelect = new DropdownSelectState({
            select,
            container: params.container,
            toolbar: params.toolbar,
            filter,
        });
        makeObservable(this, {
            init: action.bound,
            shouldShowPlaceholder: computed,
            inputValue: computed,
            shouldShowInputValue: computed,
            promotedItems: observable.ref,
            onOptionsShow: action.bound,
        });
    }
    init({ skipInitialFetch } = {}) {
        const { dropdownSelect, resetOnClose, collectionFilter } = this;
        const { collection } = collectionFilter;
        const disposers = [
            dropdownSelect.init(),
            collection.init({
                skipInitialFetch,
            }),
        ];
        if (resetOnClose) {
            dropdownSelect.events.on('close', collection.resetFiltersAndRefresh);
        }
        return () => {
            for (const disposer of disposers) {
                disposer();
            }
            if (resetOnClose) {
                dropdownSelect.events.removeListener('close', collection.resetFiltersAndRefresh);
            }
        };
    }
}
//# sourceMappingURL=DropdownCollectionFilterState.js.map