package R0;

import C3.E;
import C3.G;
import C3.W;
import D1.D;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Pair;
import android.view.Surface;
import androidx.datastore.preferences.protobuf.C0432j;
import com.google.android.gms.internal.measurement.V0;
import com.google.android.gms.internal.measurement.W0;
import com.google.android.gms.internal.measurement.Z0;
import d3.C0772c;
import d3.C0773c0;
import d3.C0805n;
import d3.C0817r0;
import d3.C0825u;
import d3.C0828v;
import d3.Q1;
import d3.Z;
import java.io.IOException;
import java.io.Serializable;
import java.lang.ref.Reference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
import k7.C1109a;
import u0.C1570f;
import u0.C1578n;
import u0.C1579o;
import u0.b0;
import u0.d0;
import x0.AbstractC1709a;

/* JADX INFO: loaded from: classes.dex */
public final class m {

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public long f5952b;

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final Object f5955e;

    public m(long j8, Bundle bundle, String str, String str2) {
        this.f5953c = str;
        this.f5954d = str2;
        this.f5955e = bundle;
        this.f5952b = j8;
    }

    public static m n(C0828v c0828v) {
        Bundle bundleA = c0828v.f12194r.a();
        return new m(c0828v.f12196t, bundleA, c0828v.f12193q, c0828v.f12195s);
    }

    public boolean a(C1109a c1109a, o7.i iVar, List list, boolean z8) {
        P6.g.e(iVar, "call");
        Iterator it = ((ConcurrentLinkedQueue) this.f5955e).iterator();
        while (true) {
            if (!it.hasNext()) {
                return false;
            }
            o7.l lVar = (o7.l) it.next();
            P6.g.d(lVar, "connection");
            synchronized (lVar) {
                if (z8) {
                    if (!(lVar.f15988g != null)) {
                        continue;
                    }
                }
                if (lVar.i(c1109a, list)) {
                    iVar.d(lVar);
                    return true;
                }
            }
        }
    }

    public void b(boolean z8) {
        S0.f fVar;
        this.f5952b = -9223372036854775807L;
        p pVar = (p) this.f5955e;
        if (pVar.f5966k == 1) {
            C0281c c0281c = pVar.f5962f;
            if (z8) {
                v vVar = c0281c.f5868a;
                y yVar = vVar.f5984b;
                yVar.f6014m = 0L;
                yVar.f6017p = -1L;
                yVar.f6015n = -1L;
                vVar.f5990h = -9223372036854775807L;
                vVar.f5988f = -9223372036854775807L;
                vVar.d(1);
                vVar.i = -9223372036854775807L;
            }
            z zVar = c0281c.f5870c;
            C0432j c0432j = zVar.f6024f;
            c0432j.f8096b = 0;
            c0432j.f8097c = 0;
            zVar.f6025g = -9223372036854775807L;
            S0.f fVar2 = zVar.f6023e;
            if (fVar2.h() > 0) {
                AbstractC1709a.d(fVar2.h() > 0);
                while (fVar2.h() > 1) {
                    fVar2.e();
                }
                Object objE = fVar2.e();
                objE.getClass();
                fVar2.a(0L, (Long) objE);
            }
            S0.f fVar3 = zVar.f6022d;
            if (fVar3.h() > 0) {
                AbstractC1709a.d(fVar3.h() > 0);
                while (fVar3.h() > 1) {
                    fVar3.e();
                }
                Object objE2 = fVar3.e();
                objE2.getClass();
                fVar3.a(0L, (d0) objE2);
            }
            c0281c.f5871d.clear();
            while (true) {
                fVar = pVar.f5958b;
                if (fVar.h() <= 1) {
                    break;
                } else {
                    fVar.e();
                }
            }
            if (fVar.h() == 1) {
                Long l8 = (Long) fVar.e();
                l8.getClass();
                c0281c.a(l8.longValue(), pVar.f5968m);
            }
            pVar.f5967l = -9223372036854775807L;
            x0.s sVar = pVar.i;
            AbstractC1709a.k(sVar);
            sVar.c(new D(pVar));
        }
    }

