"use client";
import { useCallback, useEffect, useRef, useState } from "react";
export function useHoverMode(options) {
    const { disabled, hoverTimeout: hoverTime, hoverTimeoutRef, leaveTimeout: leaveTime, leaveTimeoutRef, enableHoverMode, disableHoverMode, startDisableTimer, clearDisableTimer, setVisible: propSetVisible, defaultVisible = false } = options;
    const state = useState(defaultVisible);
    let visible;
    let setVisible;
    if (typeof propSetVisible !== "undefined") {
        setVisible = propSetVisible;
    } else {
        [visible, setVisible] = state;
    }
    const visibilityTimeout = useRef();
    const clearVisibilityTimeout = useCallback(()=>{
        window.clearTimeout(visibilityTimeout.current);
    }, []);
    // if the element is near the viewport edge, the mouseleave event might not
    // trigger correctly. for these cases, just clear any timeouts to be safe.
    // do not hide the visibility so that you can still inspect things in the
    // devtools
    useEffect(()=>{
        if (disabled) {
            return;
        }
        const handler = ()=>{
            window.clearTimeout(visibilityTimeout.current);
            // might need to play with this more or make it configurable. if the mouse
            // leaves the window, you're _normally_ not interacting with the app
            // anymore and state should reset.
            disableHoverMode();
        };
        document.addEventListener("mouseleave", handler);
        return ()=>{
            document.removeEventListener("mouseleave", handler);
        };
    }, [
        disableHoverMode,
        disabled
    ]);
    useEffect(()=>{
        return ()=>{
            window.clearTimeout(visibilityTimeout.current);
        };
    }, []);
    return {
        visible,
        setVisible: setVisible === propSetVisible ? undefined : setVisible,
        startShowFlow: useCallback((eventOrId)=>{
            const hoverTimeout = hoverTime ?? hoverTimeoutRef.current;
            if (disabled || typeof hoverTimeout === "undefined") {
                return;
            }
            let id;
            if (typeof eventOrId === "string" || typeof eventOrId === "undefined") {
                id = eventOrId || "";
            } else {
                id = eventOrId.currentTarget.id;
            }
            clearDisableTimer();
            clearVisibilityTimeout();
            visibilityTimeout.current = window.setTimeout(()=>{
                enableHoverMode(id);
                setVisible(true);
            }, hoverTimeout);
        }, [
            clearDisableTimer,
            clearVisibilityTimeout,
            disabled,
            enableHoverMode,
            hoverTime,
            hoverTimeoutRef,
            setVisible
        ]),
        startHideFlow: useCallback(()=>{
            if (disabled) {
                return;
            }
            startDisableTimer();
            clearVisibilityTimeout();
            visibilityTimeout.current = window.setTimeout(()=>{
                setVisible(false);
            }, leaveTime ?? leaveTimeoutRef.current);
        }, [
            clearVisibilityTimeout,
            disabled,
            leaveTime,
            leaveTimeoutRef,
            setVisible,
            startDisableTimer
        ]),
        clearVisibilityTimeout
    };
}

//# sourceMappingURL=useHoverMode.js.map