import { useQuery } from 'react-query';

import useGeneralError from 'src/hooks/useGeneralError';
import { acknowledgementsKeys } from 'src/pages/dashboard/acknowledgements/queries';
import { getSegmentationGroups } from 'src/services/segmentations';
import { Visibility } from 'src/types/instance';
import { useLokaliseTranslation } from 'src/utils/i18n';

export const useGetKudosSegmentations = (enabled = true) => {
  const showGeneralError = useGeneralError();
  const { t } = useLokaliseTranslation('acknowledgements');

  return useQuery({
    queryKey: acknowledgementsKeys.segmentations.groups(),
    queryFn: () => getSegmentationGroups(),
    select: response => {
      return response.data.filter(
        seg => seg.items.length > 0 && seg.visibility === Visibility.ALL,
      );
    },
    onError: (err: unknown) => {
      showGeneralError(err, t('ranking_filters.error_loading_segmentations'));
    },
    enabled,
  });
};
