// 导出工具函数
export { cn } from './cn';
export {
  MinIOXHRMultipartUploader,
  MinIOXHRUploader,
  uploadMinIOWithPolicy,
  getUploadPolicy,
  getMultipartUploadPolicy,
  type MinioProgressEvent,
  type MinioProgressCallbacks,
  type UploadResult
} from './minio';

// 格式化文件大小
export const formatFileSize = (bytes: number): string => {
  if (bytes === 0) return '0 Bytes';
  const k = 1024;
  const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  const i = Math.floor(Math.log(bytes) / Math.log(k));
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
};

// 检查文件类型是否可预览
export const isPreviewableFileType = (fileType: string | null): boolean => {
  if (!fileType) return false;
  return fileType.startsWith('image/') || fileType.startsWith('video/');
};

// 获取文件图标类型
export const getFileIconType = (fileType: string): 'image' | 'video' | 'audio' | 'pdf' | 'document' | 'spreadsheet' | 'text' | 'other' => {
  if (fileType.startsWith('image/')) return 'image';
  if (fileType.startsWith('video/')) return 'video';
  if (fileType.startsWith('audio/')) return 'audio';
  if (fileType.includes('pdf')) return 'pdf';
  if (fileType.includes('word') || fileType.includes('document')) return 'document';
  if (fileType.includes('excel') || fileType.includes('sheet')) return 'spreadsheet';
  if (fileType.includes('text')) return 'text';
  return 'other';
};

// 验证文件类型
export const validateFileType = (file: File, accept?: string): boolean => {
  if (!accept || accept === '*/*') return true;

  const acceptTypes = accept.split(',').map(type => type.trim());

  return acceptTypes.some(type => {
    if (type.startsWith('.')) {
      // 文件扩展名匹配
      const extension = type.toLowerCase();
      const fileName = file.name.toLowerCase();
      return fileName.endsWith(extension);
    } else {
      // MIME类型匹配
      return file.type.match(new RegExp(type.replace('*', '.*')));
    }
  });
};

// 验证文件大小
export const validateFileSize = (file: File, maxSizeMB: number): boolean => {
  const maxSizeBytes = maxSizeMB * 1024 * 1024;
  return file.size <= maxSizeBytes;
};