"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Divider = Divider;
exports.Group = Group;
exports.Item = Item;
exports.ItemIcon = ItemIcon;
exports.ItemText = ItemText;
exports.Outer = Outer;
exports.Root = Root;
exports.Trigger = Trigger;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _useTheme = require("../theme/use-theme.js");
var _index = require("../typography/index.js");
var _indexWeb = require("../portal/index.web.js");
var _useInteractionState = require("../hooks/useInteractionState.js");
var _context = require("./context.js");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
/**
 * ContextMenu — Web implementation
 *
 * Opens a positioned dropdown menu on right-click (contextmenu event).
 * Uses Bloom's Portal for rendering the menu above other content.
 */

// ---------------------------------------------------------------------------
// Web-specific context (extends base with position)
// ---------------------------------------------------------------------------

const WebContextMenuContext = /*#__PURE__*/(0, _react.createContext)(null);
WebContextMenuContext.displayName = 'WebContextMenuContext';
function useWebContextMenuContext() {
  const ctx = (0, _react.useContext)(WebContextMenuContext);
  if (!ctx) {
    throw new Error('ContextMenu components must be used within a ContextMenu.Root');
  }
  return ctx;
}

// ---------------------------------------------------------------------------
// Root
// ---------------------------------------------------------------------------

function Root({
  children
}) {
  const [position, setPosition] = (0, _react.useState)(null);
  const isOpen = position !== null;
  const ctx = (0, _react.useMemo)(() => ({
    isOpen,
    position,
    open: () => {
      /* open is handled by the Trigger's contextmenu event */
    },
    close: () => setPosition(null)
  }), [isOpen, position]);

  /**
   * We store the setPosition in a ref so the Trigger can call it
   * without needing a stable identity on `open`.
   */
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(WebContextMenuContext.Provider, {
    value: ctx,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(SetPositionContext.Provider, {
      value: setPosition,
      children: children
    })
  });
}
const SetPositionContext = /*#__PURE__*/(0, _react.createContext)(() => {});
SetPositionContext.displayName = 'ContextMenuSetPositionContext';

// ---------------------------------------------------------------------------
// Trigger
// ---------------------------------------------------------------------------

function Trigger({
  children,
  label,
  hint,
  style
}) {
  const ctx = useWebContextMenuContext();
  const setPosition = (0, _react.useContext)(SetPositionContext);
  const triggerRef = (0, _react.useRef)(null);
  const handleContextMenu = (0, _react.useCallback)(e => {
    e.preventDefault();
    setPosition({
      x: e.clientX,
      y: e.clientY
    });
  }, [setPosition]);
  (0, _react.useEffect)(() => {
    const node = triggerRef.current;
    if (!node) return;
    // On web, View refs resolve to DOM elements
    const element = node;
    element.addEventListener('contextmenu', handleContextMenu);
    return () => {
      element.removeEventListener('contextmenu', handleContextMenu);
    };
  }, [handleContextMenu]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
    ref: triggerRef,
    style: style,
    children: children({
      isOpen: ctx.isOpen,
      state: {
        hovered: false,
        focused: false,
        pressed: false
      },
      props: {
        onPress: null,
        onLongPress: null,
        onFocus: null,
        onBlur: null,
        accessibilityLabel: label,
        accessibilityHint: hint ?? 'Right-click to open context menu'
      }
    })
  });
}

// ---------------------------------------------------------------------------
// Outer
// ---------------------------------------------------------------------------

function Outer({
  children,
  style
}) {
  const ctx = useWebContextMenuContext();
  const theme = (0, _useTheme.useTheme)();
  const {
    isOpen,
    close,
    position
  } = ctx;
  (0, _react.useEffect)(() => {
    if (!isOpen || typeof document === 'undefined') return;
    const handleKeyDown = e => {
      if (e.key === 'Escape') {
        close();
      }
    };
    document.addEventListener('keydown', handleKeyDown);
    return () => document.removeEventListener('keydown', handleKeyDown);
  }, [isOpen, close]);
  if (!isOpen || !position) return null;
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_indexWeb.Portal, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Pressable, {
      style: styles.backdrop,
      onPress: close,
      accessibilityLabel: "Close context menu"
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
      style: [styles.dropdown, {
        top: position.y,
        left: position.x,
        backgroundColor: theme.isDark ? theme.colors.backgroundSecondary : theme.colors.background,
        borderColor: theme.colors.borderLight,
        shadowColor: theme.colors.shadow
      }, style],
      children: children
    })]
  });
}

