package Y2;

import S4.w;
import V4.C;
import com.debridstream.tv.data.home.HomeMetaItem;
import com.revenuecat.purchases.common.diagnostics.DiagnosticsEntry;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import m3.AbstractC2467b;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.json.JSONArray;
import org.json.JSONObject;
import w4.C3137A;
import x4.y;

/* JADX INFO: loaded from: classes.dex */
public final class h extends C4.j implements J4.e {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final /* synthetic */ String f8741a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final /* synthetic */ boolean f8742b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final /* synthetic */ int f8743c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final /* synthetic */ o f8744d;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public h(String str, boolean z6, int i6, o oVar, A4.d dVar) {
        super(2, dVar);
        this.f8741a = str;
        this.f8742b = z6;
        this.f8743c = i6;
        this.f8744d = oVar;
    }

    @Override // C4.a
    public final A4.d create(Object obj, A4.d dVar) {
        return new h(this.f8741a, this.f8742b, this.f8743c, this.f8744d, dVar);
    }

    @Override // J4.e
    public final Object invoke(Object obj, Object obj2) {
        return ((h) create((C) obj, (A4.d) obj2)).invokeSuspend(C3137A.f25453a);
    }

    @Override // C4.a
    public final Object invokeSuspend(Object obj) throws IOException {
        int iOptInt;
        String str;
        String str2;
        String str3;
        String str4;
        String str5 = "optString(...)";
        String str6 = "";
        B4.a aVar = B4.a.f492a;
        AbstractC2467b.l(obj);
        String strX0 = this.f8741a;
        int i6 = 0;
        if (w.A0(strX0, "discover/tv?", false)) {
            strX0 = S4.p.X0(strX0, "discover/tv?");
        } else if (w.A0(strX0, "discover/tv&", false)) {
            strX0 = S4.p.X0(strX0, "discover/tv&");
        }
        if (this.f8742b) {
            strX0 = q.e(strX0, false);
        }
        String strN = A0.a.n("https://api.themoviedb.org/3/discover/tv?api_key=7ba07eac0fde9485edfc077df9937ab6&", q.d(this.f8743c, strX0));
        Request.Builder builder = new Request.Builder();
        builder.i(strN);
        builder.a("Accept", "application/json");
        Response responseD = this.f8744d.f8779a.a(builder.b()).d();
        try {
            boolean zC = responseD.c();
            y yVar = y.f26065a;
            if (!zC) {
                responseD.close();
                return yVar;
            }
            ResponseBody responseBody = responseD.f22146j;
            if (responseBody == null) {
                responseD.close();
                return yVar;
            }
            JSONArray jSONArrayOptJSONArray = new JSONObject(responseBody.d()).optJSONArray("results");
            if (jSONArrayOptJSONArray == null) {
                responseD.close();
                return yVar;
            }
            ArrayList arrayList = new ArrayList(jSONArrayOptJSONArray.length());
            int length = jSONArrayOptJSONArray.length();
            int i7 = 0;
            while (i7 < length) {
                JSONObject jSONObjectOptJSONObject = jSONArrayOptJSONArray.optJSONObject(i7);
                if (jSONObjectOptJSONObject == null || (iOptInt = jSONObjectOptJSONObject.optInt("id", i6)) == 0) {
                    str3 = str5;
                    str4 = str6;
                } else {
                    String strOptString = jSONObjectOptJSONObject.optString("poster_path", str6);
                    kotlin.jvm.internal.o.e(strOptString);
                    String str7 = null;
                    if (!q.a(strOptString)) {
                        strOptString = null;
                    }
                    if (strOptString != null) {
                        str = "https://image.tmdb.org/t/p/w500" + strOptString;
                    } else {
                        str = null;
                    }
                    String strOptString2 = jSONObjectOptJSONObject.optString("backdrop_path", str6);
                    kotlin.jvm.internal.o.e(strOptString2);
                    if (!q.a(strOptString2)) {
                        strOptString2 = null;
                    }
                    if (strOptString2 != null) {
                        str2 = "https://image.tmdb.org/t/p/w780" + strOptString2;
                    } else {
                        str2 = null;
                    }
                    String strOptString3 = jSONObjectOptJSONObject.optString("first_air_date", str6);
                    kotlin.jvm.internal.o.e(strOptString3);
                    String strM1 = S4.p.m1(4, strOptString3);
                    if (strM1.length() == 4 && Character.isDigit(strM1.charAt(i6))) {
                        str7 = strM1;
                    }
                    String str8 = str7 == null ? str6 : str7;
                    String strValueOf = String.valueOf(iOptInt);
                    String strOptString4 = jSONObjectOptJSONObject.optString(DiagnosticsEntry.NAME_KEY);
                    kotlin.jvm.internal.o.g(strOptString4, str5);
                    String strOptString5 = jSONObjectOptJSONObject.optString("overview");
                    kotlin.jvm.internal.o.g(strOptString5, str5);
                    str3 = str5;
                    str4 = str6;
                    arrayList.add(new HomeMetaItem(strValueOf, iOptInt, "tv", strOptString4, strOptString5, str8, jSONObjectOptJSONObject.optDouble("vote_average", 0.0d), str, str2 == null ? str : str2, null, W0.e.i(str4), 0, "", null, W.a.z0(jSONObjectOptJSONObject), W.a.A0(jSONObjectOptJSONObject), 8192, null));
                }
                i7++;
                str5 = str3;
                str6 = str4;
                i6 = 0;
            }
            List listA0 = W.a.a0(arrayList);
            responseD.close();
            return listA0;
        } finally {
        }
    }
}
