import { useCallback, useEffect, useRef } from 'react';
export function useToastParameters(params) {
    const { duration = null, open, onClose } = params;
    const timerAutoHide = useRef();
    useEffect(() => {
        if (!open) {
            return undefined;
        }
        function handleKeyDown(nativeEvent) {
            if (!nativeEvent.defaultPrevented) {
                if (nativeEvent.key === 'Escape' || nativeEvent.key === 'Esc') {
                    onClose === null || onClose === void 0 ? void 0 : onClose();
                }
            }
        }
        document.addEventListener('keydown', handleKeyDown);
        return () => {
            document.removeEventListener('keydown', handleKeyDown);
        };
    }, [open, onClose]);
    const setAutoHideTimer = useCallback((autoHideDurationParam) => {
        if (!onClose || autoHideDurationParam == null) {
            return;
        }
        clearTimeout(timerAutoHide.current);
        timerAutoHide.current = setTimeout(() => {
            onClose === null || onClose === void 0 ? void 0 : onClose();
        }, autoHideDurationParam);
    }, [onClose]);
    useEffect(() => {
        if (open) {
            setAutoHideTimer(duration);
        }
        return () => {
            clearTimeout(timerAutoHide.current);
        };
    }, [open, duration, setAutoHideTimer]);
    return { params };
}
