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/components/PisellProcedure/hooks/useStepManager.ts
var useStepManager_exports = {};
__export(useStepManager_exports, {
  useStepManager: () => useStepManager
});
module.exports = __toCommonJS(useStepManager_exports);
var import_react = require("react");
var useStepManager = ({
  stepsProps,
  defaultCurrent = 0,
  onPrev,
  onNext,
  onStepsChange
}) => {
  const items = Array.isArray(stepsProps == null ? void 0 : stepsProps.items) ? stepsProps.items : [];
  const totalSteps = items.length > 0 ? items.length : 1;
  const safeDefaultCurrent = (0, import_react.useMemo)(() => {
    const num = Number(defaultCurrent);
    if (isNaN(num) || num < 0) return 0;
    if (num >= totalSteps) return Math.max(0, totalSteps - 1);
    return num;
  }, [defaultCurrent, totalSteps]);
  const [currentStep, setCurrentStep] = (0, import_react.useState)(safeDefaultCurrent);
  (0, import_react.useEffect)(() => {
    if (currentStep >= totalSteps) {
      setCurrentStep(Math.max(0, totalSteps - 1));
    }
  }, [totalSteps, currentStep]);
  const updateStep = (0, import_react.useCallback)((step) => {
    const safeStep = Math.max(0, Math.min(totalSteps - 1, step));
    setCurrentStep(safeStep);
    const result = {
      success: true,
      currentStep: safeStep,
      currentItem: items[safeStep] || null
    };
    onStepsChange == null ? void 0 : onStepsChange(result);
    return result;
  }, [items, totalSteps, onStepsChange]);
  const goPrev = (0, import_react.useCallback)(() => {
    if (currentStep <= 0 || totalSteps === 0) return null;
    const preventUpdate = onPrev == null ? void 0 : onPrev(currentStep);
    if (preventUpdate === false) return null;
    const newStep = Math.max(0, currentStep - 1);
    return updateStep(newStep);
  }, [currentStep, totalSteps, onPrev, updateStep]);
  const goNext = (0, import_react.useCallback)(() => {
    if (currentStep >= totalSteps - 1 || totalSteps === 0) return null;
    const preventUpdate = onNext == null ? void 0 : onNext(currentStep);
    if (preventUpdate === false) return null;
    const newStep = Math.min(totalSteps - 1, currentStep + 1);
    return updateStep(newStep);
  }, [currentStep, totalSteps, onNext, updateStep]);
  const goToStep = (0, import_react.useCallback)((step) => {
    if (totalSteps === 0) return null;
    if (step < 0 || step >= totalSteps) return null;
    if (step === currentStep) return null;
    return updateStep(step);
  }, [currentStep, totalSteps, updateStep]);
  return {
    currentStep,
    items,
    totalSteps,
    goPrev,
    goNext,
    goToStep
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useStepManager
});
