package com.google.firebase.sessions;

import android.content.Context;
import dn.p;
import i4.d;
import java.util.concurrent.atomic.AtomicReference;
import kotlin.Metadata;
import kotlin.coroutines.jvm.internal.l;
import kotlin.jvm.internal.i0;
import kotlin.jvm.internal.k;
import kotlin.jvm.internal.p0;
import kotlin.jvm.internal.t;
import rm.n0;
import rm.y;
import tp.m0;

@Metadata(d1 = {"\u0000B\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000e\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0007\b\u0000\u0018\u0000 \u00192\u00020\u0001:\u0002\u0019\u001aB\u0017\u0012\u0006\u0010\f\u001a\u00020\u000b\u0012\u0006\u0010\u000f\u001a\u00020\u000e¢\u0006\u0004\b\u0017\u0010\u0018J\u0010\u0010\u0005\u001a\u00020\u00042\u0006\u0010\u0003\u001a\u00020\u0002H\u0002J\u0010\u0010\t\u001a\u00020\b2\u0006\u0010\u0007\u001a\u00020\u0006H\u0016J\n\u0010\n\u001a\u0004\u0018\u00010\u0006H\u0016R\u0014\u0010\f\u001a\u00020\u000b8\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\f\u0010\rR\u0014\u0010\u000f\u001a\u00020\u000e8\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u000f\u0010\u0010R\u001a\u0010\u0012\u001a\b\u0012\u0004\u0012\u00020\u00040\u00118\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u0012\u0010\u0013R\u001a\u0010\u0015\u001a\b\u0012\u0004\u0012\u00020\u00040\u00148\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u0015\u0010\u0016¨\u0006\u001b"}, d2 = {"Lcom/google/firebase/sessions/SessionDatastoreImpl;", "Lcom/google/firebase/sessions/SessionDatastore;", "Li4/d;", "preferences", "Lcom/google/firebase/sessions/FirebaseSessionsData;", "mapSessionsData", "", "sessionId", "Lrm/n0;", "updateSessionId", "getCurrentSessionId", "Landroid/content/Context;", "context", "Landroid/content/Context;", "Lvm/g;", "backgroundDispatcher", "Lvm/g;", "Ljava/util/concurrent/atomic/AtomicReference;", "currentSessionFromDatastore", "Ljava/util/concurrent/atomic/AtomicReference;", "Lwp/e;", "firebaseSessionDataFlow", "Lwp/e;", "<init>", "(Landroid/content/Context;Lvm/g;)V", "Companion", "FirebaseSessionDataKeys", "com.google.firebase-firebase-sessions"}, k = 1, mv = {1, 8, 0})
/* loaded from: classes3.dex */
public final class SessionDatastoreImpl implements SessionDatastore {
    private static final String TAG = "FirebaseSessionsRepo";
    private final vm.g backgroundDispatcher;
    private final Context context;
    private final AtomicReference<FirebaseSessionsData> currentSessionFromDatastore;
    private final wp.e firebaseSessionDataFlow;
    private static final Companion Companion = new Companion(null);
    private static final gn.c dataStore$delegate = h4.a.b(SessionDataStoreConfigs.INSTANCE.getSESSIONS_CONFIG_NAME(), new g4.b(SessionDatastoreImpl$Companion$dataStore$2.INSTANCE), null, null, 12, null);

