"use client";
import * as React from "react";
import { useScrollLock } from "../../../../utils/esm/useScrollLock.js";
import { usePopoverRootContext, PopoverRootContext } from "./PopoverRootContext.js";
import { PopoverStore } from "../store/PopoverStore.js";
import { createChangeEventDetails } from "../../utils/createBaseUIEventDetails.js";
import { useOpenInteractionType } from "../../utils/useOpenInteractionType.js";
import { jsx } from "react/jsx-runtime";
import { FloatingTree, useFloatingParentNodeId } from "../../floating-ui-react/components/FloatingTree.js";
import { useSyncedFloatingRootContext } from "../../floating-ui-react/hooks/useSyncedFloatingRootContext.js";
import { useDismiss } from "../../floating-ui-react/hooks/useDismiss.js";
import { useRole } from "../../floating-ui-react/hooks/useRole.js";
import { useInteractions } from "../../floating-ui-react/hooks/useInteractions.js";
import { useImplicitActiveTrigger, useOpenStateTransitions } from "../../utils/popups/popupStoreUtils.js";
import { triggerHover, imperativeAction } from "../../utils/reason-parts.js";
function PopoverRootComponent({
  props
}) {
  const {
    children,
    open: openProp,
    defaultOpen: defaultOpenProp = false,
    onOpenChange,
    onOpenChangeComplete,
    modal = false,
    handle,
    triggerId: triggerIdProp,
    defaultTriggerId: defaultTriggerIdProp = null
  } = props;
  const store = PopoverStore.useStore(handle?.store, {
    open: openProp ?? defaultOpenProp,
    modal,
    activeTriggerId: triggerIdProp !== void 0 ? triggerIdProp : defaultTriggerIdProp
  });
  store.useControlledProp("open", openProp, defaultOpenProp);
  store.useControlledProp("activeTriggerId", triggerIdProp, defaultTriggerIdProp);
  const open = store.useState("open");
  const positionerElement = store.useState("positionerElement");
  const payload = store.useState("payload");
  const openReason = store.useState("openChangeReason");
  store.useContextCallback("onOpenChange", onOpenChange);
  store.useContextCallback("onOpenChangeComplete", onOpenChangeComplete);
  const {
    openMethod,
    triggerProps: interactionTypeTriggerProps,
    reset: resetOpenInteractionType
  } = useOpenInteractionType(open);
  useImplicitActiveTrigger(store);
  const {
    forceUnmount
  } = useOpenStateTransitions(open, store, () => {
    store.update({
      stickIfOpen: true,
      openChangeReason: null
    });
    resetOpenInteractionType();
  });
  useScrollLock(open && modal === true && openReason !== triggerHover && openMethod !== "touch", positionerElement);
  React.useEffect(() => {
    if (!open) {
      store.context.stickIfOpenTimeout.clear();
    }
  }, [store, open]);
  const createPopoverEventDetails = React.useCallback((reason) => {
    const details = createChangeEventDetails(reason);
    details.preventUnmountOnClose = () => {
      store.set("preventUnmountingOnClose", true);
    };
    return details;
  }, [store]);
  const handleImperativeClose = React.useCallback(() => {
    store.setOpen(false, createPopoverEventDetails(imperativeAction));
  }, [store, createPopoverEventDetails]);
  React.useImperativeHandle(props.actionsRef, () => ({
    unmount: forceUnmount,
    close: handleImperativeClose
  }), [forceUnmount, handleImperativeClose]);
  const floatingRootContext = useSyncedFloatingRootContext({
    popupStore: store,
    onOpenChange: store.setOpen
  });
  const dismiss = useDismiss(floatingRootContext, {
    outsidePressEvent: {
      // Ensure `aria-hidden` on outside elements is removed immediately
      // on outside press when trapping focus.
      mouse: modal === "trap-focus" ? "sloppy" : "intentional",
      touch: "sloppy"
    }
  });
  const role = useRole(floatingRootContext);
  const {
    getReferenceProps,
    getFloatingProps,
    getTriggerProps
  } = useInteractions([dismiss, role]);
  const activeTriggerProps = React.useMemo(() => {
    return getReferenceProps(interactionTypeTriggerProps);
  }, [getReferenceProps, interactionTypeTriggerProps]);
  const inactiveTriggerProps = React.useMemo(() => {
    return getTriggerProps(interactionTypeTriggerProps);
  }, [getTriggerProps, interactionTypeTriggerProps]);
  const popupProps = React.useMemo(() => {
    return getFloatingProps();
  }, [getFloatingProps]);
  store.useSyncedValues({
    modal,
    openMethod,
    activeTriggerProps,
    inactiveTriggerProps,
    popupProps,
    floatingRootContext,
    nested: useFloatingParentNodeId() != null
  });
  const popoverContext = React.useMemo(() => ({
    store
  }), [store]);
  return /* @__PURE__ */ jsx(PopoverRootContext.Provider, {
    value: popoverContext,
    children: typeof children === "function" ? children({
      payload
    }) : children
  });
}
function PopoverRoot(props) {
  if (usePopoverRootContext(true)) {
    return /* @__PURE__ */ jsx(PopoverRootComponent, {
      props
    });
  }
  return /* @__PURE__ */ jsx(FloatingTree, {
    children: /* @__PURE__ */ jsx(PopoverRootComponent, {
      props
    })
  });
}
export {
  PopoverRoot
};
//# sourceMappingURL=PopoverRoot.js.map
