import {Box, Skeleton, Stack} from "@mui/material";
import {useEffect} from "react";
const ImageContent = (link) => {
    useEffect(() => {
        const timer = setTimeout(() => {
            link.setIsSkeleton(false);
        }, 1000);

        return () => clearTimeout(timer);
    }, [link.isSkeleton]);

    const isPDF = link && link.imageLink && link.imageLink.attachment && link.imageLink.attachment.endsWith('.pdf');
    return (
        <>
            <Stack sx={{width :"100%"}}>
                {link.isSkeleton && (
                    <Box  sx={{display:'flex' ,justifyContent:'center' , border: '1px solid #ccc' ,p:5 , height: '700px' , borderRadius: '8px' , width:'100%'}}>
                        <Skeleton variant="rectangular"  animation="wave" sx={{ width: '100%', height: '100%' }}  />
                    </Box>
                )}
                {isPDF ? (
                        <embed src={link.imageLink.attachment} type="application/pdf" width="100%" height="700px" style={{ display: link.isSkeleton ? 'none' : 'block' }}/>
                ) : (
                    link && link.imageLink && link.imageLink.attachment && (
                        <Box sx={{display: link.isSkeleton ? 'none' : 'flex' ,justifyContent:'center' , border: '1px solid #ccc' , p:5 , height: '700px' , borderRadius: '8px',}}>
                            <Box component="img" src={link.imageLink.attachment} alt="Image Preview" width="auto"  height="auto" sx={{maxHeight: '700px' , maxWidth:'100%'}}></Box>
                        </Box>
                    )
                )}
            </Stack>
        </>
    )

}
export default ImageContent;