'use client';
import { jsx as _jsx } from "react/jsx-runtime";
import { createContext, useContext, useState, useEffect, useCallback, useRef } from 'react';
import { authStorage, fetchDeviceId } from './auth-storage';
const AuthContext = /*#__PURE__*/ createContext(null);
const DEFAULT_AUTH = {
    authType: 'none',
    authActive: true
};
export function AuthProvider({ children }) {
    const [globalAuth, setGlobalAuthState] = useState(null);
    const [isInitialized, setIsInitialized] = useState(false);
    const [specSchemes, setSpecSchemes] = useState([]);
    const [specDefaultAuth, setSpecDefaultAuth] = useState(null);
    const deviceIdRef = useRef(null);
    // Load auth from localStorage on mount (async with encryption)
    useEffect(()=>{
        async function initializeAuth() {
            try {
                // Fetch device ID from backend (required for encryption)
                const deviceId = await fetchDeviceId();
                deviceIdRef.current = deviceId;
                // Load encrypted auth from storage
                const stored = await authStorage.load(deviceId);
                if (stored) {
                    setGlobalAuthState(stored);
                }
                // Fetch security schemes from the spec
                const schemesResponse = await fetch('/api/auth-schemes');
                if (schemesResponse.ok) {
                    const data = await schemesResponse.json();
                    setSpecSchemes(data.schemes || []);
                    setSpecDefaultAuth(data.defaultAuth || null);
                }
            } catch (error) {
                console.error('[AuthProvider] Failed to initialize auth:', error);
                // Clear any potentially corrupted data
                authStorage.clear();
            } finally{
                setIsInitialized(true);
            }
        }
        initializeAuth();
    }, []);
    const setGlobalAuth = useCallback(async (auth)=>{
        if (!deviceIdRef.current) {
            console.error('[AuthProvider] Cannot save auth: no device ID');
            return;
        }
        setGlobalAuthState(auth);
        await authStorage.save(auth, deviceIdRef.current);
    }, []);
    const clearAuth = useCallback(()=>{
        setGlobalAuthState(null);
        authStorage.clear();
    }, []);
    /**
   * Get effective auth for an endpoint
   * - If endpoint has 'inherit' auth type, use global auth
   * - If endpoint has 'none' auth type and global is set, use global
   * - Otherwise use endpoint's own auth
   */ const getEffectiveAuth = useCallback((endpointAuth)=>{
        // If endpoint explicitly wants to inherit, use global
        if (endpointAuth.authType === 'inherit') {
            return globalAuth || DEFAULT_AUTH;
        }
        // If endpoint has no auth but global is configured, suggest global
        // but don't override - this allows explicit "no auth" endpoints
        if (endpointAuth.authType === 'none' && globalAuth && globalAuth.authType !== 'none') {
            // Return global auth but the UI should indicate it's using global
            return globalAuth;
        }
        // Use endpoint's own auth
        return endpointAuth;
    }, [
        globalAuth
    ]);
    /**
   * Check if an endpoint is using global auth
   */ const isUsingGlobalAuth = useCallback((endpointAuth)=>{
        if (!globalAuth || globalAuth.authType === 'none') {
            return false;
        }
        // Using global if inheriting or if endpoint has no auth
        return endpointAuth.authType === 'inherit' || endpointAuth.authType === 'none';
    }, [
        globalAuth
    ]);
    const isAuthenticated = globalAuth !== null && globalAuth.authType !== 'none';
    const value = {
        globalAuth,
        isAuthenticated,
        setGlobalAuth,
        clearAuth,
        getEffectiveAuth,
        isUsingGlobalAuth,
        specSchemes,
        specDefaultAuth
    };
    // Don't render until we've checked sessionStorage
    if (!isInitialized) {
        return null;
    }
    return /*#__PURE__*/ _jsx(AuthContext.Provider, {
        value: value,
        children: children
    });
}
export function useAuth() {
    const context = useContext(AuthContext);
    if (!context) {
        throw new Error('useAuth must be used within an AuthProvider');
    }
    return context;
}
/**
 * Get a human-readable label for auth type
 */ export function getAuthTypeLabel(auth) {
    if (!auth || !auth.authType) {
        return 'Not configured';
    }
    switch(auth.authType){
        case 'none':
            return 'No Auth';
        case 'inherit':
            return 'Inherited';
        case 'basic':
            return 'Basic Auth';
        case 'bearer':
            return 'Bearer Token';
        case 'api-key':
            return `API Key (${auth.key || 'key'})`;
        case 'oauth-2':
            return 'OAuth 2.0';
        default:
            return 'Not configured';
    }
}
/**
 * Get a masked preview of auth value
 */ export function getAuthPreview(auth) {
    switch(auth.authType){
        case 'none':
        case 'inherit':
            return '';
        case 'basic':
            return auth.username ? `${auth.username}:****` : '';
        case 'bearer':
            return auth.token ? `${auth.token.slice(0, 8)}••••` : '';
        case 'api-key':
            return auth.value ? `${auth.value.slice(0, 4)}••••` : '';
        case 'oauth-2':
            return auth.grantTypeInfo.token ? `${auth.grantTypeInfo.token.slice(0, 8)}••••` : '';
        default:
            return '';
    }
}
