import React, { createContext, useState } from 'react'

const AuthContext = createContext(null)

const initialState = {
    isLoggedIn: false,
    isLoginPending: false,
    loginError: null
}

const AuthProvider = props => {
    const [state, setState] = useState(initialState);

    const setLoginPending = (isLoginPending) => setState({ isLoginPending });
    const setLoginSuccess = (isLoggedIn) => setState({ isLoggedIn });
    const setLoginError = (loginError) => setState({ loginError });

    const login = (email, password) => {
        setLoginPending(true);
        setLoginSuccess(false);
        setLoginError(null);

        fetchLogin(email, password, error => {
            setLoginPending(false);

            if (!error) {
                setLoginSuccess(true);
            } else {
                setLoginError(error);
            }
        })
    }

    const logout = () => {
        setLoginPending(false);
        setLoginSuccess(false);
        setLoginError(null);
    }

    return (
        <AuthContext.Provider
            value={{ state, login, logout }}
        >
            {children}
        </AuthContext.Provider>
    )
}

export {
    AuthContext,
    AuthProvider
}