package m3;

import E3.v;
import Q0.C;
import S4.w;
import V4.E;
import V4.N;
import androidx.media3.common.MimeTypes;
import androidx.media3.extractor.text.ttml.TtmlNode;
import androidx.media3.extractor.ts.TsExtractor;
import com.google.android.gms.common.internal.ImagesContract;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import n2.AbstractC2612c;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.RequestBody$Companion$toRequestBody$2;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.json.JSONArray;
import org.json.JSONObject;
import x4.AbstractC3251q;
import x4.AbstractC3252r;
import x4.y;
import y4.C3274b;

/* JADX INFO: renamed from: m3.j, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C2475j {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final C2475j f20506a = new C2475j();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final OkHttpClient f20507b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final MediaType f20508c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final S4.m f20509d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final List f20510e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final List f20511f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final C2468c f20512g;

    static {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        TimeUnit timeUnit = TimeUnit.SECONDS;
        builder.c(12L);
        builder.d(20L);
        builder.e(12L);
        f20507b = new OkHttpClient(builder);
        MediaType.f22043d.getClass();
        f20508c = MediaType.Companion.a("application/json; charset=utf-8");
        f20509d = new S4.m("codecs=\"([^\"]+)\"");
        f20510e = AbstractC3252r.B(137, Integer.valueOf(TsExtractor.TS_STREAM_TYPE_DTS_HD), Integer.valueOf(TsExtractor.TS_STREAM_TYPE_E_AC3), Integer.valueOf(TsExtractor.TS_STREAM_TYPE_SPLICE_INFO), 133);
        f20511f = AbstractC3252r.B(Integer.valueOf(TsExtractor.TS_STREAM_TYPE_DTS_HD), Integer.valueOf(TsExtractor.TS_STREAM_TYPE_E_AC3), Integer.valueOf(TsExtractor.TS_STREAM_TYPE_SPLICE_INFO), 133);
        f20512g = new C2468c();
    }

    public static final int a(C2472g c2472g) {
        String strM1;
        String string = S4.p.o1(c2472g.f20497n).toString();
        if (string.length() == 0) {
            strM1 = "";
        } else {
            int iN0 = S4.p.N0(string, '.', 0, 6);
            if (iN0 > 0) {
                string = string.substring(0, iN0);
                kotlin.jvm.internal.o.g(string, "substring(...)");
            }
            String lowerCase = string.toLowerCase(Locale.ROOT);
            kotlin.jvm.internal.o.g(lowerCase, "toLowerCase(...)");
            strM1 = S4.p.m1(2, lowerCase);
        }
        int i6 = (strM1.equals("en") || w.A0(strM1, "en-", false)) ? 100 : 0;
        if (S4.p.D0(c2472g.f20498o, "original", true)) {
            i6 += 50;
        }
        return c2472g.f20499p ? i6 + 10 : i6;
    }

    public static final AbstractC2471f b(String videoId, C2468c c2468c, EnumC2466a enumC2466a) throws IOException {
        RequestBody.Companion companion = RequestBody.f22136a;
        c2468c.getClass();
        kotlin.jvm.internal.o.h(videoId, "videoId");
        String string = new JSONObject().put("context", new JSONObject().put("client", new JSONObject().put("clientName", "ANDROID").put("clientVersion", "20.03.38").put("androidSdkVersion", 34).put("hl", "en").put("gl", "US"))).put("videoId", videoId).toString();
        kotlin.jvm.internal.o.g(string, "toString(...)");
        companion.getClass();
        RequestBody$Companion$toRequestBody$2 requestBody$Companion$toRequestBody$2A = RequestBody.Companion.a(string, f20508c);
        Request.Builder builder = new Request.Builder();
        builder.i("https://www.youtube.com/youtubei/v1/player?prettyPrint=false&key=AIzaSyAO_FJ2SlqU8Q4STEHL6lAdF6a7JSNvgbk");
        builder.h(requestBody$Companion$toRequestBody$2A);
        builder.e("Content-Type", "application/json");
        builder.e("User-Agent", "com.google.android.youtube/20.03.38 (Linux; U; Android 14) gzip");
        builder.e("X-YouTube-Client-Name", "3");
        builder.e("X-YouTube-Client-Version", "20.03.38");
        Response responseD = f20507b.a(builder.b()).d();
        try {
            if (!responseD.c()) {
                throw new IllegalStateException("InnerTube HTTP " + responseD.f22143d);
            }
            ResponseBody responseBody = responseD.f22146j;
            String strD = responseBody != null ? responseBody.d() : null;
            String str = "";
            if (strD == null) {
                strD = "";
            }
            JSONObject jSONObject = new JSONObject(strD);
            if (jSONObject.has("error")) {
                JSONObject jSONObjectOptJSONObject = jSONObject.optJSONObject("error");
                String strOptString = jSONObjectOptJSONObject != null ? jSONObjectOptJSONObject.optString("message") : null;
                if (strOptString != null) {
                    str = strOptString;
                }
                if (S4.p.Q0(str)) {
                    str = "InnerTube error";
                }
                throw new IllegalStateException(str);
            }
            JSONObject jSONObjectOptJSONObject2 = jSONObject.optJSONObject("playabilityStatus");
            if (!kotlin.jvm.internal.o.c(jSONObjectOptJSONObject2 != null ? jSONObjectOptJSONObject2.optString("status") : null, "OK")) {
                responseD.close();
                return null;
            }
            JSONObject jSONObjectOptJSONObject3 = jSONObject.optJSONObject("streamingData");
            if (jSONObjectOptJSONObject3 == null) {
                responseD.close();
                return null;
            }
            AbstractC2471f abstractC2471fD = d(jSONObjectOptJSONObject3, enumC2466a);
            responseD.close();
            return abstractC2471fD;
        } catch (Throwable th) {
            try {
                throw th;
            } catch (Throwable th2) {
                android.support.v4.media.session.b.j(responseD, th);
                throw th2;
            }
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:11:0x001e  */
    /* JADX WARN: Type inference failed for: r3v0 */
    /* JADX WARN: Type inference failed for: r3v1, types: [boolean, int] */
    /* JADX WARN: Type inference failed for: r3v14 */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static List c(JSONArray jSONArray) {
        String str;
        JSONObject jSONObjectOptJSONObject;
        JSONObject jSONObjectOptJSONObject2;
        int i6;
        if (jSONArray == null || jSONArray.length() == 0) {
            return y.f26065a;
        }
        C3274b c3274bK = AbstractC2612c.k();
        int length = jSONArray.length();
        ?? r32 = 0;
        int i7 = 0;
        while (i7 < length) {
            JSONObject jSONObjectOptJSONObject3 = jSONArray.optJSONObject(i7);
            if (jSONObjectOptJSONObject3 == 0) {
                i6 = i7;
            } else {
                String strOptString = jSONObjectOptJSONObject3.optString(ImagesContract.URL);
                kotlin.jvm.internal.o.e(strOptString);
                String str2 = !S4.p.Q0(strOptString) ? strOptString : null;
                if (str2 != null) {
                    String strOptString2 = jSONObjectOptJSONObject3.optString("mimeType");
                    kotlin.jvm.internal.o.e(strOptString2);
                    S4.k kVarB = f20509d.b(strOptString2);
                    if (kVarB == null) {
                        str = null;
                    } else {
                        Object obj = ((S4.j) kVarB.a()).get(1);
                        if (S4.p.Q0((String) obj)) {
                            obj = null;
                        }
                        str = (String) obj;
                    }
                    if (str != null && (jSONObjectOptJSONObject = jSONObjectOptJSONObject3.optJSONObject("initRange")) != null && (jSONObjectOptJSONObject2 = jSONObjectOptJSONObject3.optJSONObject("indexRange")) != null) {
                        String strOptString3 = jSONObjectOptJSONObject.optString(TtmlNode.START);
                        kotlin.jvm.internal.o.g(strOptString3, "optString(...)");
                        Integer numB0 = w.B0(strOptString3);
                        if (numB0 != null) {
                            int iIntValue = numB0.intValue();
                            String strOptString4 = jSONObjectOptJSONObject.optString(TtmlNode.END);
                            kotlin.jvm.internal.o.g(strOptString4, "optString(...)");
                            Integer numB02 = w.B0(strOptString4);
                            if (numB02 != null) {
                                int iIntValue2 = numB02.intValue();
                                String strOptString5 = jSONObjectOptJSONObject2.optString(TtmlNode.START);
                                kotlin.jvm.internal.o.g(strOptString5, "optString(...)");
                                Integer numB03 = w.B0(strOptString5);
                                if (numB03 != null) {
                                    int iIntValue3 = numB03.intValue();
                                    String strOptString6 = jSONObjectOptJSONObject2.optString(TtmlNode.END);
                                    kotlin.jvm.internal.o.g(strOptString6, "optString(...)");
                                    Integer numB04 = w.B0(strOptString6);
                                    if (numB04 != null) {
                                        int iIntValue4 = numB04.intValue();
                                        boolean zA0 = w.A0(strOptString2, "video/", r32);
                                        JSONObject jSONObjectOptJSONObject4 = jSONObjectOptJSONObject3.optJSONObject("audioTrack");
                                        int iOptInt = jSONObjectOptJSONObject3.optInt("itag");
                                        int iOptInt2 = jSONObjectOptJSONObject3.optInt("bitrate", r32);
                                        int iOptInt3 = jSONObjectOptJSONObject3.optInt("width", r32);
                                        int iOptInt4 = jSONObjectOptJSONObject3.optInt("height", r32);
                                        i6 = i7;
                                        long jOptLong = jSONObjectOptJSONObject3.optLong("approxDurationMs", 0L);
                                        Long lValueOf = Long.valueOf(jOptLong);
                                        if (jOptLong <= 0) {
                                            lValueOf = null;
                                        }
                                        long jLongValue = lValueOf != null ? lValueOf.longValue() : 1L;
                                        String strOptString7 = jSONObjectOptJSONObject4 != null ? jSONObjectOptJSONObject4.optString("id") : null;
                                        String str3 = strOptString7 == null ? "" : strOptString7;
                                        String strOptString8 = jSONObjectOptJSONObject4 != null ? jSONObjectOptJSONObject4.optString("displayName") : null;
                                        c3274bK.add(new C2472g(str2, iOptInt, strOptString2, str, iOptInt2, iOptInt3, iOptInt4, iIntValue, iIntValue2, iIntValue3, iIntValue4, jLongValue, zA0, str3, strOptString8 == null ? "" : strOptString8, jSONObjectOptJSONObject4 != null ? jSONObjectOptJSONObject4.optBoolean("audioIsDefault") : false));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            i7 = i6 + 1;
            r32 = 0;
        }
        return AbstractC2612c.e(c3274bK);
    }

    public static AbstractC2471f d(JSONObject jSONObject, EnumC2466a enumC2466a) {
        int i6;
        List list;
        Object next;
        int i7;
        List listC = c(jSONObject.optJSONArray("adaptiveFormats"));
        List listC2 = c(jSONObject.optJSONArray("formats"));
        int iOrdinal = enumC2466a.ordinal();
        if (iOrdinal == 0) {
            i6 = 1080;
        } else {
            if (iOrdinal != 1) {
                throw new v();
            }
            i6 = 720;
        }
        int iOrdinal2 = enumC2466a.ordinal();
        if (iOrdinal2 == 0) {
            list = f20510e;
        } else {
            if (iOrdinal2 != 1) {
                throw new v();
            }
            list = f20511f;
        }
        ArrayList arrayList = new ArrayList();
        Iterator it = listC2.iterator();
        while (true) {
            int i8 = 0;
            if (!it.hasNext()) {
                C2472g c2472g = (C2472g) AbstractC3251q.q0(arrayList, new C(new Q2.i(8), 8));
                if (c2472g != null) {
                    return new C2470e(c2472g.f20485a);
                }
                ArrayList arrayList2 = new ArrayList();
                for (Object obj : listC) {
                    C2472g c2472g2 = (C2472g) obj;
                    if (!c2472g2.f20496m && w.A0(c2472g2.f20487c, MimeTypes.AUDIO_MP4, false)) {
                        arrayList2.add(obj);
                    }
                }
                C2472g c2472g3 = arrayList2.isEmpty() ? null : (C2472g) AbstractC3251q.q0(arrayList2, new C(new C(new Q2.i(10), 10), 11));
                ArrayList arrayList3 = new ArrayList();
                Iterator it2 = list.iterator();
                while (it2.hasNext()) {
                    int iIntValue = ((Number) it2.next()).intValue();
                    Iterator it3 = listC.iterator();
                    while (true) {
                        if (!it3.hasNext()) {
                            next = null;
                            break;
                        }
                        next = it3.next();
                        C2472g c2472g4 = (C2472g) next;
                        if (c2472g4.f20486b == iIntValue && c2472g4.f20496m) {
                            break;
                        }
                    }
                    C2472g c2472g5 = (C2472g) next;
                    if (c2472g5 != null) {
                        arrayList3.add(c2472g5);
                    }
                }
                C2472g c2472g6 = (C2472g) AbstractC3251q.g0(arrayList3);
                if (c2472g6 != null && c2472g3 != null) {
                    return new C2469d(c2472g6, c2472g3);
                }
                ArrayList arrayList4 = new ArrayList();
                for (Object obj2 : listC2) {
                    C2472g c2472g7 = (C2472g) obj2;
                    if (c2472g7.f20496m && c2472g7.f20491g <= i6) {
                        arrayList4.add(obj2);
                    }
                }
                C2472g c2472g8 = (C2472g) AbstractC3251q.q0(arrayList4, new C(new Q2.i(9), 9));
                if (c2472g8 != null) {
                    return new C2470e(c2472g8.f20485a);
                }
                return null;
            }
            Object next2 = it.next();
            C2472g c2472g9 = (C2472g) next2;
            if (c2472g9.f20496m && (i7 = c2472g9.f20491g) <= i6) {
                int iOrdinal3 = enumC2466a.ordinal();
                if (iOrdinal3 == 0) {
                    i8 = 720;
                } else if (iOrdinal3 != 1) {
                    throw new v();
                }
                if (i7 >= i8) {
                    arrayList.add(next2);
                }
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object e(String str, EnumC2466a enumC2466a, C4.c cVar) {
        C2473h c2473h;
        if (cVar instanceof C2473h) {
            c2473h = (C2473h) cVar;
            int i6 = c2473h.f20502c;
            if ((i6 & Integer.MIN_VALUE) != 0) {
                c2473h.f20502c = i6 - Integer.MIN_VALUE;
            } else {
                c2473h = new C2473h(this, cVar);
            }
        }
        Object objH = c2473h.f20500a;
        B4.a aVar = B4.a.f492a;
        int i7 = c2473h.f20502c;
        if (i7 == 0) {
            AbstractC2467b.l(objH);
            c5.e eVar = N.f8259a;
            c5.d dVar = c5.d.f13466b;
            C2474i c2474i = new C2474i(str, enumC2466a, null);
            c2473h.f20502c = 1;
            objH = E.H(dVar, c2474i, c2473h);
            if (objH == aVar) {
                return aVar;
            }
        } else {
            if (i7 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            AbstractC2467b.l(objH);
        }
        return ((w4.n) objH).f25472a;
    }
}