    public boolean c(C1579o c1579o) throws C {
        p pVar = (p) this.f5955e;
        AbstractC1709a.j(pVar.f5966k == 0);
        C1570f c1570f = c1579o.f17335B;
        if (c1570f == null || !c1570f.d()) {
            c1570f = C1570f.f17277h;
        }
        if (c1570f.f17280c != 7 || x0.v.f18553a < 34) {
        }
        Looper looperMyLooper = Looper.myLooper();
        AbstractC1709a.k(looperMyLooper);
        pVar.i = pVar.f5963g.a(looperMyLooper, null);
        try {
            pVar.f5959c.a();
            throw null;
        } catch (b0 e8) {
            throw new C(e8, c1579o);
        }
    }

    public int d(o7.l lVar, long j8) {
        byte[] bArr = l7.b.f14412a;
        ArrayList arrayList = lVar.f15996p;
        int i = 0;
        while (i < arrayList.size()) {
            Reference reference = (Reference) arrayList.get(i);
            if (reference.get() != null) {
                i++;
            } else {
                String str = "A connection to " + lVar.f15983b.f14024a.f14041h + " was leaked. Did you forget to close a response body?";
                s7.n nVar = s7.n.f16942a;
                s7.n.f16942a.j(((o7.g) reference).f15963a, str);
                arrayList.remove(i);
                lVar.f15990j = true;
                if (arrayList.isEmpty()) {
                    lVar.f15997q = j8 - this.f5952b;
                    return 0;
                }
            }
        }
        return arrayList.size();
    }

    public void e(int i) {
        y yVar = ((p) this.f5955e).f5962f.f5868a.f5984b;
        if (yVar.f6011j == i) {
            return;
        }
        yVar.f6011j = i;
        yVar.d(true);
    }

    public void f(Surface surface, x0.p pVar) {
        p pVar2 = (p) this.f5955e;
        Pair pair = pVar2.f5965j;
        if (pair != null && ((Surface) pair.first).equals(surface) && ((x0.p) pVar2.f5965j.second).equals(pVar)) {
            return;
        }
        pVar2.f5965j = Pair.create(surface, pVar);
        int i = pVar.f18543a;
    }

    public void g(float f3) {
        ((p) this.f5955e).f5962f.f5868a.g(f3);
    }

    public void h(long j8, long j9) {
        p pVar = (p) this.f5955e;
        S0.f fVar = pVar.f5958b;
        long j10 = this.f5952b;
        fVar.a(j10 == -9223372036854775807L ? 0L : j10 + 1, Long.valueOf(j8));
        pVar.f5968m = j9;
        pVar.f5962f.a(0L, j9);
    }

    public void i(List list) {
        if (((W) this.f5953c).equals(list)) {
            return;
        }
        p pVar = (p) this.f5955e;
        pVar.f5959c.getClass();
        C3.D d8 = new C3.D(4);
        d8.d(list);
        d8.d(pVar.f5961e);
        this.f5953c = d8.g();
        C1579o c1579o = (C1579o) this.f5954d;
        if (c1579o == null) {
            return;
        }
        C1578n c1578nA = c1579o.a();
        C1570f c1570f = c1579o.f17335B;
        if (c1570f == null || !c1570f.d()) {
            c1570f = C1570f.f17277h;
        }
        c1578nA.f17299A = c1570f;
        c1578nA.a();
        AbstractC1709a.k(null);
        throw null;
    }

    public void j(t tVar) {
        ((p) this.f5955e).f5962f.i = tVar;
    }

    public void k() {
        long j8 = this.f5952b;
        p pVar = (p) this.f5955e;
        if (pVar.f5967l >= j8) {
            long j9 = pVar.f5962f.f5870c.f6025g;
        }
    }

