// src/molecules/curriculumV2/curriculum-context.tsx
import {
  createContext,
  useContext,
  useState,
  useMemo,
  useCallback,
  useRef,
  useEffect
} from "react";
import { jsx } from "react/jsx-runtime";
var CurriculumContext = createContext(
  void 0
);
function CurriculumProvider(props) {
  const {
    children,
    curriculum,
    onReviewSubmit,
    onReviewValueChange,
    curriculumReviewAnswers,
    tempCurriculumReviewAnswers,
    curriculumCompleteMap,
    onChangeSelectedContent,
    onActionButtonClick,
    initialSelectedIndex
  } = props;
  const sidebarRef = useRef(null);
  const scrollNavItem = useCallback((index) => {
    var _a;
    const element = (_a = sidebarRef.current) == null ? void 0 : _a.querySelector(
      `[data-curriculum-item-index="${index}"]`
    );
    if (element) {
      element.scrollIntoView({ behavior: "smooth", block: "start" });
    }
  }, []);
  const list = curriculum.contents.list;
  const selectableIndexes = useMemo(
    () => list.map((item, i) => item.type !== "sectionTitle" ? i : null).filter((i) => i !== null),
    [list]
  );
  const [selectedIndexState, setSelectedIndexState] = useState(
    initialSelectedIndex != null ? initialSelectedIndex : selectableIndexes[0]
  );
  const setSelectedIndex = useCallback(
    (idx) => {
      if (selectableIndexes.includes(idx)) {
        setSelectedIndexState(idx);
      } else {
        const next = selectableIndexes.find((i) => i > idx);
        const prev = [...selectableIndexes].reverse().find((i) => i < idx);
        if (next !== void 0) setSelectedIndexState(next);
        else if (prev !== void 0) setSelectedIndexState(prev);
      }
    },
    [selectableIndexes]
  );
  const selectablePos = useMemo(
    () => selectableIndexes.indexOf(selectedIndexState),
    [selectableIndexes, selectedIndexState]
  );
  const hasPrev = selectablePos > 0;
  const hasNext = selectablePos < selectableIndexes.length - 1;
  const selectPrev = useCallback(
    (cb) => {
      setSelectedIndexState((cur) => {
        let prevIndex = cur;
        const pos = selectableIndexes.indexOf(cur);
        if (pos > 0) {
          prevIndex = selectableIndexes[pos - 1];
        }
        if (cb) cb(prevIndex);
        return prevIndex;
      });
    },
    [selectableIndexes]
  );
  const selectNext = useCallback(
    (cb) => {
      setSelectedIndexState((cur) => {
        let nextIndex = cur;
        const pos = selectableIndexes.indexOf(cur);
        if (pos < selectableIndexes.length - 1) {
          nextIndex = selectableIndexes[pos + 1];
        }
        if (cb) cb(nextIndex);
        return nextIndex;
      });
    },
    [selectableIndexes]
  );
  const selectedId = useMemo(
    () => {
      var _a, _b;
      return (_b = (_a = list[selectedIndexState]) == null ? void 0 : _a.uuid) != null ? _b : "";
    },
    [list, selectedIndexState]
  );
  const selectedItem = useMemo(() => {
    return list[selectedIndexState];
  }, [list, selectedIndexState]);
  const selectedSectionTitle = useMemo(() => {
    var _a;
    const prevItems = list.slice(0, selectedIndexState).reverse();
    return (_a = prevItems.find((item) => item.type === "sectionTitle")) != null ? _a : null;
  }, [list, selectedIndexState]);
  useEffect(() => {
    onChangeSelectedContent == null ? void 0 : onChangeSelectedContent(selectedItem);
  }, [selectedItem]);
  return /* @__PURE__ */ jsx(CurriculumContext.Provider, { value: {
    curriculum,
    scrollNavItem,
    selectedIndex: selectedIndexState,
    setSelectedIndex,
    selectedId,
    hasPrev,
    hasNext,
    selectPrev,
    selectNext,
    selectedSectionTitle,
    selectedItem,
    scrollAreaRef: sidebarRef,
    onReviewSubmit,
    onReviewValueChange,
    curriculumReviewAnswers,
    tempCurriculumReviewAnswers,
    curriculumCompleteMap,
    onActionButtonClick
  }, children });
}
function useCurriculumContext() {
  const ctx = useContext(CurriculumContext);
  if (!ctx)
    throw new Error(
      "useCurriculumContext must be used within CurriculumProvider"
    );
  return ctx;
}

export {
  CurriculumProvider,
  useCurriculumContext
};
//# sourceMappingURL=chunk-IYQJIEVM.js.map