import { useQuery } from 'react-query';

import useGeneralError from 'src/hooks/useGeneralError';
import { profileKeys } from 'src/pages/dashboard/profile/queries';
import { getProfileById } from 'src/services/users';

import { useTranslation } from 'src/components/dashboard/chat/i18n';

export const useProfile = (userId: number | string, isEnabled = true) => {
  const showGeneralError = useGeneralError();
  const { t } = useTranslation();

  return useQuery(profileKeys.detail(userId), () => getProfileById(userId), {
    onError: err => {
      showGeneralError(err, t('ERROR_LOADING_DATA_MESSAGE'));
    },
    enabled: isEnabled,
  });
};

export default useProfile;
