"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.CollapsibleMenuNav = CollapsibleMenuNav;
function _react() {
  const data = _interopRequireWildcard(require("react"));
  _react = function () {
    return data;
  };
  return data;
}
function _classnames() {
  const data = _interopRequireDefault(require("classnames"));
  _classnames = function () {
    return data;
  };
  return data;
}
function _designNavigation() {
  const data = require("@teambit/design.navigation.responsive-navbar");
  _designNavigation = function () {
    return data;
  };
  return data;
}
function _topBarNav() {
  const data = require("../top-bar-nav");
  _topBarNav = function () {
    return data;
  };
  return data;
}
function _menuModule() {
  const data = _interopRequireDefault(require("./menu.module.scss"));
  _menuModule = function () {
    return data;
  };
  return data;
}
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function TopBarNavComponent({
  isInMenu,
  menuItemProps
}) {
  /**
   * to accommodate for the top level nav which should display the children
   * in the dropdown secondary menu if there is a displayName set
   */
  const widgetDisplayText = menuItemProps?.displayName && isInMenu ? menuItemProps?.displayName : undefined;
  return /*#__PURE__*/_react().default.createElement(_topBarNav().TopBarNav, _extends({}, menuItemProps, {
    className: (0, _classnames().default)(menuItemProps?.className, _menuModule().default.topBarNav, isInMenu && _menuModule().default.noBorder)
  }), widgetDisplayText || menuItemProps?.children);
}
function CollapsibleMenuNav({
  navigationSlot,
  widgetSlot,
  navPlugins = [],
  widgetPlugins = [],
  className,
  secondaryNavClassName,
  activeTabIndex,
  alwaysShowActiveTab,
  children
}) {
  const plugins = (0, _react().useMemo)(() => {
    const _navPlugins = navPlugins.length > 0 ? navPlugins : navigationSlot?.toArray();
    return (_navPlugins || []).sort(sortFn);
  }, [navigationSlot, navPlugins]);
  const widgets = (0, _react().useMemo)(() => {
    const _widgetPlugins = widgetPlugins.length > 0 ? widgetPlugins : widgetSlot?.toArray();
    return (_widgetPlugins || []).sort(sortFn);
  }, [widgetSlot, widgetPlugins]);
  const links = [...plugins, ...widgets].map(([, menuItem], index) => {
    // these styles keep plugins to the left and widgets to the right.
    const lastPluginStyle = plugins.length - 1 === index ? {
      marginRight: 'auto'
    } : {};
    const firstWidgetStyle = plugins.length === index ? {
      marginLeft: 'auto'
    } : {};
    return {
      component: TopBarNavComponent,
      tabProps: {
        menuItemProps: menuItem.props
      },
      style: _objectSpread(_objectSpread({}, firstWidgetStyle), lastPluginStyle),
      className: menuItem.props.className
    };
  });
  return /*#__PURE__*/_react().default.createElement(_designNavigation().ResponsiveNavbar, {
    navClassName: (0, _classnames().default)(_menuModule().default.tab, className),
    secondaryNavClassName: secondaryNavClassName,
    style: {
      width: '100%',
      height: '100%'
    },
    priority: "none",
    tabs: links,
    defaultActiveIndex: activeTabIndex,
    alwaysShowActiveTab: alwaysShowActiveTab
  }, children);
}
function sortFn([, {
  order: first
}], [, {
  order: second
}]) {
  // 0  - equal
  // <0 - first < second
  // >0 - first > second

  return (first ?? 0) - (second ?? 0);
}

// // this is the aspect-oriented and serialize-able way to sort plugins.
// const pluginOrder = ['teambit.docs/docs', 'teambit.compositions/compositions', 'teambit.docs/docs'];
// export function toSortedArray<T>(slot: SlotRegistry<T>, order: string[]) {
//   // sort items according to the order
//   const sorted = order.map((x) => [x, slot.get(x)]).filter(([, val]) => !!val) as [string, T][];
//
//   // add all other items
//   const unsorted = slot.toArray().filter(([id]) => order.indexOf(id) < 0);
//
//   return sorted.concat(unsorted);
// }

//# sourceMappingURL=menu-nav.js.map