"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.DropdownCollectionFilterState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _AutoCompleteReadonlyState = require("../components/CollectionViewsDropdown/AutoCompleteReadonlyState");
var _DropdownSelectState = require("./DropdownSelectState");
var _mobx = require("mobx");
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) {
    (0, _defineProperty2.default)(this, "container", void 0);
    (0, _defineProperty2.default)(this, "collectionFilter", void 0);
    (0, _defineProperty2.default)(this, "dropdownSelect", void 0);
    (0, _defineProperty2.default)(this, "resetOnClose", void 0);
    (0, _defineProperty2.default)(this, "inputState", void 0);
    (0, _defineProperty2.default)(this, "promotedItems", []);
    this.resetOnClose = params.resetOnClose;
    this.container = params.container;
    this.inputState = new _AutoCompleteReadonlyState.AutoCompleteReadonlyState();
    this.collectionFilter = params.collectionFilter;
    const {
      collectionFilter
    } = this;
    const {
      select,
      filter
    } = collectionFilter;
    this.dropdownSelect = new _DropdownSelectState.DropdownSelectState({
      select,
      container: params.container,
      toolbar: params.toolbar,
      filter
    });
    (0, _mobx.makeObservable)(this, {
      init: _mobx.action.bound,
      shouldShowPlaceholder: _mobx.computed,
      inputValue: _mobx.computed,
      shouldShowInputValue: _mobx.computed,
      promotedItems: _mobx.observable.ref,
      onOptionsShow: _mobx.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);
      }
    };
  }
}
exports.DropdownCollectionFilterState = DropdownCollectionFilterState;
//# sourceMappingURL=DropdownCollectionFilterState.js.map