import { FileRejection } from 'react-dropzone';

import Box from '@mui/material/Box';
import Paper from '@mui/material/Paper';
import { alpha } from '@mui/material/styles';
import Typography from '@mui/material/Typography';

import { fData } from '@/utils/format-number';

import { fileData } from '../file-thumbnail';

// ----------------------------------------------------------------------

interface RejectionFilesProps {
  fileRejections: readonly FileRejection[];
  allowedExtensions?: string[];
}

export default function RejectionFiles({ fileRejections, allowedExtensions }: RejectionFilesProps) {
  if (!fileRejections.length) {
    return null;
  }

  return (
    <Paper
      variant="outlined"
      sx={{
        py: 1,
        px: 2,
        mt: 3,
        textAlign: 'left',
        borderStyle: 'dashed',
        borderColor: 'error.main',
        bgcolor: (theme) => alpha(theme.palette.error.main, 0.08),
      }}
    >
      {fileRejections.map(({ file, errors }) => {
        const { path } = fileData(file);
        const hasInvalidType = errors.some((e) => e.code === 'file-invalid-type');

        if (hasInvalidType && allowedExtensions && allowedExtensions.length) {
          return (
            <Box key={path} sx={{ my: 1 }}>
              <Typography variant="body2" sx={{ color: 'error.main' }}>
                Extension non autorisée. Extensions autorisées: {allowedExtensions.join(', ')}
              </Typography>
            </Box>
          );
        }

        const { size } = fileData(file);
        return (
          <Box key={path} sx={{ my: 1 }}>
            <Typography variant="subtitle2" noWrap>
              {path} - {size ? fData(size) : ''}
            </Typography>
            {errors.map((error) => (
              <Box key={error.code} component="span" sx={{ typography: 'caption' }}>
                - {error.message}
              </Box>
            ))}
          </Box>
        );
      })}
    </Paper>
  );
}
