import { operatorNeedsValue as coreOperatorNeedsValue, } from '@wix/bex-core';
import { action, computed, makeObservable, observable, reaction } from 'mobx';
import { debounce } from 'lodash';
export class OperatorFilterPickerState {
    get filter() {
        return this.toolbar.getPendingFilter(this._filter);
    }
    constructor(params) {
        this.toolbar = params.toolbar;
        this._filter = params.filter;
        this.operator = params.filter.value?.operator;
        this.value = params.filter.value?.value ?? null;
        const debounceMs = params.debounceMs ?? 300;
        this.debouncedRefresh = debounce(() => this.refreshIfChanged({ actionType: 'add' }), debounceMs);
        makeObservable(this, {
            operator: observable,
            value: observable,
            isEmpty: computed,
            init: action,
            setOperator: action,
            setValue: action,
            onChange: action,
            applyImmediately: action,
        });
    }
    get isEmpty() {
        return this.filter.isEmpty;
    }
    get currentValue() {
        return {
            operator: this.operator,
            value: this.value,
        };
    }
    init() {
        const dispose = 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 coreOperatorNeedsValue(this.operator);
    }
}
//# sourceMappingURL=OperatorFilterPickerState.js.map