"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _debounce = _interopRequireDefault(require("lodash-es/debounce"));
var _classnames = _interopRequireDefault(require("classnames"));
var _dropdownContentList = require("./dropdown-content-list");
var _dropdownMenuMultiLevelOptions = _interopRequireDefault(require("./dropdown-menu-multi-level-options"));
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 DELAY = 300;

// this will create leveling system for top level items and their subItems
// to enable effective comparisson strategy
// top items will have level:1, level:2, level:3
// while their children will be assigned a number that contains parents index and the children's index
// in the subItems collection after a . (like 1.0, 1.1, 1.2, etc)
const setLevels = (options, baseIndex) => {
  return options.map((option, i) => {
    // index can start with 1, check has to be explicit
    const index = i + 1;
    const level = baseIndex === undefined ? `${index}` : `${baseIndex}.${index}`;
    const newOption = {
      ...option,
      level
    };
    if (newOption.subOptions && newOption.subOptions.length > 0) {
      newOption.subOptions = setLevels(newOption.subOptions, level);
    }
    if (newOption.subItems && newOption.subItems.length > 0) {
      newOption.subItems = setLevels(newOption.subItems, level);
    }
    return newOption;
  });
};
const DropdownMenuMultiLevelList = ({
  options,
  onSelect,
  closeDropdown,
  selectedOption,
  contentTestId,
  contentClassName,
  isMultiLevelSectioned,
  LinkComponent
}) => {
  const preparedOptions = (0, _react.useMemo)(() => setLevels(options), [options]);
  const [state, setState] = (0, _react.useState)({
    level: null,
    isSubLevelOpen: false
  });
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const debouncedFunc = (0, _debounce.default)(setState, DELAY, {
    trailing: true
  });
  const debouncedSet = (0, _react.useRef)();
  (0, _react.useEffect)(() => {
    debouncedSet.current = (0, _debounce.default)(setState, DELAY, {
      trailing: true
    });
    return () => {
      debouncedSet.current?.cancel?.();
    };
  }, []);
  const handleMouseEnter = level => {
    return debouncedSet.current && debouncedSet.current({
      level: level || null,
      isSubLevelOpen: true
    });
  };
  const handleMouseLeave = () => {
    return debouncedSet.current && debouncedSet.current({
      level: null,
      isSubLevelOpen: true
    });
  };

  // this will trigger reopening nested menu on option click insted of closing the whole menu
  const handleSubLevelClose = () => {
    setState({
      level: null,
      isSubLevelOpen: false
    });
  };
  const multiLevelProps = {
    state,
    selectedOption,
    handleSubLevelClose,
    onMouseEnter: handleMouseEnter,
    onMouseLeave: handleMouseLeave,
    LinkComponent
  };
  return isMultiLevelSectioned ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdownContentList.DropdownSectionedContentList, {
    isMultiLevelSectioned: isMultiLevelSectioned,
    options: preparedOptions,
    onSelect: onSelect,
    onCloseDropdown: closeDropdown,
    multiLevelProps: multiLevelProps,
    contentClassName: contentClassName,
    LinkComponent: LinkComponent
  }) : /*#__PURE__*/(0, _jsxRuntime.jsx)("ul", {
    className: (0, _classnames.default)('dropdown-menu-list', contentClassName),
    role: "menu",
    "test-id": contentTestId,
    tabIndex: -1,
    children: preparedOptions.map(option => /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdownMenuMultiLevelOptions.default, {
      onSelect: onSelect,
      closeDropdown: closeDropdown,
      option: option,
      ...multiLevelProps
    }, option.key))
  });
};
var _default = exports.default = DropdownMenuMultiLevelList;