"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SideNavPanelSplitter = void 0;
var _react = _interopRequireWildcard(require("react"));
var _tinyInvariant = _interopRequireDefault(require("tiny-invariant"));
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _useIsFhsEnabled = require("../../fhs-rollout/use-is-fhs-enabled");
var _constants = require("../constants");
var _useToggleSideNav = require("../side-nav/use-toggle-side-nav");
var _useOpenLayerCount = require("../use-open-layer-count");
var _context = require("./context");
var _panelSplitter = require("./panel-splitter");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(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 (var _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); }
/**
 * Namespaces to check for open layers that would interfere with the panel splitter.
 *
 * Placed outside the component for stability, as the list is used as an effect dependency.
 */
var openLayerNamespacesToCheck = [
// We don't technically need to check the side nav for open layers, as they wouldn't overlay the
// panel splitter, as it sits within the same stacking context as the side nav. For consistency however,
// we check it as well.
_constants.openLayerObserverSideNavNamespace,
// When there is an open layer in the top nav, the top nav is given a higher z-index than the side nav.
// This means the part of the side nav panel splitter that was sitting above the top nav will no longer
// be interactive (as it is now behind the top nav). So, we need to disable the entire panel splitter.
_constants.openLayerObserverTopNavStartNamespace, _constants.openLayerObserverTopNavMiddleNamespace, _constants.openLayerObserverTopNavEndNamespace];
/**
 * _SideNavPanelSplitter_
 *
 * A component that allows the user to resize or collapse the side nav.
 * It must be used within the `SideNav` layout area.
 *
 * Example usage:
 * ```tsx
 * <SideNav>
 *   <SideNavPanelSplitter label="Double click to collapse"  />
 * </SideNav>
 * ```
 */
var SideNavPanelSplitter = exports.SideNavPanelSplitter = function SideNavPanelSplitter(_ref) {
  var label = _ref.label,
    onResizeStart = _ref.onResizeStart,
    onResizeEnd = _ref.onResizeEnd,
    testId = _ref.testId,
    _ref$shouldCollapseOn = _ref.shouldCollapseOnDoubleClick,
    shouldCollapseOnDoubleClick = _ref$shouldCollapseOn === void 0 ? true : _ref$shouldCollapseOn,
    tooltipContent = _ref.tooltipContent;
  var context = (0, _react.useContext)(_context.PanelSplitterContext);
  (0, _tinyInvariant.default)((context === null || context === void 0 ? void 0 : context.panelId) === _constants.sideNavPanelSplitterId, 'SideNavPanelSplitter must be rendered as a child of <SideNav />.');
  var toggleSideNav = (0, _useToggleSideNav.useToggleSideNav)({
    trigger: 'double-click'
  });
  var isFhsEnabled = (0, _useIsFhsEnabled.useIsFhsEnabled)();

  // The logic and state for disabling the panel splitter when there are open popups
  // in the side nav or top nav is being placed here, instead of in `SideNav`, to prevent
  // re-rendering the side nav anytime the number of open popups changes.
  var hasOpenLayers = (0, _useOpenLayerCount.useHasOpenLayers)({
    namespaces: openLayerNamespacesToCheck,
    type: 'popup'
  });
  if (hasOpenLayers && isFhsEnabled && (0, _platformFeatureFlags.fg)('platform-dst-side-nav-layering-fixes')) {
    return null;
  }
  return /*#__PURE__*/_react.default.createElement(_context.OnDoubleClickContext.Provider, {
    value: shouldCollapseOnDoubleClick ? toggleSideNav : undefined
  }, /*#__PURE__*/_react.default.createElement(_panelSplitter.PanelSplitter, {
    label: label,
    onResizeStart: onResizeStart,
    onResizeEnd: onResizeEnd,
    testId: testId,
    tooltipContent:
    // Only displaying the tooltip if double click to collapse is enabled.
    // Otherwise, we would be displaying a tooltip with "Double click to collapse", when it actually won't.
    shouldCollapseOnDoubleClick ? tooltipContent : undefined
  }));
};