package O1;

import android.content.Context;
import com.google.android.gms.internal.measurement.C1;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/* JADX INFO: loaded from: classes.dex */
public final class w {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public boolean f5079a;

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final C0226b f5081c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final N1.M f5082d;

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final Q1.b f5084f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public Y1.a f5085g;

    public w(C0226b c0226b, M5.b bVar) {
        this.f5081c = c0226b;
        this.f5082d = new u(-1, "", "");
        List list = B6.t.f1032q;
        List list2 = c0226b.f5016e;
        this.f5083e = list2 == null ? list : list2;
        ArrayList arrayListR0 = B6.k.r0(list2 != null ? list2 : list, new v(new B6.a(1, this)));
        Context context = c0226b.f5012a;
        y yVar = c0226b.f5015d;
        P6.g.e(yVar, "migrationContainer");
        bVar.b(new C0226b(context, c0226b.f5013b, c0226b.f5014c, yVar, arrayListR0, c0226b.f5017f, c0226b.f5018g, c0226b.f5019h, c0226b.i, c0226b.f5020j, c0226b.f5021k, c0226b.f5022l, c0226b.f5023m, c0226b.f5024n, c0226b.f5025o, c0226b.f5026p, c0226b.f5027q, c0226b.f5028r, c0226b.f5029s, c0226b.f5030t, c0226b.f5031u));
        throw null;
    }

    public static final void a(w wVar, X1.a aVar) throws Exception {
        Object objJ;
        x xVar = wVar.f5081c.f5018g;
        x xVar2 = x.f5087r;
        if (xVar == xVar2) {
            android.support.v4.media.session.a.i(aVar, "PRAGMA journal_mode = WAL");
        } else {
            android.support.v4.media.session.a.i(aVar, "PRAGMA journal_mode = TRUNCATE");
        }
        if (wVar.f5081c.f5018g == xVar2) {
            android.support.v4.media.session.a.i(aVar, "PRAGMA synchronous = NORMAL");
        } else {
            android.support.v4.media.session.a.i(aVar, "PRAGMA synchronous = FULL");
        }
        b(aVar);
        X1.c cVarH0 = aVar.h0("PRAGMA user_version");
        try {
            cVarH0.W();
            int iQ = (int) cVarH0.q(0);
            W3.b.j(cVarH0, null);
            N1.M m8 = wVar.f5082d;
            if (iQ != m8.f4478a) {
                android.support.v4.media.session.a.i(aVar, "BEGIN EXCLUSIVE TRANSACTION");
                int i = m8.f4478a;
                try {
                    if (iQ == 0) {
                        wVar.d(aVar);
                    } else {
                        wVar.e(aVar, iQ, i);
                    }
                    android.support.v4.media.session.a.i(aVar, "PRAGMA user_version = " + i);
                    objJ = A6.n.f457a;
                } catch (Throwable th) {
                    objJ = C1.j(th);
                }
                if (!(objJ instanceof A6.i)) {
                    android.support.v4.media.session.a.i(aVar, "END TRANSACTION");
                }
                Throwable thA = A6.j.a(objJ);
                if (thA != null) {
                    android.support.v4.media.session.a.i(aVar, "ROLLBACK TRANSACTION");
                    throw thA;
                }
            }
            wVar.f(aVar);
        } finally {
        }
    }

    public static void b(X1.a aVar) {
        X1.c cVarH0 = aVar.h0("PRAGMA busy_timeout");
        try {
            cVarH0.W();
            long jQ = cVarH0.q(0);
            W3.b.j(cVarH0, null);
            if (jQ < 3000) {
                android.support.v4.media.session.a.i(aVar, "PRAGMA busy_timeout = 3000");
            }
        } catch (Throwable th) {
            try {
                throw th;
            } catch (Throwable th2) {
                W3.b.j(cVarH0, th);
                throw th2;
            }
        }
    }

