import { useState } from 'react';
import html2canvas from 'html2canvas';

export const useScreenshot = () => {
    const [isCapturing, setIsCapturing] = useState(false);
    const [screenshot, setScreenshot] = useState<string | null>(null);

    const captureScreen = async () => {
        try {
            setIsCapturing(true);
            
            // Trouver et cacher temporairement la modal
            const modalElement = document.querySelector('.bug-whisper-widget');
            if (modalElement) {
                (modalElement as HTMLElement).style.visibility = 'hidden';
            }

            // Attendre que les changements de DOM soient appliqués
            await new Promise(resolve => setTimeout(resolve, 100));

            const canvas = await html2canvas(document.body, {
                useCORS: true,
                allowTaint: true,
                logging: false,
                scale: window.devicePixelRatio
            });

            // Restaurer la visibilité de la modal
            if (modalElement) {
                (modalElement as HTMLElement).style.visibility = 'visible';
            }

            const screenshotData = canvas.toDataURL('image/png');
            setScreenshot(screenshotData);
        } catch (error) {
            console.error('Erreur lors de la capture d\'écran:', error);
        } finally {
            setIsCapturing(false);
        }
    };

    const resetScreenshot = () => {
        setScreenshot(null);
    };

    return {
        isCapturing,
        screenshot,
        captureScreen,
        resetScreenshot
    };
};
