package Y2;

import S4.w;
import V4.C;
import com.debridstream.tv.data.home.HomeMetaItem;
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 d extends C4.j implements J4.e {

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

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

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

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

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

    @Override // C4.a
    public final A4.d create(Object obj, A4.d dVar) {
        return new d(this.f8727a, this.f8728b, this.f8729c, this.f8730d, dVar);
    }

    @Override // J4.e
    public final Object invoke(Object obj, Object obj2) {
        return ((d) 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;
        boolean z6;
        String str3;
        boolean z7 = true;
        String str4 = "optString(...)";
        B4.a aVar = B4.a.f492a;
        AbstractC2467b.l(obj);
        String strX0 = this.f8727a;
        int i6 = 0;
        if (w.A0(strX0, "discover/movie?", false)) {
            strX0 = S4.p.X0(strX0, "discover/movie?");
        } else if (w.A0(strX0, "discover/movie&", false)) {
            strX0 = S4.p.X0(strX0, "discover/movie&");
        }
        if (this.f8728b) {
            strX0 = q.e(strX0, true);
        }
        String strN = A0.a.n("https://api.themoviedb.org/3/discover/movie?api_key=7ba07eac0fde9485edfc077df9937ab6&", q.d(this.f8729c, strX0));
        Request.Builder builder = new Request.Builder();
        builder.i(strN);
        builder.a("Accept", "application/json");
        Response responseD = this.f8730d.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) {
                    z6 = z7;
                    str3 = str4;
                } else {
                    String strOptString = jSONObjectOptJSONObject.optString("poster_path", "");
                    kotlin.jvm.internal.o.e(strOptString);
                    String str5 = 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", "");
                    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("release_date", "");
                    kotlin.jvm.internal.o.e(strOptString3);
                    String strM1 = S4.p.m1(4, strOptString3);
                    z6 = z7;
                    if (strM1.length() == 4 && Character.isDigit(strM1.charAt(i6))) {
                        str5 = strM1;
                    }
                    String str6 = str5 == null ? "" : str5;
                    String strValueOf = String.valueOf(iOptInt);
                    String strOptString4 = jSONObjectOptJSONObject.optString("title");
                    kotlin.jvm.internal.o.g(strOptString4, str4);
                    String strOptString5 = jSONObjectOptJSONObject.optString("overview");
                    kotlin.jvm.internal.o.g(strOptString5, str4);
                    str3 = str4;
                    arrayList.add(new HomeMetaItem(strValueOf, iOptInt, "movie", strOptString4, strOptString5, str6, jSONObjectOptJSONObject.optDouble("vote_average", 0.0d), str, str2 == null ? str : str2, null, W0.e.i(""), 0, "", null, W.a.z0(jSONObjectOptJSONObject), W.a.A0(jSONObjectOptJSONObject), 8192, null));
                }
                i7++;
                z7 = z6;
                str4 = str3;
                i6 = 0;
            }
            List listA0 = W.a.a0(arrayList);
            responseD.close();
            return listA0;
        } finally {
        }
    }
}
