"use client"; import { useCallback, useEffect, useMemo, useState } from "react"; import { allWords, topics, wordLabel, type Word } from "@/data/vocab"; import Ar from "@/components/Ar"; import SpeakButton from "@/components/SpeakButton"; import ResultCard from "@/components/ResultCard"; import { addXp, getProgress, recordResult, reviewCard, todayStr, useProgress } from "@/lib/progress"; import { speak } from "@/lib/speech"; import { sfx } from "@/lib/sfx"; import { fireConfetti } from "@/components/AppShell"; import { gapFillQuestions, perfektQuestions, pluralQuestions, type DrillQ } from "@/lib/drills"; type Mode = "karten" | "quiz" | "luecke" | "plural" | "perfekt" | "artikel" | "memory" | "liste"; const modes: { id: Mode; de: string; ar: string }[] = [ { id: "karten", de: "🃏 Karteikarten", ar: "البطاقات" }, { id: "quiz", de: "🎯 Quiz", ar: "اختبار" }, { id: "luecke", de: "✏️ Lückentext", ar: "املأ الفراغ" }, { id: "plural", de: "🔢 Plural", ar: "الجمع" }, { id: "perfekt", de: "⏮️ hat/ist", ar: "الماضي التام" }, { id: "artikel", de: "⚡ der/die/das", ar: "لعبة الأدوات" }, { id: "memory", de: "🧠 Memory", ar: "لعبة الذاكرة" }, { id: "liste", de: "📚 Wortliste", ar: "قائمة الكلمات" }, ]; function shuffle(a: T[]): T[] { const b = [...a]; for (let i = b.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [b[i], b[j]] = [b[j], b[i]]; } return b; } export default function WortschatzPage() { const [mode, setMode] = useState("karten"); const [topic, setTopic] = useState("alle"); const [key, setKey] = useState(0); const [mounted, setMounted] = useState(false); useEffect(() => setMounted(true), []); const words = useMemo(() => (topic === "alle" ? allWords : topics.find((t) => t.id === topic)!.words), [topic]); const p = useProgress(); const known = words.filter((w) => (p.cards[w.id]?.box ?? 0) >= 3).length; return (

🃏 Wortschatz

{allWords.length} B1-Wörter in {topics.length} Themen – lernen, spielen, wiederholen! تعلّم، العب، راجع!

{modes.map((m) => ( ))}
Gelernt: {known}/{words.length}
{!mounted ? null : mode === "karten" ? ( setKey((k) => k + 1)} /> ) : mode === "quiz" ? ( setKey((k) => k + 1)} /> ) : mode === "luecke" || mode === "plural" || mode === "perfekt" ? ( setKey((k) => k + 1)} /> ) : mode === "artikel" ? ( setKey((k) => k + 1)} /> ) : mode === "memory" ? ( setKey((k) => k + 1)} /> ) : ( )}
); } // ---------------- Karteikarten (Leitner) ---------------- function Flashcards({ words, onRestart }: { words: Word[]; onRestart: () => void }) { const [queue] = useState(() => { const p = getProgress(); const t = todayStr(); const due = words.filter((w) => p.cards[w.id] && p.cards[w.id].due <= t); const fresh = words.filter((w) => !p.cards[w.id]).slice(0, 10); return shuffle([...due, ...fresh]).slice(0, 20); }); const [i, setI] = useState(0); const [flipped, setFlipped] = useState(false); const [stats, setStats] = useState({ known: 0, again: 0 }); const [extra, setExtra] = useState([]); const all = [...queue, ...extra]; const w = all[i]; const answer = useCallback( (known: boolean) => { if (!w) return; reviewCard(w.id, known); if (known) sfx.correct(); else { sfx.flip(); // Nicht gewusst: am Ende dieser Runde noch einmal zeigen if (!extra.includes(w)) setExtra((e) => [...e, w]); } setStats((s) => ({ known: s.known + (known ? 1 : 0), again: s.again + (known ? 0 : 1) })); setFlipped(false); setTimeout(() => setI((x) => x + 1), 150); }, [w, extra], ); useEffect(() => { const onKey = (e: KeyboardEvent) => { if ((e.target as HTMLElement).tagName === "SELECT") return; if (e.key === " ") { e.preventDefault(); setFlipped((f) => !f); sfx.flip(); } else if (flipped && (e.key === "1" || e.key === "ArrowLeft")) answer(false); else if (flipped && (e.key === "2" || e.key === "ArrowRight")) answer(true); }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, [flipped, answer]); useEffect(() => { if (!w && all.length > 0 && stats.known > 0) fireConfetti(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [w]); if (all.length === 0) return (
🎉

Alles wiederholt!

Für dieses Thema ist heute nichts mehr fällig. Probier ein anderes Thema oder ein Spiel!

