package androidx.room;

import android.app.ActivityManager;
import android.content.Context;
import androidx.room.RoomDatabase;
import com.google.android.gms.measurement.internal.u1;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.concurrent.Executor;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final kotlin.jvm.internal.b f4092a;

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

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

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public Executor f4097f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public Executor f4098g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public l3.f f4099h;

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final RoomDatabase.JournalMode f4101j;

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

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final l0 f4103l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final LinkedHashSet f4104m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public final LinkedHashSet f4105n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public final ArrayList f4106o;

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

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

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

    public i0(Context context, Class cls, String str) {
        nb.g.i(context, "context");
        this.f4095d = new ArrayList();
        this.f4096e = new ArrayList();
        this.f4101j = RoomDatabase.JournalMode.f3995a;
        this.f4102k = -1L;
        this.f4103l = new l0();
        this.f4104m = new LinkedHashSet();
        this.f4105n = new LinkedHashSet();
        this.f4106o = new ArrayList();
        this.f4107p = true;
        this.f4109r = true;
        this.f4092a = kotlin.jvm.internal.g.a(cls);
        this.f4093b = context;
        this.f4094c = str;
    }

    public final void a(g3.a... aVarArr) {
        for (g3.a aVar : aVarArr) {
            LinkedHashSet linkedHashSet = this.f4105n;
            linkedHashSet.add(Integer.valueOf(aVar.startVersion));
            linkedHashSet.add(Integer.valueOf(aVar.endVersion));
        }
        g3.a[] aVarArr2 = (g3.a[]) Arrays.copyOf(aVarArr, aVarArr.length);
        l0 l0Var = this.f4103l;
        l0Var.getClass();
        nb.g.i(aVarArr2, "migrations");
        for (g3.a aVar2 : aVarArr2) {
            l0Var.a(aVar2);
        }
    }

    public final RoomDatabase b() {
        String name;
        boolean z10;
        String str;
        Executor executor = this.f4097f;
        if (executor == null && this.f4098g == null) {
            m.a aVar = m.b.f30271d;
            this.f4098g = aVar;
            this.f4097f = aVar;
        } else if (executor != null && this.f4098g == null) {
            this.f4098g = executor;
        } else if (executor == null) {
            this.f4097f = this.f4098g;
        }
        LinkedHashSet linkedHashSet = this.f4105n;
        LinkedHashSet linkedHashSet2 = this.f4104m;
        nb.g.i(linkedHashSet, "migrationStartAndEndVersions");
        nb.g.i(linkedHashSet2, "migrationsNotRequiredFrom");
        if (!linkedHashSet.isEmpty()) {
            Iterator it = linkedHashSet.iterator();
            while (it.hasNext()) {
                int iIntValue = ((Number) it.next()).intValue();
                if (!(!linkedHashSet2.contains(Integer.valueOf(iIntValue)))) {
                    throw new IllegalArgumentException(u1.g("Inconsistency detected. A Migration was supplied to addMigration() that has a start or end version equal to a start version supplied to fallbackToDestructiveMigrationFrom(). Start version is: ", iIntValue).toString());
                }
            }
        }
        l3.f fVar = this.f4099h;
        if (fVar == null) {
            fVar = new com.google.android.material.shape.f();
        }
        l3.f fVar2 = fVar;
        if (this.f4102k > 0) {
            if (this.f4094c != null) {
                throw new IllegalArgumentException("Required value was null.".toString());
            }
            throw new IllegalArgumentException("Cannot create auto-closing database for an in-memory database.".toString());
        }
        String str2 = this.f4094c;
        l0 l0Var = this.f4103l;
        ArrayList arrayList = this.f4095d;
        boolean z11 = this.f4100i;
        RoomDatabase.JournalMode journalMode = this.f4101j;
        journalMode.getClass();
        Context context = this.f4093b;
        nb.g.i(context, "context");
        if (journalMode == RoomDatabase.JournalMode.f3995a) {
            Object systemService = context.getSystemService("activity");
            ActivityManager activityManager = systemService instanceof ActivityManager ? (ActivityManager) systemService : null;
            journalMode = (activityManager == null || activityManager.isLowRamDevice()) ? RoomDatabase.JournalMode.f3996b : RoomDatabase.JournalMode.f3997c;
        }
        RoomDatabase.JournalMode journalMode2 = journalMode;
        Executor executor2 = this.f4097f;
        if (executor2 == null) {
            throw new IllegalArgumentException("Required value was null.".toString());
        }
        Executor executor3 = this.f4098g;
        if (executor3 == null) {
            throw new IllegalArgumentException("Required value was null.".toString());
        }
        e eVar = new e(context, str2, fVar2, l0Var, arrayList, z11, journalMode2, executor2, executor3, null, this.f4107p, this.f4108q, linkedHashSet2, null, null, null, this.f4096e, this.f4106o, false, null, null);
        eVar.f4081v = this.f4109r;
        Class clsB = nb.g.B(this.f4092a);
        Package r32 = clsB.getPackage();
        if (r32 == null || (name = r32.getName()) == null) {
            name = "";
        }
        String canonicalName = clsB.getCanonicalName();
        nb.g.f(canonicalName);
        if (name.length() == 0) {
            z10 = true;
        } else {
            z10 = true;
            canonicalName = canonicalName.substring(name.length() + 1);
            nb.g.h(canonicalName, "substring(...)");
        }
        String strConcat = kotlin.text.h.g0(canonicalName, '.', '_').concat("_Impl");
        try {
            if (name.length() == 0) {
                str = strConcat;
            } else {
                str = name + '.' + strConcat;
            }
            Class<?> cls = Class.forName(str, z10, clsB.getClassLoader());
            nb.g.g(cls, "null cannot be cast to non-null type java.lang.Class<T of androidx.room.util.KClassUtil.findAndInstantiateDatabaseImpl>");
            RoomDatabase roomDatabase = (RoomDatabase) cls.getDeclaredConstructor(new Class[0]).newInstance(new Object[0]);
            roomDatabase.init(eVar);
            return roomDatabase;
        } catch (ClassNotFoundException e10) {
            throw new RuntimeException("Cannot find implementation for " + clsB.getCanonicalName() + ". " + strConcat + " does not exist. Is Room annotation processor correctly configured?", e10);
        } catch (IllegalAccessException e11) {
            throw new RuntimeException("Cannot access the constructor " + clsB.getCanonicalName(), e11);
        } catch (InstantiationException e12) {
            throw new RuntimeException("Failed to create an instance of " + clsB.getCanonicalName(), e12);
        }
    }
}
