package p138.p176.p177.p242.p265.p266;

import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.text.TextUtils;
import android.util.Pair;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import p138.p176.p177.p242.p258.p263.AbstractC4421;
import p138.p176.p177.p242.p258.p263.C4634;
import p138.p176.p177.p242.p258.p263.C4638;

/* JADX INFO: renamed from: ދ.ހ.֏.ހ.ބ.֏.ຳ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
public final class C4989 {

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public C4634 f15092;

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public Long f15093;

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public long f15094;

    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public final /* synthetic */ C4957 f15095;

    public /* synthetic */ C4989(C4957 c4957, C4859 c4859) {
        this.f15095 = c4957;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:23:0x006b  */
    /* JADX WARN: Removed duplicated region for block: B:53:0x0126  */
    /* JADX WARN: Removed duplicated region for block: B:58:0x0156  */
    /* JADX WARN: Removed duplicated region for block: B:62:0x017b  */
    /* JADX WARN: Removed duplicated region for block: B:67:0x0198  */
    /* JADX WARN: Removed duplicated region for block: B:68:0x019d  */
    /* JADX WARN: Removed duplicated region for block: B:75:0x01bf  */
    /* JADX WARN: Type inference failed for: r14v6, types: [long] */
    /* JADX WARN: Type inference failed for: r6v7 */
    /* JADX WARN: Type inference failed for: r6v8, types: [android.database.Cursor] */
    /* JADX WARN: Type inference failed for: r6v9 */
    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final C4634 m8524(String str, C4634 c4634) {
        ?? r6;
        Cursor cursorRawQuery;
        Pair pairCreate;
        Object obj;
        long j;
        ArrayList arrayList;
        ?? LongValue;
        String str2 = c4634.zze;
        List list = c4634.zzd;
        Long l = (Long) this.f15095.m8157().m8131(c4634, "_eid");
        boolean z = l != null;
        if (z && str2.equals("_ep")) {
            String str3 = (String) this.f15095.m8157().m8131(c4634, "_en");
            if (TextUtils.isEmpty(str3)) {
                this.f15095.mo8165().f14445.m8035("Extra parameter without an event name. eventId", l);
                return null;
            }
            if (this.f15092 == null || this.f15093 == null) {
                C4900 c4900M8158 = this.f15095.m8158();
                c4900M8158.mo8008();
                c4900M8158.m8155();
                try {
                    try {
                        cursorRawQuery = c4900M8158.m8354().rawQuery("select main_event, children_to_process from main_event_params where app_id=? and event_id=?", new String[]{str, String.valueOf(l)});
                        try {
                            if (cursorRawQuery.moveToFirst()) {
                                try {
                                    pairCreate = Pair.create((C4634) ((AbstractC4421) ((C4634.C4635) C4833.m8121(C4634.m7653(), cursorRawQuery.getBlob(0))).m7370()), Long.valueOf(cursorRawQuery.getLong(1)));
                                    cursorRawQuery.close();
                                } catch (IOException e) {
                                    c4900M8158.mo8165().f14444.m8037("Failed to merge main event. appId, eventId", C4794.m8038(str), l, e);
                                    cursorRawQuery.close();
                                    pairCreate = null;
                                }
                            } else {
                                c4900M8158.mo8165().f14452.m8034("Main event not found");
                                cursorRawQuery.close();
                                pairCreate = null;
                            }
                        } catch (SQLiteException e2) {
                            e = e2;
                            c4900M8158.mo8165().f14444.m8035("Error selecting main event", e);
                            if (cursorRawQuery != null) {
                                cursorRawQuery.close();
                            }
                            pairCreate = null;
                            if (pairCreate != null) {
                            }
                            this.f15095.mo8165().f14445.m8036("Extra parameter without existing main event. eventName, eventId", str3, l);
                            return null;
                        }
                    } catch (Throwable th) {
                        th = th;
                        r6 = LongValue;
                        if (r6 != 0) {
                            r6.close();
                        }
                        throw th;
                    }
                } catch (SQLiteException e3) {
                    e = e3;
                    cursorRawQuery = null;
                } catch (Throwable th2) {
                    th = th2;
                    r6 = 0;
                    if (r6 != 0) {
                    }
                    throw th;
                }
                if (pairCreate != null || (obj = pairCreate.first) == null) {
                    this.f15095.mo8165().f14445.m8036("Extra parameter without existing main event. eventName, eventId", str3, l);
                    return null;
                }
                this.f15092 = (C4634) obj;
                this.f15094 = ((Long) pairCreate.second).longValue();
                this.f15093 = (Long) this.f15095.m8157().m8131(this.f15092, "_eid");
                j = this.f15094 - 1;
                this.f15094 = j;
                if (j > 0) {
                    C4900 c4900M81582 = this.f15095.m8158();
                    c4900M81582.mo8008();
                    c4900M81582.mo8165().f14452.m8035("Clearing complex main event info. appId", str);
                    try {
                        c4900M81582.m8354().execSQL("delete from main_event_params where app_id=?", new String[]{str});
                    } catch (SQLiteException e4) {
                        c4900M81582.mo8165().f14444.m8035("Error clearing complex main event", e4);
                    }
                } else {
                    this.f15095.m8158().m8335(str, l, this.f15094, this.f15092);
                }
                arrayList = new ArrayList();
                for (C4638 c4638 : this.f15092.zzd) {
                    this.f15095.m8157();
                    if (C4833.m8128(c4634, c4638.zzd) == null) {
                        arrayList.add(c4638);
                    }
                }
                if (arrayList.isEmpty()) {
                    arrayList.addAll(list);
                    list = arrayList;
                } else {
                    this.f15095.mo8165().f14445.m8035("No unique parameters in main event. eventName", str3);
                }
                str2 = str3;
            } else {
                LongValue = l.longValue();
                if (LongValue != this.f15093.longValue()) {
                }
                j = this.f15094 - 1;
                this.f15094 = j;
                if (j > 0) {
                }
                arrayList = new ArrayList();
                while (r2.hasNext()) {
                }
                if (arrayList.isEmpty()) {
                }
                str2 = str3;
            }
        } else if (z) {
            this.f15093 = l;
            this.f15092 = c4634;
            Object objM8131 = this.f15095.m8157().m8131(c4634, "_epc");
            long jLongValue = ((Long) (objM8131 != null ? objM8131 : 0L)).longValue();
            this.f15094 = jLongValue;
            if (jLongValue <= 0) {
                this.f15095.mo8165().f14445.m8035("Complex event with zero extra param count. eventName", str2);
            } else {
                this.f15095.m8158().m8335(str, l, this.f15094, c4634);
            }
        }
        C4634.C4635 c4635M7363 = c4634.m7363();
        c4635M7363.m7661(str2);
        if (c4635M7363.f13775) {
            c4635M7363.m7368();
            c4635M7363.f13775 = false;
        }
        C4634.m7647((C4634) c4635M7363.f13774);
        if (c4635M7363.f13775) {
            c4635M7363.m7368();
            c4635M7363.f13775 = false;
        }
        C4634.m7650((C4634) c4635M7363.f13774, list);
        return (C4634) ((AbstractC4421) c4635M7363.m7370());
    }
}
