package com.bonus.mq.rabbit.tx;

import com.bonus.mq.MQMessageConverter;
import com.bonus.mq.tx.MQTxCheck;
import com.bonus.mq.tx.MQTxCheckComposite;
import com.bonus.mq.tx.MQTxChecker;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Iterator;
import java.util.Map;
import java.util.stream.Collectors;

@Configuration
public class RabbitTxConfiguration {
   @Bean
   public MQTxCheckComposite rocketMQTxCheckComposite(ApplicationContext applicationContext, MQMessageConverter MQMessageConverter) {
      Map<String, Object> beans = (Map)applicationContext.getBeansWithAnnotation(MQTxChecker.class).entrySet().stream().filter((entryx) -> {
         return !ScopedProxyUtils.isScopedTarget((String)entryx.getKey());
      }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
      MQTxCheckComposite rocketMQTxCheckComposite = new MQTxCheckComposite(MQMessageConverter.getMessageConverter());
      Iterator var5 = beans.entrySet().iterator();

      while(var5.hasNext()) {
         Map.Entry<String, Object> entry = (Map.Entry)var5.next();
         Class<?> clazz = AopProxyUtils.ultimateTargetClass(entry.getValue());
         if (!MQTxCheck.class.isAssignableFrom(entry.getValue().getClass())) {
            String var10002 = String.valueOf(clazz);
            throw new IllegalArgumentException(var10002 + " is not instance of " + MQTxCheck.class.getName());
         }

         rocketMQTxCheckComposite.addTrxExecutor((MQTxCheck)entry.getValue());
      }

      return rocketMQTxCheckComposite;
   }
}
