import { useCallback, useEffect, useRef } from 'react';
/**
 * Clears cell focus when the user clicks outside the table container.
 *
 * Skips clearFocus() while isEditing=true so that portaled editors (date
 * picker, select dropdown, media manager) can take focus without causing the
 * cell to lose its editing state. Cell types that open external UI are
 * responsible for calling onStartEdit() before the modal opens (which sets
 * isEditing=true) and onCancel()/onCommit() when it closes.
 */
export function useClearFocusOnBlur(cellInteraction) {
    const blurFrameId = useRef(0);
    useEffect(() => {
        return () => cancelAnimationFrame(blurFrameId.current);
    }, []);
    const onFocus = useCallback(() => {
        cancelAnimationFrame(blurFrameId.current);
    }, []);
    const onBlur = useCallback((e) => {
        cancelAnimationFrame(blurFrameId.current);
        const container = e.currentTarget;
        blurFrameId.current = requestAnimationFrame(() => {
            if (container.contains(document.activeElement)) {
                return;
            }
            if (!cellInteraction.isEditing) {
                cellInteraction.clearFocus();
            }
        });
    }, [cellInteraction]);
    return { onFocus, onBlur };
}
//# sourceMappingURL=useClearFocusOnBlur.js.map