import {useAp2iRequestService} from "~/composables/data/useAp2iRequestService";
import File from "~/models/Core/File"
import FileSaver from "file-saver";

export const useDownloadFile = async (file: File) => {
    const { apiRequestService } = useAp2iRequestService()

    const downloadUrl = `api/download/${file.id}`

    const response: any = await apiRequestService.get(downloadUrl)

    if (!response || response.size === 0) {
        console.error('Error: file ' + file.id + ' not found')
    }

    const blob = new Blob([response], { type: response.type })

    FileSaver.saveAs(blob, file.name ?? 'unknown');
}
