import React, { forwardRef, useId } from 'react';
import classNames from 'classnames';
import { faChevronDown } from '@fortawesome/free-solid-svg-icons/faChevronDown';
import { FontAwesomeIcon } from '../font-awesome-icon';
import { TruncateWithTooltip } from '../truncate-with-tooltip';
import { Tooltip, TOOLTIP_DELAY_TYPES } from '../tooltip';
import { ACCENT_TYPES, BUTTON_TYPES, Button } from '../button';
import { useDropdownTrigger } from './use-dropdown-trigger';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const DefaultTriggerContent = ({
  selectedItem,
  defaultTriggerText
}) => {
  const id = useId();
  const title = selectedItem?.title ?? defaultTriggerText;
  const key = typeof title === 'string' && title.length ? title : id;
  return /*#__PURE__*/_jsx(TruncateWithTooltip, {
    children: /*#__PURE__*/_jsx("span", {
      children: title
    })
  }, key);
};
const DropdownMenuTrigger = /*#__PURE__*/forwardRef(({
  onClick,
  isOpen,
  isDisabled,
  hasErrored,
  hasSuccess,
  hasWarnings,
  selectedItem,
  triggerOptions = {}
}, ref) => {
  const {
    labelText,
    defaultTriggerText,
    triggerId,
    triggerTestId,
    triggerTourId,
    selectedItemTestId,
    triggerTooltip,
    triggerTooltipPlacement,
    triggerDynamicContentWrapperClassName = 'selected-item',
    triggerDynamicContent,
    triggerOnClickHandler,
    triggerClassName,
    triggerIsIcon,
    triggerHideChevron,
    triggerTooltipDocLink,
    triggerAriaLabelText,
    dropdownContentId,
    triggerTooltipDelayType = TOOLTIP_DELAY_TYPES.SLOW,
    triggerAccentType = ACCENT_TYPES.COMMAND,
    triggerTabIndex = 0,
    triggerButtonSize
  } = triggerOptions;
  const {
    onMouseEnter,
    onMouseLeave,
    onMouseClick,
    onKeyDown
  } = useDropdownTrigger({
    onClick,
    triggerOnClickHandler,
    isOpen
  });
  return /*#__PURE__*/_jsxs(Button, {
    accentType: triggerIsIcon ? ACCENT_TYPES.ROUND_ICON : triggerAccentType,
    type: BUTTON_TYPES.BUTTON,
    ref: ref,
    className: classNames('dropdown-trigger dropdown-menu-trigger', triggerClassName, {
      active: isOpen,
      error: hasErrored,
      success: hasSuccess,
      warning: hasWarnings
    }),
    "tour-id": triggerTourId,
    onMouseEnter: onMouseEnter,
    onMouseLeave: onMouseLeave,
    onClick: onMouseClick,
    onKeyDown: onKeyDown,
    "aria-haspopup": "menu",
    "aria-expanded": isOpen,
    "aria-label": triggerAriaLabelText ?? undefined
    // used by a screen reader to announce menu content if menu has no focus (opened with mouse click)
    ,
    ...(isOpen && {
      'aria-owns': dropdownContentId,
      'aria-controls': dropdownContentId
    }),
    isDisabled: isDisabled,
    testId: triggerTestId,
    id: triggerId,
    hasCustomButtonContent: true,
    tabIndex: triggerTabIndex,
    size: triggerButtonSize,
    children: [labelText && /*#__PURE__*/_jsx("span", {
      className: "dropdown-trigger-label",
      children: labelText
    }), /*#__PURE__*/_jsx(Tooltip, {
      content: triggerTooltip,
      placement: triggerTooltipPlacement,
      delayType: triggerTooltipDelayType,
      docsLink: triggerTooltipDocLink,
      children: /*#__PURE__*/_jsxs("span", {
        className: triggerDynamicContentWrapperClassName,
        "test-id": selectedItemTestId,
        children: [typeof triggerDynamicContent === 'function' ? triggerDynamicContent({
          isOpen
        }) : /*#__PURE__*/_jsx(DefaultTriggerContent, {
          selectedItem: selectedItem,
          defaultTriggerText: defaultTriggerText
        }), !triggerHideChevron ? /*#__PURE__*/_jsx(FontAwesomeIcon, {
          icon: faChevronDown,
          "test-id": "chevron-down",
          className: classNames('trigger-chevron-down', {
            expanded: isOpen
          })
        }) : null]
      })
    })]
  });
});
DropdownMenuTrigger.displayName = 'DropdownMenuTrigger';
export default DropdownMenuTrigger;