/*
 * Copyright (c) 2023-2025 Datalayer, Inc.
 * Distributed under the terms of the Modified BSD License.
 */
import { useEffect, useState } from 'react';
// Import React Router hooks from our wrapper
import { useLocationRR, 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 {
//   usePathnameNext,
//   useSearchParamsNext,
// } from '../navigation/adapters/nextjs.js';
/**
 * Hook to get current location
 * Detects and uses React Router when available, falls back to native browser location
 */
export const useLocation = () => {
    // Native location state for fallback
    const [nativeLocation, setNativeLocation] = useState(() => {
        if (typeof window === 'undefined') {
            return {
                pathname: '/',
                search: '',
                hash: '',
                state: null,
                key: 'default',
            };
        }
        return {
            pathname: window.location.pathname,
            search: window.location.search,
            hash: window.location.hash,
            state: window.history.state,
            key: 'default',
        };
    });
    // Detect environment
    const isNextJs = typeof window !== 'undefined' && !!window.__NEXT_DATA__;
    const isClient = typeof window !== 'undefined';
    // Try to use React Router if available
    let routerLocation = null;
    let isReactRouter = false;
    try {
        // Only actually use the hook if we have it and not in Next.js
        if (!isNextJs && useLocationRR && isClient) {
            // eslint-disable-next-line react-hooks/rules-of-hooks
            routerLocation = useLocationRR();
            isReactRouter = !!routerLocation;
        }
    }
    catch {
        // Not in a Router context, fallback to native
    }
    useEffect(() => {
        // Only set up native listeners if not using React Router
        if (isReactRouter)
            return;
        // Listen to popstate for browser navigation changes
        const handleLocationChange = () => {
            setNativeLocation({
                pathname: window.location.pathname,
                search: window.location.search,
                hash: window.location.hash,
                state: window.history.state,
                key: 'native-' + Date.now(),
            });
        };
        window.addEventListener('popstate', handleLocationChange);
        // Also listen for custom navigation events if needed
        window.addEventListener('pushstate', handleLocationChange);
        window.addEventListener('replacestate', handleLocationChange);
        return () => {
            window.removeEventListener('popstate', handleLocationChange);
            window.removeEventListener('pushstate', handleLocationChange);
            window.removeEventListener('replacestate', handleLocationChange);
        };
    }, [isReactRouter]);
    // Return React Router location if available, otherwise native location
    return isReactRouter && routerLocation ? routerLocation : nativeLocation;
};
/**
 * Hook to get history functions
 * Detects and uses React Router history when available, falls back to browser history
 */
export const useHistory = () => {
    // Detect environment
    const isNextJs = typeof window !== 'undefined' && !!window.__NEXT_DATA__;
    const isClient = typeof window !== 'undefined';
    // Try to use React Router if available
    let routerNavigate = null;
    let isReactRouter = false;
    try {
        if (!isNextJs && useNavigateRR && isClient) {
            // eslint-disable-next-line react-hooks/rules-of-hooks
            routerNavigate = useNavigateRR();
            isReactRouter = true;
        }
    }
    catch {
        // Not in a Router context
    }
    if (isReactRouter && routerNavigate) {
        // Use React Router navigation
        return {
            back: () => {
                routerNavigate(-1);
            },
            forward: () => {
                routerNavigate(1);
            },
            replace: (to, state) => {
                routerNavigate(to, { replace: true, state });
            },
            push: (to, state) => {
                routerNavigate(to, { state });
            },
        };
    }
    // Fall back to native browser history
    return {
        back: () => {
            if (typeof window !== 'undefined') {
                window.history.back();
            }
        },
        forward: () => {
            if (typeof window !== 'undefined') {
                window.history.forward();
            }
        },
        replace: (to, state) => {
            if (typeof window !== 'undefined') {
                const url = new URL(to, window.location.href);
                window.history.replaceState(state || null, '', url.href);
                // Dispatch custom event for location hooks
                window.dispatchEvent(new Event('replacestate'));
            }
        },
        push: (to, state) => {
            if (typeof window !== 'undefined') {
                const url = new URL(to, window.location.href);
                window.history.pushState(state || null, '', url.href);
                // Dispatch custom event for location hooks
                window.dispatchEvent(new Event('pushstate'));
            }
        },
    };
};
