import { Text, UseInput } from "./Ink";
import React, { useState } from "react";

type TextInputProps = {
  onValidation: (text: string) => void;

  useInput: UseInput;
};

export const TextInput = ({ onValidation, useInput }: TextInputProps) => {
  const [text, setText] = useState("");

  useInput((input, key) => {
    if (key.return) {
      onValidation(text);
    } else if (key.delete) {
      setText(text.slice(0, text.length - 1));
    } else {
      setText(text + input);
    }
  });

  return <Text>{text}</Text>;
};
