/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.facebook.buck.features.apple.projectV2;

import com.facebook.buck.core.description.arg.BuildRuleArg;
import com.facebook.buck.core.model.BuildTarget;
import com.facebook.buck.core.model.targetgraph.TargetGraph;
import com.facebook.buck.core.model.targetgraph.TargetNode;
import com.google.common.collect.ImmutableList;
import java.util.Optional;

public class ProjectExcludeResolver {

  private final TargetGraph graph;
  private final ImmutableList<String> excludedLabels;

  @SuppressWarnings(
      "unused") // TODO will be removed in next commit where excludedTargetMatcher is used
  ProjectExcludeResolver(
      TargetGraph graph,
      ImmutableList<String> excludedLabels,
      FocusedTargetMatcher excludedTargetMatcher) {
    this.graph = graph;
    this.excludedLabels = excludedLabels;
  }

  boolean excludeTarget(BuildTarget target) {
    Optional<TargetNode<?>> optionalNode = graph.getOptional(target);
    if (optionalNode.isPresent()) {
      TargetNode<?> node = optionalNode.get();
      if (node.getConstructorArg() instanceof BuildRuleArg) {
        BuildRuleArg buildRuleArg = (BuildRuleArg) node.getConstructorArg();
        return excludeBuildRuleArg(buildRuleArg);
      }
    }
    return false;
  }

  boolean excludeBuildRuleArg(BuildRuleArg buildRuleArg) {
    return excludedLabels.stream().anyMatch(label -> buildRuleArg.getLabels().contains(label));
  }
}

@@ -1102,90 +1102,44 @@
 ls;%0A
-%0A
   
-@SuppressWarnings(%0A      %22unused%22) // TODO will be removed in next commit w
+private final FocusedTargetMatc
 her
-e
  exc
@@ -1160,16 +1160,10 @@
 cher
- is used
+;%0A
 %0A  P
@@ -1378,56 +1378,187 @@
 ;%0A  
-%7D%0A%0A  boolean excludeTarget(BuildTarget target) %7B
+  this.excludedTargetMatcher = excludedTargetMatcher;%0A  %7D%0A%0A  boolean excludeTarget(BuildTarget target) %7B%0A    if (excludedTargetMatcher.matches(target)) %7B%0A      return true;%0A    %7D%0A
 %0A   

use exclude matcher to prune focused targets