import {
  __spreadValues
} from "./chunk-CRTRMMJ7.js";

// src/molecules/curriculumV2/CurriculumContents/CurriculumReviewSteps/curriculum-review-context.tsx
import {
  createContext,
  useContext,
  useMemo,
  useState,
  useCallback,
  useEffect
} from "react";
import { useFormik } from "formik";
import { cloneDeep } from "lodash-es";
import * as Yup from "yup";
import { jsx } from "react/jsx-runtime";
var CurriculumReviewContext = createContext(void 0);
function getInitialAnswers(defaultValues, questions) {
  return questions.reduce((acc, q) => {
    var _a;
    acc[q.uuid] = (_a = defaultValues[q.uuid]) != null ? _a : "";
    return acc;
  }, {});
}
function createAnswersYupSchema(questions) {
  const shape = {};
  questions.forEach((q) => {
    let schema = Yup.string();
    if (q.required) {
      schema = schema.required(`${q.title}\uC740(\uB294) \uD544\uC218 \uC785\uB825 \uD56D\uBAA9\uC785\uB2C8\uB2E4.`).trim();
      if (q.minLength && typeof q.minLength === "number") {
        schema = schema.min(
          q.minLength,
          `${q.title}\uC740(\uB294) \uCD5C\uC18C ${q.minLength}\uC790 \uC774\uC0C1 \uC785\uB825\uD574\uC57C \uD569\uB2C8\uB2E4.`
        );
      }
    } else {
      schema = schema.min(0);
    }
    shape[q.uuid] = schema;
  });
  return Yup.object().shape(shape);
}
function CurriculumReviewProvider({
  reviewContents: _reviewContents,
  sectionTitle,
  children,
  onClickEndReview,
  onSubmit,
  defaultValues,
  curriculum,
  onReviewValueChange,
  done
}) {
  const fixedReviewContents = useMemo(() => {
    const result = cloneDeep(_reviewContents);
    result.questions = result.questions.map((question) => {
      if (question.type === "question_satisfaction_option") {
        return __spreadValues(__spreadValues({}, curriculum.contents.satisfactionOptionMeta), question);
      }
      if (question.type === "question_satisfaction_text") {
        return __spreadValues(__spreadValues({}, curriculum.contents.satisfactionTextMeta), question);
      }
      return question;
    });
    return result;
  }, [
    curriculum.contents.satisfactionOptionMeta,
    curriculum.contents.satisfactionTextMeta,
    _reviewContents
  ]);
  const [step, setStep] = useState(0);
  const answersYupSchema = useMemo(
    () => createAnswersYupSchema(fixedReviewContents.questions),
    [fixedReviewContents.questions]
  );
  const formik = useFormik({
    initialValues: {
      answers: getInitialAnswers(defaultValues != null ? defaultValues : {}, fixedReviewContents.questions)
    },
    validationSchema: Yup.object({
      answers: answersYupSchema
    }),
    onSubmit: (values) => {
      const result = onSubmit(values.answers);
      return result.then((isSuccess) => {
        if (isSuccess) {
          setStep((prev) => prev + 1);
        }
        return isSuccess;
      });
    },
    enableReinitialize: true,
    validateOnMount: true,
    validateOnChange: true
  });
  const questions = fixedReviewContents.questions;
  const handleStart = useCallback(() => {
    setStep(1);
  }, []);
  const handleNext = useCallback(() => {
    if (step < questions.length) {
      setStep(step + 1);
    } else {
      formik.handleSubmit();
      setStep(questions.length + 1);
    }
  }, [step, questions.length, formik]);
  const handlePrev = useCallback(() => {
    if (step > 1) {
      setStep(step - 1);
    }
  }, [step]);
  const onClickReviewMyAnswer = useCallback(() => {
    setStep(0);
  }, []);
  const handleSubmit = useCallback(() => {
    formik.handleSubmit();
  }, [formik]);
  const currentQuestion = useMemo(() => {
    return step > 0 && step <= questions.length ? questions[step - 1] : void 0;
  }, [step, questions]);
  const currentAnswer = useMemo(() => {
    return currentQuestion ? formik.values.answers[currentQuestion.uuid] : "";
  }, [currentQuestion, formik.values.answers]);
  const currentError = useMemo(() => {
    return currentQuestion && formik.errors.answers && formik.errors.answers[currentQuestion.uuid];
  }, [currentQuestion, formik.errors.answers]);
  const handleAnswerChange = useCallback(
    (value) => {
      if (currentQuestion) {
        void formik.setFieldValue(`answers.${currentQuestion.uuid}`, value);
      }
    },
    [currentQuestion, formik]
  );
  useEffect(() => {
    if (currentQuestion) {
      onReviewValueChange({
        reviewId: currentQuestion.uuid,
        answer: formik.values.answers
      });
    }
  }, [currentQuestion == null ? void 0 : currentQuestion.uuid, currentAnswer, onReviewValueChange]);
  return /* @__PURE__ */ jsx(
    CurriculumReviewContext.Provider,
    {
      value: {
        reviewContents: fixedReviewContents,
        formik,
        curriculum,
        sectionTitle,
        onClickEndReview,
        step,
        setStep,
        handleStart,
        handleNext,
        handlePrev,
        onClickReviewMyAnswer,
        handleSubmit,
        currentQuestion,
        currentAnswer,
        currentError,
        handleAnswerChange,
        done
      },
      children
    }
  );
}
function useCurriculumReviewContext() {
  const context = useContext(CurriculumReviewContext);
  if (!context) {
    throw new Error(
      "useCurriculumReviewContext must be used within a CurriculumReviewProvider"
    );
  }
  return context;
}

export {
  CurriculumReviewProvider,
  useCurriculumReviewContext
};
//# sourceMappingURL=chunk-GMKRNFH5.js.map