"use strict";

exports.__esModule = true;
exports.useEditableTableContextMenu = useEditableTableContextMenu;
var _react = require("react");
const MOUSE_LEAVE_PADDING = 30;
const isInsidePaddedRect = (e, rect, padding) => e.clientX >= rect.left - padding && e.clientX <= rect.right + padding && e.clientY >= rect.top - padding && e.clientY <= rect.bottom + padding;
function useEditableTableContextMenu(cellInteraction) {
  const [position, setPosition] = (0, _react.useState)(null);
  const [cell, setCell] = (0, _react.useState)(null);
  const menuRef = (0, _react.useRef)(null);
  const cellElRef = (0, _react.useRef)(null);
  const handleContextMenu = (0, _react.useCallback)(e => {
    const cellEl = e.target.closest('[data-row-key][data-column-id]');
    if (!cellEl) {
      return;
    }
    e.preventDefault();
    const rowKey = cellEl.getAttribute('data-row-key');
    const columnId = cellEl.getAttribute('data-column-id');
    if (!cellInteraction.isCellSelected(rowKey, columnId)) {
      cellInteraction.focusCell(rowKey, columnId);
    }
    cellElRef.current = cellEl;
    setPosition({
      x: e.clientX,
      y: e.clientY
    });
    setCell({
      rowKey,
      columnId
    });
  }, [cellInteraction]);
  const handleClose = (0, _react.useCallback)(() => {
    setPosition(null);
    setCell(null);
    cellElRef.current = null;
  }, []);
  (0, _react.useEffect)(() => {
    if (!position) {
      return;
    }
    const isInside = target => {
      var _menuRef$current;
      return (_menuRef$current = menuRef.current) == null ? void 0 : _menuRef$current.contains(target);
    };
    const onMouseDown = e => {
      if (!isInside(e.target)) {
        handleClose();
      }
    };
    const onScroll = e => {
      if (!isInside(e.target)) {
        handleClose();
      }
    };
    const onMouseMove = e => {
      if (!menuRef.current) {
        return;
      }
      const menuRect = menuRef.current.getBoundingClientRect();
      // Skip until the menu has been laid out
      if (menuRect.width === 0 || menuRect.height === 0) {
        return;
      }
      // Stay open if cursor is near the menu
      if (isInsidePaddedRect(e, menuRect, MOUSE_LEAVE_PADDING)) {
        return;
      }
      // Stay open if cursor is on the cell that triggered the menu
      if (cellElRef.current) {
        const cellRect = cellElRef.current.getBoundingClientRect();
        if (isInsidePaddedRect(e, cellRect, 0)) {
          return;
        }
      }
      handleClose();
    };
    document.addEventListener('mousedown', onMouseDown);
    document.addEventListener('scroll', onScroll, true);
    document.addEventListener('mousemove', onMouseMove);
    return () => {
      document.removeEventListener('mousedown', onMouseDown);
      document.removeEventListener('scroll', onScroll, true);
      document.removeEventListener('mousemove', onMouseMove);
    };
  }, [position, handleClose]);
  return {
    position,
    cell,
    menuRef,
    handleContextMenu,
    handleClose
  };
}
//# sourceMappingURL=useEditableTableContextMenu.js.map