"use client"; import Link from "next/link"; import { useEffect, useMemo, useRef, useState } from "react"; import { hoerItems, type HoerItem } from "@/data/hoeren"; import { leseItems, type LeseItem } from "@/data/lesen"; import { lessons } from "@/data/grammatik"; import { allWords, wordLabel } from "@/data/vocab"; import type { Question } from "@/data/types"; import Quiz from "@/components/Quiz"; import Ar from "@/components/Ar"; import { recordResult } from "@/lib/progress"; import { hasGermanVoice, speakSequence, stopSpeaking, TTS_MISSING_EVENT } from "@/lib/speech"; import { fireConfetti } from "@/components/AppShell"; import { sfx } from "@/lib/sfx"; function shuffle(a: T[]): T[] { return [...a].sort(() => Math.random() - 0.5); } type Block = | { kind: "lesen"; item: LeseItem } | { kind: "hoeren"; item: HoerItem } | { kind: "sprache"; title: string; questions: Question[] }; function buildExam(): Block[] { const lesen = [1, 2, 3, 4, 5].map((t) => shuffle(leseItems.filter((l) => l.teil === t))[0]).filter(Boolean); const hoeren = [1, 2, 3, 4].map((t) => shuffle(hoerItems.filter((h) => h.teil === t))[0]).filter(Boolean); const grammar: Question[] = shuffle(lessons.flatMap((l) => l.exercises.filter((e) => e.kind === "mc"))) .slice(0, 8) .map((e) => (e.kind === "mc" ? { kind: "mc", q: e.q, qAr: e.ar ?? "", options: e.options, answer: e.answer } : null)) .filter((x): x is Extract => x !== null); const vocab: Question[] = shuffle(allWords) .slice(0, 6) .map((w) => { const opts = shuffle([w, ...shuffle(allWords.filter((x) => x.id !== w.id)).slice(0, 3)]); return { kind: "mc" as const, q: `Was bedeutet „${wordLabel(w)}“?`, qAr: "ما معنى الكلمة؟", options: opts.map((o) => o.ar), answer: opts.indexOf(w) }; }); return [ ...lesen.map((item) => ({ kind: "lesen" as const, item })), ...hoeren.map((item) => ({ kind: "hoeren" as const, item })), { kind: "sprache", title: "Sprachbausteine: Grammatik & Wortschatz", questions: [...grammar, ...vocab] }, ]; } export default function PruefungPage() { const [exam, setExam] = useState(null); const [idx, setIdx] = useState(0); const [scores, setScores] = useState<{ kind: Block["kind"]; c: number; t: number }[]>([]); const [seconds, setSeconds] = useState(0); const [blockDone, setBlockDone] = useState(false); const saved = useRef(false); useEffect(() => { if (!exam || idx >= exam.length) return; const id = setInterval(() => setSeconds((s) => s + 1), 1000); return () => clearInterval(id); }, [exam, idx]); useEffect(() => () => stopSpeaking(), []); const finished = exam !== null && idx >= exam.length; const summary = useMemo(() => { const by = (k: Block["kind"]) => { const list = scores.filter((s) => s.kind === k); const c = list.reduce((a, b) => a + b.c, 0); const t = list.reduce((a, b) => a + b.t, 0); return { c, t, pct: t ? Math.round((c / t) * 100) : 0 }; }; return { lesen: by("lesen"), hoeren: by("hoeren"), sprache: by("sprache") }; }, [scores]); useEffect(() => { if (finished && !saved.current) { saved.current = true; const total = scores.reduce((a, b) => a + b.c, 0); const max = scores.reduce((a, b) => a + b.t, 0); recordResult("modelltest", "pruefung", (total / max) * 100, 30 + total * 2); if (total / max >= 0.6) { fireConfetti(); sfx.win(); } } }, [finished, scores]); if (!exam) return (

🏅 Modelltest

Simuliere die Prüfung: Lesen (5 Teile), Hören (4 Teile) und Sprachbausteine. محاكاة الامتحان: القراءة (5 أجزاء)، الاستماع (4 أجزاء) وتمارين لغوية.

So funktioniert es • كيف يعمل

  • Du siehst die Lösungen erst am Ende jedes Teils – wie in der echten Prüfung.
  • Hörtexte kannst du im Modelltest nur so oft hören wie in der Prüfung.
  • Zum Bestehen brauchst du in jedem Modul 60 %.
  • Schreiben und Sprechen übst du in den eigenen Bereichen (mit Selbstcheck oder KI-Korrektur).
ترى الحلول فقط في نهاية كل جزء – كما في الامتحان الحقيقي. لا يمكنك سماع النصوص إلا بعدد المرات المسموح بها. تحتاج 60% في كل وحدة للنجاح.
✍️
Schreiben üben
3 Aufgaben, 60 Min.
🗣️
Sprechen üben
Planen, Präsentieren, Fragen
); const mm = String(Math.floor(seconds / 60)).padStart(2, "0"); const ss = String(seconds % 60).padStart(2, "0"); if (finished) { const rows = [ { de: "Lesen", ar: "القراءة", ...summary.lesen }, { de: "Hören", ar: "الاستماع", ...summary.hoeren }, { de: "Sprachbausteine", ar: "القواعد والمفردات", ...summary.sprache }, ]; const allPassed = rows.every((r) => r.pct >= 60); return (

{allPassed ? "🏆 Bestanden!" : "💪 Fast geschafft!"}

Zeit: {mm}:{ss} Min. {allPassed ? "مبروك! أنت على الطريق الصحيح." : "استمر في التدريب – أنت تقترب!"}

{rows.map((r) => (

{r.de}

{r.ar} = 60 ? "green" : "red"}`}> {r.c}/{r.t} • {r.pct} % {r.pct >= 60 ? "✅" : "❌"}
= 60 ? "green" : ""}`} style={{ marginTop: 8 }}>
))}
🏠 Start
); } const block = exam[idx]; return (
Teil {idx + 1} / {exam.length} ⏱ {mm}:{ss}
{block.kind === "lesen" && ( <>

📖 Lesen Teil {block.item.teil}: {block.item.title}

{block.item.task}
{block.item.text}
)} {block.kind === "hoeren" && } {block.kind === "sprache" &&

🧩 {block.title}

} { stopSpeaking(); setScores((s) => [...s, { kind: block.kind, c, t }]); setBlockDone(true); }} /> {blockDone && ( )}
); } function ExamAudio({ item }: { item: HoerItem }) { const max = item.teil === 1 || item.teil === 4 ? 2 : 1; const [plays, setPlays] = useState(0); const [playing, setPlaying] = useState(false); const stop = useRef<(() => void) | null>(null); const voices = useMemo(() => { const counters = { f: 0, m: 0 }; const map: Record = {}; Object.entries(item.speakers).forEach(([n, g]) => (map[n] = { gender: g, idx: counters[g]++ })); return map; }, [item]); useEffect(() => () => stop.current?.(), []); return ( <>

🎧 Hören Teil {item.teil}: {item.title}

{item.situation}

Lies zuerst die Fragen! • اقرأ الأسئلة أولاً!
); }