import { type FieldError } from 'react-hook-form';

import { IconExclamationCircle } from '@material-hu/icons/tabler';
import FormHelperText from '@material-hu/mui/FormHelperText';
import Stack from '@material-hu/mui/Stack';
import Typography from '@material-hu/mui/Typography';

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

type Props = {
  error: FieldError | undefined;
};

const FieldErrorMessage = ({ error }: Props) => {
  const { t } = useLokaliseTranslation('validations');

  if (!error) return null;

  return (
    <FormHelperText
      sx={{
        mx: 0,
        mt: 1,
        '& *': {
          color: ({ palette }) =>
            `${palette.new?.text.feedback.error} !important`,
        },
      }}
    >
      <Stack
        component="span"
        sx={{
          alignItems: 'flex-start',
          flexDirection: 'row',
          gap: 0.5,
          '& > svg': {
            minWidth: '16px',
            height: '22px',
          },
        }}
      >
        <IconExclamationCircle size="1rem" />
        <Typography
          variant="globalS"
          sx={{ flex: 1 }}
        >
          {error.message || t('required')}
        </Typography>
      </Stack>
    </FormHelperText>
  );
};

export default FieldErrorMessage;
