import { useToast, type ToastOptions } from '@rkheftan/harmony-ui';
import { useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { Navigate } from 'react-router-dom';

export interface NavigateWithToastProps extends ToastOptions {
  to: string;
  replace?: boolean;
  translator?: string;
}

export const NavigateWithToast = (props: NavigateWithToastProps) => {
  const { t } = useTranslation(props.translator ?? 'common');
  const toast = useToast();

  useEffect(() => {
    toast({
      ...props,
      message: t(props.message),
    });
  }, []);

  return <Navigate to={props.to} replace={props.replace ? true : false} />;
};
