"use strict";

exports.__esModule = true;
exports.onItemsCreated = void 0;
var _mobx = require("mobx");
var _v = require("@wix/bex-utils/@wix/bi-logger-os-data/v2");
async function _onItemsCreated(collection, {
  items,
  reportBi
}) {
  var _collection$scrollabl;
  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((0, _v.newItemCreationEnd)({
    numSelectedItems: collection.bulkSelect.select.selectedCount,
    itemId: JSON.stringify(items.map(itemKey)),
    resultType: 'success'
  }));
  (_collection$scrollabl = collection.scrollableContent) == null || _collection$scrollabl.scrollTo == null || _collection$scrollabl.scrollTo({
    top: 0,
    behavior: 'smooth'
  });
}
const onItemsCreated = exports.onItemsCreated = (0, _mobx.action)(_onItemsCreated);
//# sourceMappingURL=onItemsCreated.js.map