import React, { useEffect } from "react";
import { Choice } from "../../../../../types/Item";
import ValidationField from "../../../../../Components/ValidationField/ValidationField";
import { useFormikContext } from "formik";
import { useTranslation } from "react-i18next";
import { getCharFromNumber } from "../../../../../utils/getCharFromNumber";
import TextField from "./TextField";
import File from "./File";
import { FaTrash } from "react-icons/fa";
import { useObjectToEdit } from "../../../../../zustand/ObjectToEditState";
import { toast } from "react-toastify";

const QuestionFIeld = ({ index, data }: { index: number; data: Choice }) => {
  const formik = useFormikContext<any>();
  console.log(index);
  const { setDeletedQuestions, DeletedQuestions } = useObjectToEdit();

  const [t] = useTranslation();
  useEffect(() => {
    setDeletedQuestions([]);
  }, [window?.location.pathname]);

  const handleDeleteQuestion = () => {
    const DeleteQuestionId = formik.values.Questions?.[index];
    if (DeleteQuestionId?.id) {
      setDeletedQuestions([...DeletedQuestions, DeleteQuestionId]);
    }
    const updatedQuestionOptions = formik.values.Questions.filter(
      (_: any, i: any) => i !== index,
    );
    formik.setFieldValue(`Questions`, updatedQuestionOptions);
  };

  return (
    <div className="ChoiceFields">
      <TextField
        className="textarea_exercise"
        placeholder={"choice"}
        label2={t(`input.question`) + ` ` + `${index + 1}`}
        name={index}
        type="TextArea"
      />
      <File
        className="file_exercise"
        label={"attachment"}
        name={index}
        type="File"
      />
      <p className="delete_question_options">
        <FaTrash onClick={handleDeleteQuestion} size={17} />
      </p>
    </div>
  );
};

export default QuestionFIeld;
