package h5;

import android.database.Cursor;
import androidx.sqlite.db.SupportSQLiteDatabase;
import com.google.android.gms.measurement.api.AppMeasurementSdk;
import com.google.firebase.messaging.Constants;
import h5.e;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import kotlin.jvm.internal.t;
import sm.c0;
import sm.p0;
import sm.q0;
import sm.w0;

/* loaded from: classes.dex */
public abstract class f {
    private static final Map a(SupportSQLiteDatabase supportSQLiteDatabase, String str) {
        Map c10;
        Map b10;
        Map i10;
        Cursor query = supportSQLiteDatabase.query("PRAGMA table_info(`" + str + "`)");
        try {
            if (query.getColumnCount() <= 0) {
                i10 = q0.i();
                bn.c.a(query, null);
                return i10;
            }
            int columnIndex = query.getColumnIndex(AppMeasurementSdk.ConditionalUserProperty.NAME);
            int columnIndex2 = query.getColumnIndex("type");
            int columnIndex3 = query.getColumnIndex("notnull");
            int columnIndex4 = query.getColumnIndex("pk");
            int columnIndex5 = query.getColumnIndex("dflt_value");
            c10 = p0.c();
            while (query.moveToNext()) {
                String name = query.getString(columnIndex);
                String type = query.getString(columnIndex2);
                boolean z10 = query.getInt(columnIndex3) != 0;
                int i11 = query.getInt(columnIndex4);
                String string = query.getString(columnIndex5);
                t.f(name, "name");
                t.f(type, "type");
                c10.put(name, new e.a(name, type, z10, i11, string, 2));
            }
            b10 = p0.b(c10);
            bn.c.a(query, null);
            return b10;
        } catch (Throwable th2) {
            try {
                throw th2;
            } catch (Throwable th3) {
                bn.c.a(query, th2);
                throw th3;
            }
        }
    }

    private static final List b(Cursor cursor) {
        List c10;
        List a10;
        List H0;
        int columnIndex = cursor.getColumnIndex("id");
        int columnIndex2 = cursor.getColumnIndex("seq");
        int columnIndex3 = cursor.getColumnIndex(Constants.MessagePayloadKeys.FROM);
        int columnIndex4 = cursor.getColumnIndex("to");
        c10 = sm.t.c();
        while (cursor.moveToNext()) {
            int i10 = cursor.getInt(columnIndex);
            int i11 = cursor.getInt(columnIndex2);
            String string = cursor.getString(columnIndex3);
            t.f(string, "cursor.getString(fromColumnIndex)");
            String string2 = cursor.getString(columnIndex4);
            t.f(string2, "cursor.getString(toColumnIndex)");
            c10.add(new e.d(i10, i11, string, string2));
        }
        a10 = sm.t.a(c10);
        H0 = c0.H0(a10);
        return H0;
    }

    private static final Set c(SupportSQLiteDatabase supportSQLiteDatabase, String str) {
        Set b10;
        Set a10;
        Cursor query = supportSQLiteDatabase.query("PRAGMA foreign_key_list(`" + str + "`)");
        try {
            int columnIndex = query.getColumnIndex("id");
            int columnIndex2 = query.getColumnIndex("seq");
            int columnIndex3 = query.getColumnIndex("table");
            int columnIndex4 = query.getColumnIndex("on_delete");
            int columnIndex5 = query.getColumnIndex("on_update");
            List b11 = b(query);
            query.moveToPosition(-1);
            b10 = w0.b();
            while (query.moveToNext()) {
                if (query.getInt(columnIndex2) == 0) {
                    int i10 = query.getInt(columnIndex);
                    ArrayList arrayList = new ArrayList();
                    ArrayList arrayList2 = new ArrayList();
                    ArrayList<e.d> arrayList3 = new ArrayList();
                    for (Object obj : b11) {
                        if (((e.d) obj).g() == i10) {
                            arrayList3.add(obj);
                        }
                    }
                    for (e.d dVar : arrayList3) {
                        arrayList.add(dVar.e());
                        arrayList2.add(dVar.m());
                    }
                    String string = query.getString(columnIndex3);
                    t.f(string, "cursor.getString(tableColumnIndex)");
                    String string2 = query.getString(columnIndex4);
                    t.f(string2, "cursor.getString(onDeleteColumnIndex)");
                    String string3 = query.getString(columnIndex5);
                    t.f(string3, "cursor.getString(onUpdateColumnIndex)");
                    b10.add(new e.c(string, string2, string3, arrayList, arrayList2));
                }
            }
            a10 = w0.a(b10);
            bn.c.a(query, null);
            return a10;
        } finally {
        }
    }

