"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.OperatorFilterPickerState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bexCore = require("@wix/bex-core");
var _mobx = require("mobx");
var _lodash = require("lodash");
class OperatorFilterPickerState {
  get filter() {
    return this.toolbar.getPendingFilter(this._filter);
  }
  constructor(params) {
    var _params$filter$value, _params$filter$value2;
    (0, _defineProperty2.default)(this, "toolbar", void 0);
    (0, _defineProperty2.default)(this, "_filter", void 0);
    (0, _defineProperty2.default)(this, "debouncedRefresh", void 0);
    (0, _defineProperty2.default)(this, "operator", void 0);
    (0, _defineProperty2.default)(this, "value", void 0);
    this.toolbar = params.toolbar;
    this._filter = params.filter;
    this.operator = (_params$filter$value = params.filter.value) == null ? void 0 : _params$filter$value.operator;
    this.value = ((_params$filter$value2 = params.filter.value) == null ? void 0 : _params$filter$value2.value) ?? null;
    const debounceMs = params.debounceMs ?? 300;
    this.debouncedRefresh = (0, _lodash.debounce)(() => this.refreshIfChanged({
      actionType: 'add'
    }), debounceMs);
    (0, _mobx.makeObservable)(this, {
      operator: _mobx.observable,
      value: _mobx.observable,
      isEmpty: _mobx.computed,
      init: _mobx.action,
      setOperator: _mobx.action,
      setValue: _mobx.action,
      onChange: _mobx.action,
      applyImmediately: _mobx.action
    });
  }
  get isEmpty() {
    return this.filter.isEmpty;
  }
  get currentValue() {
    return {
      operator: this.operator,
      value: this.value
    };
  }
  init() {
    const dispose = (0, _mobx.reaction)(() => this.filter.value, filterValue => {
      this.operator = filterValue.operator;
      this.value = filterValue.value;
    }, {
      fireImmediately: true
    });
    return () => {
      dispose();
    };
  }
  remove() {
    this.refreshIfChanged({
      actionType: 'remove'
    });
  }
  setOperator(operator) {
    this.operator = operator;
    if (!this.operatorNeedsValue()) {
      this.value = null;
    }
    this.applyImmediately();
  }
  setValue(value) {
    this.value = value;
    this.onChange();
  }

  /**
   * Called when value changes - debounced
   */
  onChange() {
    this.debouncedRefresh();
  }

  /**
   * Apply filter changes immediately (used for operator changes)
   */
  applyImmediately() {
    this.refreshIfChanged({
      actionType: 'add'
    });
  }
  refreshIfChanged(options = {}) {
    const newValue = this.currentValue;
    if (this.filter.hasDiff(newValue)) {
      this.filter.setValue(newValue, {
        emitEvents: ['change', 'beforeRefresh', 'refresh'],
        ...options
      });
      return true;
    }
    return false;
  }

  /**
   * Check if the given operator requires a value input
   */
  operatorNeedsValue() {
    return (0, _bexCore.operatorNeedsValue)(this.operator);
  }
}
exports.OperatorFilterPickerState = OperatorFilterPickerState;
//# sourceMappingURL=OperatorFilterPickerState.js.map