// Providers Context - manages email providers list
const { useState, useEffect, createContext, useContext } = React;

const ProvidersContext = createContext([]);

function ProvidersProvider({ children }) {
    const [providers, setProviders] = useState([
        { id: 'virgilio', name: 'Virgilio', domain: '@virgilio.it' }
    ]);
    
    useEffect(() => {
        fetch('/providers')
            .then(r => r.json())
            .then(data => {
                if (data.providers?.length > 0) setProviders(data.providers);
            })
            .catch(console.error);
    }, []);
    
    return (
        <ProvidersContext.Provider value={providers}>
            {children}
        </ProvidersContext.Provider>
    );
}

const useProviders = () => useContext(ProvidersContext);

// Export to global scope
window.ProvidersContext = ProvidersContext;
window.ProvidersProvider = ProvidersProvider;
window.useProviders = useProviders;

