package E2;

import C2.g;
import C3.D;
import C3.G;
import C3.W;
import G2.h;
import L0.C;
import L0.C0169y;
import L0.I;
import M4.m;
import O0.n;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Base64;
import android.util.Log;
import d3.R0;
import d3.V0;
import g3.InterfaceC0983a;
import g3.o;
import g4.C0987b;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
import u0.T;
import w2.C1699b;
import z2.i;
import z2.k;

/* JADX INFO: loaded from: classes.dex */
public final /* synthetic */ class a implements H2.b, G2.f, x0.d, InterfaceC0983a, g3.e, n {

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final /* synthetic */ int f2157q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final /* synthetic */ Object f2158r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final /* synthetic */ Object f2159s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final /* synthetic */ Object f2160t;

    public /* synthetic */ a(h hVar, Object obj, i iVar, int i) {
        this.f2157q = i;
        this.f2160t = hVar;
        this.f2158r = obj;
        this.f2159s = iVar;
    }

    @Override // x0.d
    public void a(Object obj) {
        ((I) obj).a(((E0.e) this.f2160t).f1971a, (C) this.f2159s, (C0169y) this.f2158r);
    }

    @Override // G2.f
    public Object apply(Object obj) throws Throwable {
        long jInsert;
        Cursor cursor;
        int i;
        C2.d dVar;
        C2.d dVar2;
        int i5 = 5;
        int i8 = 4;
        int i9 = 3;
        C2.d dVar3 = C2.d.f1169t;
        int i10 = 2;
        Object obj2 = this.f2158r;
        int i11 = 0;
        Object obj3 = this.f2159s;
        Object obj4 = this.f2160t;
        int i12 = 1;
        switch (this.f2157q) {
            case 1:
                SQLiteDatabase sQLiteDatabase = (SQLiteDatabase) obj;
                h hVar = (h) obj4;
                long jSimpleQueryForLong = hVar.a().compileStatement("PRAGMA page_size").simpleQueryForLong() * hVar.a().compileStatement("PRAGMA page_count").simpleQueryForLong();
                G2.a aVar = hVar.f2945t;
                long j8 = aVar.f2929a;
                z2.h hVar2 = (z2.h) obj2;
                String str = hVar2.f19251a;
                if (jSimpleQueryForLong >= j8) {
                    hVar.m(1L, dVar3, str);
                    return -1L;
                }
                i iVar = (i) obj3;
                Long lB = h.b(sQLiteDatabase, iVar);
                if (lB != null) {
                    jInsert = lB.longValue();
                } else {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put("backend_name", iVar.f19260a);
                    contentValues.put("priority", Integer.valueOf(J2.a.a(iVar.f19262c)));
                    contentValues.put("next_request_ms", (Integer) 0);
                    byte[] bArr = iVar.f19261b;
                    if (bArr != null) {
                        contentValues.put("extras", Base64.encodeToString(bArr, 0));
                    }
                    jInsert = sQLiteDatabase.insert("transport_contexts", null, contentValues);
                }
                k kVar = hVar2.f19253c;
                byte[] bArr2 = kVar.f19270b;
                int length = bArr2.length;
                int i13 = aVar.f2933e;
                boolean z8 = length <= i13;
                ContentValues contentValues2 = new ContentValues();
                contentValues2.put("context_id", Long.valueOf(jInsert));
                contentValues2.put("transport_name", str);
                contentValues2.put("timestamp_ms", Long.valueOf(hVar2.f19254d));
                contentValues2.put("uptime_ms", Long.valueOf(hVar2.f19255e));
                contentValues2.put("payload_encoding", kVar.f19269a.f18295a);
                contentValues2.put("code", hVar2.f19252b);
                contentValues2.put("num_attempts", (Integer) 0);
                contentValues2.put("inline", Boolean.valueOf(z8));
                contentValues2.put("payload", z8 ? bArr2 : new byte[0]);
                contentValues2.put("product_id", hVar2.f19257g);
                contentValues2.put("pseudonymous_id", hVar2.f19258h);
                contentValues2.put("experiment_ids_clear_blob", hVar2.i);
                contentValues2.put("experiment_ids_encrypted_blob", hVar2.f19259j);
                long jInsert2 = sQLiteDatabase.insert("events", null, contentValues2);
                if (!z8) {
                    int iCeil = (int) Math.ceil(((double) bArr2.length) / ((double) i13));
                    for (int i14 = 1; i14 <= iCeil; i14++) {
                        byte[] bArrCopyOfRange = Arrays.copyOfRange(bArr2, (i14 - 1) * i13, Math.min(i14 * i13, bArr2.length));
                        ContentValues contentValues3 = new ContentValues();
                        contentValues3.put("event_id", Long.valueOf(jInsert2));
                        contentValues3.put("sequence_num", Integer.valueOf(i14));
                        contentValues3.put("bytes", bArrCopyOfRange);
                        sQLiteDatabase.insert("event_payloads", null, contentValues3);
                    }
                }
                for (Map.Entry entry : Collections.unmodifiableMap(hVar2.f19256f).entrySet()) {
                    ContentValues contentValues4 = new ContentValues();
                    contentValues4.put("event_id", Long.valueOf(jInsert2));
                    contentValues4.put("name", (String) entry.getKey());
                    contentValues4.put("value", (String) entry.getValue());
                    sQLiteDatabase.insert("event_metadata", null, contentValues4);
                }
                return Long.valueOf(jInsert2);
            case 2:
                Cursor cursor2 = (Cursor) obj;
                h hVar3 = (h) obj4;
                hVar3.getClass();
                while (cursor2.moveToNext()) {
                    long j9 = cursor2.getLong(0);
                    int i15 = cursor2.getInt(7) != 0 ? i12 : 0;
                    h6.c cVar = new h6.c();
                    cVar.f13437f = new HashMap();
                    String string = cursor2.getString(i12);
                    if (string == null) {
                        throw new NullPointerException("Null transportName");
                    }
                    cVar.f13432a = string;
                    cVar.f13435d = Long.valueOf(cursor2.getLong(i10));
                    cVar.f13436e = Long.valueOf(cursor2.getLong(3));
                    if (i15 != 0) {
                        String string2 = cursor2.getString(4);
                        cVar.f13434c = new k(string2 == null ? h.f2941v : new C1699b(string2), cursor2.getBlob(5));
                        i = i12;
                    } else {
                        String string3 = cursor2.getString(4);
                        C1699b c1699b = string3 == null ? h.f2941v : new C1699b(string3);
                        Cursor cursorQuery = hVar3.a().query("event_payloads", new String[]{"bytes"}, "event_id = ?", new String[]{String.valueOf(j9)}, null, null, "sequence_num");
                        try {
                            ArrayList arrayList = new ArrayList();
                            int length2 = 0;
                            while (cursorQuery.moveToNext()) {
                                int i16 = i12;
                                byte[] blob = cursorQuery.getBlob(0);
                                arrayList.add(blob);
                                length2 += blob.length;
                                i12 = i16;
                                break;
                            }
                            i = i12;
                            byte[] bArr3 = new byte[length2];
                            int i17 = 0;
                            int length3 = 0;
                            while (i17 < arrayList.size()) {
                                byte[] bArr4 = (byte[]) arrayList.get(i17);
                                cursor = cursorQuery;
                                try {
                                    ArrayList arrayList2 = arrayList;
                                    System.arraycopy(bArr4, 0, bArr3, length3, bArr4.length);
                                    length3 += bArr4.length;
                                    i17++;
                                    cursorQuery = cursor;
                                    arrayList = arrayList2;
                                } catch (Throwable th) {
                                    th = th;
                                    cursor.close();
                                    throw th;
                                }
                            }
                            cursorQuery.close();
                            cVar.f13434c = new k(c1699b, bArr3);
                        } catch (Throwable th2) {
                            th = th2;
                            cursor = cursorQuery;
                        }
                    }
                    if (!cursor2.isNull(6)) {
                        cVar.f13433b = Integer.valueOf(cursor2.getInt(6));
                    }
                    if (!cursor2.isNull(8)) {
                        cVar.f13438g = Integer.valueOf(cursor2.getInt(8));
                    }
                    if (!cursor2.isNull(9)) {
                        cVar.f13439h = cursor2.getString(9);
                    }
                    if (!cursor2.isNull(10)) {
                        cVar.i = cursor2.getBlob(10);
                    }
                    if (!cursor2.isNull(11)) {
                        cVar.f13440j = cursor2.getBlob(11);
                    }
                    ((ArrayList) obj2).add(new G2.b(j9, (i) obj3, cVar.b()));
                    i12 = i;
                    i10 = 2;
                }
                return null;
            default:
                Cursor cursor3 = (Cursor) obj;
                h hVar4 = (h) obj4;
                hVar4.getClass();
                while (true) {
                    HashMap map = (HashMap) obj3;
                    if (cursor3.moveToNext()) {
                        String string4 = cursor3.getString(i11);
                        int i18 = cursor3.getInt(1);
                        C2.d dVar4 = C2.d.f1167r;
                        if (i18 != 0) {
                            if (i18 == 1) {
                                dVar4 = C2.d.f1168s;
                            } else if (i18 == 2) {
                                dVar = dVar3;
                                dVar2 = dVar;
                            } else if (i18 == i9) {
                                dVar4 = C2.d.f1170u;
                            } else if (i18 == i8) {
                                dVar4 = C2.d.f1171v;
                            } else if (i18 == i5) {
                                dVar4 = C2.d.f1172w;
                            } else if (i18 == 6) {
                                dVar4 = C2.d.f1173x;
                            } else {
                                android.support.v4.media.session.a.e("SQLiteEventStore", "%n is not valid. No matched LogEventDropped-Reason found. Treated it as REASON_UNKNOWN", Integer.valueOf(i18));
                            }
                            dVar2 = dVar3;
                            dVar = dVar4;
                        } else {
                            dVar2 = dVar3;
                            dVar = dVar4;
                        }
                        long j10 = cursor3.getLong(2);
                        if (!map.containsKey(string4)) {
                            map.put(string4, new ArrayList());
                        }
                        ((List) map.get(string4)).add(new C2.e(j10, dVar));
                        dVar3 = dVar2;
                        i5 = 5;
                        i8 = 4;
                        i9 = 3;
                        i11 = 0;
                    } else {
                        Iterator it = map.entrySet().iterator();
                        while (true) {
                            C2.a aVar2 = (C2.a) obj2;
                            if (!it.hasNext()) {
                                long jD = hVar4.f2943r.d();
                                SQLiteDatabase sQLiteDatabaseA = hVar4.a();
                                sQLiteDatabaseA.beginTransaction();
                                try {
                                    Cursor cursorRawQuery = sQLiteDatabaseA.rawQuery("SELECT last_metrics_upload_ms FROM global_log_event_state LIMIT 1", new String[0]);
                                    try {
                                        cursorRawQuery.moveToNext();
                                        C2.h hVar5 = new C2.h(cursorRawQuery.getLong(0), jD);
                                        cursorRawQuery.close();
                                        sQLiteDatabaseA.setTransactionSuccessful();
                                        sQLiteDatabaseA.endTransaction();
                                        aVar2.f1158s = hVar5;
                                        aVar2.f1159t = new C2.c(new g(hVar4.a().compileStatement("PRAGMA page_size").simpleQueryForLong() * hVar4.a().compileStatement("PRAGMA page_count").simpleQueryForLong(), G2.a.f2928f.f2929a));
                                        aVar2.f1160u = (String) hVar4.f2946u.get();
                                        return new C2.b((C2.h) aVar2.f1158s, Collections.unmodifiableList((ArrayList) aVar2.f1157r), (C2.c) aVar2.f1159t, (String) aVar2.f1160u);
                                    } catch (Throwable th3) {
                                        cursorRawQuery.close();
                                        throw th3;
                                    }
                                } catch (Throwable th4) {
                                    sQLiteDatabaseA.endTransaction();
                                    throw th4;
                                }
                            }
                            Map.Entry entry2 = (Map.Entry) it.next();
                            int i19 = C2.f.f1178c;
                            new ArrayList();
                            ((ArrayList) aVar2.f1157r).add(new C2.f((String) entry2.getKey(), Collections.unmodifiableList((List) entry2.getValue())));
                        }
                    }
                }
                break;
        }
    }

    @Override // g3.e
    public void d(Object obj) {
        o oVar = (o) this.f2159s;
        C0987b c0987b = (C0987b) this.f2158r;
        C2.a aVar = (C2.a) this.f2160t;
        try {
            M4.f fVar = (M4.f) oVar.g();
            if (fVar != null) {
                ((Executor) aVar.f1159t).execute(new N4.a(c0987b, ((R0) aVar.f1157r).O(fVar), 1));
            }
        } catch (L4.e e8) {
            Log.w("FirebaseRemoteConfig", "Exception publishing RolloutsState to subscriber. Continuing to listen for changes.", e8);
        }
    }

    @Override // g3.InterfaceC0983a
    public Object e(o oVar) {
        M4.f fVar;
        URL url;
        switch (this.f2157q) {
            case 5:
                L4.c cVar = (L4.c) this.f2160t;
                o oVar2 = (o) this.f2159s;
                if (!oVar2.i() || oVar2.g() == null) {
                    return W2.g.p(Boolean.FALSE);
                }
                M4.f fVar2 = (M4.f) oVar2.g();
                o oVar3 = (o) this.f2158r;
                if (oVar3.i() && (fVar = (M4.f) oVar3.g()) != null && fVar2.f4233c.equals(fVar.f4233c)) {
                    return W2.g.p(Boolean.FALSE);
                }
                M4.d dVar = cVar.f4000d;
                dVar.getClass();
                L4.b bVar = new L4.b(dVar, 1, fVar2);
                Executor executor = dVar.f4221a;
                return W2.g.f(executor, bVar).j(executor, new C0.b(dVar, 8, fVar2)).d(cVar.f3998b, new L4.a(cVar));
            case Z.g.STRING_SET_FIELD_NUMBER /* 6 */:
                m mVar = (m) this.f2160t;
                mVar.getClass();
                o oVar4 = (o) this.f2159s;
                if (!oVar4.i()) {
                    return W2.g.o(new L4.d("Firebase Installations failed to get installation auth token for config update listener connection.", oVar4.f()));
                }
                o oVar5 = (o) this.f2158r;
                try {
                    if (!oVar5.i()) {
                        return W2.g.o(new L4.d("Firebase Installations failed to get installation ID for config update listener connection.", oVar5.f()));
                    }
                    try {
                        url = new URL(mVar.c(mVar.f4276n));
                        break;
                    } catch (MalformedURLException unused) {
                        Log.e("FirebaseRemoteConfig", "URL is malformed");
                        url = null;
                    }
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    mVar.l(httpURLConnection, (String) oVar5.g(), ((E4.a) oVar4.g()).f2181a);
                    return W2.g.p(httpURLConnection);
                } catch (IOException e8) {
                    return W2.g.o(new L4.d("Failed to open HTTP stream connection", e8));
                }
            default:
                boolean zI = oVar.i();
                g3.h hVar = (g3.h) this.f2160t;
                if (zI) {
                    hVar.b(oVar.g());
                } else if (oVar.f() != null) {
                    hVar.a(oVar.f());
                } else if (((AtomicBoolean) this.f2159s).getAndSet(true)) {
                    ((o) ((V0) ((V0) this.f2158r).f11784r).f11784r).n(null);
                }
                return W2.g.p(null);
        }
    }

    @Override // H2.b
    public Object g() {
        b bVar = (b) this.f2160t;
        h hVar = (h) bVar.f2165d;
        hVar.getClass();
        i iVar = (i) this.f2159s;
        z2.h hVar2 = (z2.h) this.f2158r;
        String strN = android.support.v4.media.session.a.n("SQLiteEventStore");
        if (Log.isLoggable(strN, 3)) {
            Log.d(strN, "Storing event with priority=" + iVar.f19262c + ", name=" + hVar2.f19251a + " for destination " + iVar.f19260a);
        }
        ((Long) hVar.g(new a(hVar, (Object) hVar2, iVar, 1))).getClass();
        bVar.f2162a.a(iVar, 1, false);
        return null;
    }

    @Override // O0.n
    public W h(int i, T t6, int[] iArr) {
        D dJ = G.j();
        int i5 = 0;
        while (i5 < t6.f17200a) {
            int i8 = i;
            T t8 = t6;
            dJ.a(new O0.m(i8, t8, i5, (O0.k) this.f2160t, iArr[i5], (String) this.f2159s, (String) this.f2158r));
            i5++;
            i = i8;
            t6 = t8;
        }
        return dJ.g();
    }

    public /* synthetic */ a(Object obj, Object obj2, Object obj3, int i) {
        this.f2157q = i;
        this.f2160t = obj;
        this.f2159s = obj2;
        this.f2158r = obj3;
    }
}
