package O0;

import B0.C0016c;
import B0.P;
import C3.C0059v;
import C3.G;
import C3.V;
import C3.W;
import D1.F;
import L0.n0;
import N1.C0198b;
import P2.C0253j;
import android.content.Context;
import android.text.TextUtils;
import android.util.Pair;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.RandomAccess;
import u0.C1567c;
import u0.C1579o;
import u0.T;
import u0.U;
import x0.AbstractC1709a;

/* JADX INFO: loaded from: classes.dex */
public final class q extends u {
    public static final V i = new C0059v(new F(4));

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final Context f4900d;

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public k f4902f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public C0016c f4903g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public C1567c f4904h;

    public q(Context context) {
        C0253j c0253j = new C0253j(10);
        k kVar = k.D;
        this.f4899c = new Object();
        this.f4900d = context.getApplicationContext();
        this.f4901e = c0253j;
        if (kVar != null) {
            this.f4902f = kVar;
        } else {
            kVar.getClass();
            j jVar = new j(kVar);
            jVar.b(kVar);
            this.f4902f = new k(jVar);
        }
        this.f4904h = C1567c.f17266b;
        boolean z8 = this.f4902f.f4867y;
    }

    public static void a(n0 n0Var, k kVar, HashMap map) {
        for (int i5 = 0; i5 < n0Var.f3883a; i5++) {
            U u8 = (U) kVar.f17244s.get(n0Var.a(i5));
            if (u8 != null) {
                T t6 = u8.f17205a;
                U u9 = (U) map.get(Integer.valueOf(t6.f17202c));
                if (u9 == null || (u9.f17206b.isEmpty() && !u8.f17206b.isEmpty())) {
                    map.put(Integer.valueOf(t6.f17202c), u8);
                }
            }
        }
    }

    public static int b(C1579o c1579o, String str, boolean z8) {
        if (!TextUtils.isEmpty(str) && str.equals(c1579o.f17349d)) {
            return 4;
        }
        String strE = e(str);
        String strE2 = e(c1579o.f17349d);
        if (strE2 == null || strE == null) {
            return (z8 && strE2 == null) ? 1 : 0;
        }
        if (strE2.startsWith(strE) || strE.startsWith(strE2)) {
            return 3;
        }
        int i5 = x0.v.f18553a;
        return strE2.split("-", 2)[0].equals(strE.split("-", 2)[0]) ? 2 : 0;
    }

    public static String e(String str) {
        if (TextUtils.isEmpty(str) || TextUtils.equals(str, "und")) {
            return null;
        }
        return str;
    }

    public static Pair g(int i5, C0198b c0198b, int[][][] iArr, n nVar, Comparator comparator) {
        RandomAccess randomAccessP;
        C0198b c0198b2 = c0198b;
        ArrayList arrayList = new ArrayList();
        int i8 = 0;
        while (i8 < c0198b2.f4504a) {
            if (i5 == ((int[]) c0198b2.f4505b)[i8]) {
                n0 n0Var = ((n0[]) c0198b2.f4506c)[i8];
                for (int i9 = 0; i9 < n0Var.f3883a; i9++) {
                    T tA = n0Var.a(i9);
                    W wH = nVar.h(i8, tA, iArr[i8][i9]);
                    int i10 = tA.f17200a;
                    boolean[] zArr = new boolean[i10];
                    for (int i11 = 0; i11 < i10; i11++) {
                        o oVar = (o) wH.get(i11);
                        int iA = oVar.a();
                        if (!zArr[i11] && iA != 0) {
                            boolean z8 = true;
                            if (iA == 1) {
                                randomAccessP = G.p(oVar);
                            } else {
                                ArrayList arrayList2 = new ArrayList();
                                arrayList2.add(oVar);
                                int i12 = i11 + 1;
                                while (i12 < i10) {
                                    boolean z9 = z8;
                                    o oVar2 = (o) wH.get(i12);
                                    if (oVar2.a() == 2 && oVar.b(oVar2)) {
                                        arrayList2.add(oVar2);
                                        zArr[i12] = z9;
                                    }
                                    i12++;
                                    z8 = z9;
                                }
                                randomAccessP = arrayList2;
                            }
                            arrayList.add(randomAccessP);
                        }
                    }
                }
            }
            i8++;
            c0198b2 = c0198b;
        }
        if (arrayList.isEmpty()) {
            return null;
        }
        List list = (List) Collections.max(arrayList, comparator);
        int[] iArr2 = new int[list.size()];
        for (int i13 = 0; i13 < list.size(); i13++) {
            iArr2[i13] = ((o) list.get(i13)).f4881s;
        }
        o oVar3 = (o) list.get(0);
        return Pair.create(new r(oVar3.f4880r, iArr2), Integer.valueOf(oVar3.f4879q));
    }

    public final k c() {
        k kVar;
        synchronized (this.f4899c) {
            kVar = this.f4902f;
        }
        return kVar;
    }

    public final void d() {
        boolean z8;
        P p3;
        C0016c c0016c;
        synchronized (this.f4899c) {
            try {
                z8 = this.f4902f.f4867y && x0.v.f18553a >= 32 && (c0016c = this.f4903g) != null && c0016c.f717q;
            } catch (Throwable th) {
                throw th;
            }
        }
        if (!z8 || (p3 = this.f4907a) == null) {
            return;
        }
        p3.f651x.e(10);
    }

    public final void f() {
        synchronized (this.f4899c) {
            this.f4902f.getClass();
        }
    }

    public final void h(k kVar) {
        boolean zEquals;
        kVar.getClass();
        synchronized (this.f4899c) {
            zEquals = this.f4902f.equals(kVar);
            this.f4902f = kVar;
        }
        if (zEquals) {
            return;
        }
        if (kVar.f4867y && this.f4900d == null) {
            AbstractC1709a.B("DefaultTrackSelector", "Audio channel count constraints cannot be applied without reference to Context. Build the track selector instance with one of the non-deprecated constructors that take a Context argument.");
        }
        P p3 = this.f4907a;
        if (p3 != null) {
            p3.f651x.e(10);
        }
    }
}