    public final Y1.b c() {
        P.g gVar;
        Q1.b bVar = this.f5084f;
        R1.b bVar2 = bVar instanceof R1.b ? (R1.b) bVar : null;
        if (bVar2 == null || (gVar = bVar2.f6028q) == null) {
            return null;
        }
        return (Y1.b) gVar.f5246r;
    }

    public final void d(X1.a aVar) {
        P6.g.e(aVar, "connection");
        X1.c cVarH0 = aVar.h0("SELECT count(*) FROM sqlite_master WHERE name != 'android_metadata'");
        try {
            boolean z8 = false;
            if (cVarH0.W()) {
                if (cVarH0.q(0) == 0) {
                    z8 = true;
                }
            }
            W3.b.j(cVarH0, null);
            N1.M m8 = this.f5082d;
            m8.a(aVar);
            if (!z8) {
                L2.a aVarW = m8.w(aVar);
                if (!aVarW.f3978b) {
                    throw new IllegalStateException(("Pre-packaged database has an invalid schema: " + aVarW.f3979c).toString());
                }
            }
            g(aVar);
            m8.s(aVar);
            Iterator it = this.f5083e.iterator();
            while (it.hasNext()) {
                ((v) it.next()).getClass();
                if (aVar instanceof R1.a) {
                    P6.g.e(((R1.a) aVar).f6027q, "db");
                }
            }
        } finally {
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:134:0x009b A[EDGE_INSN: B:134:0x009b->B:39:0x009b BREAK  A[LOOP:4: B:9:0x0022->B:138:?], SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:15:0x002d  */
    /* JADX WARN: Removed duplicated region for block: B:17:0x0039  */
    /* JADX WARN: Removed duplicated region for block: B:19:0x0045  */
    /* JADX WARN: Removed duplicated region for block: B:25:0x005e  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void e(X1.a aVar, int i, int i5) {
        Iterable iterable;
        A6.h hVar;
        boolean z8;
        Set set;
        P6.g.e(aVar, "connection");
        C0226b c0226b = this.f5081c;
        y yVar = c0226b.f5015d;
        P6.g.e(yVar, "<this>");
        if (i == i5) {
            iterable = B6.t.f1032q;
        } else {
            boolean z9 = i5 > i;
            ArrayList arrayList = new ArrayList();
            int i8 = i;
            do {
                if (z9) {
                    if (i8 >= i5) {
                        iterable = arrayList;
                        break;
                    }
                    LinkedHashMap linkedHashMap = yVar.f5089a;
                    if (z9) {
                        TreeMap treeMap = (TreeMap) linkedHashMap.get(Integer.valueOf(i8));
                        if (treeMap != null) {
                            hVar = new A6.h(treeMap, treeMap.keySet());
                        }
                        if (hVar != null) {
                        }
                    } else {
                        TreeMap treeMap2 = (TreeMap) linkedHashMap.get(Integer.valueOf(i8));
                        hVar = treeMap2 == null ? null : new A6.h(treeMap2, treeMap2.descendingKeySet());
                        if (hVar != null) {
                            break;
                        }
                        Map map = (Map) hVar.f446q;
                        Iterator it = ((Iterable) hVar.f447r).iterator();
                        while (it.hasNext()) {
                            int iIntValue = ((Number) it.next()).intValue();
                            if (!z9) {
                                if (i5 <= iIntValue && iIntValue < i8) {
                                    Object obj = map.get(Integer.valueOf(iIntValue));
                                    P6.g.b(obj);
                                    arrayList.add(obj);
                                    z8 = true;
                                    i8 = iIntValue;
                                    break;
                                    break;
                                }
                            } else if (i8 + 1 <= iIntValue && iIntValue <= i5) {
                                Object obj2 = map.get(Integer.valueOf(iIntValue));
                                P6.g.b(obj2);
                                arrayList.add(obj2);
                                z8 = true;
                                i8 = iIntValue;
                                break;
                            }
                        }
                        z8 = false;
                    }
                } else {
                    if (i8 <= i5) {
                        iterable = arrayList;
                        break;
                    }
                    LinkedHashMap linkedHashMap2 = yVar.f5089a;
                    if (z9) {
                    }
                }
            } while (z8);
            iterable = null;
        }
        N1.M m8 = this.f5082d;
        if (iterable != null) {
            m8.v(aVar);
            Iterator it2 = iterable.iterator();
            while (it2.hasNext()) {
                ((Y4.a) it2.next()).getClass();
                if (!(aVar instanceof R1.a)) {
                    throw new A6.g("Migration functionality with a provided SQLiteDriver requires overriding the migrate(SQLiteConnection) function.");
                }
                Y1.a aVar2 = ((R1.a) aVar).f6027q;
                P6.g.e(aVar2, "database");
                aVar2.v(g6.o.f12958a.r());
            }
            L2.a aVarW = m8.w(aVar);
            if (aVarW.f3978b) {
                m8.u(aVar);
                g(aVar);
                return;
            } else {
                throw new IllegalStateException(("Migration didn't properly handle: " + aVarW.f3979c).toString());
            }
        }
        P6.g.e(c0226b, "<this>");
        boolean z10 = false;
        if ((i <= i5 || !c0226b.f5022l) && c0226b.f5021k && ((set = c0226b.f5023m) == null || !set.contains(Integer.valueOf(i)))) {
            z10 = true;
        }
        if (z10) {
            throw new IllegalStateException(("A migration from " + i + " to " + i5 + " was required but not found. Please provide the necessary Migration path via RoomDatabase.Builder.addMigration(...) or allow for destructive migrations via one of the RoomDatabase.Builder.fallbackToDestructiveMigration* functions.").toString());
        }
        if (c0226b.f5029s) {
            X1.c cVarH0 = aVar.h0("SELECT name, type FROM sqlite_master WHERE type = 'table' OR type = 'view'");
            try {
                C6.c cVar = new C6.c(10);
                while (cVarH0.W()) {
                    String strO = cVarH0.o(0);
                    if (!X6.n.M(strO, "sqlite_", false) && !strO.equals("android_metadata")) {
                        cVar.add(new A6.h(strO, Boolean.valueOf(P6.g.a(cVarH0.o(1), "view"))));
                    }
                }
                C6.c cVarD = C1.d(cVar);
                W3.b.j(cVarH0, null);
                ListIterator listIterator = cVarD.listIterator(0);
                while (true) {
                    C6.a aVar3 = (C6.a) listIterator;
                    if (!aVar3.hasNext()) {
                        break;
                    }
                    A6.h hVar2 = (A6.h) aVar3.next();
                    String str = (String) hVar2.f446q;
                    if (((Boolean) hVar2.f447r).booleanValue()) {
                        android.support.v4.media.session.a.i(aVar, "DROP VIEW IF EXISTS " + str);
                    } else {
                        android.support.v4.media.session.a.i(aVar, "DROP TABLE IF EXISTS " + str);
                    }
                }
            } finally {
            }
        } else {
            m8.c(aVar);
        }
        Iterator it3 = this.f5083e.iterator();
        while (it3.hasNext()) {
            ((v) it3.next()).getClass();
            if (aVar instanceof R1.a) {
                P6.g.e(((R1.a) aVar).f6027q, "db");
            }
        }
        m8.a(aVar);
    }

    /* JADX WARN: Removed duplicated region for block: B:11:0x0024  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void f(X1.a aVar) throws Exception {
        boolean z8;
        Object objJ;
        L2.a aVarW;
        P6.g.e(aVar, "connection");
        X1.c cVarH0 = aVar.h0("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'room_master_table'");
        try {
            if (cVarH0.W()) {
                z8 = cVarH0.q(0) != 0;
            }
            W3.b.j(cVarH0, null);
            N1.M m8 = this.f5082d;
            if (z8) {
                cVarH0 = aVar.h0("SELECT identity_hash FROM room_master_table WHERE id = 42 LIMIT 1");
                try {
                    String strO = cVarH0.W() ? cVarH0.o(0) : null;
                    W3.b.j(cVarH0, null);
                    if (!((String) m8.f4479b).equals(strO) && !((String) m8.f4480c).equals(strO)) {
                        throw new IllegalStateException(("Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number. Expected identity hash: " + ((String) m8.f4479b) + ", found: " + strO).toString());
                    }
                } finally {
                }
            } else {
                android.support.v4.media.session.a.i(aVar, "BEGIN EXCLUSIVE TRANSACTION");
                try {
                    aVarW = m8.w(aVar);
                } catch (Throwable th) {
                    objJ = C1.j(th);
                }
                if (!aVarW.f3978b) {
                    throw new IllegalStateException(("Pre-packaged database has an invalid schema: " + aVarW.f3979c).toString());
                }
                m8.u(aVar);
                g(aVar);
                objJ = A6.n.f457a;
                if (!(objJ instanceof A6.i)) {
                    android.support.v4.media.session.a.i(aVar, "END TRANSACTION");
                }
                Throwable thA = A6.j.a(objJ);
                if (thA != null) {
                    android.support.v4.media.session.a.i(aVar, "ROLLBACK TRANSACTION");
                    throw thA;
                }
            }
            m8.t(aVar);
            for (v vVar : this.f5083e) {
                vVar.getClass();
                if (aVar instanceof R1.a) {
                    Y1.a aVar2 = ((R1.a) aVar).f6027q;
                    P6.g.e(aVar2, "db");
                    vVar.f5078a.b(aVar2);
                }
            }
            this.f5079a = true;
        } finally {
            try {
                throw th;
            } finally {
            }
        }
    }

    public final void g(X1.a aVar) {
        android.support.v4.media.session.a.i(aVar, "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)");
        String str = (String) this.f5082d.f4479b;
        P6.g.e(str, "hash");
        android.support.v4.media.session.a.i(aVar, "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '" + str + "')");
    }

    public w(C0226b c0226b, N1.M m8) {
        int i;
        Q1.h hVar;
        this.f5081c = c0226b;
        this.f5082d = m8;
        List list = c0226b.f5016e;
        this.f5083e = list == null ? B6.t.f1032q : list;
        x xVar = c0226b.f5018g;
        String str = c0226b.f5013b;
        X1.b bVar = c0226b.f5030t;
        if (bVar == null) {
            if (c0226b.f5014c != null) {
                this.f5084f = new R1.b(new P.g(new Z1.h(c0226b.f5012a, str, new B7.a(this, m8.f4478a))));
            } else {
                throw new IllegalArgumentException("SQLiteManager was constructed with both null driver and open helper factory!");
            }
        } else {
            if (str == null) {
                hVar = new Q1.h(new C0225a(this, bVar));
            } else {
                C0225a c0225a = new C0225a(this, bVar);
                int iOrdinal = xVar.ordinal();
                if (iOrdinal == 1) {
                    i = 1;
                } else {
                    if (iOrdinal != 2) {
                        throw new IllegalStateException(("Can't get max number of reader for journal mode '" + xVar + '\'').toString());
                    }
                    i = 4;
                }
                int iOrdinal2 = xVar.ordinal();
                if (iOrdinal2 != 1 && iOrdinal2 != 2) {
                    throw new IllegalStateException(("Can't get max number of writers for journal mode '" + xVar + '\'').toString());
                }
                hVar = new Q1.h(c0225a, str, i);
            }
            this.f5084f = hVar;
        }
        boolean z8 = xVar == x.f5087r;
        Y1.b bVarC = c();
        if (bVarC != null) {
            bVarC.setWriteAheadLoggingEnabled(z8);
        }
    }
}
