"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Content = Content;
exports.Root = Root;
exports.Trigger = Trigger;
Object.defineProperty(exports, "usePopoverContext", {
  enumerable: true,
  get: function () {
    return _context2.usePopoverContext;
  }
});
Object.defineProperty(exports, "usePopoverControl", {
  enumerable: true,
  get: function () {
    return _context.useDialogControl;
  }
});
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
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); }
/**
 * Native `Popover` — presents its content in a bottom sheet (Bloom's
 * established native-overlay convention, shared with `Menu`/`Select`). The
 * web fork (`index.web.tsx`) anchors a floating panel to the trigger.
 */
function Root({
  children,
  control
}) {
  const defaultControl = (0, _context.useDialogControl)();
  const triggerRef = (0, _react.useRef)(null);
  const [isOpen, setIsOpen] = (0, _react.useState)(false);
  const activeControl = control ?? defaultControl;

  // Wrap the control so `isOpen` reflects open/close on native too.
  const wrappedControl = (0, _react.useMemo)(() => ({
    ...activeControl,
    open: () => {
      setIsOpen(true);
      activeControl.open();
    },
    close: cb => {
      setIsOpen(false);
      activeControl.close(cb);
    }
  }), [activeControl]);
  const context = (0, _react.useMemo)(() => ({
    control: wrappedControl,
    isOpen,
    triggerRef
  }), [wrappedControl, isOpen]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_context2.PopoverContext.Provider, {
    value: context,
    children: children
  });
}
function Trigger({
  children,
  label
}) {
  const {
    control,
    triggerRef
  } = (0, _context2.usePopoverContext)();
  const [focused, setFocused] = (0, _react.useState)(false);
  const [pressed, setPressed] = (0, _react.useState)(false);
  const rendered = children({
    control,
    state: {
      hovered: false,
      focused,
      pressed
    },
    props: {
      onPress: () => control.open(),
      onFocus: () => setFocused(true),
      onBlur: () => setFocused(false),
      accessibilityLabel: label,
      accessibilityRole: 'button',
      'aria-haspopup': 'dialog'
    }
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
    ref: triggerRef,
    collapsable: false,
    children: rendered
  });
}
function Content({
  children,
  label = 'Popover',
  style
}) {
  const {
    control
  } = (0, _context2.usePopoverContext)();
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_SheetShell.SheetShell, {
    control: control,
    label: label,
    contentStyle: style,
    children: children
  });
}
//# sourceMappingURL=index.js.map