import { jsx } from "react/jsx-runtime";
import styled from "@emotion/styled";
import { AnimatePresence, motion } from "motion/react";
import { memo, useCallback, useEffect, useRef } from "react";
const MenuContainer = styled(motion.div)`
  position: fixed;
  left: ${(props)=>props.$x}px;
  top: ${(props)=>props.$y}px;
  min-width: 180px;
  background: var(--canvas-background);
  box-shadow:
    0 8px 24px rgba(0, 0, 0, 0.08),
    0 2px 8px rgba(0, 0, 0, 0.04);
  padding: 6px 0px;
  z-index: 10000;
  pointer-events: auto;
`;
const MenuItem = styled.button`
  display: flex;
  align-items: center;
  width: 100%;
  padding: 6px 12px;
  background: transparent;
  border: none;
  cursor: pointer;
  transition: background 0.15s ease;
  font-size: 12px;
  font-weight: 400;
  color: var(--canvas-foreground-de-emp);
  text-align: left;

  &:hover {
    background: var(--canvas-background-secondary);
  }
`;
const PaneContextMenu = /*#__PURE__*/ memo(({ isOpen, position, items, onClose })=>{
    const menuRef = useRef(null);
    useEffect(()=>{
        if (!isOpen) return;
        const handleClickOutside = (event)=>{
            if (menuRef.current && !menuRef.current.contains(event.target)) onClose();
        };
        const handleEscape = (event)=>{
            if ('Escape' === event.key) onClose();
        };
        const timeoutId = setTimeout(()=>{
            document.addEventListener('click', handleClickOutside);
            document.addEventListener('keydown', handleEscape);
        }, 0);
        return ()=>{
            clearTimeout(timeoutId);
            document.removeEventListener('click', handleClickOutside);
            document.removeEventListener('keydown', handleEscape);
        };
    }, [
        isOpen,
        onClose
    ]);
    const handleItemClick = useCallback((item)=>{
        item.onClick();
        onClose();
    }, [
        onClose
    ]);
    if (0 === items.length) return null;
    return /*#__PURE__*/ jsx(AnimatePresence, {
        children: isOpen && /*#__PURE__*/ jsx(MenuContainer, {
            ref: menuRef,
            $x: position.x,
            $y: position.y,
            initial: {
                opacity: 0,
                scale: 0.95
            },
            animate: {
                opacity: 1,
                scale: 1
            },
            exit: {
                opacity: 0,
                scale: 0.95
            },
            transition: {
                duration: 0.1,
                ease: 'easeOut'
            },
            children: items.map((item)=>/*#__PURE__*/ jsx(MenuItem, {
                    onClick: ()=>handleItemClick(item),
                    children: item.label
                }, item.label))
        })
    });
});
PaneContextMenu.displayName = 'PaneContextMenu';
export { PaneContextMenu };
