// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.vcs.actions;

import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.VcsDataKeys;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.ArrayUtil;
import com.intellij.util.containers.JBIterable;
import com.intellij.vcsUtil.VcsUtil;
import org.jetbrains.annotations.NotNull;

import java.io.File;

public class VcsContextUtil {
  @NotNull
  public static JBIterable<VirtualFile> selectedFilesIterable(@NotNull DataContext context) {
    Iterable<VirtualFile> files = VcsDataKeys.VIRTUAL_FILES.getData(context);
    if (files != null) {
      return JBIterable.from(files).filter(VirtualFile::isInLocalFileSystem);
    }

    return JBIterable.empty();
  }

  @NotNull
  public static JBIterable<FilePath> selectedFilePathsIterable(@NotNull DataContext context) {
    Iterable<FilePath> paths = VcsDataKeys.FILE_PATHS.getData(context);
    if (paths != null) {
      return JBIterable.from(paths);
    }

    FilePath path = VcsDataKeys.FILE_PATH.getData(context);
    if (path != null) {
      return JBIterable.of(path);
    }

    JBIterable<File> ioFiles = selectedIOFilesIterable(context);
    if (ioFiles.isNotEmpty()) {
      return ioFiles.map(VcsUtil::getFilePath);
    }

    JBIterable<VirtualFile> virtualFiles = selectedFilesIterable(context);
    if (virtualFiles.isNotEmpty()) {
      return virtualFiles.map(VcsUtil::getFilePath);
    }

    return JBIterable.empty();
  }

  @NotNull
  public static JBIterable<File> selectedIOFilesIterable(@NotNull DataContext context) {
    File[] files = VcsDataKeys.IO_FILE_ARRAY.getData(context);
    if (!ArrayUtil.isEmpty(files)) {
      return JBIterable.of(files);
    }

    File file = VcsDataKeys.IO_FILE.getData(context);
    if (file != null) {
      return JBIterable.of(file);
    }

    return JBIterable.empty();
  }
}

@@ -547,16 +547,39 @@
 io.File;
+%0Aimport java.util.List;
 %0A%0Apublic
@@ -602,16 +602,164 @@
 tUtil %7B%0A
+  @NotNull%0A  public static List%3CVirtualFile%3E selectedFiles(@NotNull DataContext context) %7B%0A    return selectedFilesIterable(context).toList();%0A  %7D%0A%0A
   @NotNu

remove usage of VcsContext in RevertAllUnchangedFilesAction