import { useEffect } from 'react';

import useHuSnackbar from '@material-hu/components/design-system/Snackbar';
import { tokenManager } from 'src/config/tokens';
import { useLokaliseTranslation } from 'src/utils/i18n';

const useTokenManagerHandlers = () => {
  const { enqueueSnackbar } = useHuSnackbar();
  const { t } = useLokaliseTranslation('token_manager');

  // Register snackbar and translation function handler for token refresh error
  // This callback will be called directly from TokenManager when needed
  useEffect(() => {
    tokenManager.setRefreshErrorHandler(props => {
      enqueueSnackbar(props);
    });
    tokenManager.setLokaliseTFunction(props => {
      return t(props);
    });
  }, [enqueueSnackbar, t]);
};

export default useTokenManagerHandlers;
