import { KeyboardEvent, useRef, useEffect } from "react"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { useTranslation } from "@/hooks/useTranslation"; interface SentenceWord { word: string; provider: 'player' | 'ai'; } interface InputFormProps { playerInput: string; onInputChange: (value: string) => void; onSubmitWord: (e: React.FormEvent) => void; onMakeGuess: () => void; isAiThinking: boolean; hasMultipleWords: boolean; containsTargetWord: boolean; isTooLong: boolean; isValidInput: boolean; sentence: SentenceWord[]; } export const InputForm = ({ playerInput, onInputChange, onSubmitWord, onMakeGuess, isAiThinking, hasMultipleWords, containsTargetWord, isTooLong, isValidInput, sentence }: InputFormProps) => { const inputRef = useRef(null); const t = useTranslation(); useEffect(() => { if (!isAiThinking) { setTimeout(() => { inputRef.current?.focus(); }, 100); } }, [isAiThinking]); const handleKeyDown = (e: KeyboardEvent) => { if (e.shiftKey && e.key === 'Enter') { e.preventDefault(); if (!hasMultipleWords && !containsTargetWord && !isTooLong && !isAiThinking && isValidInput) { onMakeGuess(); } } }; const getInputError = () => { if (hasMultipleWords) return t.game.singleWordOnly; if (containsTargetWord) return t.game.cantUseTargetWord; if (isTooLong) return t.game.shorterWord; if (!isValidInput && playerInput) return t.game.lettersOnly; return null; }; const error = getInputError(); const canMakeGuess = (sentence.length > 0 || playerInput.trim().length > 0) && !hasMultipleWords && !containsTargetWord && !isTooLong && isValidInput && !isAiThinking; return (
onInputChange(e.target.value)} onKeyDown={handleKeyDown} placeholder={t.game.inputPlaceholder} className={`w-full ${error ? 'border-red-500 focus-visible:ring-red-500' : ''}`} disabled={isAiThinking} autoComplete="words" /> {error && (

{error}

)}
); };