لا توجد بطاقات مستحقة اليوم. جرّب موضوعاً آخر أو لعبة!
); if (!w) return (
🏁

Runde geschafft!

✅ {stats.known} gewusst • 🔁 {stats.again} nochmal

أحسنت! سأذكّرك بالكلمات في الوقت المناسب.
); return (
Karte {i + 1} / {all.length} ✅ {stats.known} • 🔁 {stats.again}
{ setFlipped((f) => !f); sfx.flip(); }} >
{topics.find((t) => t.id === w.topic)?.emoji} {topics.find((t) => t.id === w.topic)?.de}
{w.art && {w.art} } {w.de}
{w.pl &&
{w.pl}
}
e.stopPropagation()}>
Antippen zum Umdrehen • اضغط لقلب البطاقة
{w.ar}
„{w.ex}“
{w.exAr}
e.stopPropagation()}>
Tastatur: Leertaste = umdrehen • 1 = nochmal • 2 = gewusst
); } // ---------------- Quiz ---------------- function VocabQuiz({ words, topic, onRestart }: { words: Word[]; topic: string; onRestart: () => void }) { const qs = useMemo(() => { const pick = shuffle(words).slice(0, 10); return pick.map((w, n) => { const reverse = n % 2 === 1; const pool = shuffle(allWords.filter((x) => x.id !== w.id && x.topic === w.topic)).slice(0, 3); const filler = pool.length < 3 ? shuffle(allWords.filter((x) => x.id !== w.id)).slice(0, 3 - pool.length) : []; const opts = shuffle([w, ...pool, ...filler]); return { w, reverse, opts }; }); }, [words]); const [i, setI] = useState(0); const [chosen, setChosen] = useState(null); const [correct, setCorrect] = useState(0); const [result, setResult] = useState<{ xp: number } | null>(null); if (result) return ; const q = qs[i]; if (!q) return null; const pick = (id: string) => { if (chosen) return; setChosen(id); const ok = id === q.w.id; if (ok) { sfx.correct(); setCorrect((c) => c + 1); } else sfx.wrong(); setTimeout(() => { if (i + 1 >= qs.length) { const c = correct + (ok ? 1 : 0); const xp = 5 + c * 2; recordResult(`vocab-quiz-${topic}`, "wortschatz", (c / qs.length) * 100, xp); setResult({ xp }); } else { setI(i + 1); setChosen(null); } }, 1000); }; return (
Frage {i + 1} / {qs.length} ✅ {correct}
{q.reverse ? "Was heißt das auf Deutsch?" : "Was bedeutet das? • ما معنى؟"}
{q.reverse ? (
{q.w.ar}
) : (
{wordLabel(q.w)}
)}
{q.opts.map((o, k) => { let cls = "option"; if (chosen && o.id === q.w.id) cls += " correct"; else if (chosen === o.id) cls += " wrong"; return ( ); })}
); } // ---------------- Generierte Drills (Lückentext, Plural, hat/ist) ---------------- const drillInfo = { luecke: { title: "Welches Wort fehlt?", ar: "أي كلمة ناقصة؟", make: gapFillQuestions }, plural: { title: "Plural", ar: "الجمع", make: pluralQuestions }, perfekt: { title: "Perfekt mit hat oder ist?", ar: "hat أم ist؟", make: perfektQuestions }, } as const; function McDrill({ words, mode, onRestart }: { words: Word[]; mode: keyof typeof drillInfo; onRestart: () => void }) { const info = drillInfo[mode]; const qs = useMemo(() => { let list = info.make(words); if (list.length < 5) list = info.make(allWords); // Thema hat zu wenige passende Wörter return shuffle(list).slice(0, 15); }, [info, words]); const [i, setI] = useState(0); const [chosen, setChosen] = useState(null); const [correct, setCorrect] = useState(0); const [result, setResult] = useState<{ xp: number } | null>(null); if (result) return ; const q: DrillQ | undefined = qs[i]; if (!q) return null; const pick = (k: number) => { if (chosen !== null) return; setChosen(k); const ok = k === q.answer; if (ok) { sfx.correct(); setCorrect((c) => c + 1); } else sfx.wrong(); if (q.speak) speak(q.speak, { rate: 0.9 }); }; const next = () => { if (i + 1 >= qs.length) { const xp = 5 + correct * 2; recordResult(`vocab-${mode}`, "wortschatz", (correct / qs.length) * 100, xp); setResult({ xp }); } else { setI(i + 1); setChosen(null); } }; return (
{info.title} • {info.ar} {i + 1} / {qs.length} • ✅ {correct}
{q.prompt}
{q.hintAr}
{q.options.map((o, k) => { let cls = "option"; if (chosen !== null && k === q.answer) cls += " correct"; else if (chosen === k) cls += " wrong"; return ( ); })}
{chosen !== null && ( <>
{chosen === q.answer ? "Richtig! 🎉" : `Richtig: ${q.options[q.answer]}`} {q.explain && – {q.explain}}
)}
); } // ---------------- Artikel-Spiel ---------------- function ArticleGame({ words, onRestart }: { words: Word[]; onRestart: () => void }) { const nouns = useMemo(() => { const n = words.filter((w) => w.art && !w.pl?.includes("Plural")); return shuffle(n.length >= 8 ? n : allWords.filter((w) => w.art && !w.pl?.includes("Plural"))); }, [words]); const [started, setStarted] = useState(false); const [time, setTime] = useState(60); const [i, setI] = useState(0); const [score, setScore] = useState(0); const [combo, setCombo] = useState(0); const [flash, setFlash] = useState<"ok" | "no" | null>(null); const [mistakes, setMistakes] = useState([]); const [saved, setSaved] = useState(false); const over = started && time <= 0; useEffect(() => { if (!started || over) return; const id = setInterval(() => setTime((t) => t - 1), 1000); return () => clearInterval(id); }, [started, over]); useEffect(() => { if (over && !saved) { setSaved(true); if (score > 0) addXp(Math.min(40, Math.ceil(score / 2))); if (score >= 30) fireConfetti(); sfx.win(); } }, [over, saved, score]); const w = nouns[i % nouns.length]; const guess = (a: string) => { if (over) return; if (a === w.art) { const c = combo + 1; setCombo(c); setScore((s) => s + (c >= 5 ? 3 : c >= 3 ? 2 : 1)); setFlash("ok"); sfx.correct(); } else { setCombo(0); setFlash("no"); setMistakes((m) => (m.includes(w) ? m : [...m, w])); sfx.wrong(); } setTimeout(() => setFlash(null), 250); setI((x) => x + 1); }; if (!started) return (
⚡

