"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.workspaceDrawer = 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 _workspaceContext() {
  const data = require("./ui/workspace/workspace-context");
  _workspaceContext = 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 workspaceDrawer = ({
  treeWidgets,
  filtersSlot,
  drawerWidgetSlot,
  overrideUseLanes: useLanesFromProps
}) => {
  const useLanes = useLanesFromProps || _lanesHooks().useLanes;
  return new (_componentUi().ComponentsDrawer)({
    order: 0,
    id: 'workspace-components-drawer',
    name: 'COMPONENTS',
    plugins: {
      tree: {
        widgets: treeWidgets,
        customRenderer: treeNodeSlot => function TreeNode(props) {
          const children = props.node.children;
          if (!children) {
            // non collapse
            return /*#__PURE__*/_react().default.createElement(_uiFoundationUi().ComponentView, _extends({}, props, {
              treeNodeSlot: treeNodeSlot
            }));
          }
          if (props.node.payload instanceof _uiFoundationUi().ScopePayload) return /*#__PURE__*/_react().default.createElement(_uiFoundationUi().ScopeTreeNode, props);
          return /*#__PURE__*/_react().default.createElement(_uiFoundationUi().NamespaceTreeNode, props);
        }
      },
      filters: filtersSlot,
      drawerWidgets: drawerWidgetSlot
    },
    emptyMessage: 'Workspace is empty',
    useLanes,
    useComponents: () => {
      const {
        lanesModel,
        loading: lanesLoading
      } = useLanes();
      const viewedLaneId = lanesModel?.viewedLane?.id;
      const defaultLane = lanesModel?.getDefaultLane();
      const isViewingDefaultLane = viewedLaneId && defaultLane?.id.isEqual(viewedLaneId);
      const isViewingWorkspaceVersions = lanesModel?.isViewingCurrentLane();
      const {
        components: laneComponents = [],
        loading: laneCompsLoading
      } = (0, _lanesHooks2().useLaneComponents)(!isViewingWorkspaceVersions ? viewedLaneId : undefined);
      const {
        components: mainComponents = [],
        loading: mainCompsLoading
      } = (0, _lanesHooks2().useLaneComponents)(defaultLane?.id);
      const workspace = (0, _react().useContext)(_workspaceContext().WorkspaceContext);
      const {
        components: workspaceComponents
      } = workspace;
      const loading = lanesLoading || laneCompsLoading || mainCompsLoading;

      /**
       * if viewing locally checked out lane, return all components from the workspace
       * when viewing main when locally checked out to another lane, explicitly return components from the "main" lane
       * when viewing another lane when locally checked out to a different lane, return "main" + "lane" components
       * */
      if (isViewingWorkspaceVersions) {
        return {
          loading,
          components: workspaceComponents
        };
      }
      if (isViewingDefaultLane) {
        return {
          loading,
          components: mainComponents
        };
      }
      return {
        loading,
        components: mergeComponents(mainComponents, laneComponents)
      };
    }
  });
};
exports.workspaceDrawer = workspaceDrawer;
function mergeComponents(mainComponents, laneComponents) {
  const mainComponentsThatAreNotOnLane = mainComponents.filter(mainComponent => {
    return !laneComponents.find(laneComponent => laneComponent.id.toStringWithoutVersion() === mainComponent.id.toStringWithoutVersion());
  });
  return laneComponents.concat(mainComponentsThatAreNotOnLane);
}

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