"use client";
import * as React from "react";
import { useRenderElement } from "../../utils/useRenderElement.js";
import { useComboboxRootContext, useComboboxFloatingContext, useComboboxDerivedItemsContext } from "../root/ComboboxRootContext.js";
import { selectors } from "../store.js";
import { popupStateMapping } from "../../utils/popupStateMapping.js";
import { useComboboxPositionerContext } from "../positioner/ComboboxPositionerContext.js";
import { useOpenChangeComplete } from "../../utils/useOpenChangeComplete.js";
import { transitionStatusMapping } from "../../utils/stateAttributesMapping.js";
import { getDisabledMountTransitionStyles } from "../../utils/getDisabledMountTransitionStyles.js";
import { jsx } from "react/jsx-runtime";
import { FloatingFocusManager } from "../../floating-ui-react/components/FloatingFocusManager.js";
import { useStore } from "../../../../utils/esm/store/useStore.js";
import { getTarget, contains } from "../../floating-ui-react/utils/element.js";
const stateAttributesMapping = {
  ...popupStateMapping,
  ...transitionStatusMapping
};
const ComboboxPopup = /* @__PURE__ */ React.forwardRef(function ComboboxPopup2(componentProps, forwardedRef) {
  const {
    render,
    className,
    initialFocus,
    finalFocus,
    ...elementProps
  } = componentProps;
  const store = useComboboxRootContext();
  const positioning = useComboboxPositionerContext();
  const floatingRootContext = useComboboxFloatingContext();
  const {
    filteredItems
  } = useComboboxDerivedItemsContext();
  const mounted = useStore(store, selectors.mounted);
  const open = useStore(store, selectors.open);
  const openMethod = useStore(store, selectors.openMethod);
  const transitionStatus = useStore(store, selectors.transitionStatus);
  const inputInsidePopup = useStore(store, selectors.inputInsidePopup);
  const inputElement = useStore(store, selectors.inputElement);
  const empty = filteredItems.length === 0;
  useOpenChangeComplete({
    open,
    ref: store.state.popupRef,
    onComplete() {
      if (open) {
        store.state.onOpenChangeComplete(true);
      }
    }
  });
  const state = React.useMemo(() => ({
    open,
    side: positioning.side,
    align: positioning.align,
    anchorHidden: positioning.anchorHidden,
    transitionStatus,
    empty
  }), [open, positioning.side, positioning.align, positioning.anchorHidden, transitionStatus, empty]);
  const element = useRenderElement("div", componentProps, {
    state,
    ref: [forwardedRef, store.state.popupRef],
    props: [{
      role: inputInsidePopup ? "dialog" : "presentation",
      tabIndex: -1,
      onFocus(event) {
        const target = getTarget(event.nativeEvent);
        if (openMethod !== "touch" && (contains(store.state.listElement, target) || target === event.currentTarget)) {
          store.state.inputRef.current?.focus();
        }
      }
    }, getDisabledMountTransitionStyles(transitionStatus), elementProps],
    stateAttributesMapping
  });
  const computedDefaultInitialFocus = inputInsidePopup ? (interactionType) => interactionType === "touch" ? store.state.popupRef.current : inputElement : false;
  const resolvedInitialFocus = initialFocus === void 0 ? computedDefaultInitialFocus : initialFocus;
  let resolvedFinalFocus;
  if (finalFocus != null) {
    resolvedFinalFocus = finalFocus;
  } else {
    resolvedFinalFocus = inputInsidePopup ? void 0 : false;
  }
  return /* @__PURE__ */ jsx(FloatingFocusManager, {
    context: floatingRootContext,
    disabled: !mounted,
    modal: !inputInsidePopup,
    openInteractionType: openMethod,
    initialFocus: resolvedInitialFocus,
    returnFocus: resolvedFinalFocus,
    children: element
  });
});
if (process.env.NODE_ENV !== "production") ComboboxPopup.displayName = "ComboboxPopup";
export {
  ComboboxPopup
};
//# sourceMappingURL=ComboboxPopup.js.map
