import { useEffect, useRef } from "react";
//#region src/components/PisellSteps/hooks/useAnchor.ts
/**
* 锚链接功能 Hook
* 当开启锚链接时，自动滚动到当前步骤对应的锚点
* 
* @param currentStep - 当前步骤索引
* @param enableAnchor - 是否启用锚链接
* @param anchorContainer - 锚链接容器选择器或元素
*/
function useAnchor(currentStep, enableAnchor, anchorContainer) {
	const previousStepRef = useRef(currentStep);
	useEffect(() => {
		if (typeof window === "undefined" || typeof document === "undefined") return;
		if (!enableAnchor) return;
		if (previousStepRef.current === currentStep) return;
		previousStepRef.current = currentStep;
		const anchorId = `pisell-steps-anchor-${currentStep}`;
		const anchorElement = document.getElementById(anchorId);
		if (!anchorElement) return;
		if (anchorContainer) {
			if (typeof anchorContainer === "string") {
				if (document.querySelector(anchorContainer) instanceof HTMLElement) {}
			} else if (anchorContainer instanceof HTMLElement) {}
		}
		anchorElement.scrollIntoView({
			behavior: "smooth",
			block: "start",
			inline: "nearest"
		});
	}, [
		currentStep,
		enableAnchor,
		anchorContainer
	]);
}
//#endregion
export { useAnchor };
