import { saveAs } from "file-saver";

export async function download(data: Blob, fileName: string) {
  // 1️⃣ 后端返回的是错误 JSON
  if (data.type.includes("application/json")) {
    const text = await data.text();
    const err = JSON.parse(text);
    throw new Error(err.msg || "导出失败");
  }

  // 2️⃣ 正常下载
  const blob = new Blob([data], {
    type: data.type || "application/octet-stream",
  });

  saveAs(blob, fileName);
}
