"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 _tooltip = require("../tooltip");
var _truncateWithTooltip = require("../truncate-with-tooltip");
var _types = require("./types");
var _button = require("../button");
var _dropdownItemTitle = _interopRequireDefault(require("./dropdown-item-title"));
var _formField = require("../form-field");
var _useDropdownTrigger = require("./use-dropdown-trigger");
var _useTranslation = require("../hooks/use-translation");
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 = function ({
  options = [],
  selectedItem,
  defaultTriggerText,
  isLarge = false
}) {
  if (selectedItem && isLarge) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdownItemTitle.default, {
      option: selectedItem
    });
  }
  const triggerContent = getGenericTriggerContent(selectedItem, options);
  const key = selectedItem?.key;
  if (triggerContent) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: "selected-item-details",
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_truncateWithTooltip.TruncateWithTooltip, {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
          children: triggerContent
        })
      }, key)
    });
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_truncateWithTooltip.TruncateWithTooltip, {
    className: "placeholder",
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      children: defaultTriggerText
    })
  }, key);
};
function getGenericTriggerContent(selectedItem, options) {
  const selectedOptions = options.filter(option => option.isChecked).map(option => option.title);
  if (selectedItem) {
    return selectedItem.title;
  }
  if (selectedOptions.length) {
    return selectedOptions.join(', ');
  }
  return null;
}
const DropdownSelectboxTrigger = /*#__PURE__*/(0, _react.forwardRef)(({
  hasErrored,
  hasSuccess,
  hasWarnings,
  isOpen,
  isDisabled,
  isOptional,
  optionalLabelText,
  onClick,
  options,
  selectedItem,
  triggerOptions,
  isSectioned = false,
  selectbox
}, ref) => {
  const {
    t
  } = (0, _useTranslation.useTranslation)();
  const {
    defaultTriggerText,
    labelText,
    helperText,
    triggerId,
    triggerTestId,
    triggerTooltip,
    triggerAriaLabelText,
    triggerTooltipPlacement,
    triggerDynamicContent,
    triggerOnClickHandler = () => {},
    triggerClassName,
    triggerTooltipDocLink,
    required,
    dropdownContentId,
    triggerTabIndex = 0
  } = triggerOptions || {};
  const {
    onMouseEnter,
    onMouseLeave,
    onMouseClick,
    onKeyDown
  } = (0, _useDropdownTrigger.useDropdownTrigger)({
    onClick,
    triggerOnClickHandler,
    isOpen
  });
  const buttonAriaLabelledBy = [labelText ? (0, _formField.getFormFieldLabelId)(triggerId) : '',
  // announce label if exists
  `selected-item-${triggerId}` // announce selected item content
  ].filter(Boolean).join(' ');
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: "selectbox",
    children: [labelText ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_formField.FormFieldLabel, {
      id: triggerId,
      label: labelText,
      isOptional: isOptional,
      optionalLabelText: optionalLabelText ?? t('Optional'),
      isDisabled: isDisabled,
      isRequired: required
    }) : null, helperText && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: (0, _classnames.default)('helper-text', isDisabled && 'disabled'),
      children: helperText
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
      type: _button.BUTTON_TYPES.BUTTON,
      ref: ref,
      onMouseEnter: onMouseEnter,
      onMouseLeave: onMouseLeave,
      onClick: onMouseClick,
      onKeyDown: onKeyDown,
      "aria-haspopup": "menu",
      "aria-expanded": isOpen,
      "aria-label": triggerAriaLabelText ?? undefined,
      "aria-labelledby": buttonAriaLabelledBy
      // 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
      }),
      disabled: isDisabled,
      "test-id": triggerTestId,
      id: triggerId,
      className: (0, _classnames.default)('dropdown-trigger dropdown-selectbox-trigger', triggerClassName, {
        active: isOpen,
        sm: selectbox?.size === _types.DROPDOWN_MENU_SELECTBOX_SIZES.SM,
        lg: selectbox?.size === _types.DROPDOWN_MENU_SELECTBOX_SIZES.LG,
        xl: selectbox?.size === _types.DROPDOWN_MENU_SELECTBOX_SIZES.XL,
        error: hasErrored,
        success: hasSuccess,
        warnings: hasWarnings,
        sectioned: isSectioned
      }),
      tabIndex: triggerTabIndex,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_tooltip.Tooltip, {
        content: triggerTooltip,
        placement: triggerTooltipPlacement,
        delayType: _tooltip.TOOLTIP_DELAY_TYPES.SLOW,
        docsLink: triggerTooltipDocLink,
        children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
          id: `selected-item-${triggerId}`,
          className: "selected-item",
          children: [typeof triggerDynamicContent === 'function' ? triggerDynamicContent({
            isOpen
          }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(DefaultTriggerContent, {
            options: options,
            selectedItem: selectedItem,
            defaultTriggerText: defaultTriggerText,
            isLarge: selectbox?.size === _types.DROPDOWN_MENU_SELECTBOX_SIZES.XL || selectbox?.size === _types.DROPDOWN_MENU_SELECTBOX_SIZES.LG
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_fontAwesomeIcon.FontAwesomeIcon, {
            icon: _faChevronDown.faChevronDown,
            className: (0, _classnames.default)('trigger-chevron-down', {
              expanded: isOpen
            })
          })]
        })
      })
    })]
  });
});
DropdownSelectboxTrigger.displayName = 'DropdownSelectboxTrigger';
var _default = exports.default = DropdownSelectboxTrigger;