function getResponse(model) {
  const referenceText = model?.Reference;

  if (referenceText) {
    const evalution = Object.values(model["Word Phonetic Evaluation"]);

    const valueMapped = referenceText?.split(/\s+/)?.map((word, idx) => {
      const evalutionValue = evalution[idx];
      const evalationScore = evalutionValue["Word Score"];
      if (word?.toLowerCase() === evalutionValue["Word"]?.toLowerCase()) {
        return { text: word, highlight: evalationScore <= 45 };
      }
      return { text: word };
    });
    let paragraph = "<p>";
    valueMapped?.forEach((item) => {
      if (item?.highlight) {
        paragraph += `<span>${item?.text}</span> `;
      } else {
        paragraph += `${item?.text} `;
      }
    });
    paragraph = paragraph?.trim() + "</p>";
    return paragraph;
  }
  return "";
}

const setupAnalysis = (data, otherValues = {}) => {
  if (data) {
    const options = [];
    if (data?.relevance) {
      options.push({ value: "relevance", label: "Relevance" });
    }
    if (data?.speech_analysis) {
      options.push({ value: "speech", label: "Speech analysis" });
    }
    if (data?.grammar) {
      options.push({ value: "grammar", label: "Grammar" });
    }

    return { options, data: { ...data, ...otherValues } };
  }
  return null;
};

const getDialogueByModal = (ai_model_data) => {
  if (!ai_model_data) return [];
  const speech_analysis_list = ai_model_data?.data?.speech_analysis;
  const dialogues = speech_analysis_list?.map((item) => {
    const data = Object.values(item?.attempt_data)?.[0];
    return {
      data,
      audioText: item?.model_data ? getResponse(item?.model_data) : undefined,
      setupAnalysis: item?.model_data
        ? setupAnalysis(
            { speech_analysis: [{ model_data: item?.model_data }] },
            { audio: data?.recording }
          )
        : undefined,
    };
  });
  return dialogues;
};

const getQuizByModal = (ai_model_data) => {
  if (!ai_model_data) return [];

  const speech_analysis_list = ai_model_data?.data?.speech_analysis;

  const grammerSetup = (audio_id) => {
    if (!audio_id) return undefined;
    const grammer_list = ai_model_data?.data?.grammar;

    if (!grammer_list || grammer_list?.length === 0) return undefined;
    const findGrammer = grammer_list?.find(
      (item) => item?.attempt_data?.recording?.id === audio_id
    );

    return [{ model_data: findGrammer?.model_data }];
  };

  const quizist = speech_analysis_list?.map((item) => {
    const data = item?.attempt_data;
    return {
      data,
      audioText: item?.model_data ? getResponse(item?.model_data) : undefined,
      setupAnalysis: item?.model_data
        ? setupAnalysis(
            {
              speech_analysis: [{ model_data: item?.model_data }],
              grammar: grammerSetup(data?.recording?.id),
            },
            { audio: data?.recording || data, student_text: data?.text }
          )
        : undefined,
    };
  });

  return quizist;
};

const useAiUtils = () => {
  return { setupAnalysis, getResponse, getDialogueByModal, getQuizByModal };
};

export default useAiUtils;
