const MIME_TYPE_DESCRIPTIONS = {
  'image/gif': 'GIF Image',
  'image/jpeg': 'JPEG Image',
  'image/png': 'PNG Image',
  'image/tiff': 'TIFF Image',
  'image/svg+xml': 'SVG Image',
  'image/webp': 'WEBP Image',
  'audio/wav': 'WAVE Audio',
  'audio/ogg': 'OGG Audio',
  'audio/mpeg': 'MPEG Audio',
  'audio/mid': 'MIDI Track',
  'application/json': 'JSON Data',
  'application/xml': 'XML Data',
  'application/x-bittorrent': 'Torrent File',
  'application/x-zip-compressed': 'Compressed ZIP Archive',
  'application/x-x509-ca-cert': 'Certificate File',
  'application/x-tar': 'Compressed TAR Archive',
  'application/x-rar': 'Compressed RAR Archive',
  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'Workbook',
  'application/vnd.openxmlformats-officedocument.presentationml.presentation': 'Slideshow Presentation',
  'application/msword': 'Microsoft Word Document',
  'application/pdf': 'PDF Document',
  'application/postscript': 'PostScript Document',
  'application/epub+zip': 'EPUB Document',
  'message/rfc822': 'Email Message',
  'video/mp4': 'MP4 Video',
} as const;

const EXTENSION_DESCRIPTIONS = {
  rar: 'Compressed RAR Archive',
  '7z': 'Compressed 7-Zip Archive',
  psd: 'Adobe Photoshop Document',
  dmg: 'Disk Image',
  'fb2k-component': 'foobar2000 Component',
} as const;

export const getFileDescription = (type?: string, filename?: string) => {
  if (typeof (type as unknown) !== 'string') {
    return '';
  }

  if (type === 'application/octet-stream' || type === '') {
    if (typeof filename === 'string' && filename.includes('.')) {
      const extension = filename.slice(filename.lastIndexOf('.') + '.'.length).toLowerCase();
      const {
        [extension as keyof typeof EXTENSION_DESCRIPTIONS]: extensionDescription = `${extension.toUpperCase()} File`,
      } = EXTENSION_DESCRIPTIONS;

      return extensionDescription;
    }
    return `${type} File`;
  }

  const {
    [type as keyof typeof MIME_TYPE_DESCRIPTIONS]: description = type,
  } = MIME_TYPE_DESCRIPTIONS;

  return description;
};
