export default function useQR() {
  async function downloadQR(url: string, filename: string = "qrcode") {
    const res = await useNuxtApp().$api<Blob | MediaSource>(
      `/api/hygiene-qr/generate`,
      {
        method: "GET",
        query: {
          url,
          filename,
        },
      }
    );

    if (!res) throw new Error("Error downloading QR code");

    const downloadUrl = window.URL.createObjectURL(res);
    const a = document.createElement("a");
    a.href = downloadUrl;
    a.download = `${filename}.png`;
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
    window.URL.revokeObjectURL(downloadUrl);
  }

  return {
    downloadQR,
  };
}
