"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 _dropdownItem = _interopRequireDefault(require("./dropdown-item"));
var _hooks = require("../hooks");
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 DropdownMenuMultiLevelOptions = ({
  closeDropdown,
  option,
  placement = 'right-start',
  onSelect,
  selectedOption,
  onMouseEnter,
  onMouseLeave,
  handleSubLevelClose,
  state,
  LinkComponent
}) => {
  const menuItemRef = (0, _react.useRef)(null);
  const preventOuterMouseLeave = (0, _react.useRef)(false);
  const [isOpen, setIsOpen] = (0, _react.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
  } = (0, _hooks.useTreeNodeNavigation)({
    id: option.privateKey || option.key,
    onToggleExpanded: isExpanded => {
      setIsOpen(isExpanded);
    }
  });
  (0, _react.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__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdownItem.default, {
      option: option,
      ref: menuItemRef,
      onCloseDropdown: closeDropdown,
      onMouseEnter: handleMouseEnter,
      onMouseLeave: handleMouseLeave,
      onSelect: onSelect,
      selectedKey: selectedKey,
      isMultiLevelHandler: !!option.subItems,
      LinkComponent: LinkComponent
    }), option.subItems && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      onMouseEnter: handleSubItemsMouseEnter,
      onMouseLeave: handleSubItemsMouseLeave,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdown.PopperDropdownWrapper, {
        contentClassName: (0, _classnames.default)('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__*/(0, _jsxRuntime.jsx)("ul", {
          role: "menu",
          className: "dropdown-menu-list",
          tabIndex: -1,
          children: option.subItems.map(subItem => {
            return /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdownItem.default, {
              option: subItem,
              onSelect: onSelect,
              selectedKey: selectedKey,
              onCloseDropdown: handleCloseDropdown,
              LinkComponent: LinkComponent
            }, subItem.key);
          })
        })
      })
    })]
  });
};
var _default = exports.default = DropdownMenuMultiLevelOptions;