import { action, reaction } from 'mobx';
import { replaceSearchParamsWith } from '@wix/bex-core';
export class ViewsHistoryState {
    constructor(params) {
        this.viewIdSearchParamName = 'viewId';
        this.init = action(() => {
            const { views, history } = this;
            const disposers = [
                reaction(() => views.currentView, (currentView) => {
                    replaceSearchParamsWith(history, (searchParams) => {
                        if (currentView) {
                            searchParams.set(this.viewIdSearchParamName, currentView.id);
                        }
                        else {
                            searchParams.delete(this.viewIdSearchParamName);
                        }
                    });
                }, {
                    fireImmediately: true,
                }),
            ];
            return () => {
                disposers.forEach((d) => d());
            };
        });
        this.views = params.views;
        this.history = params.history;
        this._preInitHistoryPersistence();
    }
    _preInitHistoryPersistence() {
        const { history, views } = this;
        const searchParams = new URLSearchParams(history.location.search);
        const viewIdFromUrl = searchParams.get(this.viewIdSearchParamName);
        if (viewIdFromUrl) {
            views._initialViewIdFromUrl = viewIdFromUrl;
        }
    }
}
//# sourceMappingURL=ViewsHistoryState.js.map