import { jsx } from "react/jsx-runtime";
import { useRef, useCallback, useEffect } from "react";
function useOutsideClickHandler(ref, handler) {
  useEffect(() => {
    function handleClickOutside(event) {
      if (ref.current && !ref.current.contains(event.target) && (!(event.target instanceof Element) || !event.target.className.includes("ant-dropdown"))) {
        handler(event);
      }
    }
    document.addEventListener("mousedown", handleClickOutside);
    return () => {
      document.removeEventListener("mousedown", handleClickOutside);
    };
  }, [ref, handler]);
}
function ContextMenu({
  cell,
  rowData,
  link,
  onClose,
  onComplete,
  open,
  renderContextMenuComponent,
  x,
  y
}) {
  const containerRef = useRef(null);
  useOutsideClickHandler(containerRef, onClose);
  const handleComplete = useCallback(
    (action, id, data) => {
      onComplete == null ? void 0 : onComplete(action, id, data);
      onClose();
    },
    [onClose, onComplete]
  );
  const handleVisibleChange = useCallback(() => onClose(), [onClose]);
  return /* @__PURE__ */ jsx(
    "div",
    {
      ref: containerRef,
      style: {
        left: x,
        position: "fixed",
        top: y,
        zIndex: 10
      },
      children: renderContextMenuComponent == null ? void 0 : renderContextMenuComponent({
        cell,
        link,
        onClose,
        onComplete: handleComplete,
        onVisibleChange: handleVisibleChange,
        open,
        rowData
      })
    }
  );
}
export {
  ContextMenu
};
