package Q1;

import O1.G;
import O1.H;
import android.database.SQLException;
import com.google.android.gms.internal.measurement.C1;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicBoolean;

/* JADX INFO: loaded from: classes.dex */
public final class s implements H, t {

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final B6.i f5630c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final AtomicBoolean f5631d;

    public s(i iVar, boolean z8) {
        P6.g.e(iVar, "delegate");
        this.f5628a = iVar;
        this.f5629b = z8;
        this.f5630c = new B6.i();
        this.f5631d = new AtomicBoolean(false);
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    @Override // O1.InterfaceC0243t
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object a(String str, O6.l lVar, G6.c cVar) throws Throwable {
        r rVar;
        i iVar;
        s sVar;
        if (cVar instanceof r) {
            rVar = (r) cVar;
            int i = rVar.f5627z;
            if ((i & Integer.MIN_VALUE) != 0) {
                rVar.f5627z = i - Integer.MIN_VALUE;
            } else {
                rVar = new r(this, cVar);
            }
        }
        Object obj = rVar.f5625x;
        F6.a aVar = F6.a.f2580q;
        int i5 = rVar.f5627z;
        if (i5 == 0) {
            C1.N(obj);
            if (this.f5631d.get()) {
                android.support.v4.media.session.a.t(21, "Connection is recycled");
                throw null;
            }
            E6.i iVar2 = rVar.f3016r;
            P6.g.b(iVar2);
            a aVar2 = (a) iVar2.K(a.f5551r);
            if (aVar2 == null || aVar2.f5552q != this) {
                android.support.v4.media.session.a.t(21, "Attempted to use connection on a different coroutine");
                throw null;
            }
            rVar.f5621t = this;
            rVar.f5622u = str;
            rVar.f5623v = lVar;
            iVar = this.f5628a;
            rVar.f5624w = iVar;
            rVar.f5627z = 1;
            if (iVar.f5582r.b(rVar) == aVar) {
                return aVar;
            }
            sVar = this;
        } else {
            if (i5 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            i iVar3 = rVar.f5624w;
            lVar = rVar.f5623v;
            String str2 = rVar.f5622u;
            sVar = rVar.f5621t;
            C1.N(obj);
            iVar = iVar3;
            str = str2;
        }
        try {
            l lVar2 = new l(sVar, sVar.f5628a.h0(str));
            try {
                Object objB = lVar.b(lVar2);
                W3.b.j(lVar2, null);
                return objB;
            } finally {
            }
        } finally {
            iVar.a(null);
        }
    }

    @Override // O1.H
    public final Object b(G6.i iVar) {
        if (this.f5631d.get()) {
            android.support.v4.media.session.a.t(21, "Connection is recycled");
            throw null;
        }
        E6.i iVar2 = iVar.f3016r;
        P6.g.b(iVar2);
        a aVar = (a) iVar2.K(a.f5551r);
        if (aVar != null && aVar.f5552q == this) {
            return Boolean.valueOf(!this.f5630c.isEmpty());
        }
        android.support.v4.media.session.a.t(21, "Attempted to use connection on a different coroutine");
        throw null;
    }

    @Override // Q1.t
    public final X1.a c() {
        return this.f5628a;
    }

    @Override // O1.H
    public final Object d(G g8, O6.p pVar, G6.i iVar) {
        if (this.f5631d.get()) {
            android.support.v4.media.session.a.t(21, "Connection is recycled");
            throw null;
        }
        E6.i iVar2 = iVar.f3016r;
        P6.g.b(iVar2);
        a aVar = (a) iVar2.K(a.f5551r);
        if (aVar != null && aVar.f5552q == this) {
            return g(g8, pVar, iVar);
        }
        android.support.v4.media.session.a.t(21, "Attempted to use connection on a different coroutine");
        throw null;
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0015  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object e(G g8, G6.c cVar) throws Throwable {
        o oVar;
        i iVar;
        s sVar;
        if (cVar instanceof o) {
            oVar = (o) cVar;
            int i = oVar.f5608y;
            if ((i & Integer.MIN_VALUE) != 0) {
                oVar.f5608y = i - Integer.MIN_VALUE;
            } else {
                oVar = new o(this, cVar);
            }
        }
        Object obj = oVar.f5606w;
        F6.a aVar = F6.a.f2580q;
        int i5 = oVar.f5608y;
        if (i5 == 0) {
            C1.N(obj);
            oVar.f5603t = this;
            oVar.f5604u = g8;
            iVar = this.f5628a;
            oVar.f5605v = iVar;
            oVar.f5608y = 1;
            if (iVar.f5582r.b(oVar) == aVar) {
                return aVar;
            }
            sVar = this;
        } else {
            if (i5 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            i iVar2 = oVar.f5605v;
            G g9 = oVar.f5604u;
            sVar = oVar.f5603t;
            C1.N(obj);
            iVar = iVar2;
            g8 = g9;
        }
        try {
            B6.i iVar3 = sVar.f5630c;
            int i8 = iVar3.f1028s;
            boolean zIsEmpty = iVar3.isEmpty();
            i iVar4 = sVar.f5628a;
            if (zIsEmpty) {
                int iOrdinal = g8.ordinal();
                if (iOrdinal == 0) {
                    android.support.v4.media.session.a.i(iVar4, "BEGIN DEFERRED TRANSACTION");
                } else if (iOrdinal == 1) {
                    android.support.v4.media.session.a.i(iVar4, "BEGIN IMMEDIATE TRANSACTION");
                } else {
                    if (iOrdinal != 2) {
                        throw new A6.b();
                    }
                    android.support.v4.media.session.a.i(iVar4, "BEGIN EXCLUSIVE TRANSACTION");
                }
            } else {
                android.support.v4.media.session.a.i(iVar4, "SAVEPOINT '" + i8 + '\'');
            }
            iVar3.addLast(new n(i8));
            A6.n nVar = A6.n.f457a;
            iVar.a(null);
            return nVar;
        } catch (Throwable th) {
            iVar.a(null);
            throw th;
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0017  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object f(boolean z8, G6.c cVar) throws Throwable {
        p pVar;
        s sVar;
        i iVar;
        if (cVar instanceof p) {
            pVar = (p) cVar;
            int i = pVar.f5614y;
            if ((i & Integer.MIN_VALUE) != 0) {
                pVar.f5614y = i - Integer.MIN_VALUE;
            } else {
                pVar = new p(this, cVar);
            }
        }
        Object obj = pVar.f5612w;
        F6.a aVar = F6.a.f2580q;
        int i5 = pVar.f5614y;
        if (i5 == 0) {
            C1.N(obj);
            pVar.f5609t = this;
            i iVar2 = this.f5628a;
            pVar.f5610u = iVar2;
            pVar.f5611v = z8;
            pVar.f5614y = 1;
            if (iVar2.f5582r.b(pVar) == aVar) {
                return aVar;
            }
            sVar = this;
            iVar = iVar2;
        } else {
            if (i5 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            z8 = pVar.f5611v;
            iVar = pVar.f5610u;
            sVar = pVar.f5609t;
            C1.N(obj);
        }
        try {
            B6.i iVar3 = sVar.f5630c;
            if (iVar3.isEmpty()) {
                throw new IllegalStateException("Not in a transaction");
            }
            P6.g.e(iVar3, "<this>");
            if (iVar3.isEmpty()) {
                throw new NoSuchElementException("List is empty.");
            }
            n nVar = (n) iVar3.remove(B6.l.V(iVar3));
            i iVar4 = sVar.f5628a;
            if (z8) {
                nVar.getClass();
                if (iVar3.isEmpty()) {
                    android.support.v4.media.session.a.i(iVar4, "END TRANSACTION");
                } else {
                    android.support.v4.media.session.a.i(iVar4, "RELEASE SAVEPOINT '" + nVar.f5602a + '\'');
                }
            } else if (iVar3.isEmpty()) {
                android.support.v4.media.session.a.i(iVar4, "ROLLBACK TRANSACTION");
            } else {
                android.support.v4.media.session.a.i(iVar4, "ROLLBACK TRANSACTION TO SAVEPOINT '" + nVar.f5602a + '\'');
            }
            A6.n nVar2 = A6.n.f457a;
            iVar.a(null);
            return nVar2;
        } catch (Throwable th) {
            iVar.a(null);
            throw th;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:45:0x009f  */
    /* JADX WARN: Removed duplicated region for block: B:49:0x00ab A[RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:61:0x00c6  */
    /* JADX WARN: Removed duplicated region for block: B:63:0x00ca  */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object g(G g8, O6.p pVar, G6.c cVar) throws Throwable {
        q qVar;
        s sVar;
        s sVar2;
        int i;
        SQLException e8;
        Throwable th;
        if (cVar instanceof q) {
            qVar = (q) cVar;
            int i5 = qVar.f5620y;
            if ((i5 & Integer.MIN_VALUE) != 0) {
                qVar.f5620y = i5 - Integer.MIN_VALUE;
            } else {
                qVar = new q(this, cVar);
            }
        }
        Object objK = qVar.f5618w;
        F6.a aVar = F6.a.f2580q;
        int i8 = qVar.f5620y;
        try {
            if (i8 == 0) {
                C1.N(objK);
                if (g8 == null) {
                    g8 = G.f4943q;
                }
                qVar.f5615t = this;
                qVar.f5616u = pVar;
                qVar.f5620y = 1;
                if (e(g8, qVar) != aVar) {
                    sVar = this;
                }
            }
            if (i8 == 1) {
                pVar = (O6.p) qVar.f5616u;
                sVar = (s) qVar.f5615t;
                C1.N(objK);
            } else {
                if (i8 != 2) {
                    if (i8 == 3) {
                        Object obj = qVar.f5615t;
                        C1.N(objK);
                        return obj;
                    }
                    if (i8 == 4) {
                        Object obj2 = qVar.f5615t;
                        C1.N(objK);
                        return obj2;
                    }
                    if (i8 != 5) {
                        throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                    }
                    th = (Throwable) qVar.f5616u;
                    th = (Throwable) qVar.f5615t;
                    try {
                        C1.N(objK);
                        throw th;
                    } catch (SQLException e9) {
                        e8 = e9;
                        if (th != null) {
                            throw e8;
                        }
                        android.support.v4.media.session.a.a(th, e8);
                        throw th;
                    }
                }
                i = qVar.f5617v;
                sVar2 = (s) qVar.f5615t;
                try {
                    C1.N(objK);
                    boolean z8 = i != 0;
                    qVar.f5615t = objK;
                    qVar.f5620y = 3;
                    return sVar2.f(z8, qVar) != aVar ? aVar : objK;
                } catch (Throwable th2) {
                    th = th2;
                    sVar = sVar2;
                    try {
                        throw th;
                    } catch (Throwable th3) {
                        try {
                            qVar.f5615t = th;
                            qVar.f5616u = th3;
                            qVar.f5620y = 5;
                            if (sVar.f(false, qVar) != aVar) {
                                throw th3;
                            }
                        } catch (SQLException e10) {
                            e8 = e10;
                            th = th3;
                            if (th != null) {
                            }
                        }
                    }
                }
            }
            m mVar = new m(0, sVar);
            qVar.f5615t = sVar;
            qVar.f5616u = null;
            qVar.f5617v = 1;
            qVar.f5620y = 2;
            objK = pVar.k(mVar, qVar);
            if (objK != aVar) {
                sVar2 = sVar;
                i = 1;
                if (i != 0) {
                }
                qVar.f5615t = objK;
                qVar.f5620y = 3;
                if (sVar2.f(z8, qVar) != aVar) {
                }
            }
        } catch (Throwable th4) {
            th = th4;
            throw th;
        }
    }
}
