"use strict";

import React, { cloneElement, Fragment, isValidElement, useCallback, useMemo, useState } from 'react';
import { Pressable, StyleSheet, View } from 'react-native';
import { useTheme } from "../theme/use-theme.js";
import { Text } from "../typography/index.js";
import { Button } from "../button/index.js";
import { useDialogControl } from "../dialog/context.js";
import { SheetShell } from "../dialog/SheetShell.js";
import { MenuContext, ItemContext, useMenuContext, useMenuItemContext } from "./context.js";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export { useDialogControl as useMenuControl } from "../dialog/context.js";
export { useMenuContext };
export function Root({
  children,
  control
}) {
  const defaultControl = useDialogControl();
  const context = useMemo(() => ({
    control: control ?? defaultControl
  }), [control, defaultControl]);
  return /*#__PURE__*/_jsx(MenuContext.Provider, {
    value: context,
    children: children
  });
}
export function Trigger({
  children,
  label,
  role = 'button',
  hint
}) {
  const context = useMenuContext();
  const [focused, setFocused] = useState(false);
  const [pressed, setPressed] = useState(false);
  return children({
    control: context.control,
    state: {
      hovered: false,
      focused,
      pressed
    },
    props: {
      onPress: () => context.control.open(),
      onFocus: () => setFocused(true),
      onBlur: () => setFocused(false),
      accessibilityHint: hint,
      accessibilityLabel: label,
      accessibilityRole: role
    }
  });
}
export function Outer({
  children,
  showCancel
}) {
  const context = useMenuContext();
  return /*#__PURE__*/_jsx(SheetShell, {
    control: context.control,
    label: "Menu",
    children: /*#__PURE__*/_jsx(MenuContext.Provider, {
      value: context,
      children: /*#__PURE__*/_jsxs(View, {
        style: styles.outerContent,
        children: [children, showCancel && /*#__PURE__*/_jsx(Cancel, {})]
      })
    })
  });
}
export function Item({
  children,
  label,
  style,
  onPress,
  ...rest
}) {
  const theme = useTheme();
  const context = useMenuContext();
  const [focused, setFocused] = useState(false);
  const [pressed, setPressed] = useState(false);
  const handlePress = useCallback(e => {
    context.control.close(() => {
      onPress(e);
    });
  }, [context.control, onPress]);
  const isDisabled = Boolean(rest.disabled);
  const isHighlighted = (focused || pressed) && !isDisabled;
  const itemContextValue = useMemo(() => ({
    disabled: isDisabled
  }), [isDisabled]);
  return /*#__PURE__*/_jsx(Pressable, {
    ...rest,
    accessibilityHint: "",
    accessibilityLabel: label,
    onFocus: () => setFocused(true),
    onBlur: () => setFocused(false),
    onPress: handlePress,
    onPressIn: () => setPressed(true),
    onPressOut: () => setPressed(false),
    style: [styles.item, {
      backgroundColor: theme.colors.contrast50,
      borderColor: theme.colors.borderLight
    }, isHighlighted && {
      backgroundColor: theme.colors.backgroundSecondary
    }, style],
    children: /*#__PURE__*/_jsx(ItemContext.Provider, {
      value: itemContextValue,
      children: children
    })
  });
}
export function ItemText({
  children,
  style
}) {
  const theme = useTheme();
  const {
    disabled
  } = useMenuItemContext();
  return /*#__PURE__*/_jsx(Text, {
    numberOfLines: 1,
    ellipsizeMode: "middle",
    style: [styles.itemText, {
      color: disabled ? theme.colors.textTertiary : theme.colors.text
    }, ...(style ? [style] : [])],
    children: children
  });
}
export function ItemIcon({
  icon: Comp,
  fill
}) {
  const theme = useTheme();
  const {
    disabled
  } = useMenuItemContext();
  return /*#__PURE__*/_jsx(Comp, {
    size: "lg",
    fill: fill ? fill({
      disabled
    }) : disabled ? theme.colors.textTertiary : theme.colors.textSecondary
  });
}
export function Group({
  children,
  style
}) {
  const theme = useTheme();
  const childArray = React.Children.toArray(children).filter(isValidElement);
  return /*#__PURE__*/_jsx(View, {
    style: [styles.group, {
      borderColor: theme.colors.borderLight
    }, style],
    children: childArray.map((child, i) => {
      if (child.type === Item) {
        return /*#__PURE__*/_jsxs(Fragment, {
          children: [i > 0 && /*#__PURE__*/_jsx(View, {
            style: [styles.groupDivider, {
              borderBottomColor: theme.colors.borderLight
            }]
          }), /*#__PURE__*/cloneElement(child, {
            style: {
              borderRadius: 0,
              borderWidth: 0
            }
          })]
        }, i);
      }
      return null;
    })
  });
}
export function Divider() {
  return null;
}
function Cancel() {
  const context = useMenuContext();
  return /*#__PURE__*/_jsx(Button, {
    accessibilityLabel: "Close this menu",
    size: "small",
    variant: "ghost",
    onPress: () => context.control.close(),
    children: "Cancel"
  });
}
const styles = StyleSheet.create({
  outerContent: {
    gap: 16
  },
  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'
  },
  group: {
    borderRadius: 12,
    overflow: 'hidden',
    borderWidth: 1
  },
  groupDivider: {
    borderBottomWidth: 1
  }
});
//# sourceMappingURL=index.js.map