"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 _context = require("../dialog/context.js");
var _SheetShell = require("../dialog/SheetShell.js");
var _useInteractionState = require("../hooks/useInteractionState.js");
var _context2 = 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 — Native implementation
 *
 * Opens a bottom-sheet menu when the user long-presses the trigger. The
 * menu body uses bloom's internal `SheetShell` (a `BottomSheet`
 * presentation primitive with the same drag-handle + close-on-tap
 * semantics shared by `Menu` and `Select`).
 */

// ---------------------------------------------------------------------------
// Native-specific context (extends base with Dialog control)
// ---------------------------------------------------------------------------

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

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

function Root({
  children
}) {
  const control = (0, _context.useDialogControl)();
  const ctx = (0, _react.useMemo)(() => ({
    isOpen: false,
    open: () => control.open(),
    close: () => control.close(),
    control
  }), [control]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(NativeContextMenuContext.Provider, {
    value: ctx,
    children: children
  });
}

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

function Trigger({
  children,
  label,
  hint,
  style
}) {
  const {
    open
  } = useNativeContextMenuContext();
  const {
    state: focused,
    onIn: onFocus,
    onOut: onBlur
  } = (0, _useInteractionState.useInteractionState)();
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
    style: style,
    children: children({
      isOpen: false,
      state: {
        hovered: false,
        focused,
        pressed: false
      },
      props: {
        onPress: null,
        onLongPress: open,
        onFocus,
        onBlur,
        accessibilityLabel: label,
        accessibilityHint: hint ?? 'Long press to open context menu'
      }
    })
  });
}

// ---------------------------------------------------------------------------
// Outer  (the menu container — uses Bloom Dialog as a bottom sheet)
// ---------------------------------------------------------------------------

function Outer({
  children,
  style
}) {
  const {
    control
  } = useNativeContextMenuContext();
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_SheetShell.SheetShell, {
    control: control,
    label: "Context menu",
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(NativeContextMenuContext.Provider, {
      value: {
        isOpen: true,
        open: () => control.open(),
        close: () => control.close(),
        control
      },
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
        style: [styles.outerContent, style],
        children: children
      })
    })
  });
}

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

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

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

function ItemText({
  children,
  style
}) {
  const theme = (0, _useTheme.useTheme)();
  const {
    disabled
  } = (0, _context2.useItemContext)();
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_index.Text, {
    numberOfLines: 2,
    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, _context2.useItemContext)();
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(Comp, {
    size: "lg",
    fill: disabled ? theme.colors.textTertiary : theme.colors.textSecondary
  });
}

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

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

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

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

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

const styles = _reactNative.StyleSheet.create({
  outerContent: {
    gap: 4
  },
  item: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: 8,
    paddingHorizontal: 12,
    borderRadius: 8,
    overflow: 'hidden',
    borderWidth: 1,
    minHeight: 44,
    paddingVertical: 10
  },
  itemText: {
    flex: 1,
    fontSize: 16,
    fontWeight: '600'
  },
  divider: {
    borderTopWidth: _reactNative.StyleSheet.hairlineWidth,
    marginVertical: 4
  }
});
//# sourceMappingURL=index.js.map