import { TaskState, } from '@wix/bex-core';
import { cairoSectionCtaClicked } from '@wix/bex-core/bi';
import { action, makeObservable, observable, runInAction } from 'mobx';
export class ViewTypeState {
    constructor(params) {
        this.viewType = 'table';
        this.initTask = new TaskState();
        this.container = params.container;
        this.dataCapsule = params.dataCapsule;
        this.componentType = params.componentType;
        this.collection = params.collection;
        makeObservable(this, {
            viewType: observable.ref,
            changeViewType: action.bound,
        });
    }
    init() {
        this.initTask.runOnce(async () => {
            await this._getViewTypeFromStorage();
        });
    }
    async _getViewTypeFromStorage() {
        try {
            const res = (await this.dataCapsule.getItem('view-type'));
            runInAction(() => {
                this.viewType = res || this.viewType;
            });
        }
        catch (err) {
            this.container.errorMonitor.captureException(err);
        }
    }
    changeViewType(newViewType) {
        this.container.reportBi(cairoSectionCtaClicked({
            ctaType: 'More actions',
            loaction: 'Collection Toolbar',
            location: 'Collection Toolbar',
            componentType: this.componentType,
            sectionType: this.viewType,
            ctaName: `layout switch to ${newViewType}`,
            ...this.collection?._commonDynamicBiParams(),
        }));
        this.viewType = newViewType === 'grid' ? 'grid' : 'table';
        this.dataCapsule.setItem('view-type', newViewType);
    }
}
//# sourceMappingURL=ViewTypeState.js.map