import {
  Box,
  FormControl,
  FormErrorMessage,
  FormHelperText,
  FormLabel,
  Textarea,
} from "@chakra-ui/react";
import { Trans } from "@lingui/macro";
import { FieldError } from "react-hook-form";
import LimitProgress from "./LimitProgress";

interface Props {
  error?: FieldError;
  fieldName: string;
  title: string;
  value: string;
  maxLength: number;
  register: any;
  placeholder?: string;
}

const MarkdownTextarea: React.FC<Props> = ({
  error,
  fieldName,
  title,
  value,
  maxLength,
  register,
  placeholder,
}) => {
  return (
    <FormControl isInvalid={!!error}>
      <FormLabel htmlFor={fieldName} mt={4}>
        {title}
      </FormLabel>
      <Textarea
        ref={register}
        name={fieldName}
        placeholder={placeholder}
        resize="vertical"
        rows={6}
      />
      <FormHelperText display="flex" alignItems="center">
        <LimitProgress
          currentLength={value.length}
          maxLength={maxLength}
          mr={3}
        />
        <Box>
          <Trans>
            Markdown is supported -{" "}
            <a href="/markdown" target="_blank" rel="noreferrer noopener">
              https://sendou.ink/markdown
            </a>
          </Trans>
        </Box>
      </FormHelperText>
      <FormErrorMessage>{error?.message}</FormErrorMessage>
    </FormControl>
  );
};

export default MarkdownTextarea;
