"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _dropdown = require("../dropdown");
var _dropdownMenu = require("../dropdown-menu");
var _defaultLinkComponent = require("../dropdown-menu/default-link-component");
var _button = require("../button");
var _hooks = require("../hooks");
var _dropdownMenuContent = require("../dropdown-menu/dropdown-menu-content");
var _utils = require("../dropdown-menu/utils");
var _dropdownButtonTrigger = _interopRequireDefault(require("./dropdown-button-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 handleSearch = () => {};
/**
  @midnight-gray-supported
  @alpine-light-supported
 */
function DropdownButton({
  options: originalOptions,
  label,
  onDropdownSelect,
  className,
  buttonTestId,
  contentTestId = 'contentTestId',
  isDisabled = false,
  placement = _dropdown.DROPDOWN_PLACEMENT_TYPES.BOTTOM_END,
  buttonProps = {},
  dropdownMenuContentProps = {},
  dropdownProps = {},
  buttonAccentType = _button.ACCENT_TYPES.PRIMARY,
  isMultiLevel = false,
  isMultiLevelSectioned = false,
  LinkComponent = _defaultLinkComponent.DefaultLinkComponent
}) {
  const [isMenuOpen, setIsMenuOpen] = (0, _react.useState)(false);
  const [isKeyboardNavigationDisabled, setIsKeyboardNavigationDisabled] = (0, _react.useState)(true);

  // last focused element is not limited to only trigger, it can also be a table cell
  (0, _hooks.useReturnFocusOnClose)({
    isShown: isMenuOpen
  });
  const adapter = (0, _react.useMemo)(() => (0, _dropdownMenuContent.getMenuTreeNavigationAdapter)({
    isExpandedByDefault: false
  }), []);
  const options = (0, _react.useMemo)(() => (0, _utils.getOptionsWithPrivateKey)(originalOptions), [originalOptions]);
  const footerOption = (0, _react.useMemo)(() => dropdownMenuContentProps.footerOption ? (0, _utils.getFooterOptionWithPrivateKey)(dropdownMenuContentProps.footerOption) : null, [dropdownMenuContentProps]);
  const treeNavigationOptions = (0, _react.useMemo)(() => {
    const navigatebleleOptions = !dropdownMenuContentProps.isSectioned ? options : (0, _utils.getFlattenedSubsectionOptions)(options);
    const treeOptions = footerOption ? [...navigatebleleOptions, footerOption] : navigatebleleOptions;
    return treeOptions;
  }, [dropdownMenuContentProps, options, footerOption, isMenuOpen]);
  const onSelectWrapper = (option, files) => {
    if (option?.privateKey) {
      // eslint-disable-next-line @typescript-eslint/no-unused-vars
      const {
        privateKey,
        ...omittedOption
      } = option;
      if (files) {
        return onDropdownSelect(omittedOption, files);
      }
      return onDropdownSelect(omittedOption);
    }
    return onDropdownSelect(option);
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    className: (0, _classnames.default)('dropdown-button-container', className),
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_hooks.TreeNavigationProvider, {
      adapter: adapter,
      tree: treeNavigationOptions,
      isDisabled: isKeyboardNavigationDisabled,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdown.Dropdown, {
        placement: placement,
        hasCustomFocusHandling: true,
        onOpen: openedWithKeyboard => {
          setIsMenuOpen(openedWithKeyboard);
        },
        ...dropdownProps,
        onClose: () => {
          setIsKeyboardNavigationDisabled(true);
          dropdownProps.onClose?.();
        },
        renderAnchor: ({
          ref,
          onClick,
          isOpen
        }) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdownButtonTrigger.default, {
          ref: ref,
          onClick: onClick,
          isOpen: isOpen,
          testId: buttonTestId,
          onKeyboardClick: () => {
            setIsKeyboardNavigationDisabled(false);
          },
          isDisabled: isDisabled,
          accentType: buttonAccentType,
          buttonProps: buttonProps,
          children: label
        }),
        children: ({
          closeDropdown
        }) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdownMenu.DropdownMenuContent, {
          onSelect: onSelectWrapper,
          options: options,
          isMultiLevel: isMultiLevel,
          isMultiLevelSectioned: isMultiLevelSectioned,
          closeDropdown: (...args) => {
            closeDropdown(...args);
            setIsMenuOpen(false);
          },
          isSectioned: false,
          showNoResults: false,
          renderCustomHeader: null,
          handleSearch: handleSearch,
          contentTestId: contentTestId,
          labelledBy: buttonTestId,
          ...dropdownMenuContentProps,
          footerOption: footerOption,
          LinkComponent: LinkComponent
        })
      })
    })
  });
}
var _default = exports.default = DropdownButton;