"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileIcon = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const icons_1 = require("@gravity-ui/icons");
const uikit_1 = require("@gravity-ui/uikit");
const cn_1 = require("../../../utils/cn.js");
require("./FileIcon.css");
const b = (0, cn_1.block)('file-icon');
const ICON_SIZE_MAP = { s: 12, m: 16, l: 20 };
const CODE_SUBTYPES = ['json', 'xml', 'html', 'javascript', 'typescript'];
const ARCHIVE_KEYWORDS = ['zip', 'tar', 'gzip'];
const TEXT_KEYWORDS = ['spreadsheet', 'csv', 'word', 'document'];
function getIconByMimeType(mimeType) {
    var _a;
    const type = mimeType.split('/')[0];
    const subtype = (_a = mimeType.split('/')[1]) !== null && _a !== void 0 ? _a : '';
    if (type === 'image')
        return icons_1.File;
    if (type === 'audio' || subtype.includes('audio'))
        return icons_1.Headphones;
    if (type === 'video')
        return icons_1.Video;
    if (type === 'text')
        return subtype === 'plain' ? icons_1.FileText : icons_1.FileCode;
    if (subtype === 'pdf')
        return icons_1.FileLetterP;
    if (CODE_SUBTYPES.includes(subtype))
        return icons_1.FileCode;
    if (ARCHIVE_KEYWORDS.some((kw) => subtype.includes(kw)))
        return icons_1.FileZipper;
    if (TEXT_KEYWORDS.some((kw) => subtype.includes(kw)))
        return icons_1.FileText;
    if (subtype.includes('presentation'))
        return icons_1.File;
    return icons_1.File;
}
function getIconByExtension(ext) {
    const lower = ext.toLowerCase();
    if (['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg', 'bmp', 'ico'].includes(lower))
        return icons_1.File;
    if (['mp3', 'wav', 'ogg', 'flac', 'aac', 'm4a'].includes(lower))
        return icons_1.MusicNote;
    if (['mp4', 'avi', 'mov', 'mkv', 'webm', 'flv'].includes(lower))
        return icons_1.Video;
    if (['pdf'].includes(lower))
        return icons_1.FileLetterP;
    if (['zip', 'tar', 'gz', 'rar', '7z', 'bz2'].includes(lower))
        return icons_1.FileZipper;
    if (['js', 'ts', 'tsx', 'jsx', 'py', 'go', 'rs', 'java', 'cpp', 'c', 'cs'].includes(lower)) {
        return icons_1.Code;
    }
    if (['json', 'xml', 'html', 'css', 'yml', 'yaml'].includes(lower))
        return icons_1.FileCode;
    if (['txt', 'md', 'log', 'csv', 'tsv'].includes(lower))
        return icons_1.FileText;
    return icons_1.File;
}
function resolveIcon(mimeType, fileName) {
    if (mimeType)
        return getIconByMimeType(mimeType);
    if (fileName) {
        const parts = fileName.split('.');
        if (parts.length > 1)
            return getIconByExtension(parts[parts.length - 1]);
    }
    return icons_1.File;
}
/**
 * FileIcon displays an icon representing a file based on its MIME type or file name extension.
 */
const FileIcon = ({ mimeType, fileName, size = 'm', className }) => {
    const icon = resolveIcon(mimeType, fileName);
    return (0, jsx_runtime_1.jsx)(uikit_1.Icon, { data: icon, size: ICON_SIZE_MAP[size], className: b({ size }, className) });
};
exports.FileIcon = FileIcon;
//# sourceMappingURL=FileIcon.js.map
