'use client';
import { useEffect, useState } from 'react';
import { compareVersions } from '../lib/compare-versions.js';
export function useNpmVersionCheck(currentVersion) {
    const [latest, setLatest] = useState(null);
    const [loading, setLoading] = useState(true);
    useEffect(() => {
        let cancelled = false;
        async function check() {
            try {
                const res = await fetch('/api/npm-version');
                if (!res.ok)
                    return;
                const data = (await res.json());
                if (!cancelled && data.latest) {
                    setLatest(data.latest);
                }
            }
            catch {
                // Silently fail — version check is non-critical
            }
            finally {
                if (!cancelled)
                    setLoading(false);
            }
        }
        check();
        return () => {
            cancelled = true;
        };
    }, []);
    const updateAvailable = latest !== null && compareVersions(latest, currentVersion) > 0;
    return { latest, updateAvailable, loading };
}
