import * as ImageManipulator from 'expo-image-manipulator';
import * as VideoThumbnail from 'expo-video-thumbnails';

export const extractVideoThumbnail = async (videoUri: string): Promise<{ uri: string; file: File }> => {
    try {
        // 获取视频的第一帧
        const { uri: thumbnailUri } = await VideoThumbnail.getThumbnailAsync(
            videoUri,
            {
                time: 1000, // 1秒的位置
                quality: 0.8,
            }
        );

        // 转换为 WebP 格式
        const manipResult = await ImageManipulator.manipulateAsync(
            thumbnailUri,
            [{ resize: { width: 800 } }], // 调整大小以提高性能
            {
                compress: 0.8,
                format: ImageManipulator.SaveFormat.WEBP
            }
        );

        // 转换为 File 对象
        const response = await fetch(manipResult.uri);
        const blob = await response.blob();
        const file = new File(
            [blob],
            `thumb_${Date.now()}.webp`,
            { type: 'image/webp' }
        );

        return { uri: manipResult.uri, file };
    } catch (error) {
        console.error('Error generating video thumbnail:', error);
        throw new Error('无法生成视频缩略图: ' + (error instanceof Error ? error.message : String(error)));
    }
};