package androidx.room.coroutines;

import android.database.SQLException;
import java.util.concurrent.atomic.AtomicBoolean;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.intrinsics.CoroutineSingletons;
import kotlin.jvm.functions.Function2;
import kotlin.jvm.internal.Ref$ObjectRef;
import kotlin.time.DurationUnit;
import kotlinx.coroutines.internal.w;

/* JADX INFO: loaded from: classes.dex */
public final class f implements c {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final h f4018a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final h f4019b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final ThreadLocal f4020c = new ThreadLocal();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final AtomicBoolean f4021d = new AtomicBoolean(false);

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final long f4022e;

    /* JADX WARN: Type inference failed for: r1v3, types: [androidx.room.coroutines.e] */
    public f(final androidx.room.b bVar) {
        long jD0;
        int i10 = zd.a.f37168c;
        DurationUnit durationUnit = DurationUnit.SECONDS;
        nb.g.i(durationUnit, "unit");
        if (durationUnit.compareTo(durationUnit) <= 0) {
            jD0 = ua.b.m(30, durationUnit, DurationUnit.NANOSECONDS) << 1;
            int i11 = zd.b.f37169a;
        } else {
            jD0 = hb.a.D0(30, durationUnit);
        }
        this.f4022e = jD0;
        final int i12 = 2;
        final String str = ":memory:";
        h hVar = new h(1, new sd.a() { // from class: androidx.room.coroutines.e
            @Override // sd.a
            public final Object invoke() throws Exception {
                int i13 = i12;
                String str2 = str;
                k3.b bVar2 = bVar;
                switch (i13) {
                    case 0:
                        k3.a aVarOpen = bVar2.open(str2);
                        ua.b.t("PRAGMA query_only = 1", aVarOpen);
                        break;
                }
                return bVar2.open(str2);
            }
        });
        this.f4018a = hVar;
        this.f4019b = hVar;
    }

