package jetbrains.mps.execution.api.commands;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import jetbrains.mps.logging.Logger;
import java.util.List;
import org.jetbrains.annotations.Nullable;
import jetbrains.mps.internal.collections.runtime.IterableUtils;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import java.util.ArrayList;
import java.io.File;

@GeneratedClass(node = "r:da044acc-81a4-4fd8-b89a-91df4cfe6214(jetbrains.mps.execution.api.commands)/6868250101935612520", model = "r:da044acc-81a4-4fd8-b89a-91df4cfe6214(jetbrains.mps.execution.api.commands)")
public class ListCommandPart extends AbstractCommandPart implements CommandPart {
  private static final Logger LOG = Logger.getLogger(ListCommandPart.class);
  public ListCommandPart(List<? extends Object> list) {
    this(list, null);
  }
  public ListCommandPart(List<? extends Object> list, @Nullable String separator) {
    if ((separator == null || separator.length() == 0)) {
      addCommands(getCommands(list));
    } else {
      addCommands(IterableUtils.join(Sequence.fromIterable(getCommands(list)), separator));
    }
  }
  private Iterable<String> getCommands(List<? extends Object> list) {
    return ListSequence.fromList(list).translate((it) -> ListCommandPart.this.getCommandsFromItem(it));
  }
  private Iterable<String> getCommandsFromItem(Object item) {
    if (item == null) {
      return ListSequence.fromList(new ArrayList<String>());
    }
    if (item instanceof String) {
      if (isNotEmptyString(((String) item))) {
        return ProcessHandlerBuilder.splitCommandInParts((String) item);
      }
    } else if (item instanceof File) {
      String path = ((File) item).getAbsolutePath();
      if ((path != null && path.length() > 0)) {
        return Sequence.<String>singleton(path);
      }
    } else if (item instanceof CommandPart) {
      return ((CommandPart) item).getCommandList();
    } else if (item instanceof Iterable) {
      List<String> result = ListSequence.fromList(new ArrayList<String>());
      for (Object it : (Iterable) item) {
        ListSequence.fromList(result).addSequence(Sequence.fromIterable(getCommandsFromItem(it)));
      }
      return result;
    } else {
      if (LOG.isErrorLevel()) {
        LOG.error("Unknown type of command part " + item);
      }
    }
    return ListSequence.fromList(new ArrayList<String>());
  }
  private static boolean isNotEmptyString(String str) {
    return str != null && str.length() > 0;
  }
}
