package com.google.firebase.crashlytics.internal.concurrency;

import C0.J;
import I0.C0290e0;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Base64;
import android.util.Log;
import com.google.android.gms.tasks.CancellationTokenSource;
import com.google.android.gms.tasks.Continuation;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource;
import com.revenuecat.purchases.common.diagnostics.DiagnosticsEntry;
import com.revenuecat.purchases.common.diagnostics.DiagnosticsTracker;
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.atomic.AtomicBoolean;
import m3.AbstractC2478m;
import r3.i;
import r3.j;
import r3.l;
import u3.C2964a;
import u3.c;
import u3.d;
import u3.e;
import u3.f;
import u3.g;
import w3.C3136a;
import y3.C3264a;
import y3.C3265b;
import y3.C3270g;
import y3.InterfaceC3268e;

/* JADX INFO: loaded from: classes3.dex */
public final /* synthetic */ class a implements Continuation, z3.b, InterfaceC3268e {

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

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

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

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

    public /* synthetic */ a(Object obj, Object obj2, Object obj3, int i6) {
        this.f13918a = i6;
        this.f13919b = obj;
        this.f13920c = obj2;
        this.f13921d = obj3;
    }

    @Override // y3.InterfaceC3268e
    public Object apply(Object obj) throws Throwable {
        Cursor cursor;
        int i6;
        String str;
        long jInsert;
        c cVar;
        c cVar2;
        String str2 = "bytes";
        int i7 = 5;
        int i8 = 4;
        int i9 = 3;
        c cVar3 = c.CACHE_FULL;
        int i10 = 2;
        Object obj2 = this.f13921d;
        int i11 = 0;
        Object obj3 = this.f13920c;
        Object obj4 = this.f13919b;
        int i12 = 1;
        switch (this.f13918a) {
            case 2:
                Cursor cursor2 = (Cursor) obj;
                C3270g c3270g = (C3270g) obj4;
                c3270g.getClass();
                while (cursor2.moveToNext()) {
                    long j4 = cursor2.getLong(0);
                    int i13 = cursor2.getInt(7) != 0 ? i12 : 0;
                    C0290e0 c0290e0 = new C0290e0();
                    c0290e0.f2365g = new HashMap();
                    String string = cursor2.getString(i12);
                    if (string == null) {
                        throw new NullPointerException("Null transportName");
                    }
                    c0290e0.f2360b = string;
                    c0290e0.f2363e = Long.valueOf(cursor2.getLong(i10));
                    c0290e0.f2364f = Long.valueOf(cursor2.getLong(3));
                    if (i13 != 0) {
                        String string2 = cursor2.getString(4);
                        c0290e0.f2362d = new l(string2 == null ? C3270g.f26190f : new o3.b(string2), cursor2.getBlob(5));
                        str = str2;
                        i6 = i12;
                    } else {
                        String string3 = cursor2.getString(4);
                        o3.b bVar = string3 == null ? C3270g.f26190f : new o3.b(string3);
                        Cursor cursorQuery = c3270g.a().query("event_payloads", new String[]{str2}, "event_id = ?", new String[]{String.valueOf(j4)}, null, null, "sequence_num");
                        try {
                            ArrayList arrayList = new ArrayList();
                            int length = 0;
                            while (cursorQuery.moveToNext()) {
                                int i14 = i12;
                                byte[] blob = cursorQuery.getBlob(0);
                                arrayList.add(blob);
                                length += blob.length;
                                i12 = i14;
                                break;
                            }
                            i6 = i12;
                            byte[] bArr = new byte[length];
                            str = str2;
                            int i15 = 0;
                            int length2 = 0;
                            while (i15 < arrayList.size()) {
                                byte[] bArr2 = (byte[]) arrayList.get(i15);
                                cursor = cursorQuery;
                                try {
                                    System.arraycopy(bArr2, 0, bArr, length2, bArr2.length);
                                    length2 += bArr2.length;
                                    i15++;
                                    cursorQuery = cursor;
                                } catch (Throwable th) {
                                    th = th;
                                    cursor.close();
                                    throw th;
                                }
                            }
                            cursorQuery.close();
                            c0290e0.f2362d = new l(bVar, bArr);
                        } catch (Throwable th2) {
                            th = th2;
                            cursor = cursorQuery;
                        }
                    }
                    if (!cursor2.isNull(6)) {
                        c0290e0.f2361c = Integer.valueOf(cursor2.getInt(6));
                    }
                    if (!cursor2.isNull(8)) {
                        c0290e0.f2366h = Integer.valueOf(cursor2.getInt(8));
                    }
                    if (!cursor2.isNull(9)) {
                        c0290e0.f2367i = cursor2.getString(9);
                    }
                    if (!cursor2.isNull(10)) {
                        c0290e0.f2368j = cursor2.getBlob(10);
                    }
                    if (!cursor2.isNull(11)) {
                        c0290e0.f2369k = cursor2.getBlob(11);
                    }
                    ((ArrayList) obj3).add(new C3265b(j4, (j) obj2, c0290e0.c()));
                    i12 = i6;
                    str2 = str;
                    i10 = 2;
                }
                return null;
            case 3:
                SQLiteDatabase sQLiteDatabase = (SQLiteDatabase) obj;
                C3270g c3270g2 = (C3270g) obj4;
                long jSimpleQueryForLong = c3270g2.a().compileStatement("PRAGMA page_size").simpleQueryForLong() * c3270g2.a().compileStatement("PRAGMA page_count").simpleQueryForLong();
                C3264a c3264a = c3270g2.f26194d;
                long j6 = c3264a.f26180a;
                i iVar = (i) obj3;
                String str3 = iVar.f23962a;
                if (jSimpleQueryForLong >= j6) {
                    c3270g2.f(1L, cVar3, str3);
                    return -1L;
                }
                j jVar = (j) obj2;
                Long lB = C3270g.b(sQLiteDatabase, jVar);
                if (lB != null) {
                    jInsert = lB.longValue();
                } else {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put("backend_name", jVar.f23972a);
                    contentValues.put("priority", Integer.valueOf(B3.a.a(jVar.f23974c)));
                    contentValues.put("next_request_ms", (Integer) 0);
                    byte[] bArr3 = jVar.f23973b;
                    if (bArr3 != null) {
                        contentValues.put("extras", Base64.encodeToString(bArr3, 0));
                    }
                    jInsert = sQLiteDatabase.insert("transport_contexts", null, contentValues);
                }
                l lVar = iVar.f23964c;
                byte[] bArr4 = lVar.f23982b;
                int length3 = bArr4.length;
                int i16 = c3264a.f26184e;
                boolean z6 = length3 <= i16;
                ContentValues contentValues2 = new ContentValues();
                contentValues2.put("context_id", Long.valueOf(jInsert));
                contentValues2.put("transport_name", str3);
                contentValues2.put("timestamp_ms", Long.valueOf(iVar.f23965d));
                contentValues2.put("uptime_ms", Long.valueOf(iVar.f23966e));
                contentValues2.put("payload_encoding", lVar.f23981a.f21808a);
                contentValues2.put("code", iVar.f23963b);
                contentValues2.put("num_attempts", (Integer) 0);
                contentValues2.put("inline", Boolean.valueOf(z6));
                contentValues2.put("payload", z6 ? bArr4 : new byte[0]);
                contentValues2.put(DiagnosticsTracker.PRODUCT_ID_KEY, iVar.f23968g);
                contentValues2.put("pseudonymous_id", iVar.f23969h);
                contentValues2.put("experiment_ids_clear_blob", iVar.f23970i);
                contentValues2.put("experiment_ids_encrypted_blob", iVar.f23971j);
                long jInsert2 = sQLiteDatabase.insert("events", null, contentValues2);
                if (!z6) {
                    int iCeil = (int) Math.ceil(((double) bArr4.length) / ((double) i16));
                    for (int i17 = 1; i17 <= iCeil; i17++) {
                        byte[] bArrCopyOfRange = Arrays.copyOfRange(bArr4, (i17 - 1) * i16, Math.min(i17 * i16, bArr4.length));
                        ContentValues contentValues3 = new ContentValues();
                        contentValues3.put("event_id", Long.valueOf(jInsert2));
                        contentValues3.put("sequence_num", Integer.valueOf(i17));
                        contentValues3.put("bytes", bArrCopyOfRange);
                        sQLiteDatabase.insert("event_payloads", null, contentValues3);
                    }
                }
                for (Map.Entry entry : Collections.unmodifiableMap(iVar.f23967f).entrySet()) {
                    ContentValues contentValues4 = new ContentValues();
                    contentValues4.put("event_id", Long.valueOf(jInsert2));
                    contentValues4.put(DiagnosticsEntry.NAME_KEY, (String) entry.getKey());
                    contentValues4.put("value", (String) entry.getValue());
                    sQLiteDatabase.insert("event_metadata", null, contentValues4);
                }
                return Long.valueOf(jInsert2);
            default:
                Cursor cursor3 = (Cursor) obj;
                C3270g c3270g3 = (C3270g) obj4;
                c3270g3.getClass();
                while (true) {
                    HashMap map = (HashMap) obj3;
                    if (cursor3.moveToNext()) {
                        String string4 = cursor3.getString(i11);
                        int i18 = cursor3.getInt(1);
                        c cVar4 = c.REASON_UNKNOWN;
                        if (i18 != 0) {
                            if (i18 == 1) {
                                cVar4 = c.MESSAGE_TOO_OLD;
                            } else if (i18 == 2) {
                                cVar = cVar3;
                                cVar2 = cVar;
                            } else if (i18 == i9) {
                                cVar4 = c.PAYLOAD_TOO_BIG;
                            } else if (i18 == i8) {
                                cVar4 = c.MAX_RETRIES_REACHED;
                            } else if (i18 == i7) {
                                cVar4 = c.INVALID_PAYLOD;
                            } else if (i18 == 6) {
                                cVar4 = c.SERVER_ERROR;
                            } else {
                                AbstractC2478m.g("SQLiteEventStore", "%n is not valid. No matched LogEventDropped-Reason found. Treated it as REASON_UNKNOWN", Integer.valueOf(i18));
                            }
                            cVar2 = cVar3;
                            cVar = cVar4;
                        } else {
                            cVar2 = cVar3;
                            cVar = cVar4;
                        }
                        long j7 = cursor3.getLong(2);
                        if (!map.containsKey(string4)) {
                            map.put(string4, new ArrayList());
                        }
                        ((List) map.get(string4)).add(new d(j7, cVar));
                        cVar3 = cVar2;
                        i7 = 5;
                        i8 = 4;
                        i9 = 3;
                        i11 = 0;
                    } else {
                        Iterator it = map.entrySet().iterator();
                        while (true) {
                            J j8 = (J) obj2;
                            if (!it.hasNext()) {
                                long jF = c3270g3.f26192b.f();
                                SQLiteDatabase sQLiteDatabaseA = c3270g3.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();
                                        g gVar = new g(cursorRawQuery.getLong(0), jF);
                                        cursorRawQuery.close();
                                        sQLiteDatabaseA.setTransactionSuccessful();
                                        sQLiteDatabaseA.endTransaction();
                                        j8.f533b = gVar;
                                        j8.f535d = new u3.b(new f(c3270g3.a().compileStatement("PRAGMA page_size").simpleQueryForLong() * c3270g3.a().compileStatement("PRAGMA page_count").simpleQueryForLong(), C3264a.f26179f.f26180a));
                                        j8.f536e = (String) c3270g3.f26195e.get();
                                        return new C2964a((g) j8.f533b, Collections.unmodifiableList((ArrayList) j8.f534c), (u3.b) j8.f535d, (String) j8.f536e);
                                    } catch (Throwable th3) {
                                        cursorRawQuery.close();
                                        throw th3;
                                    }
                                } catch (Throwable th4) {
                                    sQLiteDatabaseA.endTransaction();
                                    throw th4;
                                }
                            }
                            Map.Entry entry2 = (Map.Entry) it.next();
                            int i19 = e.f24579c;
                            new ArrayList();
                            ((ArrayList) j8.f534c).add(new e((String) entry2.getKey(), Collections.unmodifiableList((List) entry2.getValue())));
                        }
                    }
                }
                break;
        }
    }

    @Override // z3.b
    public Object b() {
        C3136a c3136a = (C3136a) this.f13919b;
        C3270g c3270g = (C3270g) c3136a.f25451d;
        c3270g.getClass();
        j jVar = (j) this.f13920c;
        i iVar = (i) this.f13921d;
        String strL = AbstractC2478m.l("SQLiteEventStore");
        if (Log.isLoggable(strL, 3)) {
            Log.d(strL, "Storing event with priority=" + jVar.f23974c + ", name=" + iVar.f23962a + " for destination " + jVar.f23972a);
        }
        ((Long) c3270g.c(new a(c3270g, iVar, jVar, 3))).getClass();
        c3136a.f25448a.a(jVar, 1, false);
        return null;
    }

    @Override // com.google.android.gms.tasks.Continuation
    public Object then(Task task) {
        return CrashlyticsTasks.lambda$race$0((TaskCompletionSource) this.f13919b, (AtomicBoolean) this.f13920c, (CancellationTokenSource) this.f13921d, task);
    }
}
