"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/molecules/curriculumV2/CurriculumContents/CurriculumReviewSteps/curriculum-review-context.tsx
var curriculum_review_context_exports = {};
__export(curriculum_review_context_exports, {
  CurriculumReviewProvider: () => CurriculumReviewProvider,
  useCurriculumReviewContext: () => useCurriculumReviewContext
});
module.exports = __toCommonJS(curriculum_review_context_exports);
var import_react = require("react");
var import_formik = require("formik");
var import_lodash_es = require("lodash-es");
var Yup = __toESM(require("yup"), 1);
var import_jsx_runtime = require("react/jsx-runtime");
var CurriculumReviewContext = (0, import_react.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 = (0, import_react.useMemo)(() => {
    const result = (0, import_lodash_es.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] = (0, import_react.useState)(0);
  const answersYupSchema = (0, import_react.useMemo)(
    () => createAnswersYupSchema(fixedReviewContents.questions),
    [fixedReviewContents.questions]
  );
  const formik = (0, import_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 = (0, import_react.useCallback)(() => {
    setStep(1);
  }, []);
  const handleNext = (0, import_react.useCallback)(() => {
    if (step < questions.length) {
      setStep(step + 1);
    } else {
      formik.handleSubmit();
      setStep(questions.length + 1);
    }
  }, [step, questions.length, formik]);
  const handlePrev = (0, import_react.useCallback)(() => {
    if (step > 1) {
      setStep(step - 1);
    }
  }, [step]);
  const onClickReviewMyAnswer = (0, import_react.useCallback)(() => {
    setStep(0);
  }, []);
  const handleSubmit = (0, import_react.useCallback)(() => {
    formik.handleSubmit();
  }, [formik]);
  const currentQuestion = (0, import_react.useMemo)(() => {
    return step > 0 && step <= questions.length ? questions[step - 1] : void 0;
  }, [step, questions]);
  const currentAnswer = (0, import_react.useMemo)(() => {
    return currentQuestion ? formik.values.answers[currentQuestion.uuid] : "";
  }, [currentQuestion, formik.values.answers]);
  const currentError = (0, import_react.useMemo)(() => {
    return currentQuestion && formik.errors.answers && formik.errors.answers[currentQuestion.uuid];
  }, [currentQuestion, formik.errors.answers]);
  const handleAnswerChange = (0, import_react.useCallback)(
    (value) => {
      if (currentQuestion) {
        void formik.setFieldValue(`answers.${currentQuestion.uuid}`, value);
      }
    },
    [currentQuestion, formik]
  );
  (0, import_react.useEffect)(() => {
    if (currentQuestion) {
      onReviewValueChange({
        reviewId: currentQuestion.uuid,
        answer: formik.values.answers
      });
    }
  }, [currentQuestion == null ? void 0 : currentQuestion.uuid, currentAnswer, onReviewValueChange]);
  return /* @__PURE__ */ (0, import_jsx_runtime.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 = (0, import_react.useContext)(CurriculumReviewContext);
  if (!context) {
    throw new Error(
      "useCurriculumReviewContext must be used within a CurriculumReviewProvider"
    );
  }
  return context;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  CurriculumReviewProvider,
  useCurriculumReviewContext
});
//# sourceMappingURL=curriculum-review-context.cjs.map