"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/molecules/curriculumV2/curriculum-context.tsx
var curriculum_context_exports = {};
__export(curriculum_context_exports, {
  CurriculumProvider: () => CurriculumProvider,
  useCurriculumContext: () => useCurriculumContext
});
module.exports = __toCommonJS(curriculum_context_exports);
var import_react = require("react");
var import_jsx_runtime = require("react/jsx-runtime");
var CurriculumContext = (0, import_react.createContext)(
  void 0
);
function CurriculumProvider(props) {
  const {
    children,
    curriculum,
    onReviewSubmit,
    onReviewValueChange,
    curriculumReviewAnswers,
    tempCurriculumReviewAnswers,
    curriculumCompleteMap,
    onChangeSelectedContent,
    onActionButtonClick,
    initialSelectedIndex
  } = props;
  const sidebarRef = (0, import_react.useRef)(null);
  const scrollNavItem = (0, import_react.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 = (0, import_react.useMemo)(
    () => list.map((item, i) => item.type !== "sectionTitle" ? i : null).filter((i) => i !== null),
    [list]
  );
  const [selectedIndexState, setSelectedIndexState] = (0, import_react.useState)(
    initialSelectedIndex != null ? initialSelectedIndex : selectableIndexes[0]
  );
  const setSelectedIndex = (0, import_react.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 = (0, import_react.useMemo)(
    () => selectableIndexes.indexOf(selectedIndexState),
    [selectableIndexes, selectedIndexState]
  );
  const hasPrev = selectablePos > 0;
  const hasNext = selectablePos < selectableIndexes.length - 1;
  const selectPrev = (0, import_react.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 = (0, import_react.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 = (0, import_react.useMemo)(
    () => {
      var _a, _b;
      return (_b = (_a = list[selectedIndexState]) == null ? void 0 : _a.uuid) != null ? _b : "";
    },
    [list, selectedIndexState]
  );
  const selectedItem = (0, import_react.useMemo)(() => {
    return list[selectedIndexState];
  }, [list, selectedIndexState]);
  const selectedSectionTitle = (0, import_react.useMemo)(() => {
    var _a;
    const prevItems = list.slice(0, selectedIndexState).reverse();
    return (_a = prevItems.find((item) => item.type === "sectionTitle")) != null ? _a : null;
  }, [list, selectedIndexState]);
  (0, import_react.useEffect)(() => {
    onChangeSelectedContent == null ? void 0 : onChangeSelectedContent(selectedItem);
  }, [selectedItem]);
  return /* @__PURE__ */ (0, import_jsx_runtime.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 = (0, import_react.useContext)(CurriculumContext);
  if (!ctx)
    throw new Error(
      "useCurriculumContext must be used within CurriculumProvider"
    );
  return ctx;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  CurriculumProvider,
  useCurriculumContext
});
//# sourceMappingURL=curriculum-context.cjs.map