// packages/ui/src/utils/useAuthVisibility.ts
/**
 * @fileoverview useAuthVisibility Hook
 * @description Determines when to show/hide auth UI based on feature status
 *
 * This hook encapsulates the common pattern for auth UI visibility:
 * - Hide if status !== 'ready' or not available (not ready yet)
 * - Show with loading state during initialization
 * - Show normal UI when status === 'ready'
 *
 * @version 0.0.3
 * @since 0.0.1
 * @author AMBROISE PARK Consulting
 */
import { useMemo } from 'react';
import { useAuthSafe } from './useAuthSafe';
/**
 * Hook to determine auth UI visibility and loading states
 *
 * **Visibility Rules:**
 * - Hide if `status !== 'ready' || !isAvailable` (not ready or not installed)
 * - Show with loading if `status === 'initializing'` during operations
 * - Show normal UI when `status === 'ready' && isAvailable`
 *
 * @returns AuthVisibilityState with visibility flags
 *
 * @example
 * ```typescript
 * const { shouldHide, isLoading, isReady, isAuthenticated } = useAuthVisibility();
 *
 * if (shouldHide) return null;
 *
 * if (isLoading) {
 *   return <Button disabled><Spinner /></Button>;
 * }
 *
 * if (isAuthenticated) {
 *   return <UserMenu />;
 * }
 *
 * return <SignInButton />;
 * ```
 */
export function useAuthVisibility() {
    // Read all values first - each triggers subscription
    const status = useAuthSafe('status');
    const isAvailable = useAuthSafe('isAvailable');
    const user = useAuthSafe('user');
    // Derive all visibility state atomically from current values
    // useMemo ensures consistent state within a single render
    return useMemo(() => {
        const shouldHide = status !== 'ready' || !isAvailable;
        const isLoading = status === 'initializing';
        const isReady = status === 'ready' && isAvailable;
        const isAuthenticated = Boolean(user);
        return {
            shouldHide,
            isLoading,
            isReady,
            isAuthenticated,
        };
    }, [status, isAvailable, user]);
}
