"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SidebarMenu = SidebarMenu;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _sidebarMenuItem = require("./sidebar-menu-item");
var _utils = require("./utils");
var _sidebarMenuLink = require("./sidebar-menu-link");
var _useTranslation = require("../hooks/use-translation");
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); }
/** SidebarMenu enables navigation between different pages/pieces of content
 * @midnight-gray-supported
 * @alpine-light-supported
 */
function SidebarMenu({
  options,
  activeKey,
  onSelect,
  onDelete,
  onEdit,
  onChange,
  className,
  testId = 'sidebar-menu',
  LinkComponent = _sidebarMenuLink.SidebarMenuLink,
  levels = _utils.defaultLevelsConfig,
  shouldShowActiveBgOnTopLevel = true,
  editItemInputPlaceholder,
  deleteAriaLabel,
  editAriaLabel
}) {
  const {
    t
  } = (0, _useTranslation.useTranslation)();
  const levelsConfig = {
    ..._utils.defaultLevelsConfig,
    ...levels
  };
  const flatOptions = (0, _react.useMemo)(() => (0, _utils.getFlatOptions)(options), [options]);
  const activeAncestorKeys = (0, _react.useMemo)(() => (0, _utils.getAncestorKeys)(flatOptions, activeKey), [flatOptions, activeKey]);
  const [expandedKeys, setExpandedKeys] = (0, _react.useState)([activeKey, ...activeAncestorKeys]);
  (0, _react.useEffect)(() => {
    setExpandedKeys([activeKey, ...activeAncestorKeys]);
  }, [flatOptions, activeKey, activeAncestorKeys]);
  const handleActiveItemChange = (0, _react.useCallback)((e, targetKey) => {
    const option = flatOptions.find(({
      key
    }) => key === targetKey);
    const isActive = targetKey === activeKey;
    if (typeof levelsConfig !== 'undefined' && typeof option !== 'undefined' && levelsConfig?.nonSelectable?.includes(option.level)) {
      return;
    }
    if (activeAncestorKeys.includes(targetKey)) {
      setExpandedKeys((0, _utils.getAncestorKeys)(flatOptions, targetKey));
    } else {
      const isExpanded = expandedKeys.includes(targetKey);
      setExpandedKeys(isExpanded ? (0, _utils.getAncestorKeys)(flatOptions, targetKey) : [targetKey, ...(0, _utils.getAncestorKeys)(flatOptions, targetKey)]);
    }
    if (!isActive) {
      onSelect(targetKey, e);
    }
  }, [activeAncestorKeys, expandedKeys, flatOptions, activeKey]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("ul", {
    className: (0, _classnames.default)('sidebar-menu', className),
    "test-id": testId,
    children: options.map(({
      key,
      validity,
      ...rest
    }) => {
      const optionTestId = rest.testId || key.toString();
      return /*#__PURE__*/(0, _react.createElement)(_sidebarMenuItem.SidebarMenuItem, {
        ...rest,
        itemKey: key,
        activeKey: activeKey,
        expandedKeys: !expandedKeys ? [] : expandedKeys,
        level: 0,
        levels: levelsConfig,
        key: key,
        handleActiveItemChange: handleActiveItemChange,
        testId: optionTestId,
        LinkComponent: LinkComponent,
        shouldShowActiveBgOnTopLevel: shouldShowActiveBgOnTopLevel,
        onDelete: onDelete,
        onEdit: onEdit,
        onChange: onChange,
        validity: validity,
        editItemInputPlaceholder: editItemInputPlaceholder ?? t('Enter item value'),
        deleteAriaLabel: deleteAriaLabel ?? t('Delete'),
        editAriaLabel: editAriaLabel ?? t('Edit')
      });
    })
  });
}