import { Box, Typography } from "@mui/material";

import { Select as SelectComponent } from "../../../pages/Questions/Select";

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

import type { QuizQuestionSelect } from "../../../model/questionTypes/select";

type SelectProps = {
  stepNumber: number;
  question: QuizQuestionSelect;
};

export const Select = ({ stepNumber, question }: SelectProps) => {
  const { answers } = useQuizViewStore();
  const { answer } = answers.find(({ step }) => step === stepNumber) ?? {};

  return (
    <Box>
      <Typography variant="h5">{question.title}</Typography>
      <Box
        sx={{
          display: "flex",
          flexDirection: "column",
          width: "100%",
          marginTop: "20px",
        }}
      >
        <SelectComponent
          activeItemIndex={Number(answer) || 0}
          items={question.content.variants.map(({ answer }) => answer)}
          onChange={(_, value) => {
            updateAnswer(stepNumber, String(value));
          }}
        />
      </Box>
    </Box>
  );
};
