'use client';


import { useEffect, useState } from "react";

//#region src/components/Navbar/useNavigation.ts
/**
* Navigation Actions Hook
*
* A comprehensive hook for managing navigation interactions and scroll-based section detection.
* Provides automatic active section detection based on scroll position and handles smooth
* scrolling navigation behaviors.
*
* Features:
* - Automatic active section detection based on scroll position
* - Smooth scrolling to sections within the same page
* - Logo click handling with home navigation
* - Section click handling with conditional scrolling vs navigation
* - Passive scroll event listeners for optimal performance
* - Viewport-aware active section calculation (using screen height / 4 offset)
*
* Active Section Detection:
* - Monitors all `<section>` elements on the page
* - Calculates which section is currently in the "active" zone
* - Active zone is defined as the top 25% of the viewport
* - Updates activeSection state as user scrolls
*
* Navigation Behaviors:
* - Logo click: Scrolls to top if on home page, navigates to home if on other pages
* - Section click: Smooth scrolls if on same page, executes callback if different page
* - All scrolling uses smooth behavior for better UX
*
* @example
* Basic usage in navigation component:
* ```tsx
* const { activeSection, onClickLogo, onClickSection } = useNavActions();
*
* // In navigation items
* const navItems = sections.map(section => (
*   <TabSelectorItem
*     key={section.id}
*     isActive={activeSection === section.id}
*     onClick={() => onClickSection(section.id, section.url, section.onClick)}
*   >
*     {section.label}
*   </TabSelectorItem>
* ));
*
* // In logo
* <Logo onClick={() => onClickLogo(navigate)} />
* ```
*
* @example
* Advanced usage with routing:
* ```tsx
* const { activeSection, onClickLogo, onClickSection } = useNavActions();
* const navigate = useNavigate();
*
* const handleLogoClick = () => {
*   onClickLogo((url) => {
*     navigate(url);
*     // Additional logic like analytics
*     trackEvent('logo_click');
*   });
* };
*
* const handleSectionClick = (sectionId: string) => {
*   onClickSection(
*     sectionId,
*     `/page#${sectionId}`,
*     () => {
*       navigate(`/page#${sectionId}`);
*       trackEvent('section_navigation', { sectionId });
*     }
*   );
* };
* ```
*
* Performance Considerations:
* - Uses passive scroll event listeners to prevent blocking
* - Automatically cleans up event listeners on unmount
* - Efficiently queries DOM elements using native selectors
* - Calculates section positions dynamically for accuracy
*
* @returns Object containing navigation state and action handlers
*/
const useNavActions = () => {
	/** Currently active section ID based on scroll position */
	const [activeSection, setActiveSection] = useState(null);
	/**
	* Detects which section is currently active based on scroll position
	* Uses viewport-aware calculation to determine active section
	*/
	const detectActiveSection = () => {
		const scrollY = window.scrollY;
		const sections = document.querySelectorAll("section");
		const sectionsData = [];
		sections.forEach((section) => sectionsData.push({
			id: section.id,
			offsetTop: section.offsetTop,
			offsetHeight: section.offsetHeight
		}));
		const currentSection = sectionsData.find((section) => section.offsetTop <= scrollY + window.screen.height / 4 && section.offsetTop + section.offsetHeight > scrollY + window.screen.height / 4);
		if (currentSection) setActiveSection(currentSection.id);
	};
	useEffect(() => {
		window.addEventListener("scroll", detectActiveSection, { passive: true });
		return () => {
			window.removeEventListener("scroll", detectActiveSection);
		};
	}, []);
	/**
	* Handles logo click behavior
	* Scrolls to top if on home page, navigates to home if on other pages
	*
	* @param onClick - Callback function to handle navigation (e.g., router.push)
	*/
	const onClickLogo = (onClick) => {
		setActiveSection(null);
		if (window.location.pathname === "/") window.scrollTo({
			top: 0,
			behavior: "smooth"
		});
		else onClick("/");
	};
	/**
	* Handles section navigation click behavior
	* Smooth scrolls if on same page, executes callback if different page
	*
	* @param sectionId - ID of the target section element
	* @param url - URL of the page containing the section (optional)
	* @param onClick - Callback function to handle navigation (optional)
	*/
	const onClickSection = (sectionId, url, onClick) => {
		setActiveSection(sectionId);
		if (window.location.pathname === url) {
			const sectionEl = document.getElementById(sectionId);
			if (sectionEl) sectionEl.scrollIntoView({
				behavior: "smooth",
				block: "center",
				inline: "nearest"
			});
		} else onClick?.();
	};
	return {
		activeSection,
		onClickLogo,
		onClickSection
	};
};

//#endregion
export { useNavActions };
//# sourceMappingURL=useNavigation.mjs.map