    @kotlin.coroutines.jvm.internal.f(c = "com.google.firebase.sessions.SessionDatastoreImpl$1", f = "SessionDatastore.kt", l = {82}, 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})
    /* renamed from: com.google.firebase.sessions.SessionDatastoreImpl$1, reason: invalid class name */
    /* loaded from: classes3.dex */
    static final class AnonymousClass1 extends l implements p {
        int label;

        AnonymousClass1(vm.d<? super AnonymousClass1> dVar) {
            super(2, dVar);
        }

        @Override // kotlin.coroutines.jvm.internal.a
        public final vm.d<n0> create(Object obj, vm.d<?> dVar) {
            return new AnonymousClass1(dVar);
        }

        @Override // kotlin.coroutines.jvm.internal.a
        public final Object invokeSuspend(Object obj) {
            Object f10;
            f10 = wm.d.f();
            int i10 = this.label;
            if (i10 == 0) {
                y.b(obj);
                wp.e eVar = SessionDatastoreImpl.this.firebaseSessionDataFlow;
                final SessionDatastoreImpl sessionDatastoreImpl = SessionDatastoreImpl.this;
                wp.f fVar = new wp.f() { // from class: com.google.firebase.sessions.SessionDatastoreImpl.1.1
                    public final Object emit(FirebaseSessionsData firebaseSessionsData, vm.d<? super n0> dVar) {
                        SessionDatastoreImpl.this.currentSessionFromDatastore.set(firebaseSessionsData);
                        return n0.f34904a;
                    }

                    @Override // wp.f
                    public /* bridge */ /* synthetic */ Object emit(Object obj2, vm.d dVar) {
                        return emit((FirebaseSessionsData) obj2, (vm.d<? super n0>) dVar);
                    }
                };
                this.label = 1;
                if (eVar.collect(fVar, this) == f10) {
                    return f10;
                }
            } else {
                if (i10 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                y.b(obj);
            }
            return n0.f34904a;
        }

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

    /* JADX INFO: Access modifiers changed from: private */
    @Metadata(d1 = {"\u0000 \n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0005\n\u0002\u0010\u000e\n\u0002\b\u0005\b\u0082\u0003\u0018\u00002\u00020\u0001B\t\b\u0002¢\u0006\u0004\b\r\u0010\u000eR%\u0010\t\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003*\u00020\u00028BX\u0082\u0084\u0002¢\u0006\f\n\u0004\b\u0005\u0010\u0006\u001a\u0004\b\u0007\u0010\bR\u0014\u0010\u000b\u001a\u00020\n8\u0002X\u0082T¢\u0006\u0006\n\u0004\b\u000b\u0010\f¨\u0006\u000f"}, d2 = {"Lcom/google/firebase/sessions/SessionDatastoreImpl$Companion;", "", "Landroid/content/Context;", "Lf4/f;", "Li4/d;", "dataStore$delegate", "Lgn/c;", "getDataStore", "(Landroid/content/Context;)Lf4/f;", "dataStore", "", "TAG", "Ljava/lang/String;", "<init>", "()V", "com.google.firebase-firebase-sessions"}, k = 1, mv = {1, 8, 0})
    /* loaded from: classes3.dex */
    public static final class Companion {
        static final /* synthetic */ kn.l[] $$delegatedProperties = {p0.i(new i0(Companion.class, "dataStore", "getDataStore(Landroid/content/Context;)Landroidx/datastore/core/DataStore;", 0))};

        private Companion() {
        }

        public /* synthetic */ Companion(k kVar) {
            this();
        }

        /* JADX INFO: Access modifiers changed from: private */
        public final f4.f getDataStore(Context context) {
            return (f4.f) SessionDatastoreImpl.dataStore$delegate.a(context, $$delegatedProperties[0]);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    @Metadata(d1 = {"\u0000\u0014\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0018\u0002\n\u0002\u0010\u000e\n\u0002\b\u0007\bÂ\u0002\u0018\u00002\u00020\u0001B\t\b\u0002¢\u0006\u0004\b\b\u0010\tR\u001d\u0010\u0004\u001a\b\u0012\u0004\u0012\u00020\u00030\u00028\u0006¢\u0006\f\n\u0004\b\u0004\u0010\u0005\u001a\u0004\b\u0006\u0010\u0007¨\u0006\n"}, d2 = {"Lcom/google/firebase/sessions/SessionDatastoreImpl$FirebaseSessionDataKeys;", "", "Li4/d$a;", "", "SESSION_ID", "Li4/d$a;", "getSESSION_ID", "()Li4/d$a;", "<init>", "()V", "com.google.firebase-firebase-sessions"}, k = 1, mv = {1, 8, 0})
    /* loaded from: classes3.dex */
    public static final class FirebaseSessionDataKeys {
        public static final FirebaseSessionDataKeys INSTANCE = new FirebaseSessionDataKeys();
        private static final d.a SESSION_ID = i4.f.f("session_id");

        private FirebaseSessionDataKeys() {
        }

        public final d.a getSESSION_ID() {
            return SESSION_ID;
        }
    }

    public SessionDatastoreImpl(Context context, vm.g backgroundDispatcher) {
        t.g(context, "context");
        t.g(backgroundDispatcher, "backgroundDispatcher");
        this.context = context;
        this.backgroundDispatcher = backgroundDispatcher;
        this.currentSessionFromDatastore = new AtomicReference<>();
        final wp.e f10 = wp.g.f(Companion.getDataStore(context).getData(), new SessionDatastoreImpl$firebaseSessionDataFlow$1(null));
        this.firebaseSessionDataFlow = new wp.e() { // from class: com.google.firebase.sessions.SessionDatastoreImpl$special$$inlined$map$1

            @Metadata(d1 = {"\u0000\f\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0003\u0010\u0006\u001a\u00020\u0003\"\u0004\b\u0000\u0010\u0000\"\u0004\b\u0001\u0010\u00012\u0006\u0010\u0002\u001a\u00028\u0000H\u008a@¢\u0006\u0004\b\u0004\u0010\u0005"}, d2 = {"T", "R", "value", "Lrm/n0;", "emit", "(Ljava/lang/Object;Lvm/d;)Ljava/lang/Object;", "<anonymous>"}, k = 3, mv = {1, 8, 0})
            /* renamed from: com.google.firebase.sessions.SessionDatastoreImpl$special$$inlined$map$1$2, reason: invalid class name */
            /* loaded from: classes3.dex */
            public static final class AnonymousClass2<T> implements wp.f {
                final /* synthetic */ wp.f $this_unsafeFlow;
                final /* synthetic */ SessionDatastoreImpl this$0;

                @kotlin.coroutines.jvm.internal.f(c = "com.google.firebase.sessions.SessionDatastoreImpl$special$$inlined$map$1$2", f = "SessionDatastore.kt", l = {223}, m = "emit")
                @Metadata(k = 3, mv = {1, 8, 0}, xi = 48)
                /* renamed from: com.google.firebase.sessions.SessionDatastoreImpl$special$$inlined$map$1$2$1, reason: invalid class name */
                /* loaded from: classes3.dex */
                public static final class AnonymousClass1 extends kotlin.coroutines.jvm.internal.d {
                    Object L$0;
                    int label;
                    /* synthetic */ Object result;

                    public AnonymousClass1(vm.d dVar) {
                        super(dVar);
                    }

                    @Override // kotlin.coroutines.jvm.internal.a
                    public final Object invokeSuspend(Object obj) {
                        this.result = obj;
                        this.label |= Integer.MIN_VALUE;
                        return AnonymousClass2.this.emit(null, this);
                    }
                }

                public AnonymousClass2(wp.f fVar, SessionDatastoreImpl sessionDatastoreImpl) {
                    this.$this_unsafeFlow = fVar;
                    this.this$0 = sessionDatastoreImpl;
                }

                /* JADX WARN: Removed duplicated region for block: B:15:0x0031  */
                /* JADX WARN: Removed duplicated region for block: B:8:0x0023  */
                @Override // wp.f
                /*
                    Code decompiled incorrectly, please refer to instructions dump.
                */
                public final Object emit(Object obj, vm.d dVar) {
                    AnonymousClass1 anonymousClass1;
                    Object f10;
                    int i10;
                    FirebaseSessionsData mapSessionsData;
                    if (dVar instanceof AnonymousClass1) {
                        anonymousClass1 = (AnonymousClass1) dVar;
                        int i11 = anonymousClass1.label;
                        if ((i11 & Integer.MIN_VALUE) != 0) {
                            anonymousClass1.label = i11 - Integer.MIN_VALUE;
                            Object obj2 = anonymousClass1.result;
                            f10 = wm.d.f();
                            i10 = anonymousClass1.label;
                            if (i10 != 0) {
                                y.b(obj2);
                                wp.f fVar = this.$this_unsafeFlow;
                                mapSessionsData = this.this$0.mapSessionsData((i4.d) obj);
                                anonymousClass1.label = 1;
                                if (fVar.emit(mapSessionsData, anonymousClass1) == f10) {
                                    return f10;
                                }
                            } else {
                                if (i10 != 1) {
                                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                                }
                                y.b(obj2);
                            }
                            return n0.f34904a;
                        }
                    }
                    anonymousClass1 = new AnonymousClass1(dVar);
                    Object obj22 = anonymousClass1.result;
                    f10 = wm.d.f();
                    i10 = anonymousClass1.label;
                    if (i10 != 0) {
                    }
                    return n0.f34904a;
                }
            }

            @Override // wp.e
            public Object collect(wp.f fVar, vm.d dVar) {
                Object f11;
                Object collect = wp.e.this.collect(new AnonymousClass2(fVar, this), dVar);
                f11 = wm.d.f();
                return collect == f11 ? collect : n0.f34904a;
            }
        };
        tp.k.d(tp.n0.a(backgroundDispatcher), null, null, new AnonymousClass1(null), 3, null);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final FirebaseSessionsData mapSessionsData(i4.d preferences) {
        return new FirebaseSessionsData((String) preferences.b(FirebaseSessionDataKeys.INSTANCE.getSESSION_ID()));
    }

    @Override // com.google.firebase.sessions.SessionDatastore
    public String getCurrentSessionId() {
        FirebaseSessionsData firebaseSessionsData = this.currentSessionFromDatastore.get();
        if (firebaseSessionsData != null) {
            return firebaseSessionsData.getSessionId();
        }
        return null;
    }

    @Override // com.google.firebase.sessions.SessionDatastore
    public void updateSessionId(String sessionId) {
        t.g(sessionId, "sessionId");
        tp.k.d(tp.n0.a(this.backgroundDispatcher), null, null, new SessionDatastoreImpl$updateSessionId$1(this, sessionId, null), 3, null);
    }
}
