package com.google.firebase.sessions;

import android.os.Message;
import android.util.Log;
import com.google.firebase.sessions.api.FirebaseSessionsDependencies;
import com.google.firebase.sessions.api.SessionSubscriber;
import dn.p;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import kotlin.Metadata;
import kotlin.coroutines.jvm.internal.l;
import rm.n0;
import rm.y;
import sm.c0;
import sm.u;
import tp.m0;

/* JADX INFO: Access modifiers changed from: package-private */
@kotlin.coroutines.jvm.internal.f(c = "com.google.firebase.sessions.SessionLifecycleClient$sendLifecycleEvents$1", f = "SessionLifecycleClient.kt", l = {151}, m = "invokeSuspend")
@Metadata(d1 = {"\u0000\n\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\u0010\u0002\u001a\u00020\u0001*\u00020\u0000H\u008a@"}, d2 = {"Ltp/m0;", "Lrm/n0;", "<anonymous>"}, k = 3, mv = {1, 8, 0})
/* loaded from: classes3.dex */
public final class SessionLifecycleClient$sendLifecycleEvents$1 extends l implements p {
    final /* synthetic */ List<Message> $messages;
    int label;
    final /* synthetic */ SessionLifecycleClient this$0;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public SessionLifecycleClient$sendLifecycleEvents$1(SessionLifecycleClient sessionLifecycleClient, List<Message> list, vm.d<? super SessionLifecycleClient$sendLifecycleEvents$1> dVar) {
        super(2, dVar);
        this.this$0 = sessionLifecycleClient;
        this.$messages = list;
    }

    @Override // kotlin.coroutines.jvm.internal.a
    public final vm.d<n0> create(Object obj, vm.d<?> dVar) {
        return new SessionLifecycleClient$sendLifecycleEvents$1(this.this$0, this.$messages, dVar);
    }

    @Override // kotlin.coroutines.jvm.internal.a
    public final Object invokeSuspend(Object obj) {
        Object f10;
        boolean z10;
        Message latestByCode;
        Message latestByCode2;
        List q10;
        List Z;
        List J0;
        f10 = wm.d.f();
        int i10 = this.label;
        if (i10 == 0) {
            y.b(obj);
            FirebaseSessionsDependencies firebaseSessionsDependencies = FirebaseSessionsDependencies.INSTANCE;
            this.label = 1;
            obj = firebaseSessionsDependencies.getRegisteredSubscribers$com_google_firebase_firebase_sessions(this);
            if (obj == f10) {
                return f10;
            }
        } else {
            if (i10 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            y.b(obj);
        }
        Map map = (Map) obj;
        if (map.isEmpty()) {
            Log.d(SessionLifecycleClient.TAG, "Sessions SDK did not have any dependent SDKs register as dependencies. Events will not be sent.");
        } else {
            Collection values = map.values();
            if (!(values instanceof Collection) || !values.isEmpty()) {
                Iterator it = values.iterator();
                while (it.hasNext()) {
                    if (((SessionSubscriber) it.next()).isDataCollectionEnabled()) {
                        z10 = false;
                        break;
                    }
                }
            }
            z10 = true;
            if (z10) {
                Log.d(SessionLifecycleClient.TAG, "Data Collection is disabled for all subscribers. Skipping this Event");
            } else {
                latestByCode = this.this$0.getLatestByCode(this.$messages, 2);
                latestByCode2 = this.this$0.getLatestByCode(this.$messages, 1);
                q10 = u.q(latestByCode, latestByCode2);
                Z = c0.Z(q10);
                J0 = c0.J0(Z, new Comparator() { // from class: com.google.firebase.sessions.SessionLifecycleClient$sendLifecycleEvents$1$invokeSuspend$$inlined$sortedBy$1
                    /* JADX WARN: Multi-variable type inference failed */
                    @Override // java.util.Comparator
                    public final int compare(T t10, T t11) {
                        int d10;
                        d10 = um.c.d(Long.valueOf(((Message) t10).getWhen()), Long.valueOf(((Message) t11).getWhen()));
                        return d10;
                    }
                });
                SessionLifecycleClient sessionLifecycleClient = this.this$0;
                Iterator it2 = J0.iterator();
                while (it2.hasNext()) {
                    sessionLifecycleClient.sendMessageToServer((Message) it2.next());
                }
            }
        }
        return n0.f34904a;
    }

    @Override // dn.p
    public final Object invoke(m0 m0Var, vm.d<? super n0> dVar) {
        return ((SessionLifecycleClient$sendLifecycleEvents$1) create(m0Var, dVar)).invokeSuspend(n0.f34904a);
    }
}
