package com.airlock.iam.plugin.framework.misc.config;

import com.airlock.iam.plugin.framework.application.configuration.MutablePluginConfiguration;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

public final class CombiningWalkerScope implements SubtreeWalkerScope {
   private final List<SubtreeWalkerScope> scopes;

   @Override
   public Stream<MutablePluginConfiguration> getNexts(MutablePluginConfiguration current) {
      return this.scopes.stream().flatMap(scope -> scope.getNexts(current));
   }

   @SuppressFBWarnings(justification = "generated code")
   CombiningWalkerScope(List<SubtreeWalkerScope> scopes) {
      this.scopes = scopes;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static CombiningWalkerScope.CombiningWalkerScopeBuilder with() {
      return new CombiningWalkerScope.CombiningWalkerScopeBuilder();
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class CombiningWalkerScopeBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private ArrayList<SubtreeWalkerScope> scopes;

      @SuppressFBWarnings(justification = "generated code")
      CombiningWalkerScopeBuilder() {
      }

      @SuppressFBWarnings(justification = "generated code")
      public CombiningWalkerScope.CombiningWalkerScopeBuilder scope(SubtreeWalkerScope scope) {
         if (this.scopes == null) {
            this.scopes = new ArrayList<>();
         }

         this.scopes.add(scope);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CombiningWalkerScope.CombiningWalkerScopeBuilder scopes(Collection<? extends SubtreeWalkerScope> scopes) {
         if (scopes == null) {
            throw new NullPointerException("scopes cannot be null");
         }

         if (this.scopes == null) {
            this.scopes = new ArrayList<>();
         }

         this.scopes.addAll(scopes);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CombiningWalkerScope.CombiningWalkerScopeBuilder clearScopes() {
         if (this.scopes != null) {
            this.scopes.clear();
         }

         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CombiningWalkerScope combine() {
         return new CombiningWalkerScope(switch (this.scopes == null ? 0 : this.scopes.size()) {
            case 0 -> Collections.emptyList();
            case 1 -> Collections.singletonList(this.scopes.get(0));
            default -> Collections.unmodifiableList(new ArrayList<>(this.scopes));
         });
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "CombiningWalkerScope.CombiningWalkerScopeBuilder(scopes=" + this.scopes + ")";
      }
   }
}
