import DatePicker from "react-datepicker";
import { Box, Typography } from "@mui/material";

import { useQuizViewStore, updateAnswer } from "@root/quizView";

import "react-datepicker/dist/react-datepicker.css";

import type { QuizQuestionDate } from "../../../model/questionTypes/date";

type DateProps = {
  currentQuestion: QuizQuestionDate;
};

export const Date = ({ currentQuestion }: DateProps) => {
  const { answers } = useQuizViewStore();
  const { answer } = answers.find(({ questionId }) => questionId === currentQuestion.content.id) ?? {};

  return (
    <Box>
      <Typography variant="h5">{currentQuestion.title}</Typography>
      <Box
        sx={{
          display: "flex",
          flexDirection: "column",
          width: "100%",
          marginTop: "20px",
        }}
      >
        <DatePicker
          selected={answer ? new window.Date(answer) : new window.Date()}
          onChange={(date) => updateAnswer(currentQuestion.content.id, String(date))}
        />
      </Box>
    </Box>
  );
};
