/**
 * 共享的文件类型常量和 MIME 推断工具
 * 供前端 useFileUploader 和服务端 file-uploader 复用
 */

export const ACCEPT_FILE_TYPE_LIST = [
  '.pdf', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx',
  '.txt', '.json', '.csv', '.md',
  '.png', '.jpg', '.jpeg', '.gif', '.bmp', '.webp', '.svg', '.ico',
  '.html', '.py', '.jsonld', '.xml', '.zip',
  '.mp3', '.mp4', '.mov', '.m4a',
  '.pdb', '.mermaid',
]

const MIME_MAP: Record<string, string> = {
  '.pdf': 'application/pdf',
  '.doc': 'application/msword',
  '.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  '.xls': 'application/vnd.ms-excel',
  '.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  '.ppt': 'application/vnd.ms-powerpoint',
  '.pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  '.txt': 'text/plain',
  '.json': 'application/json',
  '.csv': 'text/csv',
  '.md': 'text/markdown',
  '.png': 'image/png',
  '.jpg': 'image/jpeg',
  '.jpeg': 'image/jpeg',
  '.gif': 'image/gif',
  '.bmp': 'image/bmp',
  '.webp': 'image/webp',
  '.svg': 'image/svg+xml',
  '.ico': 'image/x-icon',
  '.html': 'text/html',
  '.py': 'text/x-python',
  '.jsonld': 'application/ld+json',
  '.xml': 'application/xml',
  '.zip': 'application/zip',
  '.mp3': 'audio/mpeg',
  '.mp4': 'video/mp4',
  '.mov': 'video/quicktime',
  '.m4a': 'audio/mp4',
  '.pdb': 'application/vnd.microsoft.portable-executable',
  '.mermaid': 'text/mermaid',
}

export function getFileExtension(filename: string): string {
  return `.${(filename.split('.').pop() || '').toLowerCase()}`
}

export function getMimeByExtension(filename: string): string | undefined {
  const ext = getFileExtension(filename)
  return MIME_MAP[ext]
}

export function isSupportedFileType(filename: string): boolean {
  const ext = getFileExtension(filename)
  return ACCEPT_FILE_TYPE_LIST.includes(ext)
}
