import { TaskState, } from '@wix/bex-core';
import { computed, makeObservable, runInAction } from 'mobx';
import { EditableTableState, } from './EditableTable';
import { GridState } from './GridState';
import { TableState } from './TableState';
import { ToolbarCollectionState } from './ToolbarCollectionState';
import { ViewTypeState } from './ViewTypeState';
const componentType = 'LayoutSwitch';
export class LayoutSwitchState {
    get viewType() {
        return this.viewTypeState.viewType;
    }
    set viewType(v) {
        runInAction(() => {
            this.viewTypeState.viewType = v;
        });
    }
    get options() {
        return this.viewTypeState.options;
    }
    constructor(params) {
        this.initTask = new TaskState();
        this.collection = params.collection;
        this.containerOverrides = params.containerOverrides;
        this.toolbar = new ToolbarCollectionState({
            componentType,
            collection: this.collection,
            container: params.container,
            reportBi: (biParams) => params.container.reportBi({
                ...biParams,
                params: {
                    ...biParams.params,
                    componentStatus: this.viewTypeState.viewType,
                },
            }),
        });
        this.tableState = params.table
            ? new TableState({
                collection: this.collection,
                container: params.container,
                toolbar: this.toolbar,
            })
            : undefined;
        this.gridState = params.grid
            ? new GridState({
                collection: this.collection,
                container: params.container,
                toolbar: this.toolbar,
            })
            : undefined;
        this.spreadsheetState = params.spreadsheet
            ? new EditableTableState({
                collection: this.collection,
                container: params.container,
                toolbar: this.toolbar,
                mutations: params.spreadsheet.mutations,
                permissions: params.spreadsheet.permissions,
                fieldActions: params.spreadsheet.fieldActions,
                createOptimisticActions: params.spreadsheet.createOptimisticActions,
            })
            : undefined;
        const enabledLayouts = [
            params.table ? 'table' : null,
            params.grid ? 'grid' : null,
            params.spreadsheet ? 'spreadsheet' : null,
        ].filter(Boolean);
        this.viewTypeState = new ViewTypeState({
            container: params.container,
            dataCapsule: this.toolbar.dataCapsule,
            componentType,
            collection: this.collection,
            options: enabledLayouts,
        });
        this.toolbar.layoutState = this;
        makeObservable(this, {
            viewType: computed,
            options: computed,
        });
    }
    init() {
        const { viewTypeState, initTask } = this;
        viewTypeState.init();
        initTask.runOnce(async () => {
            viewTypeState.initTask.runOnce();
            await viewTypeState.initTask.status.promise;
        });
    }
}
//# sourceMappingURL=LayoutSwitchState.js.map