import * as Yup from "yup";
import { Question } from "../../../types/Item";
import { getLocalStorage } from "../../../utils/LocalStorage";
import { QUESTION_OBJECT_KEY } from "../../../config/AppKey";

export const getInitialValues = (objectToEdit: Question): any => {
  const tags = objectToEdit?.tags?.map((item: any, index: number) => {
    return { ...item, key: index };
  });

  return {
    id: objectToEdit?.id ?? null,
    content: objectToEdit?.content ?? "",
    image: objectToEdit?.image ?? "",
    subject_id: objectToEdit?.subject_id ?? "",
    isBase: 0,
    parent_id: objectToEdit?.parent_id ?? "",
    QuestionOptions: objectToEdit?.QuestionOptions ?? [],
    tags: tags ?? [],
  };
};

export const getValidationSchema = () => {
  // validate input
  return Yup.object().shape({
    image: Yup.string().nullable(),
    content: Yup.string().required("validation.required"),
    QuestionOptions: Yup.array()
      .of(
        Yup.object().shape({
          answer: Yup.string().required("validation.required"),
          answer_image: Yup.string().nullable(),
          isCorrect: Yup.boolean(),
        }),
      )
      .nullable("Params are required"),
  });
};

export const getInitialValuesBase = (objectToEdit: Question): any => {
  const tags = objectToEdit?.tags?.map((item: any, index: number) => {
    return { ...item, key: index };
  });
  console.log(objectToEdit);

  const newQuestions = objectToEdit?.Questions?.map((item: any) => {
    const tags = item?.tags?.map((tag: any) => ({
      id: tag?.id,
      name: tag?.name,
      key: `${tag?.id}_key_${tag?.name}`,
    }));

    return {
      ...item,
      tags,
    };
  });

  const questions = newQuestions ?? [];

  return {
    id: objectToEdit?.id ?? null,
    content: objectToEdit?.content ?? "",
    image: objectToEdit?.image ?? "",
    subject_id: objectToEdit?.subject_id ?? "",
    isBase: 1,
    parent_id: objectToEdit?.parent_id ?? "",
    Questions: questions,
  };
};

export const getValidationSchemaBase = () => {
  // validate input
  return Yup.object().shape({
    image: Yup.string().nullable(),
    content: Yup.string().required("validation.required"),
    Questions: Yup.array().of(
      Yup.object().shape({
        image: Yup.string().nullable(),
        content: Yup.string().required("validation.required"),
        QuestionOptions: Yup.array()
          .of(
            Yup.object().shape({
              answer: Yup.string().required("validation.required"),
              answer_image: Yup.string().nullable(),
              isCorrect: Yup.boolean(),
            }),
          )
          .nullable("required"),
      }),
    ),
  });
};

export function processTags(DataToSend: any) {
  const oldTags = DataToSend?.tags
    ?.map((item: any, index: number) => {
      if (typeof item?.id === "number") {
        return item?.id;
      }
    })
    .filter((item: any) => item !== undefined);

  const newTags = DataToSend?.tags
    ?.map((item: any, index: number) => {
      if (typeof item?.id === "string" && item?.name !== "") {
        return { name: item?.name };
      }
    })
    .filter((item: any) => item !== undefined);

  return { new: newTags, old: oldTags };
}
