"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SidebarMenuItem = SidebarMenuItem;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _truncateWithTooltip = require("../truncate-with-tooltip");
var _tooltip = require("../tooltip");
var _sidebarSubmenu = require("./sidebar-submenu");
var _sidebarMenuLink = require("./sidebar-menu-link");
var _sidebarMenuItemControls = require("./sidebar-menu-item-controls");
var _sidebarMenuItemEditForm = require("./sidebar-menu-item-edit-form");
var _utils = require("./utils");
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); }
function SidebarMenuItem({
  name,
  value,
  itemKey,
  level,
  testId,
  expandedKeys,
  activeKey,
  handleActiveItemChange,
  isExternal,
  isShown,
  isDisabled,
  isEditable,
  isDeletable,
  link,
  route,
  shouldShowActiveBgOnTopLevel,
  onDelete,
  onEdit,
  onChange,
  validity,
  levels = _utils.defaultLevelsConfig,
  LinkComponent = _sidebarMenuLink.SidebarMenuLink,
  subOptions = [],
  onClick = () => {},
  hasErrors = () => '',
  tooltipText = '',
  deleteAriaLabel,
  editAriaLabel,
  editItemInputPlaceholder,
  helperText,
  shouldNotTruncate
}) {
  const [isEditMode, setIsEditMode] = (0, _react.useState)(false);
  const onEnterEditMode = (0, _react.useCallback)(() => {
    setIsEditMode(true);
  }, []);
  const onExitEditMode = (0, _react.useCallback)(() => {
    setIsEditMode(false);
  }, []);
  const getNameMarginClass = (0, _react.useCallback)(() => {
    if (onEdit && onDelete && isEditable && isDeletable) {
      return 'margin-right-8';
    }
    if (onEdit && isEditable || onDelete && isDeletable) {
      return 'margin-right-4';
    }
    return '';
  }, [onEdit, onDelete, isEditable, isDeletable]);
  if (isShown === false) {
    return null;
  }
  const nameMarginClass = getNameMarginClass();
  const levelsConfig = {
    ..._utils.defaultLevelsConfig,
    ...levels
  };
  const isCollapsible = !levelsConfig?.nonCollapsible?.includes(level);
  const withActiveBackground = shouldShowActiveBgOnTopLevel ? level === 0 : expandedKeys[0] === itemKey;
  const showSubmenu = !!subOptions.length && (!isCollapsible || expandedKeys.includes(itemKey));
  const isActive = itemKey === activeKey || expandedKeys.includes(itemKey);
  const isActionsPresent = !!(onEdit && isEditable) || !!(onDelete && isDeletable);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("li", {
    className: (0, _classnames.default)('sidebar-menu-item', `sidebar-menu-item-level-${level}`, isActionsPresent && 'with-actions'),
    "test-id": testId,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: (0, _classnames.default)('sidebar-menu-item-wrapper', {
        'with-active-background': withActiveBackground,
        'sidebar-menu-item-wrapper-disabled': isDisabled
      }),
      "test-id": `sidebar-menu-item-wrapper-${itemKey}`,
      children: [!isEditMode && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(LinkComponent, {
          className: (0, _classnames.default)('sidebar-menu-link', {
            active: isActive,
            error: hasErrors()
          }),
          itemKey: itemKey,
          onClick: (e, key) => {
            onClick(e, key);
            handleActiveItemChange(e, key);
          },
          hasErrors: hasErrors,
          link: link,
          route: route,
          isExternal: isExternal,
          isDisabled: isDisabled,
          testId: `${testId}-link`,
          children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
            className: "sidebar-menu-item-container",
            children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
              className: "sidebar-menu-item-header",
              children: [!tooltipText && shouldNotTruncate && /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
                className: (0, _classnames.default)(nameMarginClass, value && 'with-value'),
                children: name
              }), !tooltipText && !shouldNotTruncate && /*#__PURE__*/(0, _jsxRuntime.jsx)(_truncateWithTooltip.TruncateWithTooltip, {
                className: (0, _classnames.default)(nameMarginClass, value && 'with-value'),
                children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
                  children: name
                })
              }), tooltipText && /*#__PURE__*/(0, _jsxRuntime.jsx)(_tooltip.Tooltip, {
                content: tooltipText,
                placement: "top",
                children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
                  className: (0, _classnames.default)(nameMarginClass, {
                    'sidebar-menu-link-name': !shouldNotTruncate
                  }),
                  children: name
                })
              }), !!value && /*#__PURE__*/(0, _jsxRuntime.jsx)(_truncateWithTooltip.TruncateWithTooltip, {
                className: (0, _classnames.default)('sidebar-menu-link-value', {
                  'should-not-truncate': shouldNotTruncate
                }),
                children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
                  children: value
                })
              })]
            }), !!helperText && /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
              className: "sidebar-menu-item-helper-text",
              children: helperText
            })]
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebarMenuItemControls.SidebarMenuItemControls, {
          onDelete: onDelete,
          onEdit: onEnterEditMode,
          isEditable: !!isEditable,
          isDeletable: !!isDeletable,
          itemKey: itemKey,
          level: level,
          deleteAriaLabel: deleteAriaLabel,
          editAriaLabel: editAriaLabel
        })]
      }), isEditMode && !!onEdit && /*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebarMenuItemEditForm.SidebarMenuItemEditForm, {
        initValue: name,
        onExit: onExitEditMode,
        onSubmit: onEdit,
        itemKey: itemKey,
        level: level,
        validity: validity,
        onChange: onChange,
        editItemInputPlaceholder: editItemInputPlaceholder
      })]
    }), showSubmenu && /*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebarSubmenu.SidebarSubMenu, {
      activeKey: activeKey,
      expandedKeys: expandedKeys,
      subOptions: subOptions,
      level: level + 1,
      levels: levels,
      handleActiveItemChange: handleActiveItemChange,
      testId: `${testId}-submenu`,
      LinkComponent: LinkComponent,
      shouldShowActiveBgOnTopLevel: shouldShowActiveBgOnTopLevel,
      onDelete: onDelete,
      onEdit: onEdit,
      isEditable: isEditable,
      isDeletable: isDeletable,
      onChange: onChange,
      deleteAriaLabel: deleteAriaLabel,
      editAriaLabel: editAriaLabel,
      editItemInputPlaceholder: editItemInputPlaceholder
    })]
  });
}