import { action, makeObservable, observable } from 'mobx';
export class ToolbarResponsivePriorityState {
    constructor(params) {
        this._shouldShrink = false;
        this._count = params.count;
        this._minWidth = params.minWidth ?? 0;
        this._collapsedWidth = params.minWidth ?? 0;
        this.responsive = params.responsive;
        this.getNextToShrink = params.getNextToShrink;
        this.getNextToExpand = params.getNextToExpand;
        makeObservable(this, {
            _minWidth: observable.ref,
            _shouldShrink: observable.ref,
            _syncWidth: action,
            _expandIfNeeded: action,
            _shrinkIfNeeded: action,
        });
    }
    _syncWidth() {
        const { _groupElement, _shouldShrink, responsive } = this;
        if (responsive.responsiveDisabled) {
            return;
        }
        if (!_groupElement) {
            return;
        }
        if (_shouldShrink) {
            this._collapsedWidth = _groupElement.scrollWidth;
        }
        else {
            this._minWidth = _groupElement.scrollWidth;
        }
    }
    _expandIfNeeded({ preCalculatedAvailableSpace, } = {}) {
        const { responsive, _collapsedWidth } = this;
        const { _nextToExpand, actionsAvailableSpace: { available }, } = responsive;
        if (_nextToExpand !== this ||
            (preCalculatedAvailableSpace ?? available) + _collapsedWidth <=
                this._minWidth ||
            !this._shouldShrink) {
            return;
        }
        this._shouldShrink = false;
        responsive._nextToExpand = _nextToExpand.getNextToExpand();
        responsive._nextToShrink = this;
        responsive.actionsAvailableSpace._syncThrottled();
        // experimental - expanding the next item without waiting for the layout tick
        // const availableAfterExpand = available + _collapsedWidth - this._minWidth;
        // responsive._nextToExpand._expandIfNeeded({
        //   preCalculatedAvailableSpace: availableAfterExpand,
        // });
    }
    _shrinkIfNeeded() {
        const { responsive } = this;
        const { _nextToShrink, overflow } = responsive;
        if (_nextToShrink !== this ||
            !overflow._isOverflowing ||
            this._shouldShrink) {
            return;
        }
        this._shouldShrink = true;
        responsive._nextToShrink = _nextToShrink.getNextToShrink();
        responsive._nextToExpand = this;
        responsive.overflow._syncThrottled();
    }
}
//# sourceMappingURL=ToolbarResponsivePriorityState.js.map