"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _faChevronDown = require("@fortawesome/free-solid-svg-icons/faChevronDown");
var _fontAwesomeIcon = require("../font-awesome-icon");
var _truncateWithTooltip = require("../truncate-with-tooltip");
var _tooltip = require("../tooltip");
var _button = require("../button");
var _useDropdownTrigger = require("./use-dropdown-trigger");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const DefaultTriggerContent = ({
  selectedItem,
  defaultTriggerText
}) => {
  const id = (0, _react.useId)();
  const title = selectedItem?.title ?? defaultTriggerText;
  const key = typeof title === 'string' && title.length ? title : id;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_truncateWithTooltip.TruncateWithTooltip, {
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      children: title
    })
  }, key);
};
const DropdownMenuTrigger = /*#__PURE__*/(0, _react.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.TOOLTIP_DELAY_TYPES.SLOW,
    triggerAccentType = _button.ACCENT_TYPES.COMMAND,
    triggerTabIndex = 0,
    triggerButtonSize
  } = triggerOptions;
  const {
    onMouseEnter,
    onMouseLeave,
    onMouseClick,
    onKeyDown
  } = (0, _useDropdownTrigger.useDropdownTrigger)({
    onClick,
    triggerOnClickHandler,
    isOpen
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_button.Button, {
    accentType: triggerIsIcon ? _button.ACCENT_TYPES.ROUND_ICON : triggerAccentType,
    type: _button.BUTTON_TYPES.BUTTON,
    ref: ref,
    className: (0, _classnames.default)('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__*/(0, _jsxRuntime.jsx)("span", {
      className: "dropdown-trigger-label",
      children: labelText
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_tooltip.Tooltip, {
      content: triggerTooltip,
      placement: triggerTooltipPlacement,
      delayType: triggerTooltipDelayType,
      docsLink: triggerTooltipDocLink,
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
        className: triggerDynamicContentWrapperClassName,
        "test-id": selectedItemTestId,
        children: [typeof triggerDynamicContent === 'function' ? triggerDynamicContent({
          isOpen
        }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(DefaultTriggerContent, {
          selectedItem: selectedItem,
          defaultTriggerText: defaultTriggerText
        }), !triggerHideChevron ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_fontAwesomeIcon.FontAwesomeIcon, {
          icon: _faChevronDown.faChevronDown,
          "test-id": "chevron-down",
          className: (0, _classnames.default)('trigger-chevron-down', {
            expanded: isOpen
          })
        }) : null]
      })
    })]
  });
});
DropdownMenuTrigger.displayName = 'DropdownMenuTrigger';
var _default = exports.default = DropdownMenuTrigger;