"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useEscapeClickEffect = void 0;
const react_1 = require("react");
/**
 * хук клика по кнопке клавиатуры `esc`
 */
const useEscapeClickEffect = ({ onEscape, isActive, preventBubbling, }) => {
    (0, react_1.useEffect)(() => {
        if (!isActive) {
            return;
        }
        const onKeydown = (e) => {
            if (e.code === 'Escape') {
                if (preventBubbling) {
                    e.stopPropagation();
                    e.stopImmediatePropagation();
                }
                onEscape?.(e, 'escape');
            }
        };
        window.addEventListener('keydown', onKeydown, true);
        return () => {
            window.removeEventListener('keydown', onKeydown, true);
        };
    }, [isActive]);
};
exports.useEscapeClickEffect = useEscapeClickEffect;
