import { action, makeObservable, observable } from 'mobx';
import { PageState } from '../PageState';
import { addResizeObserver } from '../resizeObserverCompat';
import { CollectionPageStateBIReporter } from './CollectionPageStateBIReporter';
import throttle from 'lodash/throttle';
export const NARROW_PAGE_BREAKPOINT = 960;
export class CollectionPageState {
    constructor(params) {
        this.hasHeader = false;
        this.total = false;
        this.hideHeaderTotal = false;
        this.scrollTopBeforeUnmount = null;
        this.isNarrow = false;
        this.startTime = performance.now();
        this.pageState = new PageState();
        this.primaryToolbar = null;
        this.pageTitle = params.pageTitle;
        this.hasHeader = params.hasHeader;
        this.hideHeaderTotal = params.hideHeaderTotal;
        this.withinRouter = params.withinRouter;
        if (params.reportBI) {
            this.bi = new CollectionPageStateBIReporter({
                reportBI: params.reportBI,
                collectionPageState: this,
            });
        }
        makeObservable(this, {
            pageTitle: observable.ref,
            hasHeader: observable.ref,
            hideHeaderTotal: observable.ref,
            total: observable.ref,
            primaryToolbar: observable.ref,
            isNarrow: observable.ref,
            _updateIsNarrow: action.bound,
        });
    }
    _updateIsNarrow() {
        const el = this.pageState.stackingRootRef.current;
        this.isNarrow = el ? el.clientWidth < NARROW_PAGE_BREAKPOINT : false;
    }
    init() {
        this.bi?.init();
        const scrollableContent = this.pageState.scrollableContentRef.current;
        if (!scrollableContent) {
            return () => { };
        }
        const scrollDisposer = addResizeObserver(scrollableContent, () => {
            if (this.scrollTopBeforeUnmount === null) {
                return;
            }
            if (scrollableContent.scrollHeight >= this.scrollTopBeforeUnmount) {
                scrollableContent.scrollTo({
                    top: this.scrollTopBeforeUnmount,
                });
                this.scrollTopBeforeUnmount = null;
            }
        });
        this._updateIsNarrow();
        const narrowDisposer = addResizeObserver(this.pageState.stackingRootRef.current, throttle(this._updateIsNarrow, 100));
        this.bi?.appLoaded?.();
        return () => {
            scrollDisposer?.();
            narrowDisposer?.();
        };
    }
}
//# sourceMappingURL=CollectionPageState.js.map