import {
  CurriculumReviewSteps_default
} from "./chunk-3KH45IO4.js";
import {
  useCurriculumContext
} from "./chunk-IYQJIEVM.js";
import {
  Dialog
} from "./chunk-JQ7VQ5SB.js";
import {
  IconButton
} from "./chunk-XK6VEVOK.js";
import {
  Flex
} from "./chunk-25HMMI7R.js";
import {
  Button
} from "./chunk-ZVDAEY5Q.js";
import {
  Box
} from "./chunk-4Y5BEXVN.js";
import {
  Typo
} from "./chunk-PMJIFLDT.js";
import {
  __spreadValues
} from "./chunk-CRTRMMJ7.js";

// src/molecules/curriculumV2/CurriculumContents/curriculum-review.tsx
import { useCallback, useMemo, useState } from "react";
import { Cross1Icon } from "@radix-ui/react-icons";
import { jsx, jsxs } from "react/jsx-runtime";
function CurriculumReview(props) {
  const { item, sectionTitle } = props;
  const { description, buttonTitle } = item;
  const {
    curriculum,
    onReviewSubmit: _onReviewSubmit,
    onReviewValueChange: _onReviewValueChange,
    curriculumReviewAnswers,
    tempCurriculumReviewAnswers,
    curriculumCompleteMap
  } = useCurriculumContext();
  const [open, setOpen] = useState(false);
  const [isChanged, setIsChanged] = useState(false);
  const [closeDialogOpen, setCloseDialogOpen] = useState(false);
  const onReviewValueChange = useCallback(
    ({
      reviewId,
      answer
    }) => {
      if (curriculumReviewAnswers[reviewId] === answer[reviewId]) {
        return;
      }
      setIsChanged(true);
      _onReviewValueChange({ reviewId, answer });
    },
    [_onReviewValueChange, curriculumReviewAnswers]
  );
  const onReviewSubmit = useCallback(
    (values) => {
      return _onReviewSubmit({ reviewId: item.uuid, answer: values }).then(
        (result) => {
          setIsChanged(false);
          return result;
        }
      );
    },
    [item.uuid, _onReviewSubmit]
  );
  const onClickEndReview = useCallback(() => {
    setOpen(false);
  }, []);
  const onClickCloseDrawer = useCallback(() => {
    if (isChanged) {
      setCloseDialogOpen(true);
    } else {
      setOpen(false);
    }
  }, [isChanged]);
  const mode = useMemo(() => {
    return item.questions.every(
      (question) => curriculumReviewAnswers[question.uuid] && curriculumReviewAnswers[question.uuid] !== ""
    ) ? "edit" : "create";
  }, [curriculumReviewAnswers, item.questions]);
  const defaultValues = useMemo(() => {
    const allAnswers = __spreadValues(__spreadValues({}, tempCurriculumReviewAnswers), curriculumReviewAnswers);
    return item.questions.reduce((acc, question) => {
      acc[question.uuid] = allAnswers[question.uuid] || "";
      return acc;
    }, {});
  }, [curriculumReviewAnswers, item.questions, tempCurriculumReviewAnswers]);
  const done = useMemo(() => {
    return Boolean(curriculumCompleteMap[item.uuid]);
  }, [curriculumCompleteMap, item.uuid]);
  const bgColor = done ? "var(--color-panel-solid)" : void 0;
  return /* @__PURE__ */ jsxs(Dialog.Root, { onOpenChange: setOpen, open, children: [
    /* @__PURE__ */ jsxs(
      Flex,
      {
        align: "center",
        direction: "column",
        height: "100%",
        justify: "center",
        px: "3",
        py: "3",
        style: {
          backgroundImage: `url(https://tipp-coaching-live.s3.ap-northeast-2.amazonaws.com/images/curriculum_review_bg.png)`,
          backgroundPosition: "center",
          backgroundSize: "cover",
          backgroundRepeat: "no-repeat"
        },
        width: "100%",
        children: [
          /* @__PURE__ */ jsx(Typo, { color: "gray", weight: "bold", children: curriculum.title }),
          /* @__PURE__ */ jsx(Typo, { align: "center", as: "p", color: "gray", children: description }),
          /* @__PURE__ */ jsx(Dialog.Trigger, { children: /* @__PURE__ */ jsx(
            Button,
            {
              mt: "3",
              style: { minWidth: 232, backgroundColor: bgColor },
              variant: done ? "outline" : "solid",
              children: /* @__PURE__ */ jsx(Typo, { truncate: true, children: done ? "\uB098\uC758 \uC751\uB2F5 \uB2E4\uC2DC\uBCF4\uAE30" : buttonTitle })
            }
          ) })
        ]
      }
    ),
    /* @__PURE__ */ jsx(
      Dialog.Content,
      {
        height: "100%",
        hideCloseButton: true,
        style: { borderRadius: 0, padding: 0 },
        title: "",
        width: "100%",
        children: /* @__PURE__ */ jsxs(
          Flex,
          {
            align: "center",
            height: "100%",
            justify: "center",
            pb: "3",
            position: "relative",
            width: "100%",
            children: [
              /* @__PURE__ */ jsx(
                CurriculumReviewSteps_default,
                {
                  contents: item,
                  curriculum,
                  defaultValues,
                  done,
                  mode,
                  onClickEndReview,
                  onReviewValueChange,
                  onSubmit: onReviewSubmit,
                  title: sectionTitle
                }
              ),
              /* @__PURE__ */ jsx(Box, { position: "absolute", right: "16px", top: "16px", children: /* @__PURE__ */ jsxs(
                IconButton,
                {
                  color: "gray",
                  onClick: onClickCloseDrawer,
                  variant: "ghost",
                  children: [
                    /* @__PURE__ */ jsx(Box, { display: { initial: "none", md: "block" }, children: /* @__PURE__ */ jsx(Cross1Icon, { height: 20, width: 20 }) }),
                    /* @__PURE__ */ jsx(Box, { display: { initial: "block", md: "none" }, children: /* @__PURE__ */ jsx(Cross1Icon, { height: 16, width: 16 }) })
                  ]
                }
              ) }),
              /* @__PURE__ */ jsx(Dialog.Root, { onOpenChange: setCloseDialogOpen, open: closeDialogOpen, children: /* @__PURE__ */ jsx(
                Dialog.Content,
                {
                  buttons: /* @__PURE__ */ jsxs(Flex, { gap: "3", justify: "end", children: [
                    /* @__PURE__ */ jsx(Dialog.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { color: "gray", variant: "outline", children: "\uB3CC\uC544\uAC00\uAE30" }) }),
                    /* @__PURE__ */ jsx(Dialog.Close, { children: /* @__PURE__ */ jsx(Button, { color: "red", onClick: onClickEndReview, children: "\uB098\uAC00\uAE30" }) })
                  ] }),
                  title: "\uC791\uC131\uC744 \uC911\uB2E8\uD558\uC2DC\uACA0\uC2B5\uB2C8\uAE4C?",
                  children: /* @__PURE__ */ jsx(Typo, { children: "\uD398\uC774\uC9C0\uC5D0\uC11C \uBC97\uC5B4\uB098\uBA74 \uC791\uC131\uD55C \uB0B4\uC6A9\uC774 \uC800\uC7A5\uB418\uC9C0 \uC54A\uC2B5\uB2C8\uB2E4." })
                }
              ) })
            ]
          }
        )
      }
    )
  ] });
}
var curriculum_review_default = CurriculumReview;

export {
  curriculum_review_default
};
//# sourceMappingURL=chunk-PHGGXL3R.js.map