import once from 'lodash/once';
import { getNodeOrLeaf } from './getNodeOrLeaf';
import { action, makeObservable } from 'mobx';
export class NestedTableNodeStateOptimisticActions {
    constructor(node) {
        this.node = node;
        makeObservable(this, {
            _propagate: action,
            _runAction: action,
            deleteMany: action,
            updateMany: action,
        });
    }
    _getActionsParams(params) {
        const primaryActionParams = {
            ...params,
            submit: once(params.submit),
        };
        const secondaryActionParams = {
            ...primaryActionParams,
            successToast: undefined,
            errorToast: undefined,
        };
        return {
            primaryActionParams,
            secondaryActionParams,
        };
    }
    _runAction(action, params) {
        const { node: { flatModeCollection, children, nestedTable }, } = this;
        const { primaryActionParams, secondaryActionParams } = this._getActionsParams(params);
        if (children?._optimisticActions) {
            action(children._optimisticActions, primaryActionParams);
        }
        if (flatModeCollection?._optimisticActions) {
            action(flatModeCollection._optimisticActions, secondaryActionParams);
        }
        if (nestedTable.toolbar.collection !== children &&
            nestedTable.toolbar.collection._optimisticActions) {
            action(nestedTable.toolbar.collection._optimisticActions, secondaryActionParams);
        }
    }
    _propagate(items, action) {
        const { node } = this;
        if (!node.children) {
            return;
        }
        const { itemKey, result: { _keyedItemsMap }, } = node.collection;
        const directItems = items.map((item) => _keyedItemsMap.get(itemKey(item)));
        const directNodes = directItems.map((keyedItem) => getNodeOrLeaf(node, keyedItem));
        for (const subNode of directNodes) {
            const subItems = subNode.children?.result.items;
            if (!subItems?.length) {
                continue;
            }
            action(subNode, subItems);
        }
    }
    createOne(item, params) {
        this.createMany([item], params);
    }
    createMany(items, params) {
        this._runAction((a, p) => a.createMany(items, p), params);
    }
    updateAll(patchOrFn, params) {
        this._runAction((a, p) => a.updateAll(patchOrFn, p), params);
    }
    deleteMany(items, params) {
        const { primaryActionParams, secondaryActionParams } = this._getActionsParams(params);
        this._propagate(items, (subNode, subItems) => {
            subNode.optimisticActions?.deleteMany(subItems, secondaryActionParams);
        });
        this._runAction((a, p) => a.deleteMany(items, p), primaryActionParams);
    }
    deleteOne(item, params) {
        this.deleteMany([item], params);
    }
    deleteAll(params) {
        this._runAction((a, p) => a.deleteAll(p), params);
    }
    updateMany(items, params) {
        const { primaryActionParams, secondaryActionParams } = this._getActionsParams(params);
        if (params?.propagate) {
            this._propagate(items, (subNode, subItems) => {
                subNode.optimisticActions?.updateMany(subItems, secondaryActionParams);
            });
        }
        this._runAction((a, p) => a.updateMany(items, p), primaryActionParams);
    }
    updateOne(item, params) {
        this.updateMany([item], params);
    }
}
//# sourceMappingURL=NestedTableNodeStateOptimisticActions.js.map