import React, { useState } from 'react' import { Modal } from '@/components/ui/Modal' import { ContentBlock } from '@/components/shared/ContentBlock' import { getCEFRColor } from '@/lib/utils/flashcardUtils' import { BluePlayButton } from '@/components/shared/BluePlayButton' import { useWordAnalysis } from './hooks/useWordAnalysis' import type { WordAnalysis } from './types' interface WordPopupProps { selectedWord: string | null analysis: Record isOpen: boolean onClose: () => void onSaveWord?: (word: string, analysis: WordAnalysis) => Promise<{ success: boolean; error?: string }> isSaving?: boolean } export const WordPopup: React.FC = ({ selectedWord, analysis, isOpen, onClose, onSaveWord, isSaving = false }) => { const { getWordProperty } = useWordAnalysis() const [isPlaying, setIsPlaying] = useState(false) if (!selectedWord || !analysis?.[selectedWord]) { return null } const wordAnalysis = analysis[selectedWord] const handlePlayPronunciation = (text: string, lang?: string) => { if (isPlaying) { speechSynthesis.cancel() setIsPlaying(false) return } const utterance = new SpeechSynthesisUtterance(text) utterance.lang = lang || 'en-US' utterance.rate = 0.8 utterance.onstart = () => setIsPlaying(true) utterance.onend = () => setIsPlaying(false) utterance.onerror = () => setIsPlaying(false) speechSynthesis.speak(utterance) } const handleSaveWord = async () => { if (onSaveWord) { await onSaveWord(selectedWord, wordAnalysis) } } return ( {/* Header */}

{getWordProperty(wordAnalysis, 'word')}

{getWordProperty(wordAnalysis, 'partOfSpeech')}
{getWordProperty(wordAnalysis, 'pronunciation')}
{getWordProperty(wordAnalysis, 'cefr')}
{/* Content */}
{/* Translation */}

{getWordProperty(wordAnalysis, 'translation')}

{/* Definition */}

{getWordProperty(wordAnalysis, 'definition')}

{/* Example */} {(() => { const example = getWordProperty(wordAnalysis, 'example') return example && example !== 'null' && example !== 'undefined' })() && (

"{getWordProperty(wordAnalysis, 'example')}"

{getWordProperty(wordAnalysis, 'exampleTranslation')}

)} {/* Synonyms */} {(() => { const synonyms = getWordProperty(wordAnalysis, 'synonyms') return synonyms && Array.isArray(synonyms) && synonyms.length > 0 })() && (
{getWordProperty(wordAnalysis, 'synonyms')?.map((synonym: string, index: number) => ( {synonym} ))}
)}
{/* Save Button */} {onSaveWord && (
)}
) }