der, die oder das?

60 Sekunden – so viele Artikel wie möglich! Combo ab 3 richtigen = doppelte Punkte.

60 ثانية – أكبر عدد ممكن من الأدوات! سلسلة من 3 إجابات صحيحة = نقاط مضاعفة.
● der = blau ● die = rot ● das = grün
); if (over) return (
{score >= 30 ? "🏆" : score >= 15 ? "🌟" : "💪"}
{score} Punkte
+{Math.min(40, Math.ceil(score / 2))} XP
{mistakes.length > 0 && (
Diese Wörter wiederholen: {mistakes.slice(0, 10).map((m) => (
{m.art} {" "} {m.de} – {m.ar}
))}
)}
); return (
⏱ {time}s {combo >= 3 && 🔥 Combo x{combo >= 5 ? 3 : 2}} {score} P
{w.de}
{w.ar}
{(["der", "die", "das"] as const).map((a) => ( ))}
); } // ---------------- Memory ---------------- function Memory({ words, onRestart }: { words: Word[]; onRestart: () => void }) { const cards = useMemo(() => { const pick = shuffle(words).slice(0, 6); return shuffle(pick.flatMap((w) => [{ key: w.id + "-de", id: w.id, text: wordLabel(w), ar: false }, { key: w.id + "-ar", id: w.id, text: w.ar, ar: true }])); }, [words]); const [open, setOpen] = useState([]); const [matched, setMatched] = useState([]); const [moves, setMoves] = useState(0); const won = matched.length === cards.length / 2; const [awarded, setAwarded] = useState(false); useEffect(() => { if (won && !awarded) { setAwarded(true); recordResult("vocab-memory", "wortschatz", Math.max(40, 100 - (moves - 6) * 8), 12); fireConfetti(); sfx.win(); } }, [won, awarded, moves]); const click = (k: number) => { if (open.length === 2 || open.includes(k) || matched.includes(cards[k].id)) return; sfx.flip(); const next = [...open, k]; setOpen(next); if (!cards[k].ar) speak(cards[k].text, { rate: 0.9 }); if (next.length === 2) { setMoves((m) => m + 1); const [a, b] = next; if (cards[a].id === cards[b].id) { setTimeout(() => { setMatched((m) => [...m, cards[a].id]); setOpen([]); sfx.correct(); }, 400); } else setTimeout(() => setOpen([]), 900); } }; return (
Finde die Paare: Deutsch ↔ Arabisch • جد الأزواج Züge: {moves}
{cards.map((c, k) => { const shown = open.includes(k) || matched.includes(c.id); return ( ); })}
{won && (
🧠🎉
Geschafft in {moves} Zügen!
أحسنت! ذاكرة ممتازة.
)}
); } // ---------------- Wortliste ---------------- function WordList({ words }: { words: Word[] }) { const [q, setQ] = useState(""); const p = useProgress(); const list = words.filter((w) => !q || w.de.toLowerCase().includes(q.toLowerCase()) || w.ar.includes(q)); return (
setQ(e.target.value)} placeholder="🔎 Suchen … • ابحث" />
{list.map((w) => { const box = p.cards[w.id]?.box ?? 0; return (
{w.art && {w.art} } {w.de} {w.pl && ({w.pl})}
{w.ar}
{w.ex}
{"●".repeat(box)} {"○".repeat(5 - box)}
); })}
); }