'use client';

import * as React from 'react';
import PropTypes from 'prop-types';
import { FloatingFocusManager } from '@floating-ui/react';
import { useSelectRootContext } from '../root/SelectRootContext.js';
import { popupStateMapping } from '../../utils/popupStateMapping.js';
import { useComponentRenderer } from '../../utils/useComponentRenderer.js';
import { useForkRef } from '../../utils/useForkRef.js';
import { useSelectPopup } from './useSelectPopup.js';
import { useSelectPositionerContext } from '../positioner/SelectPositionerContext.js';
import { mergeProps } from '../../merge-props/index.js';
import { transitionStatusMapping } from '../../utils/styleHookMapping.js';
import { useOpenChangeComplete } from '../../utils/useOpenChangeComplete.js';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const customStyleHookMapping = {
  ...popupStateMapping,
  ...transitionStatusMapping
};

/**
 * A container for the select items.
 * Renders a `<div>` element.
 *
 * Documentation: [Base UI Select](https://base-ui.com/react/components/select)
 */
const SelectPopup = /*#__PURE__*/React.forwardRef(function SelectPopup(props, forwardedRef) {
  const {
    render,
    className,
    ...otherProps
  } = props;
  const {
    id,
    open,
    popupRef,
    transitionStatus,
    alignItemToTrigger,
    mounted,
    modal,
    onOpenChangeComplete
  } = useSelectRootContext();
  const positioner = useSelectPositionerContext();
  useOpenChangeComplete({
    open,
    ref: popupRef,
    onComplete() {
      if (open) {
        onOpenChangeComplete?.(true);
      }
    }
  });
  const {
    getPopupProps
  } = useSelectPopup();
  const mergedRef = useForkRef(forwardedRef, popupRef);
  const state = React.useMemo(() => ({
    open,
    transitionStatus,
    side: positioner.side,
    align: positioner.align
  }), [open, transitionStatus, positioner]);
  const {
    renderElement
  } = useComponentRenderer({
    propGetter: getPopupProps,
    render: render ?? 'div',
    ref: mergedRef,
    className,
    state,
    customStyleHookMapping,
    extraProps: transitionStatus === 'starting' ? mergeProps({
      style: {
        transition: 'none'
      }
    }, otherProps) : otherProps
  });
  const popupSelector = `[data-id="${id}-popup"]`;
  const html = React.useMemo(() => ({
    __html: `${popupSelector}{scrollbar-width:none}${popupSelector}::-webkit-scrollbar{display:none}`
  }), [popupSelector]);
  return /*#__PURE__*/_jsxs(React.Fragment, {
    children: [id && alignItemToTrigger && /*#__PURE__*/_jsx("style", {
      // eslint-disable-next-line react/no-danger
      dangerouslySetInnerHTML: html
    }), /*#__PURE__*/_jsx(FloatingFocusManager, {
      context: positioner.context,
      modal: false,
      disabled: !mounted,
      visuallyHiddenDismiss: modal ? 'Dismiss popup' : undefined,
      children: renderElement()
    })]
  });
});
process.env.NODE_ENV !== "production" ? SelectPopup.propTypes /* remove-proptypes */ = {
  // ┌────────────────────────────── Warning ──────────────────────────────┐
  // │ These PropTypes are generated from the TypeScript type definitions. │
  // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
  // └─────────────────────────────────────────────────────────────────────┘
  /**
   * @ignore
   */
  children: PropTypes.node,
  /**
   * CSS class applied to the element, or a function that
   * returns a class based on the component’s state.
   */
  className: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
  /**
   * @ignore
   */
  id: PropTypes.string,
  /**
   * Allows you to replace the component’s HTML element
   * with a different tag, or compose it with another component.
   *
   * Accepts a `ReactElement` or a function that returns the element to render.
   */
  render: PropTypes.oneOfType([PropTypes.element, PropTypes.func])
} : void 0;
export { SelectPopup };