/* globals html2canvas */

import { fetchPost } from "./utils.mjs";

async function exportImageAsync(element, fileName, indicator, graphName) {
    const canvas = await html2canvas(element);
    const image = canvas.toDataURL("image/png", 1.0);
    const tempLink = document.createElement("a");
    tempLink.download = fileName;
    tempLink.href = image;
    document.body.appendChild(tempLink);
    tempLink.click();
    document.body.removeChild(tempLink);
    tempLink.remove();
    fetchPost("/api/tracking/event/", {
        indicator: indicator.name,
        event: "download",
        objet: graphName,
        type: "image",
    });
}

export { exportImageAsync };
