"use strict";

import React, { useMemo, useRef, useState } from 'react';
import { View } from 'react-native';
import { useDialogControl } from "../dialog/context.js";
import { SheetShell } from "../dialog/SheetShell.js";
import { PopoverContext, usePopoverContext } from "./context.js";
import { jsx as _jsx } from "react/jsx-runtime";
export { useDialogControl as usePopoverControl } from "../dialog/context.js";
export { usePopoverContext };

/**
 * 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.
 */
export function Root({
  children,
  control
}) {
  const defaultControl = useDialogControl();
  const triggerRef = useRef(null);
  const [isOpen, setIsOpen] = useState(false);
  const activeControl = control ?? defaultControl;

  // Wrap the control so `isOpen` reflects open/close on native too.
  const wrappedControl = useMemo(() => ({
    ...activeControl,
    open: () => {
      setIsOpen(true);
      activeControl.open();
    },
    close: cb => {
      setIsOpen(false);
      activeControl.close(cb);
    }
  }), [activeControl]);
  const context = useMemo(() => ({
    control: wrappedControl,
    isOpen,
    triggerRef
  }), [wrappedControl, isOpen]);
  return /*#__PURE__*/_jsx(PopoverContext.Provider, {
    value: context,
    children: children
  });
}
export function Trigger({
  children,
  label
}) {
  const {
    control,
    triggerRef
  } = usePopoverContext();
  const [focused, setFocused] = useState(false);
  const [pressed, setPressed] = 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__*/_jsx(View, {
    ref: triggerRef,
    collapsable: false,
    children: rendered
  });
}
export function Content({
  children,
  label = 'Popover',
  style
}) {
  const {
    control
  } = usePopoverContext();
  return /*#__PURE__*/_jsx(SheetShell, {
    control: control,
    label: label,
    contentStyle: style,
    children: children
  });
}
//# sourceMappingURL=index.js.map