package defpackage;

import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.text.TextUtils;
import android.util.Pair;
import java.io.IOException;
import java.io.Serializable;
import java.lang.ref.Reference;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentLinkedQueue;

/* JADX INFO: renamed from: ᵚᵨ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C2736 {

    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public Object f10520;

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public Object f10521;

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public long f10522;

    /* JADX INFO: renamed from: ᵷᵺʾᵖ, reason: contains not printable characters */
    public final Object f10523;

    public C2736(C5377 c5377) {
        this.f10522 = 300000000000L;
        this.f10520 = c5377.m10966();
        this.f10521 = new C2280(this, AbstractC3653.m8231(new StringBuilder(), AbstractC1190.f5067, " ConnectionPool connection closer"));
        this.f10523 = new ConcurrentLinkedQueue();
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:47:0x0100 A[PHI: r6 r16 r17
      0x0100: PHI (r6v3 android.database.Cursor) = (r6v4 android.database.Cursor), (r6v6 android.database.Cursor) binds: [B:61:0x012b, B:46:0x00f9] A[DONT_GENERATE, DONT_INLINE]
      0x0100: PHI (r16v3 ᵷⁱᵹ) = (r16v5 ᵷⁱᵹ), (r16v9 ᵷⁱᵹ) binds: [B:61:0x012b, B:46:0x00f9] A[DONT_GENERATE, DONT_INLINE]
      0x0100: PHI (r17v2 long) = (r17v4 long), (r17v7 long) binds: [B:61:0x012b, B:46:0x00f9] A[DONT_GENERATE, DONT_INLINE]] */
    /* JADX WARN: Removed duplicated region for block: B:64:0x0130  */
    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public C4943 m6632(String str, C4943 c4943) throws Throwable {
        Cursor cursor;
        C4943 c49432;
        long j;
        Cursor cursorRawQuery;
        Pair pair;
        Object obj;
        Pair pair2;
        String strM10274 = c4943.m10274();
        List listM10263 = c4943.m10263();
        C1549 c1549 = (C1549) this.f10523;
        C2894 c2894 = c1549.f18892;
        C2894 c28942 = c1549.f18892;
        C1103 c1103 = (C1103) ((ᵜᵪʾ) c1549).ˌᴵ;
        c2894.m6933();
        C0114 c0114M5259 = C1962.m5259("_eid", c4943);
        Long l = (Long) (c0114M5259 == null ? null : C1962.m5258(c0114M5259));
        if (l != null) {
            if (strM10274.equals("_ep")) {
                c2894.m6933();
                C0114 c0114M52592 = C1962.m5259("_en", c4943);
                String str2 = (String) (c0114M52592 == null ? null : C1962.m5258(c0114M52592));
                if (TextUtils.isEmpty(str2)) {
                    C3485 c3485 = c1103.f4729;
                    C1103.m3564(c3485);
                    c3485.f13415.m4474(l, "Extra parameter without an event name. eventId");
                    return null;
                }
                if (((C4943) this.f10520) == null || ((Long) this.f10521) == null || l.longValue() != ((Long) this.f10521).longValue()) {
                    C1694 c1694 = c2894.f11109;
                    C2894.m6882(c1694);
                    C1103 c11032 = (C1103) ((ᵜᵪʾ) c1694).ˌᴵ;
                    c1694.ˈᵲᵢ();
                    c1694.m5026();
                    try {
                        cursorRawQuery = c1694.m4625().rawQuery("select main_event, children_to_process from main_event_params where app_id=? and event_id=?", new String[]{str, l.toString()});
                        try {
                            try {
                                if (cursorRawQuery.moveToFirst()) {
                                    c49432 = null;
                                    try {
                                        try {
                                            Pair pairCreate = Pair.create((C4943) ((C4225) C1962.m5264(C4943.m10252(), cursorRawQuery.getBlob(0))).m9869(), Long.valueOf(cursorRawQuery.getLong(1)));
                                            cursorRawQuery.close();
                                            pair2 = pairCreate;
                                        } catch (SQLiteException e) {
                                            e = e;
                                            j = 0;
                                            C3485 c34852 = c11032.f4729;
                                            C1103.m3564(c34852);
                                            c34852.f13423.m4474(e, "Error selecting main event");
                                            if (cursorRawQuery != null) {
                                                cursorRawQuery.close();
                                            }
                                            pair = c49432;
                                            if (pair != 0) {
                                            }
                                            C3485 c34853 = c1103.f4729;
                                            C1103.m3564(c34853);
                                            c34853.f13415.m4475("Extra parameter without existing main event. eventName, eventId", str2, l);
                                            return c49432;
                                        }
                                    } catch (IOException e2) {
                                        C3485 c34854 = c11032.f4729;
                                        C1103.m3564(c34854);
                                        j = 0;
                                        try {
                                            c34854.f13423.m4477("Failed to merge main event. appId, eventId", C3485.m7979(str), l, e2);
                                        } catch (SQLiteException e3) {
                                            e = e3;
                                            C3485 c348522 = c11032.f4729;
                                            C1103.m3564(c348522);
                                            c348522.f13423.m4474(e, "Error selecting main event");
                                            if (cursorRawQuery != null) {
                                            }
                                            pair = c49432;
                                            if (pair != 0) {
                                            }
                                            C3485 c348532 = c1103.f4729;
                                            C1103.m3564(c348532);
                                            c348532.f13415.m4475("Extra parameter without existing main event. eventName, eventId", str2, l);
                                            return c49432;
                                        }
                                        cursorRawQuery.close();
                                        pair = c49432;
                                    }
                                } else {
                                    C3485 c34855 = c11032.f4729;
                                    C1103.m3564(c34855);
                                    c34855.f13414.m4476("Main event not found");
                                    cursorRawQuery.close();
                                    pair2 = null;
                                    c49432 = null;
                                }
                                j = 0;
                                pair = pair2;
                            } catch (SQLiteException e4) {
                                e = e4;
                                c49432 = null;
                            }
                        } catch (Throwable th) {
                            th = th;
                            cursor = cursorRawQuery;
                            if (cursor != null) {
                                cursor.close();
                            }
                            throw th;
                        }
                    } catch (SQLiteException e5) {
                        e = e5;
                        c49432 = null;
                        j = 0;
                        cursorRawQuery = null;
                    } catch (Throwable th2) {
                        th = th2;
                        cursor = null;
                    }
                    if (pair != 0 || (obj = pair.first) == null) {
                        C3485 c3485322 = c1103.f4729;
                        C1103.m3564(c3485322);
                        c3485322.f13415.m4475("Extra parameter without existing main event. eventName, eventId", str2, l);
                        return c49432;
                    }
                    this.f10520 = (C4943) obj;
                    this.f10522 = ((Long) pair.second).longValue();
                    c28942.m6933();
                    this.f10521 = (Long) C1962.m5272("_eid", (C4943) this.f10520);
                } else {
                    j = 0;
                }
                long j2 = this.f10522 - 1;
                this.f10522 = j2;
                if (j2 <= j) {
                    C1694 c16942 = c28942.f11109;
                    C2894.m6882(c16942);
                    C1103 c11033 = (C1103) ((ᵜᵪʾ) c16942).ˌᴵ;
                    c16942.ˈᵲᵢ();
                    C3485 c34856 = c11033.f4729;
                    C1103.m3564(c34856);
                    c34856.f13414.m4474(str, "Clearing complex main event info. appId");
                    try {
                        c16942.m4625().execSQL("delete from main_event_params where app_id=?", new String[]{str});
                    } catch (SQLiteException e6) {
                        C3485 c34857 = c11033.f4729;
                        C1103.m3564(c34857);
                        c34857.f13423.m4474(e6, "Error clearing complex main event");
                    }
                } else {
                    C1694 c16943 = c28942.f11109;
                    C2894.m6882(c16943);
                    c16943.m4597(str, l, this.f10522, (C4943) this.f10520);
                }
                ArrayList arrayList = new ArrayList();
                for (C0114 c0114 : ((C4943) this.f10520).m10263()) {
                    c28942.m6933();
                    if (C1962.m5259(c0114.m1189(), c4943) == null) {
                        arrayList.add(c0114);
                    }
                }
                if (arrayList.isEmpty()) {
                    C3485 c34858 = c1103.f4729;
                    C1103.m3564(c34858);
                    c34858.f13415.m4474(str2, "No unique parameters in main event. eventName");
                } else {
                    arrayList.addAll(listM10263);
                    listM10263 = arrayList;
                }
                strM10274 = str2;
            } else {
                this.f10521 = l;
                this.f10520 = c4943;
                c2894.m6933();
                C0114 c0114M52593 = C1962.m5259("_epc", c4943);
                Serializable serializableM5258 = c0114M52593 == null ? null : C1962.m5258(c0114M52593);
                long jLongValue = ((Long) (serializableM5258 != null ? serializableM5258 : 0L)).longValue();
                this.f10522 = jLongValue;
                if (jLongValue <= 0) {
                    C3485 c34859 = c1103.f4729;
                    C1103.m3564(c34859);
                    c34859.f13415.m4474(strM10274, "Complex event with zero extra param count. eventName");
                } else {
                    C1694 c16944 = c2894.f11109;
                    C2894.m6882(c16944);
                    c16944.m4597(str, l, this.f10522, c4943);
                }
            }
        }
        C4225 c4225 = (C4225) c4943.m9884();
        c4225.m9178(strM10274);
        c4225.m9868();
        ((C4943) c4225.f17955).m10276();
        c4225.m9868();
        ((C4943) c4225.f17955).m10269(listM10263);
        return (C4943) c4225.m9869();
    }

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public int m6633(C4013 c4013, long j) {
        TimeZone timeZone = AbstractC1190.f5068;
        ArrayList arrayList = c4013.f15415;
        int i = 0;
        while (i < arrayList.size()) {
            Reference reference = (Reference) arrayList.get(i);
            if (reference.get() != null) {
                i++;
            } else {
                String str = "A connection to " + c4013.f15413.f1565.f1327 + " was leaked. Did you forget to close a response body?";
                AbstractC2570 abstractC2570 = AbstractC2570.f9922;
                AbstractC2570.f9922.mo6292(((C3016) reference).f11610, str);
                arrayList.remove(i);
                if (arrayList.isEmpty()) {
                    c4013.f15419 = j - this.f10522;
                    return 0;
                }
            }
        }
        return arrayList.size();
    }
}
