"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.scopeDrawer = void 0;
function _react() {
  const data = _interopRequireWildcard(require("react"));
  _react = function () {
    return data;
  };
  return data;
}
function _componentUi() {
  const data = require("@teambit/component.ui.component-drawer");
  _componentUi = function () {
    return data;
  };
  return data;
}
function _uiFoundationUi() {
  const data = require("@teambit/ui-foundation.ui.side-bar");
  _uiFoundationUi = function () {
    return data;
  };
  return data;
}
function _lanesHooks() {
  const data = require("@teambit/lanes.hooks.use-lanes");
  _lanesHooks = function () {
    return data;
  };
  return data;
}
function _lanesHooks2() {
  const data = require("@teambit/lanes.hooks.use-lane-components");
  _lanesHooks2 = function () {
    return data;
  };
  return data;
}
function _scopeUiHooks() {
  const data = require("@teambit/scope.ui.hooks.scope-context");
  _scopeUiHooks = function () {
    return data;
  };
  return data;
}
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 _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); }
const scopeDrawer = ({
  treeWidgets,
  filtersSlot,
  drawerWidgetSlot,
  assumeScopeInUrl = false,
  overrideUseComponents,
  overrideUseLanes: useLanesFromProps
}) => {
  const useLanes = useLanesFromProps || _lanesHooks().useLanes;
  const customScopeTreeNodeRenderer = (treeNodeSlot, host) => function TreeNode(props) {
    const children = props.node.children;
    if (!children) return /*#__PURE__*/_react().default.createElement(_uiFoundationUi().ComponentView, _extends({}, props, {
      treeNodeSlot: treeNodeSlot
      /**
       * this is a hack to get around incompatible component id b/w the new sidebar and components in this workspace
       * the new sidebar has been tagged with the new LanesModel component which has been upgraded to a new major component id version
       * which makes it incompatible with all other components in this workspace
       *
       * hopefully when wave gets released it becomes seamless to update all dependents for a given version
       */,
      useLanes: useLanes,
      scopeName: host?.name
    }));

    // skip over scope node and render only children
    if (props.node.payload instanceof _uiFoundationUi().ScopePayload) {
      const scopeNameFromNode = props.node.id.slice(0, -1);
      const scope = host?.name;

      /**
       * this is only valid when viewing component from a lane
       * if the lane component is from a different scope than the current scope
       * show the scope node
       */
      if (scopeNameFromNode !== scope) {
        return /*#__PURE__*/_react().default.createElement(_uiFoundationUi().ScopeTreeNode, props);
      }
      return /*#__PURE__*/_react().default.createElement(_react().default.Fragment, null, children.map(childNode => /*#__PURE__*/_react().default.createElement(TreeNode, _extends({
        key: childNode.id
      }, props, {
        node: childNode
      }))));
    }
    return /*#__PURE__*/_react().default.createElement(_uiFoundationUi().NamespaceTreeNode, props);
  };
  return new (_componentUi().ComponentsDrawer)({
    assumeScopeInUrl,
    order: 0,
    id: 'scope-components-drawer',
    name: 'COMPONENTS',
    plugins: {
      tree: {
        widgets: treeWidgets,
        customRenderer: customScopeTreeNodeRenderer
      },
      filters: filtersSlot,
      drawerWidgets: drawerWidgetSlot
    },
    useHost: () => (0, _scopeUiHooks().useScope)(),
    useLanes,
    emptyMessage: 'Scope is empty',
    // TODO: create an interface for Component host.
    transformTree: host => {
      return rootNode => {
        const thisScopeIndex = rootNode.children?.findIndex(node => {
          if (!(node.payload instanceof _uiFoundationUi().ScopePayload)) return undefined;
          const scopeNameFromNode = node.id.slice(0, -1);
          return scopeNameFromNode === host?.name;
        });
        const thisScope = rootNode.children && thisScopeIndex !== undefined ? rootNode.children[thisScopeIndex] : undefined;
        if (thisScope && thisScope.children && rootNode.children) {
          delete rootNode.children[thisScopeIndex];
          const children = rootNode.children.concat(thisScope.children);
          rootNode.children = children;
        }
        return rootNode;
      };
    },
    useComponents: overrideUseComponents || (() => {
      const {
        lanesModel,
        loading: lanesLoading
      } = useLanes();
      const viewedLaneId = lanesModel?.viewedLane?.id;
      const {
        components: laneComponents = [],
        loading: laneCompsLoading
      } = (0, _lanesHooks2().useLaneComponents)(viewedLaneId?.isDefault() ? undefined : viewedLaneId);
      const {
        components: mainComponents
      } = (0, _react().useContext)(_scopeUiHooks().ScopeContext);
      // lane components + main components
      const components = mergeComponents(mainComponents, laneComponents);
      return {
        loading: lanesLoading || laneCompsLoading,
        components
      };
    })
  });
};
exports.scopeDrawer = scopeDrawer;
function mergeComponents(mainComponents, laneComponents) {
  const mainComponentsThatAreNotOnLane = mainComponents.filter(mainComponent => {
    return !laneComponents.find(laneComponent => laneComponent.id.toStringWithoutVersion() === mainComponent.id.toStringWithoutVersion());
  });
  return laneComponents.concat(mainComponentsThatAreNotOnLane);
}

//# sourceMappingURL=scope.ui.drawer.js.map