import { TaskState } from '@wix/bex-core';
import { ColumnSizesService } from '../services';
import { makeObservable, observable, runInAction } from 'mobx';
export class ColumnSizesState {
    constructor(params) {
        this.initTask = new TaskState();
        this.columnWidths = undefined;
        this.container = params.container;
        this.columnSizeService = new ColumnSizesService({
            fqdn: params.fqdn,
            storage: params.dataCapsule,
            errorHandler: params.container.errorHandler,
        });
        makeObservable(this, {
            columnWidths: observable.ref,
        });
    }
    init() {
        this.initTask.runOnce(async () => {
            const data = await this.columnSizeService.fetchColumnSizes();
            runInAction(() => {
                this.columnWidths = data?.columnWidths;
            });
        });
    }
    mergeSizes(baseColumns) {
        const merged = baseColumns.map((column) => {
            const persistedWidth = column.id
                ? this.columnWidths?.[column.id]
                : undefined;
            return persistedWidth ? { ...column, width: persistedWidth } : column;
        });
        return merged;
    }
    async updateColumnWidth(columnId, width) {
        const oldColumnWidths = { ...this.columnWidths };
        const newColumnWidths = {
            ...oldColumnWidths,
            [columnId]: width,
        };
        runInAction(() => {
            this.columnWidths = newColumnWidths;
        });
        try {
            await this.columnSizeService.updateColumnWidths(newColumnWidths);
        }
        catch (error) {
            const resolvedError = this.container.errorHandler?.getResolvedError?.(error);
            this.container?.showToast?.({
                biName: 'cairo-column-sizes-update-error',
                message: resolvedError?.message,
                type: 'ERROR',
            });
        }
    }
}
//# sourceMappingURL=ColumnSizesState.js.map