"use client"; import { useState, useMemo } from "react"; interface MatchingPair { left: string; right: string; } interface MatchingPlayerProps { id: string; title?: string; pairs: MatchingPair[]; allowRetry?: boolean; } export default function MatchingPlayer({ id, title, pairs, allowRetry = true }: MatchingPlayerProps) { const [selectedLeft, setSelectedLeft] = useState(null); const [matches, setMatches] = useState>({}); const [submitted, setSubmitted] = useState(false); const shuffledRight = useMemo(() => { return (pairs || []) .map((p, i) => ({ value: p.right, originalIdx: i })) .sort(() => Math.random() - 0.5); }, [pairs]); const handleMatch = (leftIdx: number, rightIdx: number) => { if (submitted) return; setMatches(prev => ({ ...prev, [leftIdx]: rightIdx })); setSelectedLeft(null); }; const handleReset = () => { setSubmitted(false); setMatches({}); setSelectedLeft(null); }; return (

{title || "Emparejamiento de Conceptos"}

{(pairs || []).map((pair, i) => ( ))}
{shuffledRight.map((item, i) => { const matchedLeftIdx = Object.keys(matches).find(k => matches[parseInt(k)] === item.originalIdx); const isCorrect = submitted && matchedLeftIdx !== undefined && parseInt(matchedLeftIdx) === item.originalIdx; const isWrong = submitted && matchedLeftIdx !== undefined && parseInt(matchedLeftIdx) !== item.originalIdx; return ( ); })}
{allowRetry && (
{!submitted && Object.keys(matches).length === (pairs || []).length && ( )} {submitted && ( )}
)}
); }