import { useEffect,useState } from "react";

export default function ChatInput({ onSend, fileUploaded }) {
  const [input, setInput] = useState("");

  const handleSubmit = () => {
   if (!input.trim() || fileUploaded) return;
    onSend(input);
    setInput("");
  };

  useEffect(() => {
  if (!fileUploaded) setInput("");
}, [fileUploaded]);

     // Disable input if no file uploaded
  const isInputDisabled = fileUploaded;
  const isButtonDisabled = fileUploaded || !input.trim();
  return (
    <div className="chat-input">
      <textarea
        placeholder="Paste error, ask DevMate..."
        value={input}
        onChange={(e) => setInput(e.target.value)}
         disabled={isInputDisabled} // disable typing if no file
         onKeyDown={(e) => e.key === "Enter" && !isButtonDisabled && handleSubmit()}
      />
      <button onClick={handleSubmit} disabled={isButtonDisabled}>Send</button>
    </div>
  );
}
