// 音频配置管理 - 简化版
export interface AudioFormat {
  mimeType: string;
  extension: string;
  codec: string;
  quality: "low" | "medium" | "high" | "lossless";
  maxBitrate: number;
}

// 支持的音频格式配置
export const SUPPORTED_AUDIO_FORMATS: Record<string, AudioFormat> = {
  webm: {
    mimeType: "audio/webm;codecs=opus",
    extension: ".webm",
    codec: "opus",
    quality: "high",
    maxBitrate: 128000,
  },
  ogg: {
    mimeType: "audio/ogg;codecs=opus",
    extension: ".ogg",
    codec: "opus",
    quality: "high",
    maxBitrate: 192000,
  },
  aac: {
    mimeType: "audio/aac",
    extension: ".aac",
    codec: "aac",
    quality: "high",
    maxBitrate: 256000,
  },
};

// 获取浏览器支持的音频格式
export function getSupportedFormats(): AudioFormat[] {
  const supported: AudioFormat[] = [];

  // 检查 WebM 支持
  if (MediaRecorder.isTypeSupported("audio/webm;codecs=opus")) {
    supported.push(SUPPORTED_AUDIO_FORMATS.webm);
  }

  // 检查 OGG 支持
  if (MediaRecorder.isTypeSupported("audio/ogg;codecs=opus")) {
    supported.push(SUPPORTED_AUDIO_FORMATS.ogg);
  }

  // 检查 AAC 支持
  if (MediaRecorder.isTypeSupported("audio/aac")) {
    supported.push(SUPPORTED_AUDIO_FORMATS.aac);
  }

  // 如果没有支持的格式，至少返回 WebM
  if (supported.length === 0) {
    supported.push(SUPPORTED_AUDIO_FORMATS.webm);
  }

  return supported;
}

// 获取最佳录音格式
export function getBestRecordingFormat(): AudioFormat {
  const supported = getSupportedFormats();
  return supported[0] || SUPPORTED_AUDIO_FORMATS.webm;
}

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