"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;
Object.defineProperty(exports, "useMenuContext", {
  enumerable: true,
  get: function () {
    return _context2.useMenuContext;
  }
});
Object.defineProperty(exports, "useMenuControl", {
  enumerable: true,
  get: function () {
    return _context.useDialogControl;
  }
});
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _useTheme = require("../theme/use-theme.js");
var _index = require("../typography/index.js");
var _index2 = require("../button/index.js");
var _context = require("../dialog/context.js");
var _SheetShell = require("../dialog/SheetShell.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); }
function Root({
  children,
  control
}) {
  const defaultControl = (0, _context.useDialogControl)();
  const context = (0, _react.useMemo)(() => ({
    control: control ?? defaultControl
  }), [control, defaultControl]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_context2.MenuContext.Provider, {
    value: context,
    children: children
  });
}
function Trigger({
  children,
  label,
  role = 'button',
  hint
}) {
  const context = (0, _context2.useMenuContext)();
  const [focused, setFocused] = (0, _react.useState)(false);
  const [pressed, setPressed] = (0, _react.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
    }
  });
}
function Outer({
  children,
  showCancel
}) {
  const context = (0, _context2.useMenuContext)();
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_SheetShell.SheetShell, {
    control: context.control,
    label: "Menu",
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_context2.MenuContext.Provider, {
      value: context,
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
        style: styles.outerContent,
        children: [children, showCancel && /*#__PURE__*/(0, _jsxRuntime.jsx)(Cancel, {})]
      })
    })
  });
}
function Item({
  children,
  label,
  style,
  onPress,
  ...rest
}) {
  const theme = (0, _useTheme.useTheme)();
  const context = (0, _context2.useMenuContext)();
  const [focused, setFocused] = (0, _react.useState)(false);
  const [pressed, setPressed] = (0, _react.useState)(false);
  const handlePress = (0, _react.useCallback)(e => {
    context.control.close(() => {
      onPress(e);
    });
  }, [context.control, onPress]);
  const isDisabled = Boolean(rest.disabled);
  const isHighlighted = (focused || pressed) && !isDisabled;
  const itemContextValue = (0, _react.useMemo)(() => ({
    disabled: isDisabled
  }), [isDisabled]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.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__*/(0, _jsxRuntime.jsx)(_context2.ItemContext.Provider, {
      value: itemContextValue,
      children: children
    })
  });
}
function ItemText({
  children,
  style
}) {
  const theme = (0, _useTheme.useTheme)();
  const {
    disabled
  } = (0, _context2.useMenuItemContext)();
  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
  });
}
function ItemIcon({
  icon: Comp,
  fill
}) {
  const theme = (0, _useTheme.useTheme)();
  const {
    disabled
  } = (0, _context2.useMenuItemContext)();
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(Comp, {
    size: "lg",
    fill: fill ? fill({
      disabled
    }) : disabled ? theme.colors.textTertiary : theme.colors.textSecondary
  });
}
function Group({
  children,
  style
}) {
  const theme = (0, _useTheme.useTheme)();
  const childArray = _react.default.Children.toArray(children).filter(_react.isValidElement);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
    style: [styles.group, {
      borderColor: theme.colors.borderLight
    }, style],
    children: childArray.map((child, i) => {
      if (child.type === Item) {
        return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_react.Fragment, {
          children: [i > 0 && /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
            style: [styles.groupDivider, {
              borderBottomColor: theme.colors.borderLight
            }]
          }), /*#__PURE__*/(0, _react.cloneElement)(child, {
            style: {
              borderRadius: 0,
              borderWidth: 0
            }
          })]
        }, i);
      }
      return null;
    })
  });
}
function Divider() {
  return null;
}
function Cancel() {
  const context = (0, _context2.useMenuContext)();
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_index2.Button, {
    accessibilityLabel: "Close this menu",
    size: "small",
    variant: "ghost",
    onPress: () => context.control.close(),
    children: "Cancel"
  });
}
const styles = _reactNative.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