package com.google.firebase.abt; import android.content.Context; import com.google.firebase.analytics.connector.AnalyticsConnector; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; /* loaded from: classes.dex */ public class FirebaseABTesting { private final AnalyticsConnector analyticsConnector; private Integer maxUserProperties = null; private final String originService; public FirebaseABTesting(Context context, AnalyticsConnector analyticsConnector, String str) { this.analyticsConnector = analyticsConnector; this.originService = str; } public void replaceAllExperiments(List> list) throws AbtException { throwAbtExceptionIfAnalyticsIsNull(); if (list == null) { throw new IllegalArgumentException("The replacementExperiments list is null."); } replaceAllExperimentsWith(convertMapsToExperimentInfos(list)); } public void removeAllExperiments() throws AbtException { throwAbtExceptionIfAnalyticsIsNull(); removeExperiments(getAllExperimentsInAnalytics()); } private void replaceAllExperimentsWith(List list) throws AbtException { if (list.isEmpty()) { removeAllExperiments(); return; } HashSet hashSet = new HashSet(); Iterator it = list.iterator(); while (it.hasNext()) { hashSet.add(it.next().getExperimentId()); } List allExperimentsInAnalytics = getAllExperimentsInAnalytics(); HashSet hashSet2 = new HashSet(); Iterator it2 = allExperimentsInAnalytics.iterator(); while (it2.hasNext()) { hashSet2.add(it2.next().name); } removeExperiments(getExperimentsToRemove(allExperimentsInAnalytics, hashSet)); addExperiments(getExperimentsToAdd(list, hashSet2)); } private ArrayList getExperimentsToRemove(List list, Set set) { ArrayList arrayList = new ArrayList<>(); for (AnalyticsConnector.ConditionalUserProperty conditionalUserProperty : list) { if (!set.contains(conditionalUserProperty.name)) { arrayList.add(conditionalUserProperty); } } return arrayList; } private ArrayList getExperimentsToAdd(List list, Set set) { ArrayList arrayList = new ArrayList<>(); for (AbtExperimentInfo abtExperimentInfo : list) { if (!set.contains(abtExperimentInfo.getExperimentId())) { arrayList.add(abtExperimentInfo); } } return arrayList; } private void addExperiments(List list) { ArrayDeque arrayDeque = new ArrayDeque(getAllExperimentsInAnalytics()); int maxUserPropertiesInAnalytics = getMaxUserPropertiesInAnalytics(); for (AbtExperimentInfo abtExperimentInfo : list) { while (arrayDeque.size() >= maxUserPropertiesInAnalytics) { removeExperimentFromAnalytics(((AnalyticsConnector.ConditionalUserProperty) arrayDeque.pollFirst()).name); } AnalyticsConnector.ConditionalUserProperty conditionalUserProperty = abtExperimentInfo.toConditionalUserProperty(this.originService); addExperimentToAnalytics(conditionalUserProperty); arrayDeque.offer(conditionalUserProperty); } } private void removeExperiments(Collection collection) { Iterator it = collection.iterator(); while (it.hasNext()) { removeExperimentFromAnalytics(it.next().name); } } private static List convertMapsToExperimentInfos(List> list) throws AbtException { ArrayList arrayList = new ArrayList(); Iterator> it = list.iterator(); while (it.hasNext()) { arrayList.add(AbtExperimentInfo.fromMap(it.next())); } return arrayList; } private void addExperimentToAnalytics(AnalyticsConnector.ConditionalUserProperty conditionalUserProperty) { this.analyticsConnector.setConditionalUserProperty(conditionalUserProperty); } private void throwAbtExceptionIfAnalyticsIsNull() throws AbtException { if (this.analyticsConnector == null) { throw new AbtException("The Analytics SDK is not available. Please check that the Analytics SDK is included in your app dependencies."); } } private void removeExperimentFromAnalytics(String str) { this.analyticsConnector.clearConditionalUserProperty(str, null, null); } private int getMaxUserPropertiesInAnalytics() { if (this.maxUserProperties == null) { this.maxUserProperties = Integer.valueOf(this.analyticsConnector.getMaxUserProperties(this.originService)); } return this.maxUserProperties.intValue(); } private List getAllExperimentsInAnalytics() { return this.analyticsConnector.getConditionalUserProperties(this.originService, ""); } }