import React, { useMemo, useState } from 'react';
import classNames from 'classnames';
import { Dropdown, DROPDOWN_PLACEMENT_TYPES } from '../dropdown';
import { DropdownMenuContent } from '../dropdown-menu';
import { DefaultLinkComponent } from '../dropdown-menu/default-link-component';
import { ACCENT_TYPES } from '../button';
import { TreeNavigationProvider, useReturnFocusOnClose } from '../hooks';
import { getMenuTreeNavigationAdapter } from '../dropdown-menu/dropdown-menu-content';
import { getOptionsWithPrivateKey, getFlattenedSubsectionOptions, getFooterOptionWithPrivateKey } from '../dropdown-menu/utils';
import DropdownButtonTrigger from './dropdown-button-trigger';
import { jsx as _jsx } from "react/jsx-runtime";
const handleSearch = () => {};
/**
  @midnight-gray-supported
  @alpine-light-supported
 */
function DropdownButton({
  options: originalOptions,
  label,
  onDropdownSelect,
  className,
  buttonTestId,
  contentTestId = 'contentTestId',
  isDisabled = false,
  placement = DROPDOWN_PLACEMENT_TYPES.BOTTOM_END,
  buttonProps = {},
  dropdownMenuContentProps = {},
  dropdownProps = {},
  buttonAccentType = ACCENT_TYPES.PRIMARY,
  isMultiLevel = false,
  isMultiLevelSectioned = false,
  LinkComponent = DefaultLinkComponent
}) {
  const [isMenuOpen, setIsMenuOpen] = useState(false);
  const [isKeyboardNavigationDisabled, setIsKeyboardNavigationDisabled] = useState(true);

  // last focused element is not limited to only trigger, it can also be a table cell
  useReturnFocusOnClose({
    isShown: isMenuOpen
  });
  const adapter = useMemo(() => getMenuTreeNavigationAdapter({
    isExpandedByDefault: false
  }), []);
  const options = useMemo(() => getOptionsWithPrivateKey(originalOptions), [originalOptions]);
  const footerOption = useMemo(() => dropdownMenuContentProps.footerOption ? getFooterOptionWithPrivateKey(dropdownMenuContentProps.footerOption) : null, [dropdownMenuContentProps]);
  const treeNavigationOptions = useMemo(() => {
    const navigatebleleOptions = !dropdownMenuContentProps.isSectioned ? options : 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__*/_jsx("div", {
    className: classNames('dropdown-button-container', className),
    children: /*#__PURE__*/_jsx(TreeNavigationProvider, {
      adapter: adapter,
      tree: treeNavigationOptions,
      isDisabled: isKeyboardNavigationDisabled,
      children: /*#__PURE__*/_jsx(Dropdown, {
        placement: placement,
        hasCustomFocusHandling: true,
        onOpen: openedWithKeyboard => {
          setIsMenuOpen(openedWithKeyboard);
        },
        ...dropdownProps,
        onClose: () => {
          setIsKeyboardNavigationDisabled(true);
          dropdownProps.onClose?.();
        },
        renderAnchor: ({
          ref,
          onClick,
          isOpen
        }) => /*#__PURE__*/_jsx(DropdownButtonTrigger, {
          ref: ref,
          onClick: onClick,
          isOpen: isOpen,
          testId: buttonTestId,
          onKeyboardClick: () => {
            setIsKeyboardNavigationDisabled(false);
          },
          isDisabled: isDisabled,
          accentType: buttonAccentType,
          buttonProps: buttonProps,
          children: label
        }),
        children: ({
          closeDropdown
        }) => /*#__PURE__*/_jsx(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
        })
      })
    })
  });
}
export default DropdownButton;