    public W0 l(W0 w02, String str) throws Throwable {
        long j8;
        Cursor cursorRawQuery;
        Pair pairCreate;
        Object obj;
        String strT = w02.t();
        List listU = w02.u();
        C0772c c0772c = (C0772c) this.f5955e;
        Q1 q12 = c0772c.f11473r;
        q12.f();
        Long l8 = (Long) C0773c0.O(w02, "_eid");
        if (l8 != null) {
            boolean zEquals = strT.equals("_ep");
            C0817r0 c0817r0 = (C0817r0) c0772c.f5146q;
            if (zEquals) {
                q12.f();
                String str2 = (String) C0773c0.O(w02, "_en");
                Cursor cursor = null;
                if (TextUtils.isEmpty(str2)) {
                    Z z8 = c0817r0.f12150y;
                    C0817r0.k(z8);
                    z8.f11876w.c(l8, "Extra parameter without an event name. eventId");
                    return null;
                }
                W0 w03 = (W0) this.f5953c;
                Q1 q13 = c0772c.f11473r;
                if (w03 == null || ((Long) this.f5954d) == null || l8.longValue() != ((Long) this.f5954d).longValue()) {
                    C0805n c0805n = q12.f11683s;
                    Q1.L(c0805n);
                    C0817r0 c0817r02 = (C0817r0) c0805n.f5146q;
                    c0805n.E();
                    c0805n.F();
                    try {
                        cursorRawQuery = c0805n.A0().rawQuery("select main_event, children_to_process from main_event_params where app_id=? and event_id=?", new String[]{str, l8.toString()});
                        try {
                            try {
                                if (cursorRawQuery.moveToFirst()) {
                                    j8 = 0;
                                    try {
                                        try {
                                            pairCreate = Pair.create((W0) ((V0) C0773c0.k0(W0.r(), cursorRawQuery.getBlob(0))).h(), Long.valueOf(cursorRawQuery.getLong(1)));
                                            cursorRawQuery.close();
                                        } catch (IOException e8) {
                                            Z z9 = c0817r02.f12150y;
                                            C0817r0.k(z9);
                                            z9.f11875v.e("Failed to merge main event. appId, eventId", Z.M(str), l8, e8);
                                            cursorRawQuery.close();
                                            pairCreate = null;
                                        }
                                    } catch (SQLiteException e9) {
                                        e = e9;
                                        Z z10 = c0817r02.f12150y;
                                        C0817r0.k(z10);
                                        z10.f11875v.c(e, "Error selecting main event");
                                        if (cursorRawQuery != null) {
                                            cursorRawQuery.close();
                                        }
                                        pairCreate = null;
                                        if (pairCreate != null) {
                                        }
                                        Z z11 = c0817r0.f12150y;
                                        C0817r0.k(z11);
                                        z11.f11876w.d("Extra parameter without existing main event. eventName, eventId", str2, l8);
                                        return null;
                                    }
                                } else {
                                    Z z12 = c0817r02.f12150y;
                                    C0817r0.k(z12);
                                    z12.D.b("Main event not found");
                                    cursorRawQuery.close();
                                    pairCreate = null;
                                    j8 = 0;
                                }
                            } catch (Throwable th) {
                                th = th;
                                cursor = cursorRawQuery;
                                if (cursor != null) {
                                    cursor.close();
                                }
                                throw th;
                            }
                        } catch (SQLiteException e10) {
                            e = e10;
                            j8 = 0;
                        }
                    } catch (SQLiteException e11) {
                        e = e11;
                        j8 = 0;
                        cursorRawQuery = null;
                    } catch (Throwable th2) {
                        th = th2;
                    }
                    if (pairCreate != null || (obj = pairCreate.first) == null) {
                        Z z112 = c0817r0.f12150y;
                        C0817r0.k(z112);
                        z112.f11876w.d("Extra parameter without existing main event. eventName, eventId", str2, l8);
                        return null;
                    }
                    this.f5953c = (W0) obj;
                    this.f5952b = ((Long) pairCreate.second).longValue();
                    q13.f();
                    this.f5954d = (Long) C0773c0.O((W0) this.f5953c, "_eid");
                } else {
                    j8 = 0;
                }
                long j9 = this.f5952b - 1;
                this.f5952b = j9;
                if (j9 <= j8) {
                    C0805n c0805n2 = q13.f11683s;
                    Q1.L(c0805n2);
                    c0805n2.E();
                    C0817r0 c0817r03 = (C0817r0) c0805n2.f5146q;
                    Z z13 = c0817r03.f12150y;
                    C0817r0.k(z13);
                    z13.D.c(str, "Clearing complex main event info. appId");
                    try {
                        c0805n2.A0().execSQL("delete from main_event_params where app_id=?", new String[]{str});
                    } catch (SQLiteException e12) {
                        Z z14 = c0817r03.f12150y;
                        C0817r0.k(z14);
                        z14.f11875v.c(e12, "Error clearing complex main event");
                    }
                } else {
                    C0805n c0805n3 = q13.f11683s;
                    Q1.L(c0805n3);
                    c0805n3.q0(str, l8, this.f5952b, (W0) this.f5953c);
                }
                ArrayList arrayList = new ArrayList();
                for (Z0 z02 : ((W0) this.f5953c).u()) {
                    q13.f();
                    if (C0773c0.N(w02, z02.s()) == null) {
                        arrayList.add(z02);
                    }
                }
                if (arrayList.isEmpty()) {
                    Z z15 = c0817r0.f12150y;
                    C0817r0.k(z15);
                    z15.f11876w.c(str2, "No unique parameters in main event. eventName");
                } else {
                    arrayList.addAll(listU);
                    listU = arrayList;
                }
                strT = str2;
            } else {
                this.f5954d = l8;
                this.f5953c = w02;
                q12.f();
                Serializable serializableO = C0773c0.O(w02, "_epc");
                long jLongValue = ((Long) (serializableO != null ? serializableO : 0L)).longValue();
                this.f5952b = jLongValue;
                if (jLongValue <= 0) {
                    Z z16 = c0817r0.f12150y;
                    C0817r0.k(z16);
                    z16.f11876w.c(strT, "Complex event with zero extra param count. eventName");
                } else {
                    C0805n c0805n4 = q12.f11683s;
                    Q1.L(c0805n4);
                    c0805n4.q0(str, l8, this.f5952b, w02);
                }
            }
        }
        V0 v02 = (V0) w02.f();
        v02.k();
        W0.z((W0) v02.f9801r, strT);
        v02.k();
        W0.x((W0) v02.f9801r);
        v02.k();
        W0.v((W0) v02.f9801r, listU);
        return (W0) v02.h();
    }

    public C0828v m() {
        C0825u c0825u = new C0825u(new Bundle((Bundle) this.f5955e));
        return new C0828v((String) this.f5953c, c0825u, (String) this.f5954d, this.f5952b);
    }

    public String toString() {
        switch (this.f5951a) {
            case 1:
                return "origin=" + ((String) this.f5954d) + ",name=" + ((String) this.f5953c) + ",params=" + ((Bundle) this.f5955e).toString();
            default:
                return super.toString();
        }
    }

    public m(n7.d dVar) {
        TimeUnit timeUnit = TimeUnit.MINUTES;
        P6.g.e(dVar, "taskRunner");
        P6.g.e(timeUnit, "timeUnit");
        this.f5952b = timeUnit.toNanos(5L);
        this.f5953c = dVar.e();
        this.f5954d = new n7.b(this, I1.a.r(new StringBuilder(), l7.b.f14418g, " ConnectionPool"));
        this.f5955e = new ConcurrentLinkedQueue();
    }

    public m(p pVar, Context context) {
        this.f5955e = pVar;
        x0.v.I(context);
        E e8 = G.f1197r;
        this.f5953c = W.f1225u;
        this.f5952b = -9223372036854775807L;
    }
}