    @Override // java.lang.AutoCloseable
    public final void close() {
        if (this.f4021d.compareAndSet(false, true)) {
            this.f4018a.c();
            this.f4019b.c();
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:63:0x0138  */
    /* JADX WARN: Removed duplicated region for block: B:66:0x0144 A[Catch: all -> 0x0074, TRY_LEAVE, TryCatch #2 {all -> 0x0074, blocks: (B:22:0x006f, B:59:0x011b, B:64:0x0139, B:66:0x0144, B:77:0x0192, B:78:0x019d), top: B:105:0x006f }] */
    /* JADX WARN: Removed duplicated region for block: B:77:0x0192 A[Catch: all -> 0x0074, TRY_ENTER, TryCatch #2 {all -> 0x0074, blocks: (B:22:0x006f, B:59:0x011b, B:64:0x0139, B:66:0x0144, B:77:0x0192, B:78:0x019d), top: B:105:0x006f }] */
    /* JADX WARN: Removed duplicated region for block: B:7:0x001b  */
    /* JADX WARN: Type inference failed for: r14v2 */
    /* JADX WARN: Type inference failed for: r14v3, types: [java.lang.Object] */
    /* JADX WARN: Type inference failed for: r14v6 */
    /* JADX WARN: Type inference failed for: r3v19 */
    /* JADX WARN: Type inference failed for: r3v20 */
    /* JADX WARN: Type inference failed for: r3v6, types: [androidx.room.coroutines.h, java.lang.Object] */
    /* JADX WARN: Type inference failed for: r4v12 */
    /* JADX WARN: Type inference failed for: r4v13 */
    /* JADX WARN: Type inference failed for: r4v3 */
    /* JADX WARN: Type inference failed for: r4v4 */
    /* JADX WARN: Type inference failed for: r4v5, types: [androidx.room.coroutines.h] */
    /* JADX WARN: Type inference failed for: r4v6, types: [androidx.room.coroutines.h] */
    /* JADX WARN: Type inference failed for: r4v7 */
    /* JADX WARN: Type inference failed for: r6v14, types: [T, androidx.room.coroutines.k] */
    /* JADX WARN: Type inference failed for: r9v1, types: [androidx.room.coroutines.d] */
    @Override // androidx.room.coroutines.c
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object r(boolean z10, Function2 function2, Continuation continuation) throws Throwable {
        ConnectionPoolImpl$useConnection$1 connectionPoolImpl$useConnection$1;
        Throwable th;
        ?? r42;
        Ref$ObjectRef ref$ObjectRef;
        Function2 function22;
        ?? r14;
        Ref$ObjectRef ref$ObjectRef2;
        kotlin.coroutines.h hVar;
        f fVar;
        Object obj;
        Ref$ObjectRef ref$ObjectRef3;
        ?? r43;
        final boolean z11 = z10;
        if (continuation instanceof ConnectionPoolImpl$useConnection$1) {
            connectionPoolImpl$useConnection$1 = (ConnectionPoolImpl$useConnection$1) continuation;
            int i10 = connectionPoolImpl$useConnection$1.label;
            if ((i10 & Integer.MIN_VALUE) != 0) {
                connectionPoolImpl$useConnection$1.label = i10 - Integer.MIN_VALUE;
            } else {
                connectionPoolImpl$useConnection$1 = new ConnectionPoolImpl$useConnection$1(this, continuation);
            }
        }
        Object objInvoke = connectionPoolImpl$useConnection$1.result;
        CoroutineSingletons coroutineSingletons = CoroutineSingletons.f29226a;
        int i11 = connectionPoolImpl$useConnection$1.label;
        if (i11 == 0) {
            kotlin.a.d(objInvoke);
            if (this.f4021d.get()) {
                ua.b.I(21, "Connection pool is closed");
                throw null;
            }
            ThreadLocal threadLocal = this.f4020c;
            k kVar = (k) threadLocal.get();
            eb.e eVar = b.f4011b;
            if (kVar == null) {
                b bVar = (b) connectionPoolImpl$useConnection$1.getContext().m(eVar);
                kVar = bVar != null ? bVar.f4012a : null;
            }
            if (kVar != null) {
                if (!z11 && kVar.f4040b) {
                    ua.b.I(1, "Cannot upgrade connection from reader to writer");
                    throw null;
                }
                if (connectionPoolImpl$useConnection$1.getContext().m(eVar) == null) {
                    b bVar2 = new b(kVar);
                    nb.g.i(threadLocal, "<this>");
                    kotlin.coroutines.h hVarA = kotlin.coroutines.e.a(bVar2, new w(kVar, threadLocal));
                    ConnectionPoolImpl$useConnection$2 connectionPoolImpl$useConnection$2 = new ConnectionPoolImpl$useConnection$2(function2, kVar, null);
                    connectionPoolImpl$useConnection$1.label = 1;
                    objInvoke = hb.a.G0(connectionPoolImpl$useConnection$1, hVarA, connectionPoolImpl$useConnection$2);
                    if (objInvoke == coroutineSingletons) {
                        return coroutineSingletons;
                    }
                } else {
                    connectionPoolImpl$useConnection$1.label = 2;
                    objInvoke = function2.invoke(kVar, connectionPoolImpl$useConnection$1);
                    if (objInvoke == coroutineSingletons) {
                        return coroutineSingletons;
                    }
                }
                return objInvoke;
            }
            ?? r32 = z11 ? this.f4018a : this.f4019b;
            Ref$ObjectRef ref$ObjectRef4 = new Ref$ObjectRef();
            try {
                kotlin.coroutines.h context = connectionPoolImpl$useConnection$1.getContext();
                long j10 = this.f4022e;
                ?? r92 = new sd.a() { // from class: androidx.room.coroutines.d
                    @Override // sd.a
                    public final Object invoke() {
                        f fVar2 = this.f4013a;
                        fVar2.getClass();
                        String str = z11 ? "reader" : "writer";
                        StringBuilder sb = new StringBuilder();
                        sb.append("Timed out attempting to acquire a " + str + " connection.");
                        sb.append("\n\nWriter pool:\n");
                        fVar2.f4019b.d(sb);
                        sb.append("Reader pool:");
                        sb.append('\n');
                        fVar2.f4018a.d(sb);
                        try {
                            ua.b.I(5, sb.toString());
                            throw null;
                        } catch (SQLException e10) {
                            e10.printStackTrace();
                            return kd.e.f29109a;
                        }
                    }
                };
                connectionPoolImpl$useConnection$1.L$0 = this;
                connectionPoolImpl$useConnection$1.L$1 = function2;
                connectionPoolImpl$useConnection$1.L$2 = r32;
                connectionPoolImpl$useConnection$1.L$3 = ref$ObjectRef4;
                connectionPoolImpl$useConnection$1.L$4 = context;
                connectionPoolImpl$useConnection$1.L$5 = ref$ObjectRef4;
                connectionPoolImpl$useConnection$1.Z$0 = z11;
                connectionPoolImpl$useConnection$1.label = 3;
                Object objB = r32.b(j10, r92, connectionPoolImpl$useConnection$1);
                if (objB == coroutineSingletons) {
                    return coroutineSingletons;
                }
                function22 = function2;
                r14 = r32;
                ref$ObjectRef2 = ref$ObjectRef4;
                ref$ObjectRef = ref$ObjectRef2;
                objInvoke = objB;
                hVar = context;
                fVar = this;
                g gVar = (g) objInvoke;
                gVar.getClass();
                nb.g.i(hVar, "context");
                gVar.f4025c = hVar;
                gVar.f4026d = new Throwable();
                if (fVar.f4018a == fVar.f4019b) {
                    ref$ObjectRef2.element = new k(gVar, fVar.f4018a == fVar.f4019b && z11);
                    obj = ref$ObjectRef.element;
                    if (obj != null) {
                    }
                }
            } catch (Throwable th2) {
                th = th2;
                r42 = r32;
                ref$ObjectRef = ref$ObjectRef4;
            }
        } else {
            if (i11 == 1 || i11 == 2) {
                kotlin.a.d(objInvoke);
                return objInvoke;
            }
            if (i11 == 3) {
                z11 = connectionPoolImpl$useConnection$1.Z$0;
                ref$ObjectRef2 = (Ref$ObjectRef) connectionPoolImpl$useConnection$1.L$5;
                hVar = (kotlin.coroutines.h) connectionPoolImpl$useConnection$1.L$4;
                ref$ObjectRef = (Ref$ObjectRef) connectionPoolImpl$useConnection$1.L$3;
                h hVar2 = (h) connectionPoolImpl$useConnection$1.L$2;
                function22 = (Function2) connectionPoolImpl$useConnection$1.L$1;
                fVar = (f) connectionPoolImpl$useConnection$1.L$0;
                try {
                    kotlin.a.d(objInvoke);
                    r14 = hVar2;
                    g gVar2 = (g) objInvoke;
                    gVar2.getClass();
                    nb.g.i(hVar, "context");
                    gVar2.f4025c = hVar;
                    gVar2.f4026d = new Throwable();
                    ref$ObjectRef2.element = new k(gVar2, fVar.f4018a == fVar.f4019b && z11);
                    obj = ref$ObjectRef.element;
                    if (obj != null) {
                        throw new IllegalArgumentException("Required value was null.".toString());
                    }
                    k kVar2 = (k) obj;
                    b bVar3 = new b(kVar2);
                    ThreadLocal threadLocal2 = fVar.f4020c;
                    nb.g.i(threadLocal2, "<this>");
                    kotlin.coroutines.h hVarA2 = kotlin.coroutines.e.a(bVar3, new w(kVar2, threadLocal2));
                    ConnectionPoolImpl$useConnection$4 connectionPoolImpl$useConnection$4 = new ConnectionPoolImpl$useConnection$4(function22, ref$ObjectRef, null);
                    connectionPoolImpl$useConnection$1.L$0 = r14;
                    connectionPoolImpl$useConnection$1.L$1 = ref$ObjectRef;
                    connectionPoolImpl$useConnection$1.L$2 = null;
                    connectionPoolImpl$useConnection$1.L$3 = null;
                    connectionPoolImpl$useConnection$1.L$4 = null;
                    connectionPoolImpl$useConnection$1.L$5 = null;
                    connectionPoolImpl$useConnection$1.label = 4;
                    objInvoke = hb.a.G0(connectionPoolImpl$useConnection$1, hVarA2, connectionPoolImpl$useConnection$4);
                    if (objInvoke == coroutineSingletons) {
                        return coroutineSingletons;
                    }
                    ref$ObjectRef3 = ref$ObjectRef;
                    r43 = r14;
                } catch (Throwable th3) {
                    th = th3;
                    r42 = hVar2;
                }
            } else {
                if (i11 != 4) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                ref$ObjectRef3 = (Ref$ObjectRef) connectionPoolImpl$useConnection$1.L$1;
                h hVar3 = (h) connectionPoolImpl$useConnection$1.L$0;
                try {
                    kotlin.a.d(objInvoke);
                    r43 = hVar3;
                } catch (Throwable th4) {
                    ref$ObjectRef = ref$ObjectRef3;
                    th = th4;
                    r42 = hVar3;
                }
            }
            try {
                throw th;
            } finally {
            }
        }
        try {
            k kVar3 = (k) ref$ObjectRef3.element;
            if (kVar3 != null) {
                g gVar3 = kVar3.f4039a;
                if (kVar3.f4042d.compareAndSet(false, true)) {
                    try {
                        ua.b.t("ROLLBACK TRANSACTION", gVar3);
                    } catch (SQLException unused) {
                    }
                }
                gVar3.f4025c = null;
                gVar3.f4026d = null;
                r43.e(gVar3);
            }
        } catch (Throwable unused2) {
        }
        return objInvoke;
    }

    /* JADX WARN: Type inference failed for: r0v5, types: [androidx.room.coroutines.e] */
    /* JADX WARN: Type inference failed for: r2v2, types: [androidx.room.coroutines.e] */
    public f(final androidx.room.b bVar, final String str, int i10) {
        long jD0;
        final int i11 = 0;
        int i12 = zd.a.f37168c;
        DurationUnit durationUnit = DurationUnit.SECONDS;
        nb.g.i(durationUnit, "unit");
        final int i13 = 1;
        if (durationUnit.compareTo(durationUnit) <= 0) {
            jD0 = ua.b.m(30, durationUnit, DurationUnit.NANOSECONDS) << 1;
            int i14 = zd.b.f37169a;
        } else {
            jD0 = hb.a.D0(30, durationUnit);
        }
        this.f4022e = jD0;
        if (i10 > 0) {
            this.f4018a = new h(i10, new sd.a() { // from class: androidx.room.coroutines.e
                @Override // sd.a
                public final Object invoke() throws Exception {
                    int i132 = i11;
                    String str2 = str;
                    k3.b bVar2 = bVar;
                    switch (i132) {
                        case 0:
                            k3.a aVarOpen = bVar2.open(str2);
                            ua.b.t("PRAGMA query_only = 1", aVarOpen);
                            break;
                    }
                    return bVar2.open(str2);
                }
            });
            this.f4019b = new h(1, new sd.a() { // from class: androidx.room.coroutines.e
                @Override // sd.a
                public final Object invoke() throws Exception {
                    int i132 = i13;
                    String str2 = str;
                    k3.b bVar2 = bVar;
                    switch (i132) {
                        case 0:
                            k3.a aVarOpen = bVar2.open(str2);
                            ua.b.t("PRAGMA query_only = 1", aVarOpen);
                            break;
                    }
                    return bVar2.open(str2);
                }
            });
            return;
        }
        throw new IllegalArgumentException("Maximum number of readers must be greater than 0".toString());
    }
}
