"use client";
import { useEffect } from "react";
const noop = ()=>{
// do nothing
};
/**
 * @example
 * ```ts
 * import { usePageInactive } from "@react-md/core/usePageInactive";
 * import { useCallback, useEffect, useRef, useState } from "react";
 *
 * function Example(): null {
 *   const [visible, setVisible] = useState(false);
 *   const timeout = useRef<number | undefined>();
 *   const startTimeout = useCallback(() => {
 *     timeout.current = window.setTimeout(() => {
 *       setVisible(false);
 *     }, 10000);
 *   }, []);
 *   useEffect(() => {
 *     return () => {
 *       window.clearTimeout(timeout.current);
 *     };
 *   }, []);
 *
 *   usePageInactive({
 *     onChange(active) {
 *       if (!active) {
 *         window.clearTimeout(timeout.current);
 *         setVisible(false);
 *       } else {
 *         startTimeout();
 *       }
 *     },
 *   });
 *
 *   // pretend implementation
 *   return null;
 * }
 * ```
 * @since 6.0.0
 */ export function usePageInactive(options) {
    const { type = "focus", disabled, onChange, onDisabledCleanup = noop } = options;
    useEffect(()=>{
        if (disabled) {
            onDisabledCleanup();
            return;
        }
        const callback = (event)=>{
            let active = document.visibilityState === "visible";
            if (event.type === "blur") {
                active = false;
            } else if (event.type === "focus") {
                active = true;
            }
            onChange(active);
        };
        document.addEventListener("visibilitychange", callback);
        if (type === "focus") {
            window.addEventListener("blur", callback);
            window.addEventListener("focus", callback);
        }
        return ()=>{
            document.removeEventListener("visibilitychange", callback);
            window.removeEventListener("blur", callback);
            window.removeEventListener("focus", callback);
        };
    }, [
        disabled,
        onChange,
        onDisabledCleanup,
        type
    ]);
}

//# sourceMappingURL=usePageInactive.js.map