import { syncWithSharedQuery, TaskState, } from '@wix/bex-core';
import { action, computed, makeObservable, observable, runInAction, } from 'mobx';
import { createNestedOptimisticActions } from './createNestedOptimisticActions';
export class NestedCollectionNodeState {
    constructor(params) {
        this.parentKeyToChildrenMap = observable.map([], {
            deep: false,
        });
        this._expanded = false;
        this._hovered = false;
        this.initTask = new TaskState();
        const { nestedCollection } = params;
        this.nestedCollection = nestedCollection;
        this.levelDescriptor = params.levelDescriptor;
        this.parent = params.parent;
        this.parentNode = params.parentNode;
        this._notifyAll = params.notifyAll;
        runInAction(() => {
            this._expanded =
                !nestedCollection.isFetchAllAborted && nestedCollection.expandAll;
        });
        const nestedOptimisticActionsState = {
            parentId: this.parent?.id ?? 'null',
            sequences: nestedCollection.sequences,
            query: nestedCollection.query,
            levelDescriptor: this.levelDescriptor,
        };
        const { collection, parentFilter } = params.nodeCollection ??
            this.levelDescriptor.createCollection(nestedOptimisticActionsState);
        createNestedOptimisticActions(this.nestedCollection.container, collection, {
            ...nestedOptimisticActionsState,
            parentFilter,
        });
        if (!nestedCollection.isFetchAllAborted) {
            collection.queryObserver.setOptions({
                ...collection.queryObserver.options,
                staleTime: Infinity,
            });
        }
        this.collection = collection;
        this.parentFilter = parentFilter;
        const { parent, nestedCollection: { query: sharedQuery }, } = this;
        const { query } = collection;
        syncWithSharedQuery(query, sharedQuery);
        if (parent != null && parentFilter != null) {
            parentFilter.persistent = true;
            parentFilter.setValue([parent.key]);
        }
        makeObservable(this, {
            init: action,
            _toggleExpand: action,
            _changeHover: action,
            _expanded: observable.ref,
            _hovered: observable.ref,
            hoveredOrParentHovered: computed,
        });
    }
    async _toggleExpand() {
        this._expanded = !this._expanded;
        if (this._expanded) {
            try {
                await this.collection.initialFetch();
            }
            catch (e) {
                console.error(e);
            }
        }
    }
    get hoveredOrParentHovered() {
        return this._hovered || !!this.parentNode?.hoveredOrParentHovered;
    }
    _changeHover(hovered) {
        this._hovered = hovered;
    }
    init({ skipCollectionInit } = {}) {
        const { initTask, collection, nestedCollection } = this;
        if (collection._optimisticActions) {
            nestedCollection.events.on('dispose', collection._optimisticActions.init());
        }
        const disposers = [
            !skipCollectionInit
                ? collection.init({
                    skipInitialFetch: nestedCollection.isFetchAllAborted === true && !this._expanded,
                    skipFilterListeners: true,
                })
                : undefined,
        ];
        initTask.runOnce(async () => {
            collection.initTask.runOnce();
            await collection.initTask.status.promise;
        });
        return () => {
            disposers.forEach((d) => d?.());
        };
    }
    _createNode(parent, params) {
        const { nestedCollection } = this;
        return new NestedCollectionNodeState({
            parent,
            parentNode: this,
            nestedCollection,
            levelDescriptor: params.levelDescriptor,
            notifyAll: params.notifyAll,
        });
    }
}
//# sourceMappingURL=NestedCollectionNodeState.js.map