import api from '@/api';
import { getAll } from './search';

//
// MARK: Delete user
//
const loadingActionsID: Ref<number[]> = ref([0]);
const deleteUser = async (ID: number) => {
  if (!ID) {
    return;
  }

  setLoadingActions([ID]);

  const [_, e] = await api.users.delete(ID);
  if (e && typeof e === 'string') {
    // TODO notify error
  } else {
    getAll();
  }

  setLoadingActions([0]);
};
const deleteMany = async (IDs: number[]) => {
  if (!IDs.length) {
    return;
  }

  setLoadingActions(IDs);

  const [_, e] = await api.users.deleteMany(IDs);
  if (e && typeof e === 'string') {
    // TODO notify error
  } else {
    getAll();
  }

  setLoadingActions([0]);
};
const blockMany = async (IDs: number[]) => {
  if (!IDs.length) {
    return;
  }

  setLoadingActions(IDs);

  const [_, e] = await api.users.blockMany(IDs);
  if (e && typeof e === 'string') {
    // TODO notify error
  } else {
    getAll();
  }

  setLoadingActions([0]);
};
const unblockMany = async (IDs: number[]) => {
  if (!IDs.length) {
    return;
  }

  setLoadingActions(IDs);

  const [_, e] = await api.users.unblockMany(IDs);
  if (e && typeof e === 'string') {
    // TODO notify error
  } else {
    getAll();
  }

  setLoadingActions([0]);
};
const setLoadingActions = (IDs: number[]) => (loadingActionsID.value = IDs);

export { loadingActionsID, deleteUser, deleteMany, blockMany, unblockMany };
