import React, { useState } from 'react'; import { TRANSLATIONS } from '../constants'; import { AppLanguage } from '../types'; import { generateImage, generateVideo } from '../services/geminiService'; import { Loader2, Image as ImageIcon, Video, Download, Sparkles, Key } from 'lucide-react'; interface ToolsProps { language: AppLanguage; hasCustomKey?: boolean; } const Tools: React.FC = ({ language, hasCustomKey }) => { const t = TRANSLATIONS[language]; const [activeTab, setActiveTab] = useState<'image' | 'video'>('image'); const [prompt, setPrompt] = useState(''); const [loading, setLoading] = useState(false); const [resultUrl, setResultUrl] = useState(null); const [imageSize, setImageSize] = useState<"1K" | "2K" | "4K">("1K"); const [videoRatio, setVideoRatio] = useState<"16:9" | "9:16">("16:9"); const [error, setError] = useState(null); const handleGenerate = async () => { if (!prompt.trim()) return; // Users must select their own API key for image/video generation tasks // Skip this check if user has manually configured a key in settings if (!hasCustomKey && typeof (window as any).aistudio !== 'undefined') { const hasKey = await (window as any).aistudio.hasSelectedApiKey(); if (!hasKey) { await (window as any).aistudio.openSelectKey(); // Proceeding assuming selection was successful per guidelines } } setLoading(true); setError(null); setResultUrl(null); try { if (activeTab === 'image') { const images = await generateImage(prompt, imageSize); if (images.length > 0) setResultUrl(images[0]); } else { const video = await generateVideo(prompt, videoRatio); setResultUrl(video); } } catch (e: any) { if (!hasCustomKey && e.message && e.message.includes("Requested entity was not found.")) { // Handle race condition or invalid key by prompting re-selection, ONLY if no custom key if (typeof (window as any).aistudio !== 'undefined') { await (window as any).aistudio.openSelectKey(); } } setError(e.message || t.genError); } finally { setLoading(false); } }; return (