'use client';
// packages/ui/src/routing/hooks/useNavigate.next.ts
/**
 * @fileoverview Next.js-specific navigation hooks
 * @description Navigation hooks for Next.js App Router
 *
 * @version 0.0.1
 * @since 0.0.1
 * @author AMBROISE PARK Consulting
 */
import { useRouter as useNextRouter } from 'next/navigation';
import { useCallback } from 'react';
import { isClient, useOverlayStore } from '@donotdev/core';
export function useNavigate() {
    const router = useNextRouter();
    const closeAll = useOverlayStore((state) => state.closeAll);
    return useCallback((to, options) => {
        if (to === 'back') {
            closeAll();
            return router.back();
        }
        closeAll();
        if (options?.preserveScroll && isClient()) {
            const scrollY = window.scrollY;
            if (options?.replace) {
                router.replace(to);
            }
            else {
                router.push(to);
            }
            requestAnimationFrame(() => {
                window.scrollTo({ top: scrollY, behavior: 'auto' });
            });
            return;
        }
        if (options?.replace)
            return router.replace(to);
        return router.push(to);
    }, [router, closeAll]);
}
export function useBack() {
    const router = useNextRouter();
    return useCallback(() => router.back(), [router]);
}
export function useRefresh() {
    const router = useNextRouter();
    return useCallback(() => router.refresh(), [router]);
}
export function usePrefetch() {
    const router = useNextRouter();
    return useCallback((to) => router.prefetch(to), [router]);
}
