export function useZipDownload() {
  async function downloadAsZip(
    attachments: { id: number; filename: string }[],
    basePath: string,
    zipFilename: string
  ) {
    const { request } = useApi()
    const { default: JSZip } = await import('jszip')
    const zip = new JSZip()

    await Promise.all(
      attachments.map(async (attachment) => {
        const blob = await request<Blob>(`${basePath}/${attachment.id}/download`, { responseType: 'blob' })
        zip.file(attachment.filename, blob)
      })
    )

    const zipBlob = await zip.generateAsync({ type: 'blob' })
    const url = URL.createObjectURL(zipBlob)
    const link = document.createElement('a')
    link.href = url
    link.download = zipFilename
    link.click()
    URL.revokeObjectURL(url)
  }

  return { downloadAsZip }
}
