import { action, computed, makeObservable, observable } from 'mobx';
import { addResizeObserver } from '@wix/bex-core';
export class HorizontalSpaceAvailableState {
    constructor(params) {
        this._directChildClientWidth = 0;
        this._clientWidth = 0;
        const { state } = params;
        this.container = state.container;
        this._state = state;
        makeObservable(this, {
            _sync: action.bound,
            _directChildClientWidth: observable.ref,
            _clientWidth: observable.ref,
            available: computed,
        });
        this._syncThrottled = this.container.lodash.throttle(() => {
            window.requestAnimationFrame(this._sync);
        }, 200);
    }
    _sync() {
        const { _element, _child } = this;
        if (!_element || !_child) {
            return;
        }
        this._clientWidth = _element.clientWidth;
        this._directChildClientWidth = _child.scrollWidth;
    }
    get available() {
        return this._clientWidth - this._directChildClientWidth;
    }
    initLayout() {
        const { _element, _child, _state } = this;
        if (_state.responsiveDisabled) {
            return;
        }
        if (!_element || !_child) {
            return;
        }
        this._sync();
    }
    init() {
        const { _element, _child, _state } = this;
        if (_state.responsiveDisabled) {
            return;
        }
        if (!_element || !_child) {
            return;
        }
        const disposers = [addResizeObserver(_child, this._syncThrottled)];
        return () => {
            disposers.forEach((d) => d?.());
        };
    }
}
//# sourceMappingURL=HorizontalSpaceAvailableState.js.map