private static SkipOverwriteChoice handleExistingFiles(SkipOverwriteChoice defaultChoice,
                                                         int @Nullable [] choice,
                                                         String newName,
                                                         PsiDirectory targetDirectory,
                                                         @NlsContexts.DialogTitle String title,
                                                         @NotNull MultiMap<PsiDirectory, PsiFile> existingFiles,
                                                         @NotNull List<? super PsiFile> added,
                                                         ProgressIndicator progressIndicator) {
    for (PsiDirectory tDirectory : existingFiles.keySet()) {
      Collection<PsiFile> replacementFiles = existingFiles.get(tDirectory);
      for (Iterator<PsiFile> iterator = replacementFiles.iterator(); iterator.hasNext(); ) {
        PsiFile replacement = iterator.next();
        String name = newName == null || tDirectory != targetDirectory ? replacement.getName() : newName;
        PsiFile existing = tDirectory.findFile(name);
        if (existing == null) continue;

        SkipOverwriteChoice userChoice = defaultChoice;
        Project project = targetDirectory.getProject();
        if (userChoice == null) {
          userChoice = SkipOverwriteChoice.askUser(targetDirectory, name, title, choice != null);

          if (userChoice == SkipOverwriteChoice.SKIP_ALL) {
            return userChoice;
          }
          else if (userChoice == SkipOverwriteChoice.OVERWRITE_ALL) {
            Consumer<ProgressIndicator> r = pi -> handleExistingFiles(SkipOverwriteChoice.OVERWRITE_ALL, choice, newName, targetDirectory, title, existingFiles, added, pi);
            if (Registry.is("run.refactorings.under.progress")) {
              CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManagerEx.getApplicationEx().runWriteActionWithCancellableProgressInDispatchThread(title, project, null, r), title, null);
            }
            else {
              r.accept(null);
            }
            return SkipOverwriteChoice.OVERWRITE_ALL;
          }
        }

        iterator.remove();
        ThrowableRunnable<RuntimeException> doCopy = () -> {
          if (progressIndicator != null) {
            progressIndicator.setText2(InspectionsBundle.message("processing.progress.text", existing.getName()));
          }
          existing.delete();
          ((PsiDirectoryImpl)targetDirectory).executeWithUpdatingAddedFilesDisabled(() -> ContainerUtil.addIfNotNull(added, tDirectory.copyFileFrom(name, replacement)));
        };

        if (userChoice == SkipOverwriteChoice.OVERWRITE || userChoice == SkipOverwriteChoice.OVERWRITE_ALL && !Registry.is("run.refactorings.under.progress")) {
          WriteCommandAction.writeCommandAction(project)
            .withName(title)
            .run(doCopy);
        }
        else if (userChoice == SkipOverwriteChoice.OVERWRITE_ALL) {
          doCopy.run();
        }
      }
    }
    return defaultChoice;
  }
private static SkipOverwriteChoice handleExistingFiles(SkipOverwriteChoice defaultChoice,
                                                         int @Nullable [] choice,
                                                         String newName,
                                                         PsiDirectory targetDirectory,
                                                         @NlsContexts.DialogTitle String title,
                                                         @NotNull MultiMap<PsiDirectory, PsiFile> existingFiles,
                                                         @NotNull List<? super PsiFile> added,
                                                         ProgressIndicator progressIndicator)