import { action } from 'mobx';
import { newItemCreationEnd } from '@wix/bex-utils/@wix/bi-logger-os-data/v2';
async function _onItemsCreated(collection, { items, reportBi }) {
    const { queryClient, query, itemKey } = collection;
    query.resetFilters();
    query.resetPage();
    const { queryKey } = collection.queryKeyOptions;
    await collection.clearResultAndMoveToStart();
    const existingData = queryClient.getQueryData(queryKey);
    if (existingData) {
        const nonExistingItems = items.filter((item) => !existingData.items.some((existingItem) => itemKey(existingItem) === itemKey(item)));
        queryClient.setQueryData(queryKey, {
            ...existingData,
            items: [...nonExistingItems, ...existingData.items],
            total: (existingData.total ?? existingData.items.length) +
                nonExistingItems.length,
            ...(existingData.available && {
                available: existingData.available + nonExistingItems.length,
            }),
        });
        await collection.clearResultAndMoveToStart();
    }
    reportBi(newItemCreationEnd({
        numSelectedItems: collection.bulkSelect.select.selectedCount,
        itemId: JSON.stringify(items.map(itemKey)),
        resultType: 'success',
    }));
    collection.scrollableContent?.scrollTo?.({
        top: 0,
        behavior: 'smooth',
    });
}
export const onItemsCreated = action(_onItemsCreated);
//# sourceMappingURL=onItemsCreated.js.map