    private static final e.C0454e d(SupportSQLiteDatabase supportSQLiteDatabase, String str, boolean z10) {
        List S0;
        List S02;
        Cursor query = supportSQLiteDatabase.query("PRAGMA index_xinfo(`" + str + "`)");
        try {
            int columnIndex = query.getColumnIndex("seqno");
            int columnIndex2 = query.getColumnIndex("cid");
            int columnIndex3 = query.getColumnIndex(AppMeasurementSdk.ConditionalUserProperty.NAME);
            int columnIndex4 = query.getColumnIndex("desc");
            if (columnIndex != -1 && columnIndex2 != -1 && columnIndex3 != -1 && columnIndex4 != -1) {
                TreeMap treeMap = new TreeMap();
                TreeMap treeMap2 = new TreeMap();
                while (query.moveToNext()) {
                    if (query.getInt(columnIndex2) >= 0) {
                        int i10 = query.getInt(columnIndex);
                        String columnName = query.getString(columnIndex3);
                        String str2 = query.getInt(columnIndex4) > 0 ? "DESC" : "ASC";
                        Integer valueOf = Integer.valueOf(i10);
                        t.f(columnName, "columnName");
                        treeMap.put(valueOf, columnName);
                        treeMap2.put(Integer.valueOf(i10), str2);
                    }
                }
                Collection values = treeMap.values();
                t.f(values, "columnsMap.values");
                S0 = c0.S0(values);
                Collection values2 = treeMap2.values();
                t.f(values2, "ordersMap.values");
                S02 = c0.S0(values2);
                e.C0454e c0454e = new e.C0454e(str, z10, S0, S02);
                bn.c.a(query, null);
                return c0454e;
            }
            bn.c.a(query, null);
            return null;
        } finally {
        }
    }

    private static final Set e(SupportSQLiteDatabase supportSQLiteDatabase, String str) {
        Set b10;
        Set a10;
        Cursor query = supportSQLiteDatabase.query("PRAGMA index_list(`" + str + "`)");
        try {
            int columnIndex = query.getColumnIndex(AppMeasurementSdk.ConditionalUserProperty.NAME);
            int columnIndex2 = query.getColumnIndex("origin");
            int columnIndex3 = query.getColumnIndex("unique");
            if (columnIndex != -1 && columnIndex2 != -1 && columnIndex3 != -1) {
                b10 = w0.b();
                while (query.moveToNext()) {
                    if (t.b("c", query.getString(columnIndex2))) {
                        String name = query.getString(columnIndex);
                        boolean z10 = true;
                        if (query.getInt(columnIndex3) != 1) {
                            z10 = false;
                        }
                        t.f(name, "name");
                        e.C0454e d10 = d(supportSQLiteDatabase, name, z10);
                        if (d10 == null) {
                            bn.c.a(query, null);
                            return null;
                        }
                        b10.add(d10);
                    }
                }
                a10 = w0.a(b10);
                bn.c.a(query, null);
                return a10;
            }
            bn.c.a(query, null);
            return null;
        } finally {
        }
    }

    public static final e f(SupportSQLiteDatabase database, String tableName) {
        t.g(database, "database");
        t.g(tableName, "tableName");
        return new e(tableName, a(database, tableName), c(database, tableName), e(database, tableName));
    }
}
