import React, { useRef, useState, useEffect } from 'react';
import classname from 'classnames';
import { PopperDropdownWrapper } from '../dropdown';
import DropdownItem from './dropdown-item';
import { useTreeNodeNavigation } from '../hooks';
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
const DropdownMenuMultiLevelOptions = ({
  closeDropdown,
  option,
  placement = 'right-start',
  onSelect,
  selectedOption,
  onMouseEnter,
  onMouseLeave,
  handleSubLevelClose,
  state,
  LinkComponent
}) => {
  const menuItemRef = useRef(null);
  const preventOuterMouseLeave = useRef(false);
  const [isOpen, setIsOpen] = useState(option.level === state.level ? state.isSubLevelOpen : false);
  const selectedKey = selectedOption && selectedOption.key;
  const handleMouseEnter = () => {
    return onMouseEnter(option.level);
  };
  const handleMouseLeave = () => {
    return onMouseLeave(option.level);
  };
  const handleSubItemsMouseEnter = () => {
    menuItemRef?.current?.classList.add('hovered-item');
    return onMouseEnter(option.level);
  };
  const handleSubItemsMouseLeave = () => {
    menuItemRef.current?.classList.remove('hovered-item');
    if (!preventOuterMouseLeave.current) {
      return onMouseLeave(option.level);
    }
  };
  const handleCloseDropdown = () => {
    closeDropdown();
  };
  const {
    node
  } = useTreeNodeNavigation({
    id: option.privateKey || option.key,
    onToggleExpanded: isExpanded => {
      setIsOpen(isExpanded);
    }
  });
  useEffect(() => {
    setIsOpen(option.level === state.level ? state.isSubLevelOpen : node.isExpanded);
    if (option.subItems && option.subItems.some(val => val.isFileUpload)) {
      preventOuterMouseLeave.current = true;
    }
  }, [option, state, node]);
  return /*#__PURE__*/_jsxs(_Fragment, {
    children: [/*#__PURE__*/_jsx(DropdownItem, {
      option: option,
      ref: menuItemRef,
      onCloseDropdown: closeDropdown,
      onMouseEnter: handleMouseEnter,
      onMouseLeave: handleMouseLeave,
      onSelect: onSelect,
      selectedKey: selectedKey,
      isMultiLevelHandler: !!option.subItems,
      LinkComponent: LinkComponent
    }), option.subItems && /*#__PURE__*/_jsx("div", {
      onMouseEnter: handleSubItemsMouseEnter,
      onMouseLeave: handleSubItemsMouseLeave,
      children: /*#__PURE__*/_jsx(PopperDropdownWrapper, {
        contentClassName: classname('dropdown-content ', {
          'outside-click-listener-ignore': preventOuterMouseLeave.current
        }),
        closeDropdown: handleSubLevelClose,
        modifiers: [{
          name: 'offset',
          options: {
            offset: [-13, 0]
          }
        }],
        anchorRef: menuItemRef.current,
        open: isOpen,
        placement: placement,
        wrapperContentTestId: `${option.testId}-wrapper`,
        children: /*#__PURE__*/_jsx("ul", {
          role: "menu",
          className: "dropdown-menu-list",
          tabIndex: -1,
          children: option.subItems.map(subItem => {
            return /*#__PURE__*/_jsx(DropdownItem, {
              option: subItem,
              onSelect: onSelect,
              selectedKey: selectedKey,
              onCloseDropdown: handleCloseDropdown,
              LinkComponent: LinkComponent
            }, subItem.key);
          })
        })
      })
    })]
  });
};
export default DropdownMenuMultiLevelOptions;