/*
 * Copyright (C) 2022 The Dagger Authors.
 *
 * 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 dagger.internal.codegen.xprocessing;

import static androidx.room.compiler.processing.compat.XConverters.toJavac;

import androidx.room.compiler.processing.XExecutableParameterElement;
import androidx.room.compiler.processing.XTypeElement;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.TypeSpec;

// TODO(bcorso): Consider moving these methods into XProcessing library.
/** A utility class for JavaPoet types to interface with XProcessing types. */
public final class JavaPoetExt {
  /** Returns the {@link ParameterSpec} for the given parameter. */
  public static ParameterSpec getParameterSpec(XExecutableParameterElement parameter) {
    return ParameterSpec.get(toJavac(parameter));
  }

  /**
   * Marks the given type so that it avoids clashes with nested classes in the given {@link
   * TypeSpec.Builder}.
   */
  public static TypeSpec.Builder avoidClashesWithNestedClasses(
      TypeSpec.Builder typeBuilder, XTypeElement type) {
    return typeBuilder.avoidClashesWithNestedClasses(toJavac(type));
  }

  private JavaPoetExt() {}
}

@@ -718,16 +718,91 @@
 toJavac;
+%0Aimport static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
 %0A%0Aimport
@@ -861,24 +861,72 @@
 terElement;%0A
+import androidx.room.compiler.processing.XType;%0A
 import andro
@@ -1463,12 +1463,17 @@
   * 
-Mark
+Configure
 s th
@@ -1484,102 +1484,271 @@
 ven 
-type so that it avoids
+%7B@link TypeSpec.Builder%7D so that it fully qualifies all
  clas
-hes with nested classes in the given %7B@link%0A   * TypeSpec.Builder%7D.
+ses nested in%0A   * the given %7B@link XTypeElement%7D and all classes nested within any super type of the given %7B@link%0A   * XTypeElement%7D.%0A   *%0A   * @see TypeSpec.Builder#avoidClashesWithNestedClasses(Class)
 %0A   
@@ -1837,21 +1837,17 @@
 Builder 
-typeB
+b
 uilder, 
@@ -1863,24 +1863,31 @@
 ent type
+Element
 ) %7B%0A    
 return t
@@ -1882,71 +1882,366 @@
     
-return typeBuilder.avoidClashesWithNestedClasses(toJavac(type))
+typeElement%0A        .getEnclosedTypeElements()%0A        .forEach(nestedTypeElement -%3E builder.alwaysQualify(getSimpleName(nestedTypeElement)));%0A%0A    typeElement.getSuperTypes().stream()%0A        .filter(XTypes::isDeclared)%0A        .map(XType::getTypeElement)%0A        .forEach(superType -%3E avoidClashesWithNestedClasses(builder, superType));%0A%0A    return builder
 ;%0A  

Create an XProcessing compatible version of JavaPoet's avoidClashesWithNestedClasses.