"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.SelectFieldState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
class SelectFieldState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "select", void 0);
    (0, _defineProperty2.default)(this, "filter", void 0);
    this.select = params.select;
    this.filter = params.filter;
    (0, _mobx.makeObservable)(this, {
      init: _mobx.action.bound,
      syncSelectedItems: _mobx.action.bound
    });
    this.syncSelectedItems();
  }
  syncSelectedItems() {
    const {
      select,
      filter
    } = this;
    select.clear();
    select.finalizeMany(filter.value);
  }
  init() {
    const disposers = [(0, _mobx.reaction)(() => this.filter.value, () => {
      const {
        select: {
          selectedValues
        },
        filter
      } = this;
      if (filter.value !== selectedValues) {
        this.syncSelectedItems();
      }
    }), (0, _mobx.reaction)(() => this.select.selectedValues, () => {
      const {
        select: {
          selectedValues
        },
        filter
      } = this;
      if (filter.value !== selectedValues) {
        filter.setValue(selectedValues, {
          emitEvents: ['change', 'beforeRefresh', 'refresh']
        });
      }
    })];
    return () => {
      disposers.forEach(d => d());
    };
  }
}
exports.SelectFieldState = SelectFieldState;
//# sourceMappingURL=SelectFieldState.js.map