import { StringUtils } from '@imengyu/imengyu-utils';

/**
 * 常见扩展名 → MIME（用于 OSS Content-Type、saveAnnex `mimetype` 等）
 * 未收录的扩展名请按需追加。
 */
export const EXTENSION_TO_MIME: Readonly<Record<string, string>> = {
  // 图片
  jpg: 'image/jpeg',
  jpeg: 'image/jpeg',
  jpe: 'image/jpeg',
  png: 'image/png',
  gif: 'image/gif',
  bmp: 'image/bmp',
  webp: 'image/webp',
  svg: 'image/svg+xml',
  ico: 'image/x-icon',
  heic: 'image/heic',
  heif: 'image/heif',
  avif: 'image/avif',
  tif: 'image/tiff',
  tiff: 'image/tiff',

  // 视频
  mp4: 'video/mp4',
  m4v: 'video/x-m4v',
  webm: 'video/webm',
  mov: 'video/quicktime',
  qt: 'video/quicktime',
  wmv: 'video/x-ms-wmv',
  avi: 'video/x-msvideo',
  flv: 'video/x-flv',
  mkv: 'video/x-matroska',
  '3gp': 'video/3gpp',
  '3g2': 'video/3gpp2',
  ts: 'video/mp2t',

  // 音频
  mp3: 'audio/mpeg',
  m4a: 'audio/mp4',
  aac: 'audio/aac',
  wav: 'audio/wav',
  wave: 'audio/wav',
  ogg: 'audio/ogg',
  opus: 'audio/opus',
  flac: 'audio/flac',
  wma: 'audio/x-ms-wma',
  amr: 'audio/amr',
  aiff: 'audio/aiff',
  aif: 'audio/aiff',

  // 文档
  txt: 'text/plain',
  rtf: 'application/rtf',
  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',
  csv: 'text/csv',
  ppt: 'application/vnd.ms-powerpoint',
  pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  odt: 'application/vnd.oasis.opendocument.text',
  ods: 'application/vnd.oasis.opendocument.spreadsheet',
  odp: 'application/vnd.oasis.opendocument.presentation',
  md: 'text/markdown',
  markdown: 'text/markdown',

  // Web / 数据
  html: 'text/html',
  htm: 'text/html',
  css: 'text/css',
  js: 'text/javascript',
  mjs: 'text/javascript',
  json: 'application/json',
  xml: 'application/xml',
  yaml: 'application/yaml',
  yml: 'application/yaml',

  // 压缩包
  zip: 'application/zip',
  rar: 'application/vnd.rar',
  '7z': 'application/x-7z-compressed',
  gz: 'application/gzip',
  tar: 'application/x-tar',
};

const DEFAULT_MIME = 'application/octet-stream';

/** 规范化扩展名：去前导点、小写 */
export function normalizeFileExtension(extOrPath: string): string {
  const raw = extOrPath.includes('.')
    ? StringUtils.path.getFileExt(extOrPath)
    : extOrPath;
  return raw.replace(/^\./, '').toLowerCase();
}

/** 仅根据扩展名（可带或不带 `.`，也可传完整路径）解析 MIME */
export function getMimeTypeFromExtension(extOrPath: string): string {
  const ext = normalizeFileExtension(extOrPath);
  return EXTENSION_TO_MIME[ext] ?? DEFAULT_MIME;
}

/**
 * 根据本地路径或 URL 推断 MIME（依赖扩展名）
 */
export function getMimeType(filePath: string): string {
  if (!filePath)
    return DEFAULT_MIME;
  return getMimeTypeFromExtension(filePath);
}

/** 是否图片类 MIME */
export function isImageMime(mime: string): boolean {
  return mime.startsWith('image/');
}

/** 是否视频类 MIME */
export function isVideoMime(mime: string): boolean {
  return mime.startsWith('video/');
}

/** 是否音频类 MIME */
export function isAudioMime(mime: string): boolean {
  return mime.startsWith('audio/');
}
