package androidx.room;

import android.database.Cursor;
import androidx.media3.container.NalUnitUtil;
import androidx.media3.exoplayer.upstream.CmcdData;
import androidx.room.RoomDatabase;
import defpackage.cr4;
import defpackage.fi7;
import defpackage.ik2;
import defpackage.je9;
import defpackage.le9;
import defpackage.m38;
import defpackage.n41;
import defpackage.qw5;
import defpackage.vx8;
import defpackage.xt8;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;
import kotlin.Metadata;

/* JADX INFO: loaded from: classes.dex */
@m38
@Metadata(d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0004\b\u0017\u0018\u00002\u00020\u0001:\u0003\u0002\u0003\u0004¨\u0006\u0005"}, d2 = {"Landroidx/room/k;", "Lle9$a;", CmcdData.OBJECT_TYPE_AUDIO_ONLY, "b", "c", "room-runtime_release"}, k = 1, mv = {1, 8, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
@vx8
public class k extends le9.a {
    public androidx.room.a b;
    public final b c;
    public final String d;
    public final String e;

    @Metadata(d1 = {"\u0000\n\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\b\u0086\u0003\u0018\u00002\u00020\u0001¨\u0006\u0002"}, d2 = {"Landroidx/room/k$a;", "", "room-runtime_release"}, k = 1, mv = {1, 8, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    @vx8
    public static final class a {
    }

    @m38
    @Metadata(d1 = {"\u0000\n\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\b'\u0018\u00002\u00020\u0001¨\u0006\u0002"}, d2 = {"Landroidx/room/k$b;", "", "room-runtime_release"}, k = 1, mv = {1, 8, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public static abstract class b {
        public final int a;

        public b(int i) {
            this.a = i;
        }

        public abstract void a(je9 je9Var);

        public abstract void b(je9 je9Var);

        public abstract void c(je9 je9Var);

        public abstract void d(je9 je9Var);

        public void e(je9 je9Var) {
            cr4.g(je9Var, "db");
        }

        public void f(je9 je9Var) {
            cr4.g(je9Var, "db");
        }

        public c g(je9 je9Var) {
            cr4.g(je9Var, "db");
            throw new UnsupportedOperationException("validateMigration is deprecated");
        }
    }

    @m38
    @Metadata(d1 = {"\u0000\n\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\b\u0017\u0018\u00002\u00020\u0001¨\u0006\u0002"}, d2 = {"Landroidx/room/k$c;", "", "room-runtime_release"}, k = 1, mv = {1, 8, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public static class c {
        public final boolean a;
        public final String b;

        public c(boolean z, String str) {
            this.a = z;
            this.b = str;
        }
    }

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public k(androidx.room.a aVar, b bVar, String str, String str2) {
        super(bVar.a);
        cr4.g(aVar, "configuration");
        this.b = aVar;
        this.c = bVar;
        this.d = str;
        this.e = str2;
    }

    @Override // le9.a
    public final void b(je9 je9Var) {
        cr4.g(je9Var, "db");
    }

    @Override // le9.a
    public final void c(je9 je9Var) throws IOException {
        cr4.g(je9Var, "db");
        Cursor cursorQuery = je9Var.query("SELECT count(*) FROM sqlite_master WHERE name != 'android_metadata'");
        try {
            boolean z = false;
            if (cursorQuery.moveToFirst()) {
                if (cursorQuery.getInt(0) == 0) {
                    z = true;
                }
            }
            cursorQuery.close();
            b bVar = this.c;
            bVar.a(je9Var);
            if (!z) {
                c cVarG = bVar.g(je9Var);
                if (!cVarG.a) {
                    throw new IllegalStateException("Pre-packaged database has an invalid schema: " + cVarG.b);
                }
            }
            g(je9Var);
            bVar.c(je9Var);
        } finally {
        }
    }

    @Override // le9.a
    public final void d(je9 je9Var, int i, int i2) {
        cr4.g(je9Var, "db");
        f(je9Var, i, i2);
    }

    /* JADX WARN: Removed duplicated region for block: B:10:0x001d  */
    @Override // le9.a
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void e(je9 je9Var) throws IOException {
        boolean z;
        cr4.g(je9Var, "db");
        Cursor cursorQuery = je9Var.query("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name='room_master_table'");
        try {
            if (cursorQuery.moveToFirst()) {
                z = cursorQuery.getInt(0) != 0;
            }
            cursorQuery.close();
            b bVar = this.c;
            if (z) {
                Cursor cursorQuery2 = je9Var.query(new xt8("SELECT identity_hash FROM room_master_table WHERE id = 42 LIMIT 1"));
                try {
                    String string = cursorQuery2.moveToFirst() ? cursorQuery2.getString(0) : null;
                    cursorQuery2.close();
                    String str = this.d;
                    if (!str.equals(string) && !this.e.equals(string)) {
                        throw new IllegalStateException(fi7.h("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: ", str, ", found: ", string));
                    }
                } finally {
                }
            } else {
                c cVarG = bVar.g(je9Var);
                if (!cVarG.a) {
                    throw new IllegalStateException("Pre-packaged database has an invalid schema: " + cVarG.b);
                }
                bVar.e(je9Var);
                g(je9Var);
            }
            bVar.d(je9Var);
            this.b = null;
        } finally {
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:19:0x0039  */
    /* JADX WARN: Removed duplicated region for block: B:73:0x0084 A[EDGE_INSN: B:73:0x0084->B:37:0x0084 BREAK  A[LOOP:1: B:12:0x0023->B:76:?], SYNTHETIC] */
    @Override // le9.a
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void f(je9 je9Var, int i, int i2) {
        Set set;
        Iterable iterable;
        TreeMap treeMap;
        boolean z;
        cr4.g(je9Var, "db");
        androidx.room.a aVar = this.b;
        b bVar = this.c;
        if (aVar != null) {
            RoomDatabase.e eVar = aVar.d;
            eVar.getClass();
            if (i == i2) {
                iterable = ik2.a;
            } else {
                boolean z2 = i2 > i;
                ArrayList arrayList = new ArrayList();
                int iIntValue = i;
                do {
                    if (z2) {
                        if (iIntValue >= i2) {
                            iterable = arrayList;
                            break;
                        }
                        treeMap = (TreeMap) eVar.a.get(Integer.valueOf(iIntValue));
                        if (treeMap == null) {
                            break;
                        }
                        for (Integer num : z2 ? treeMap.descendingKeySet() : treeMap.keySet()) {
                            if (z2) {
                                int i3 = iIntValue + 1;
                                cr4.f(num, "targetVersion");
                                int iIntValue2 = num.intValue();
                                if (i3 <= iIntValue2 && iIntValue2 <= i2) {
                                    Object obj = treeMap.get(num);
                                    cr4.d(obj);
                                    arrayList.add(obj);
                                    iIntValue = num.intValue();
                                    z = true;
                                    break;
                                }
                            } else {
                                cr4.f(num, "targetVersion");
                                int iIntValue3 = num.intValue();
                                if (i2 <= iIntValue3 && iIntValue3 < iIntValue) {
                                    Object obj2 = treeMap.get(num);
                                    cr4.d(obj2);
                                    arrayList.add(obj2);
                                    iIntValue = num.intValue();
                                    z = true;
                                    break;
                                    break;
                                }
                            }
                        }
                        z = false;
                    } else {
                        if (iIntValue <= i2) {
                            iterable = arrayList;
                            break;
                        }
                        treeMap = (TreeMap) eVar.a.get(Integer.valueOf(iIntValue));
                        if (treeMap == null) {
                        }
                    }
                } while (z);
                iterable = null;
            }
            if (iterable != null) {
                bVar.f(je9Var);
                Iterator it = iterable.iterator();
                while (it.hasNext()) {
                    ((qw5) it.next()).a(je9Var);
                }
                c cVarG = bVar.g(je9Var);
                if (!cVarG.a) {
                    throw new IllegalStateException("Migration didn't properly handle: " + cVarG.b);
                }
                bVar.e(je9Var);
                g(je9Var);
                return;
            }
        }
        androidx.room.a aVar2 = this.b;
        if (aVar2 != null) {
            if (!((i <= i2 || !aVar2.k) && aVar2.j && ((set = aVar2.l) == null || !set.contains(Integer.valueOf(i))))) {
                bVar.b(je9Var);
                bVar.a(je9Var);
                return;
            }
        }
        throw new IllegalStateException(n41.l("A migration from ", i, " to ", " was required but not found. Please provide the necessary Migration path via RoomDatabase.Builder.addMigration(Migration ...) or allow for destructive migrations via one of the RoomDatabase.Builder.fallbackToDestructiveMigration* methods.", i2));
    }

    public final void g(je9 je9Var) {
        je9Var.execSQL("CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)");
        String str = this.d;
        cr4.g(str, "hash");
        je9Var.execSQL("INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '" + str + "')");
    }
}
