import { addResizeObserver } from '@wix/bex-core';
export class ToolbarLeftGroupState {
    constructor(params) {
        this._element = null;
        this._prevWidth = 0;
        this.EXPANSION_THRESHOLD = 20;
        this._handleResize = () => {
            const element = this._element;
            if (!element) {
                return;
            }
            const width = element.clientWidth;
            const prevWidth = this._prevWidth;
            const isAtMinWidth = width <= this.minWidth + 2; // 2px tolerance
            const isGrowing = width > prevWidth;
            const hasSpaceToExpand = width > this.minWidth + this.EXPANSION_THRESHOLD;
            if (isAtMinWidth) {
                this._responsive.overflow._scrollWidth =
                    this._responsive.overflow._clientWidth + 1;
                this._responsive._nextToShrink._shrinkIfNeeded();
            }
            else if (isGrowing && hasSpaceToExpand) {
                const availableSpace = width - this.minWidth;
                this._responsive._nextToExpand._expandIfNeeded({
                    preCalculatedAvailableSpace: availableSpace,
                });
            }
            if (this.mode === 'tabs') {
                const tabsState = this._getTabsState?.();
                tabsState?.responsive.setAvailableWidth(width);
            }
            this._prevWidth = width;
        };
        this._responsive = params.responsive;
        this.mode = params.mode;
        this.minWidth = params.minWidth;
        this._getTabsState = params.getTabsState;
    }
    init(element) {
        this._element = null;
        this._prevWidth = 0;
        if (!element) {
            return () => { };
        }
        this._element = element;
        const disposer = addResizeObserver(element, this._handleResize);
        this._handleResize();
        return () => {
            disposer?.();
            this._element = null;
            this._prevWidth = 0;
        };
    }
}
//# sourceMappingURL=ToolbarLeftGroupState.js.map