// ---------------------------------------------------------------------------
// Item
// ---------------------------------------------------------------------------

function Item({
  children,
  label,
  onPress,
  disabled = false,
  style
}) {
  const theme = (0, _useTheme.useTheme)();
  const {
    close
  } = useWebContextMenuContext();
  const {
    state: hovered,
    onIn: onMouseEnter,
    onOut: onMouseLeave
  } = (0, _useInteractionState.useInteractionState)();
  const {
    state: focused,
    onIn: onFocus,
    onOut: onBlur
  } = (0, _useInteractionState.useInteractionState)();
  const isHighlighted = (hovered || focused) && !disabled;
  const handlePress = (0, _react.useCallback)(() => {
    onPress();
    close();
  }, [onPress, close]);
  const itemCtx = (0, _react.useMemo)(() => ({
    disabled
  }), [disabled]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Pressable, {
    accessibilityHint: "",
    accessibilityLabel: label,
    disabled: disabled,
    onPress: handlePress,
    onFocus: onFocus,
    onBlur: onBlur,
    onMouseEnter,
    onMouseLeave,
    style: [styles.item, isHighlighted && {
      backgroundColor: theme.isDark ? theme.colors.contrast50 : theme.colors.backgroundSecondary
    }, style],
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_context.ItemCtx.Provider, {
      value: itemCtx,
      children: children
    })
  });
}

// ---------------------------------------------------------------------------
// ItemText
// ---------------------------------------------------------------------------

function ItemText({
  children,
  style
}) {
  const theme = (0, _useTheme.useTheme)();
  const {
    disabled
  } = (0, _context.useItemContext)();
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_index.Text, {
    numberOfLines: 1,
    ellipsizeMode: "middle",
    style: [styles.itemText, {
      color: disabled ? theme.colors.textTertiary : theme.colors.text
    }, ...(style ? [style] : [])],
    children: children
  });
}

// ---------------------------------------------------------------------------
// ItemIcon
// ---------------------------------------------------------------------------

function ItemIcon({
  icon: Comp
}) {
  const theme = (0, _useTheme.useTheme)();
  const {
    disabled
  } = (0, _context.useItemContext)();
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(Comp, {
    size: "md",
    fill: disabled ? theme.colors.textTertiary : theme.colors.textSecondary
  });
}

// ---------------------------------------------------------------------------
// Group
// ---------------------------------------------------------------------------

function Group({
  children
}) {
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
    children: children
  });
}

// ---------------------------------------------------------------------------
// Divider
// ---------------------------------------------------------------------------

function Divider() {
  const theme = (0, _useTheme.useTheme)();
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
    style: [styles.divider, {
      backgroundColor: theme.colors.borderLight
    }]
  });
}

// ---------------------------------------------------------------------------
// Styles
// ---------------------------------------------------------------------------

const styles = _reactNative.StyleSheet.create({
  backdrop: {
    position: 'fixed',
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
    zIndex: 50,
    // Opt back in from the Portal root's `pointer-events: none`.
    pointerEvents: 'auto'
  },
  dropdown: {
    position: 'fixed',
    zIndex: 60,
    borderRadius: 8,
    padding: 4,
    borderWidth: 1,
    overflow: 'hidden',
    shadowOpacity: 0.15,
    shadowRadius: 12,
    shadowOffset: {
      width: 0,
      height: 4
    },
    minWidth: 180,
    maxWidth: 280,
    // Opt back in from the Portal root's `pointer-events: none`.
    pointerEvents: 'auto'
  },
  item: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: 12,
    paddingVertical: 8,
    borderRadius: 6,
    overflow: 'hidden',
    paddingHorizontal: 10,
    minHeight: 32
  },
  itemText: {
    flex: 1,
    fontWeight: '600'
  },
  divider: {
    height: 1,
    marginVertical: 4
  }
});
//# sourceMappingURL=index.web.js.map