export function saveBlob(blob: Blob, filename = "download.bin") {
  const url = URL.createObjectURL(blob);
  const a = document.createElement("a");
  a.href = url;
  a.download = filename;
  document.body.appendChild(a);
  a.click();
  a.remove();
  URL.revokeObjectURL(url);
}

export function filenameFromContentDisposition(
  cd?: string,
  fallback = "download.bin",
) {
  if (!cd) return fallback;
  const m = cd.match(/filename\*=UTF-8''([^;]+)|filename="?([^"]+)"?/i);
  const name = decodeURIComponent(m?.[1] || m?.[2] || "");
  return name || fallback;
}

export function lastSegment(path: string, fallback = "download.bin") {
  return path?.split("/").pop() || fallback;
}
