import { useNavigate } from 'react-router-dom';

import { IconArrowRight } from '@material-hu/icons/tabler';
import Button from '@material-hu/mui/Button';
import { type SxProps, type Theme } from '@material-hu/mui/styles';

import { useLokaliseTranslation } from 'src/utils/i18n';

import { useCollaboratorStatus } from '../hooks/useCollaboratorStatus';
import { trackLoanHistoryLinkClicked } from '../track';
import { microloansRoutes } from '../routes';

export type HistoryLinkProps = {
  source: 'request-offer' | 'status';
  sx?: SxProps<Theme>;
};

const HistoryLink = ({ source, sx }: HistoryLinkProps) => {
  const navigate = useNavigate();
  const { t } = useLokaliseTranslation('loans');
  const { hasFinishedPlans, isReady } = useCollaboratorStatus();

  const handleClick = () => {
    trackLoanHistoryLinkClicked(source);
    navigate(microloansRoutes.history());
  };

  if (!isReady || !hasFinishedPlans) {
    return null;
  }

  return (
    <Button
      variant="text"
      color="primary"
      endIcon={
        <IconArrowRight
          size={16}
          stroke={2}
        />
      }
      onClick={handleClick}
      sx={[
        {
          alignSelf: 'flex-start',
          textTransform: 'none',
          px: 2.5,
        },
        ...(Array.isArray(sx) ? sx : [sx]),
      ]}
    >
      {t('history.link')}
    </Button>
  );
};

export default HistoryLink;
