/*
 * Copyright (c) 2023-2025 Datalayer, Inc.
 * Distributed under the terms of the Modified BSD License.
 */
import { useCallback } from 'react';
import { useLayoutStore } from '../state/index.js';
import { createNativeNavigate } from '../navigation/adapters/native.js';
// Import React Router hooks from our wrapper
import { useNavigateRR } from '../navigation/adapters/react-router.js';
// Import Next.js hooks from our wrapper
// Currently not used but kept for future Next.js support
// import { useRouterNext } from '../navigation/adapters/nextjs.js';
/**
 * Main navigation hook that provides a universal navigate function
 * Works with React Router, Next.js, or native browser navigation
 */
export const useNavigate = () => {
    const layoutStore = useLayoutStore();
    // Detect environment
    const isNextJs = typeof window !== 'undefined' && !!window.__NEXT_DATA__;
    const isClient = typeof window !== 'undefined';
    // Try to use React Router's useNavigate if available
    let rrNavigate = null;
    let isReactRouter = false;
    if (!isNextJs && useNavigateRR && isClient) {
        try {
            // eslint-disable-next-line react-hooks/rules-of-hooks
            rrNavigate = useNavigateRR();
            isReactRouter = !!rrNavigate;
        }
        catch {
            // Not in a Router context
        }
    }
    // If React Router is available, return it directly (wrapped for consistency)
    if (isReactRouter && rrNavigate) {
        // eslint-disable-next-line react-hooks/rules-of-hooks
        return useCallback((to, options) => {
            // Scroll to top when navigating (except for history navigation)
            if (typeof to === 'string') {
                window.scrollTo(0, 0);
                document.body.scrollTop = 0;
            }
            // For React Router, pass through directly
            return rrNavigate(to, options);
        }, [rrNavigate]);
    }
    // Otherwise use native navigation with our custom behavior
    const baseNavigate = createNativeNavigate();
    // Wrap with our custom behavior for native navigation
    // eslint-disable-next-line react-hooks/rules-of-hooks
    const navigate = useCallback((location, optionsOrEvent, resetPortals = true, extraOptions) => {
        // Handle number for history navigation (e.g., -1 to go back)
        if (typeof location === 'number') {
            window.history.go(location);
            return;
        }
        // Handle different call signatures for native navigation
        let options = undefined;
        let event = undefined;
        if (optionsOrEvent && optionsOrEvent.preventDefault) {
            // Legacy signature with event as second parameter
            event = optionsOrEvent;
            options = extraOptions;
        }
        else if (typeof optionsOrEvent === 'boolean') {
            // Legacy signature with resetPortals as second parameter
            resetPortals = optionsOrEvent;
            options = extraOptions;
        }
        else {
            // Standard signature with options as second parameter
            options = optionsOrEvent;
        }
        if (event) {
            event.preventDefault();
        }
        if (resetPortals) {
            layoutStore.resetLeftPortal();
            layoutStore.resetRightPortal();
        }
        window.scrollTo(0, 0);
        document.body.scrollTop = 0;
        // Use native navigation
        baseNavigate(location, options);
    }, [baseNavigate, layoutStore]);
    return navigate;
};
export default useNavigate;
