public static void preselectJdkForNewModule(@Nullable Project project,
                                              @Nullable String lastUsedSdk,
                                              @NotNull JdkComboBox jdkComboBox,
                                              @Nullable ModuleBuilder moduleBuilder,
                                              @NotNull Condition<? super SdkTypeId> sdkFilter) {
    Condition<? super SdkTypeId> sdkTypeFilter = getSdkTypeFilter(moduleBuilder, sdkFilter);

    jdkComboBox.reloadModel();

    if (project != null) {
      Sdk sdk = ProjectRootManager.getInstance(project).getProjectSdk();
      if (sdk != null && sdkTypeFilter.value(sdk.getSdkType())) {
        // select project SDK
        jdkComboBox.setSelectedItem(jdkComboBox.showProjectSdkItem());
        return;
      }
    }

    Project defaultProject = ProjectManager.getInstance().getDefaultProject();
    Sdk selected = ProjectRootManager.getInstance(defaultProject).getProjectSdk();
    if (selected != null && sdkFilter.value(selected.getSdkType())) {
      // select default project SDK
      jdkComboBox.setSelectedJdk(selected);
      return;
    }

    if (lastUsedSdk != null) {
      Sdk sdk = ProjectJdkTable.getInstance().findJdk(lastUsedSdk);
      if (sdk != null && sdkTypeFilter.value(sdk.getSdkType())) {
        // select last used SDK
        jdkComboBox.setSelectedJdk(sdk);
        return;
      }
    }

    Sdk best = null;
    ComboBoxModel<JdkComboBox.JdkComboBoxItem> model = jdkComboBox.getModel();
    for (int i = 0; i < model.getSize(); i++) {
      if (!(model.getElementAt(i) instanceof JdkComboBox.ActualJdkComboBoxItem item)) continue;

      Sdk jdk = item.getJdk();

      SdkTypeId jdkType = jdk.getSdkType();
      if (!sdkFilter.value(jdkType)) continue;

      if (best == null) {
        best = jdk;
        continue;
      }

      SdkTypeId bestType = best.getSdkType();
      //it is in theory possible to have several SDK types here, let's just pick the first lucky type for now
      if (bestType == jdkType && bestType.versionComparator().compare(best, jdk) < 0) {
        best = jdk;
      }
    }

    if (best != null) {
      jdkComboBox.setSelectedJdk(best);
    }
    else {
      jdkComboBox.setSelectedItem(jdkComboBox.showNoneSdkItem());
    }
  }
public static void preselectJdkForNewModule(@Nullable Project project,
                                              @Nullable String lastUsedSdk,
                                              @NotNull JdkComboBox jdkComboBox,
                                              @Nullable ModuleBuilder moduleBuilder,
                                              @NotNull Condition<? super SdkTypeId> sdkFilter)