import { useCallback, useState } from 'react';
import { useSyncedRef } from '../useSyncedRef/index.js';
import { resolveHookState } from '../util/resolveHookState.js';
export function useToggle(initialState = false, ignoreReactEvents = true) {
    const [state, setState] = useState(initialState);
    const ignoreReactEventsRef = useSyncedRef(ignoreReactEvents);
    return [
        state,
        useCallback((nextState) => {
            setState((previousState) => {
                if (nextState === undefined ||
                    (ignoreReactEventsRef.current &&
                        typeof nextState === 'object' &&
                        (nextState.constructor.name === 'SyntheticBaseEvent' ||
                            typeof nextState._reactName === 'string'))) {
                    return !previousState;
                }
                return Boolean(resolveHookState(nextState, previousState));
            });
        }, []),
    ];
}
