import { IconType } from 'react-icons';
import {
    AiFillAudio,
    AiFillFile,
    AiFillFileImage,
    AiFillFilePdf,
    AiFillFileZip,
    AiFillVideoCamera,
} from 'react-icons/ai';

import { FileDocumentFileType } from '../../../api/types';

interface Props {
    doc: FileDocumentFileType
    size?: number;
    className?: string;
}

const FileTypeIcon: React.FC<Props> = ({ doc, size = 40, className = '' }) => {

    const type = doc?.type;

    if (!type) {
        return <AiFillFile size={size} className={className} />;
    }

    const iconMap: { [key: string]: IconType } = {
        'image': AiFillFileImage,
        'audio': AiFillAudio,
        'video': AiFillVideoCamera,
        'application/pdf': AiFillFilePdf,
        'application/zip': AiFillFileZip,
        'application/x-zip-compressed': AiFillFileZip,
    };

    // Пробуем найти полное совпадение
    if (iconMap[type]) {
        const Icon = iconMap[type];
        return <Icon size={size} className={className} />;
    }

    // Пробуем по первой части типа: "image/png" -> "image"
    const prefix = type.split('/')[0];

    if (prefix == 'image') {
        return <img src={doc.url} alt={doc.name}
            style={{ width: size, height: size }}
            className={` object-cover ${className} rounded-xl bg-white`} />;
    }

    if (iconMap[prefix]) {
        const Icon = iconMap[prefix];
        return <Icon size={size} className={className} />;
    }

    // Заглушка
    return <AiFillFile size={size} className={className} />;
};

export default FileTypeIcon;