private static void doBuildDependencies(@NotNull ProjectResolverContext resolverCtx,
                                          @NotNull Map<String, Pair<DataNode<GradleSourceSetData>, ExternalSourceSet>> sourceSetMap,
                                          @NotNull final ArtifactMappingService artifactsMap,
                                          @NotNull Map<ExternalDependencyId, ExternalDependency> mergedDependencyMap,
                                          @NotNull DataNode<? extends ExternalEntityData> ownerDataNode,
                                          @NotNull Collection<ExternalDependency> dependencies,
                                          @Nullable DataNode<ProjectData> ideProject) throws IllegalStateException {
    AtomicInteger classpathOrderShift = new AtomicInteger(0);
    Set<ExternalDependencyId> processedDependencies = new HashSet<>();
    Map<ExternalDependencyId, Collection<ExternalDependency>> transitiveDependenciesMap = groupTransitiveDependenciesById(dependencies);

    for (ExternalDependency dependency : dependencies) {
      if (processedDependencies.add(dependency.getId())) {
        Collection<ExternalDependency> transitiveDependencies = transitiveDependenciesMap.getOrDefault(dependency.getId(), Collections.emptySet());
        final ExternalDependency mergedDependency = mergedDependencyMap.getOrDefault(dependency.getId(), dependency);
        DataNode<? extends ExternalEntityData> depOwnerDataNode = null;

        if (mergedDependency instanceof ExternalProjectDependency projectDependency) {
          depOwnerDataNode = createDependencyDataNode(projectDependency,
                                                      resolverCtx,
                                                      ownerDataNode,
                                                      transitiveDependencies.isEmpty(),
                                                      sourceSetMap,
                                                      artifactsMap,
                                                      classpathOrderShift);
        }
        else if (mergedDependency instanceof ExternalLibraryDependency libraryDependency) {
          depOwnerDataNode = createDependencyDataNode(libraryDependency, ownerDataNode, ideProject, classpathOrderShift);
        }
        else if (mergedDependency instanceof ExternalMultiLibraryDependency multiDep) {
          depOwnerDataNode = createDependencyDataNode(multiDep, ownerDataNode, classpathOrderShift);
        }
        else if (mergedDependency instanceof FileCollectionDependency fileCollectionDependency) {
          depOwnerDataNode = createDependencyDataNode(fileCollectionDependency, ownerDataNode, classpathOrderShift);
        }
        else if (mergedDependency instanceof UnresolvedExternalDependency unresolvedDep) {
          depOwnerDataNode = createDependencyDataNode(unresolvedDep, resolverCtx, ownerDataNode, ideProject, classpathOrderShift);
        }

        if (depOwnerDataNode != null && !transitiveDependencies.isEmpty()) {
          doBuildDependencies(resolverCtx, sourceSetMap, artifactsMap, mergedDependencyMap, depOwnerDataNode, transitiveDependencies,
                              ideProject);
        }
      }
    }
  }
private static void doBuildDependencies(@NotNull ProjectResolverContext resolverCtx,
                                          @NotNull Map<String, Pair<DataNode<GradleSourceSetData>, ExternalSourceSet>> sourceSetMap,
                                          @NotNull final ArtifactMappingService artifactsMap,
                                          @NotNull Map<ExternalDependencyId, ExternalDependency> mergedDependencyMap,
                                          @NotNull DataNode<? extends ExternalEntityData> ownerDataNode,
                                          @NotNull Collection<ExternalDependency> dependencies,
                                          @Nullable DataNode<ProjectData> ideProject)