import React,{ useState,useContext } from 'react';
import FocusVisibleContext from './focusVisibleContext';

const useFocusVisible = ({disabled})=>{
    let [focusedState, setFocused] = useState(false);
    let { hadKeyboardEvent, isInitialized } = useContext(FocusVisibleContext);
    if (disabled && focusedState) {
        setFocused(false);
    }
    if (isInitialized) {
        focusedState = hadKeyboardEvent && focusedState;
    } else {
        focusedState = focusedState;
    }
    return { 
        focusVisible : focusedState,
        focusProps:{
            onBlur: () => {
                setFocused(false);
            },
            onFocus: () => {
                setFocused(true);
            }
        }
    }
}   

export default useFocusVisible
