"use client";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
const noop = ()=>{
// do nothing
};
/**
 * @since 6.0.0
 */ export function createHoverModeContext(options = {}) {
    const { defaultActiveId = "", hoverTimeout, leaveTimeout = 0 } = options;
    return {
        activeId: defaultActiveId,
        activeIdRef: {
            current: defaultActiveId
        },
        hoverTimeoutRef: {
            current: hoverTimeout
        },
        leaveTimeoutRef: {
            current: leaveTimeout
        },
        animatedOnceRef: {
            current: false
        },
        enableHoverMode: noop,
        disableHoverMode: noop,
        startDisableTimer: noop,
        clearDisableTimer: noop
    };
}
/**
 * @example Creating a Hover Mode Group
 * ```tsx
 * import type {
 *   HoverModeConfiguration,
 *   HoverModeContext,
 * } from "@react-md/core";
 * import {
 *   createHoverModeContext,
 *   useHoverModeProvider,
 * } from "@react-md/core";
 * import type { ReactElement } from "react";
 * import { createContext, useContext } from "react";
 *
 * // extend as needed
 * type CustomHoverMode = HoverModeContext;
 *
 * // you can also add defaults if there is no parent provider.
 * const context = createContext<CustomHoverMode>(createHoverModeContext());
 * const { Provider } = context;
 *
 * interface Props extends HoverModeConfiguration {
 *   children: ReactNode;
 * }
 *
 * export function CustomHoverModeProvider({
 *   children,
 *   // change to whatever defaults you want
 *   hoverTimeout = 3000,
 *   leaveTimeout = 3000,
 *   defaultActiveId = "",
 *   disableTimeout = 5000,
 * }: Props): ReactElement {
 *   const context = useHoverModeProvider({
 *     hoverTimeout,
 *     leaveTimeout,
 *     defaultActiveId,
 *     disableTimeout,
 *   });
 *
 *   return <Provider value={context}>{children}</Provider>;
 * }
 * ```
 *
 * @see {@link CreateHoverModeContextOptions}
 * @see {@link useHoverMode}
 * @since 6.0.0 The `HoverModeProvider` component was replaced by this
 * hook implementation. After developing the `MenuBar`, I realized the hover
 * mode should normally be grouped by related components or types instead of a
 * top-level catch all.
 */ export function useHoverModeProvider(options) {
    const { hoverTimeout, leaveTimeout = 0, forceRerender = false, defaultActiveId = "", disableTimeout } = options;
    const [activeId, setActiveId] = useState(defaultActiveId);
    const activeIdRef = useRef(defaultActiveId);
    const hoverTimeoutRef = useRef(hoverTimeout);
    const leaveTimeoutRef = useRef(leaveTimeout);
    const animatedOnceRef = useRef(!!defaultActiveId);
    const disableHoverModeTimeout = useRef();
    const clearDisableTimer = useCallback(()=>{
        window.clearTimeout(disableHoverModeTimeout.current);
    }, []);
    const enableHoverMode = useCallback((activeId)=>{
        clearDisableTimer();
        activeIdRef.current = activeId;
        hoverTimeoutRef.current = 0;
        if (forceRerender) {
            setActiveId(activeId);
        }
    }, [
        clearDisableTimer,
        forceRerender
    ]);
    const disableHoverMode = useCallback(()=>{
        clearDisableTimer();
        activeIdRef.current = "";
        hoverTimeoutRef.current = hoverTimeout;
        animatedOnceRef.current = false;
        if (forceRerender) {
            setActiveId("");
        }
    }, [
        clearDisableTimer,
        forceRerender,
        hoverTimeout
    ]);
    const startDisableTimer = useCallback(()=>{
        if (typeof disableTimeout !== "number") {
            return;
        }
        clearDisableTimer();
        disableHoverModeTimeout.current = window.setTimeout(()=>{
            disableHoverMode();
        }, disableTimeout);
    }, [
        clearDisableTimer,
        disableHoverMode,
        disableTimeout
    ]);
    useEffect(()=>{
        hoverTimeoutRef.current = hoverTimeout;
        return ()=>{
            window.clearTimeout(disableHoverModeTimeout.current);
        };
    }, [
        hoverTimeout
    ]);
    return useMemo(()=>({
            activeId,
            activeIdRef,
            hoverTimeoutRef,
            leaveTimeoutRef,
            animatedOnceRef,
            enableHoverMode,
            disableHoverMode,
            startDisableTimer,
            clearDisableTimer
        }), [
        activeId,
        enableHoverMode,
        disableHoverMode,
        startDisableTimer,
        clearDisableTimer
    ]);
}

//# sourceMappingURL=useHoverModeProvider.js.map