require("../../../_virtual/_rolldown/runtime.js");
let react = require("react");
//#region src/components/PisellProcedure/hooks/useStepManager.ts
/**
* 步骤管理 Hook
* 
* @description 管理多步骤流程的状态和操作
* @example
* const { currentStep, goPrev, goNext } = useStepManager({
*   stepsProps: { items: [...] },
*   defaultCurrent: 0,
*   onPrev: (current) => { ... },
*   onNext: (current) => { ... },
*   onStepsChange: (result) => { ... },
* });
*/
const useStepManager = ({ stepsProps, defaultCurrent = 0, onPrev, onNext, onStepsChange }) => {
	const items = Array.isArray(stepsProps === null || stepsProps === void 0 ? void 0 : stepsProps.items) ? stepsProps.items : [];
	const totalSteps = items.length > 0 ? items.length : 1;
	const [currentStep, setCurrentStep] = (0, react.useState)((0, 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]));
	(0, react.useEffect)(() => {
		if (currentStep >= totalSteps) setCurrentStep(Math.max(0, totalSteps - 1));
	}, [totalSteps, currentStep]);
	/**
	* 更新步骤
	*/
	const updateStep = (0, 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 || onStepsChange === void 0 || onStepsChange(result);
		return result;
	}, [
		items,
		totalSteps,
		onStepsChange
	]);
	return {
		currentStep,
		items,
		totalSteps,
		goPrev: (0, react.useCallback)(() => {
			if (currentStep <= 0 || totalSteps === 0) return null;
			if ((onPrev === null || onPrev === void 0 ? void 0 : onPrev(currentStep)) === false) return null;
			return updateStep(Math.max(0, currentStep - 1));
		}, [
			currentStep,
			totalSteps,
			onPrev,
			updateStep
		]),
		goNext: (0, react.useCallback)(() => {
			if (currentStep >= totalSteps - 1 || totalSteps === 0) return null;
			if ((onNext === null || onNext === void 0 ? void 0 : onNext(currentStep)) === false) return null;
			return updateStep(Math.min(totalSteps - 1, currentStep + 1));
		}, [
			currentStep,
			totalSteps,
			onNext,
			updateStep
		]),
		goToStep: (0, 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
		])
	};
};
//#endregion
exports.useStepManager = useStepManager;
