"use strict";

/**
 * 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`).
 */
import React, { createContext, useCallback, useContext, useMemo } from 'react';
import { Pressable, StyleSheet, View } from 'react-native';
import { useTheme } from "../theme/use-theme.js";
import { Text } from "../typography/index.js";
import { useDialogControl } from "../dialog/context.js";
import { SheetShell } from "../dialog/SheetShell.js";
import { useInteractionState } from "../hooks/useInteractionState.js";
import { ItemCtx, useItemContext } from "./context.js";

// ---------------------------------------------------------------------------
// Native-specific context (extends base with Dialog control)
// ---------------------------------------------------------------------------
import { jsx as _jsx } from "react/jsx-runtime";
const NativeContextMenuContext = /*#__PURE__*/createContext(null);
NativeContextMenuContext.displayName = 'NativeContextMenuContext';
function useNativeContextMenuContext() {
  const ctx = useContext(NativeContextMenuContext);
  if (!ctx) {
    throw new Error('ContextMenu components must be used within a ContextMenu.Root');
  }
  return ctx;
}

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

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

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

export function Trigger({
  children,
  label,
  hint,
  style
}) {
  const {
    open
  } = useNativeContextMenuContext();
  const {
    state: focused,
    onIn: onFocus,
    onOut: onBlur
  } = useInteractionState();
  return /*#__PURE__*/_jsx(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)
// ---------------------------------------------------------------------------

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

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

export function Item({
  children,
  label,
  onPress,
  disabled = false,
  style
}) {
  const theme = useTheme();
  const {
    close
  } = useNativeContextMenuContext();
  const {
    state: focused,
    onIn: onFocus,
    onOut: onBlur
  } = useInteractionState();
  const {
    state: pressed,
    onIn: onPressIn,
    onOut: onPressOut
  } = useInteractionState();
  const isHighlighted = (focused || pressed) && !disabled;
  const handlePress = useCallback(() => {
    close();
    onPress();
  }, [close, onPress]);
  const itemCtx = useMemo(() => ({
    disabled
  }), [disabled]);
  return /*#__PURE__*/_jsx(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__*/_jsx(ItemCtx.Provider, {
      value: itemCtx,
      children: children
    })
  });
}

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

export function ItemText({
  children,
  style
}) {
  const theme = useTheme();
  const {
    disabled
  } = useItemContext();
  return /*#__PURE__*/_jsx(Text, {
    numberOfLines: 2,
    ellipsizeMode: "middle",
    style: [styles.itemText, {
      color: disabled ? theme.colors.textTertiary : theme.colors.text
    }, ...(style ? [style] : [])],
    children: children
  });
}

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

export function ItemIcon({
  icon: Comp
}) {
  const theme = useTheme();
  const {
    disabled
  } = useItemContext();
  return /*#__PURE__*/_jsx(Comp, {
    size: "lg",
    fill: disabled ? theme.colors.textTertiary : theme.colors.textSecondary
  });
}

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

export function Group({
  children,
  style
}) {
  return /*#__PURE__*/_jsx(View, {
    style: style,
    children: children
  });
}

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

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

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

const styles = 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: StyleSheet.hairlineWidth,
    marginVertical: 4
  }
});
//# sourceMappingURL=index.js.map