import { QuizQuestionsWithVariants } from "@frontend/squzanswerer";
import { addQuestionOwnVariant, addQuestionVariant, updateQuestion } from "@root/questions/actions";

export const useAddAnswer = () => {
  const onClickAddAnAnswer = (question: QuizQuestionsWithVariants) => {
    addQuestionVariant(question.id);
  };
  interface SwitchOwnProps {
    question: QuizQuestionsWithVariants;
    checked: boolean
  }
  const switchOwn = ({ question, checked }: SwitchOwnProps) => {
    if (!question.content.variants.some(v => v.isOwn) && checked) {
      addQuestionOwnVariant(question.id)
    }

    updateQuestion<QuizQuestionVariant>(question.id, (question) => {
      question.content.own = checked;
    });
  }

  return {
    onClickAddAnAnswer,
    switchOwn
  };
};
