import { CollectionState, stringFilter, CollectionOptimisticActions, } from '@wix/bex-core';
import { action, computed, makeObservable, observable, runInAction, } from 'mobx';
export class KanbanStageState {
    constructor(params) {
        this.isRefetching = false;
        this.container = params.container;
        this.name = params.name;
        this.key = params.key;
        this.kanbanId = params.kanbanId;
        this.stage = params.stage;
        this._total = params.initialPage?.total ?? undefined;
        this.itemsCollectionState = new CollectionState({
            errorMonitor: this.container.errorMonitor,
            history: this.container.history,
            lodash: this.container.lodash,
            onlineState: this.container.onlineState,
            queryCache: this.container.queryCache,
            translate: this.container.translate,
            queryName: `kanban-items-${this.kanbanId}-${this.key}`,
            fetchData: params.fetchData,
            itemKey: params.itemKey,
            itemName: params.itemName,
            filters: {
                ...params.query.filters,
                stageId: stringFilter({ initialValue: this.key }),
            },
            initialPage: params.initialPage,
            errorHandler: this.container.errorHandler,
            paginationMode: 'cursor',
            search: params.query.search,
        });
        this.optimisticActions = new CollectionOptimisticActions({
            collection: this.itemsCollectionState,
            container: this.container,
        });
        makeObservable(this, {
            name: observable,
            _total: observable,
            fetchMore: action,
            stageName: computed,
            stageKey: computed,
            isRefetching: observable,
            hasError: computed,
            setTotal: action,
            isLoading: computed,
            totalItems: computed,
        });
    }
    init() {
        const disposers = [
            this.itemsCollectionState.init({ skipFilterListeners: true }),
            this.optimisticActions.init(),
        ];
        return () => {
            disposers.forEach((disposer) => disposer?.());
        };
    }
    get hasError() {
        return this.itemsCollectionState.result.status?.isError;
    }
    items() {
        return this.itemsCollectionState.keyedItems.map((item) => item.item);
    }
    itemName(item) {
        return this.itemsCollectionState.itemName(item);
    }
    itemKey(item) {
        return this.itemsCollectionState.itemKey(item);
    }
    get totalItems() {
        return this._total;
    }
    setTotal(total) {
        this._total = total;
    }
    get stageName() {
        return this.name;
    }
    get stageKey() {
        return this.key;
    }
    get isLoading() {
        return this.itemsCollectionState.initTask.status.isLoading;
    }
    fetchMore() {
        this.itemsCollectionState.fetchNextPageIfNeeded();
    }
    isFetchingMore() {
        return true;
    }
    deleteItem(item) {
        this.itemsCollectionState.optimisticActions.deleteOne(item, {
            submit: () => Promise.resolve(),
        });
        runInAction(() => {
            this.setTotal((this._total ?? 0) - 1);
        });
    }
    createItem(item) {
        this.itemsCollectionState.optimisticActions.createOne(item, {
            submit: () => Promise.resolve(),
        });
        runInAction(() => {
            this.setTotal((this._total ?? 0) + 1);
        });
    }
    showErrorToast(error) {
        const resolvedError = this.container.errorHandler?.getResolvedError?.(error);
        resolvedError && this.container.errorHandler?.showError?.(resolvedError);
    }
    updateItem(updatedItem) {
        const allItems = this.itemsCollectionState.result.keyedItems.map((k) => k.item);
        const updatedItems = allItems.map((item) => this.itemKey(item) === this.itemKey(updatedItem) ? updatedItem : item);
        this.itemsCollectionState.optimisticActions.updateMany(updatedItems, {
            submit: () => Promise.resolve(updatedItems),
            keepPosition: true,
        });
    }
    async refetchStage() {
        runInAction(() => {
            this.isRefetching = true;
        });
        try {
            await this.itemsCollectionState.refreshAllPages();
        }
        finally {
            runInAction(() => {
                this.isRefetching = false;
            });
        }
    }
}
//# sourceMappingURL=KanbanStageState.js.map