package org.apache.logging.log4j.core.config.plugins.visitors;

import java.lang.annotation.Annotation;
import java.lang.reflect.Member;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.plugins.util.TypeConverters;
import org.apache.logging.log4j.core.lookup.StrSubstitutor;
import org.apache.logging.log4j.core.util.Assert;
import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.util.Strings;

public abstract class AbstractPluginVisitor<A extends Annotation> implements PluginVisitor<A> {
   protected static final Logger LOGGER = StatusLogger.getLogger();
   protected final Class<A> clazz;
   protected A annotation;
   protected String[] aliases;
   protected Class<?> conversionType;
   protected StrSubstitutor substitutor;
   protected Member member;

   protected AbstractPluginVisitor(Class<A> clazz) {
      this.clazz = clazz;
   }

   public PluginVisitor<A> setAnnotation(Annotation annotation) {
      Annotation a = (Annotation)Assert.requireNonNull(annotation, "No annotation was provided");
      if (this.clazz.isInstance(annotation)) {
         this.annotation = annotation;
      }

      return this;
   }

   public PluginVisitor<A> setAliases(String... aliases) {
      this.aliases = aliases;
      return this;
   }

   public PluginVisitor<A> setConversionType(Class<?> conversionType) {
      this.conversionType = (Class)Assert.requireNonNull(conversionType, "No conversion type class was provided");
      return this;
   }

   public PluginVisitor<A> setStrSubstitutor(StrSubstitutor substitutor) {
      this.substitutor = (StrSubstitutor)Assert.requireNonNull(substitutor, "No StrSubstitutor was provided");
      return this;
   }

   public PluginVisitor<A> setMember(Member member) {
      this.member = member;
      return this;
   }

   protected static String removeAttributeValue(Map<String, String> attributes, String name, String... aliases) {
      Iterator i$ = attributes.entrySet().iterator();

      while(true) {
         String key;
         String value;
         do {
            if (!i$.hasNext()) {
               return null;
            }

            Entry<String, String> entry = (Entry)i$.next();
            key = (String)entry.getKey();
            value = (String)entry.getValue();
            if (key.equalsIgnoreCase(name)) {
               attributes.remove(key);
               return value;
            }
         } while(aliases == null);

         String[] arr$ = aliases;
         int len$ = aliases.length;

         for(int i$ = 0; i$ < len$; ++i$) {
            String alias = arr$[i$];
            if (key.equalsIgnoreCase(alias)) {
               attributes.remove(key);
               return value;
            }
         }
      }
   }

   protected Object convert(String value, Object defaultValue) {
      return defaultValue instanceof String ? TypeConverters.convert(value, this.conversionType, Strings.trimToNull((String)defaultValue)) : TypeConverters.convert(value, this.conversionType, defaultValue);
   }
}
