import { useState, useCallback, useRef, useEffect } from 'react';

/**
 * useNetworkStatus Hook
 * Detects online/offline status and network quality
 */
/**
 * Get the Network Information API connection object
 */
const getConnection = () => {
    if (typeof navigator === "undefined")
        return undefined;
    const nav = navigator;
    return nav.connection || nav.mozConnection || nav.webkitConnection;
};
/**
 * Hook for detecting online/offline status and network quality
 *
 * @example
 * const { isOnline, wasOffline, effectiveType } = useNetworkStatus();
 *
 * // Process queued actions when coming back online
 * useEffect(() => {
 *   if (wasOffline && isOnline) {
 *     processQueuedActions();
 *   }
 * }, [wasOffline, isOnline]);
 *
 * // Show quality indicator
 * if (effectiveType === 'slow-2g' || effectiveType === '2g') {
 *   return <SlowNetworkWarning />;
 * }
 */
const useNetworkStatus = () => {
    const [isOnline, setIsOnline] = useState(() => typeof navigator !== "undefined" ? navigator.onLine : true);
    const [wasOffline, setWasOffline] = useState(false);
    const [networkInfo, setNetworkInfo] = useState({});
    const updateNetworkInfo = useCallback(() => {
        const connection = getConnection();
        if (connection) {
            setNetworkInfo({
                effectiveType: connection.effectiveType,
                downlink: connection.downlink,
                rtt: connection.rtt,
                saveData: connection.saveData,
            });
        }
    }, []);
    const onlineTimerRef = useRef(undefined);
    useEffect(() => {
        const handleOnline = () => {
            setIsOnline(true);
            setWasOffline(true);
            updateNetworkInfo();
            // Reset wasOffline after a short delay
            // This gives consuming code time to react to the transition
            onlineTimerRef.current = setTimeout(() => setWasOffline(false), 1000);
        };
        const handleOffline = () => {
            setIsOnline(false);
        };
        const handleConnectionChange = () => {
            updateNetworkInfo();
        };
        // Set up online/offline listeners
        window.addEventListener("online", handleOnline);
        window.addEventListener("offline", handleOffline);
        // Set up Network Information API listener
        const connection = getConnection();
        if (connection) {
            connection.addEventListener("change", handleConnectionChange);
            updateNetworkInfo(); // Get initial values
        }
        return () => {
            clearTimeout(onlineTimerRef.current);
            window.removeEventListener("online", handleOnline);
            window.removeEventListener("offline", handleOffline);
            if (connection) {
                connection.removeEventListener("change", handleConnectionChange);
            }
        };
    }, [updateNetworkInfo]);
    return {
        isOnline,
        wasOffline,
        ...networkInfo,
    };
};

export { useNetworkStatus };
