"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getMenuTreeNavigationAdapter = exports.default = exports.SEARCH_ID = void 0;
var _react = _interopRequireDefault(require("react"));
var _utils = require("../hooks/use-tree-navigation/utils");
var _search = require("../search");
var _dropdownItem = _interopRequireDefault(require("./dropdown-item"));
var _dropdownList = _interopRequireDefault(require("./dropdown-list"));
var _dropdownMenuMultiLevelList = _interopRequireDefault(require("./dropdown-menu-multi-level-list"));
var _hooks = require("../hooks");
var _constants = require("../hooks/use-tree-navigation/constants");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const getMapFn = isExpandedByDefault => {
  const mapFunction = (option, parents) => {
    let treeNode;
    let children = [];
    // use isLeaf in keys to distinguish between TreeNode and DropdownMenuOption,
    // we passing TreeNode directly in case we have a search input
    if (!('isLeaf' in option)) {
      children = option.subItems || [];
      treeNode = {
        id: option.privateKey || option.key,
        label: String(option.title),
        parent: parents[0] ?? null,
        parents,
        isDisabled: option.disabled || children.length && children.every(c => c.disabled || c.isShown === false) || option.isShown === false,
        isExpanded: !!children.length && isExpandedByDefault,
        isExpandable: !!children.length,
        isFocused: false,
        isLeaf: !children.length,
        children: children.map(({
          key
        }) => key)
      };
    } else {
      // consumer may pass DropdownMenuOption without `key` property despite it being required, so adding safety measure
      treeNode = {
        ...(0, _utils.getDummyNode)(option.id),
        ...option
      };
    }
    return {
      node: treeNode,
      children
    };
  };
  return mapFunction;
};
const getMenuTreeNavigationAdapter = ({
  isExpandedByDefault
}) => {
  const mapFn = getMapFn(isExpandedByDefault);
  return {
    isExpandedByDefault,
    getFlatNodes: tree => (0, _utils.flattenTree)(tree, mapFn),
    getFocusableNodes: _utils.getFocusableNodes
  };
};
exports.getMenuTreeNavigationAdapter = getMenuTreeNavigationAdapter;
const SEARCH_ID = exports.SEARCH_ID = 'dropdown-menu-search';
const DropdownMenuContent = ({
  options,
  footerOption,
  onSelect = () => {},
  isSectioned,
  isMultiLevel = false,
  isMultiLevelSectioned = false,
  closeDropdown = () => {},
  handleSearch,
  searchId = SEARCH_ID,
  showSearch,
  searchPlaceholderText = '',
  searchAutoComplete,
  searchQuery = '',
  showNoResults,
  noResultsText = '',
  contentClassName = '',
  selectedKey,
  selectedItem,
  contentTestId,
  renderCustomHeader = null,
  labelledBy,
  LinkComponent
}) => {
  const isFooterMultiLevel = !!footerOption?.subItems?.length;
  const showFooterOption = footerOption && footerOption.isShown !== false;
  const {
    treeItemAttrs: searchAttrs,
    ref: setRefCallback
  } = (0, _hooks.useTreeNodeNavigation)({
    id: searchId,
    getKeyActionFn: _utils.getKeyActionFnSearch,
    onCustomButtonKeyDown: () => closeDropdown(),
    ignoreKeys: [_constants.KEYS.SPACE, _constants.KEYS.LEFT, _constants.KEYS.RIGHT]
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [showSearch && /*#__PURE__*/(0, _jsxRuntime.jsx)(_search.Search, {
      onChange: handleSearch,
      value: searchQuery,
      placeholder: searchPlaceholderText,
      autoComplete: searchAutoComplete,
      className: "dropdown-search",
      testId: "dropdown-menu-search",
      id: searchId,
      autoFocus: false,
      ref: setRefCallback,
      onKeyDown: searchAttrs.onKeyDown
    }), renderCustomHeader && renderCustomHeader(), showNoResults && /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: "no-results-message",
      "test-id": "dropdown-menu-no-results-message",
      children: noResultsText
    }), !showNoResults && /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdownList.default, {
      options: options,
      onCloseDropdown: closeDropdown,
      selectedKey: selectedKey,
      selectedItem: selectedItem,
      onSelect: onSelect,
      contentTestId: contentTestId,
      contentClassName: contentClassName,
      isMultiLevel: isMultiLevel,
      isMultiLevelSectioned: isMultiLevelSectioned,
      isSectioned: isSectioned,
      labelledBy: labelledBy,
      LinkComponent: LinkComponent
    }), showFooterOption && isFooterMultiLevel && /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdownMenuMultiLevelList.default, {
      closeDropdown: closeDropdown,
      selectedOption: selectedItem,
      options: [footerOption],
      onSelect: onSelect,
      contentTestId: "footer-multi-level-content",
      contentClassName: "dropdown-menu-footer",
      LinkComponent: LinkComponent
    }), showFooterOption && !isFooterMultiLevel && /*#__PURE__*/(0, _jsxRuntime.jsx)("ul", {
      className: "dropdown-menu-footer",
      role: "menu",
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdownItem.default, {
        option: footerOption,
        onSelect: onSelect,
        onCloseDropdown: closeDropdown,
        LinkComponent: LinkComponent
      })
    })]
  });
};
DropdownMenuContent.displayName = 'DropdownMenuContent';
var _default = exports.default = DropdownMenuContent;