// Libs
// -----------------------------------------------------------------------------
import { useCallback } from 'react';

// App
// -----------------------------------------------------------------------------
import { runAsyncWithMessages } from 'components/Messages';
import { GenericDashboardLocale } from '../types';
import { GenericDataStore } from 'stores/GenericDataStore';
import { GenericDashboardUiStore } from 'stores/GenericDashboardUiStore';

/**
 * Получить функцию для удаления нескольких элемента таблицы.
 */
export const useCollectiveDelete = <T, TB = T>(
  locale: GenericDashboardLocale,
  dataStore: GenericDataStore<T, TB>,
  uiStore: GenericDashboardUiStore<T>,
): ((ids: Array<number | string>) => void) => {
  return useCallback(
    async (ids: Array<number | string>): Promise<void> => {
      await runAsyncWithMessages(
        async () => {
          await dataStore.deleteMany(ids);
        },
        {
          okMessage: locale.deletionSuccess,
          errorMessage: locale.deletionError,
        },
      );

      uiStore.setSelectedRowKeys([]);
      dataStore.setCurrentPage(1);
      await dataStore.fetchForCurrentPage();
    },
    [locale, dataStore, uiStore],
  );
};
