import { isAnEmptyRichText } from "@/utils/utils";
import { numberToUpperCase } from "@/plugins/utils";

export function modifyQuestionCont(qData) {
  const { question, number } = qData;
  question.quesBody = appendNoToRichText(question.quesBody, `${number}、`);
  if (question.quesOptions && question.quesOptions.length) {
    question.quesOptions.forEach((option, optionIndex) => {
      option.optionBody = appendNoToRichText(
        option.optionBody,
        `${numberToUpperCase(optionIndex + 1)}、`
      );
    });
  }
  if (question.subQuestions && question.subQuestions.length) {
    question.subQuestions.forEach((subq, subIndex) => {
      modifyQuestionCont({ question: subq, number: subIndex + 1 });
    });
  }
}

function getRichStruct(blocks) {
  return {
    sections: [
      {
        blocks: [...blocks],
      },
    ],
  };
}

function appendNoToRichText(richText, noStr) {
  const cont = {
    type: "text",
    value: noStr,
  };
  if (isAnEmptyRichText(richText)) {
    return getRichStruct([cont]);
  }

  richText.sections[0].blocks.unshift(cont);
  return richText;
}
