"use strict";

exports.__esModule = true;
exports.useClearFocusOnBlur = useClearFocusOnBlur;
var _react = require("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.
 */
function useClearFocusOnBlur(cellInteraction) {
  const blurFrameId = (0, _react.useRef)(0);
  (0, _react.useEffect)(() => {
    return () => cancelAnimationFrame(blurFrameId.current);
  }, []);
  const onFocus = (0, _react.useCallback)(() => {
    cancelAnimationFrame(blurFrameId.current);
  }, []);
  const onBlur = (0, _react.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