import { useCallback, useEffect, useRef, useState } from "react";
//#region src/components/PisellSteps/hooks/useStepsState.ts
/**
* 管理步骤条状态的 Hook（支持受控和非受控模式）
* 
* @param current - 受控模式下的当前步骤
* @param defaultCurrent - 非受控模式下的默认当前步骤
* @param onChange - 步骤变更回调函数
* @returns [currentStep, setCurrentStep] - 当前步骤和设置步骤的函数
*/
function useStepsState(current, defaultCurrent = 0, onChange) {
	const safeDefaultCurrent = typeof defaultCurrent === "number" && !isNaN(defaultCurrent) && defaultCurrent >= 0 ? defaultCurrent : 0;
	const safeCurrent = current !== void 0 && typeof current === "number" && !isNaN(current) && current >= 0 ? current : void 0;
	const [innerCurrent, setInnerCurrent] = useState(safeDefaultCurrent);
	const previousCurrentRef = useRef(safeDefaultCurrent);
	const isControlled = safeCurrent !== void 0;
	const currentStep = isControlled ? safeCurrent : innerCurrent;
	const setCurrentStep = useCallback((value) => {
		const previousCurrent = previousCurrentRef.current;
		if (!isControlled) setInnerCurrent(value);
		if (onChange && value !== previousCurrent) onChange(value);
		previousCurrentRef.current = value;
	}, [isControlled, onChange]);
	useEffect(() => {
		if (isControlled && safeCurrent !== void 0) previousCurrentRef.current = safeCurrent;
	}, [safeCurrent, isControlled]);
	return [currentStep, setCurrentStep];
}
//#endregion
export { useStepsState };
