package Y2;

import V4.C;
import com.debridstream.tv.data.home.HomeMetaItem;
import com.google.firebase.sessions.settings.RemoteSettings;
import com.revenuecat.purchases.common.diagnostics.DiagnosticsEntry;
import java.io.IOException;
import java.util.ArrayList;
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 g extends C4.j implements J4.e {

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

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

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

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

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public g(int i6, boolean z6, int i7, o oVar, A4.d dVar) {
        super(2, dVar);
        this.f8737a = i6;
        this.f8738b = z6;
        this.f8739c = i7;
        this.f8740d = oVar;
    }

    @Override // C4.a
    public final A4.d create(Object obj, A4.d dVar) {
        return new g(this.f8737a, this.f8738b, this.f8739c, this.f8740d, dVar);
    }

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

    /* JADX WARN: Finally extract failed */
    @Override // C4.a
    public final Object invokeSuspend(Object obj) throws IOException {
        int iOptInt;
        String str;
        String str2;
        String str3;
        String str4;
        String str5;
        String str6 = "";
        B4.a aVar = B4.a.f492a;
        AbstractC2467b.l(obj);
        y yVar = y.f26065a;
        int i6 = this.f8737a;
        if (i6 <= 0) {
            return yVar;
        }
        String str7 = "movie";
        String str8 = "tv";
        boolean z6 = this.f8738b;
        StringBuilder sbS = Z0.o.s(i6, "https://api.themoviedb.org/3/", z6 ? "tv" : "movie", RemoteSettings.FORWARD_SLASH_STRING, "/similar?api_key=7ba07eac0fde9485edfc077df9937ab6&language=en&page=");
        sbS.append(this.f8739c);
        String string = sbS.toString();
        Request.Builder builder = new Request.Builder();
        builder.i(string);
        builder.a("Accept", "application/json");
        Response responseD = this.f8740d.f8779a.a(builder.b()).d();
        try {
            if (!responseD.c()) {
                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;
            int i8 = 0;
            while (i8 < length) {
                JSONObject jSONObjectOptJSONObject = jSONArrayOptJSONArray.optJSONObject(i8);
                if (jSONObjectOptJSONObject == null || (iOptInt = jSONObjectOptJSONObject.optInt("id", i7)) == 0) {
                    str3 = str6;
                    str4 = str7;
                    str5 = str8;
                } else {
                    String strOptString = jSONObjectOptJSONObject.optString("poster_path", str6);
                    kotlin.jvm.internal.o.e(strOptString);
                    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 = z6 ? jSONObjectOptJSONObject.optString("first_air_date", str6) : jSONObjectOptJSONObject.optString("release_date", str6);
                    kotlin.jvm.internal.o.e(strOptString3);
                    String strM1 = S4.p.m1(4, strOptString3);
                    String str9 = (strM1.length() == 4 && Character.isDigit(strM1.charAt(0))) ? strM1 : null;
                    String str10 = str9 == null ? str6 : str9;
                    String strValueOf = String.valueOf(iOptInt);
                    String str11 = z6 ? str8 : str7;
                    String strOptString4 = jSONObjectOptJSONObject.optString(z6 ? DiagnosticsEntry.NAME_KEY : "title");
                    kotlin.jvm.internal.o.e(strOptString4);
                    String strOptString5 = jSONObjectOptJSONObject.optString("overview");
                    str3 = str6;
                    kotlin.jvm.internal.o.g(strOptString5, "optString(...)");
                    str4 = str7;
                    str5 = str8;
                    arrayList.add(new HomeMetaItem(strValueOf, iOptInt, str11, strOptString4, strOptString5, str10, jSONObjectOptJSONObject.optDouble("vote_average", 0.0d), str, str2 == null ? str : str2, null, W0.e.i(str3), 0, "", null, W.a.z0(jSONObjectOptJSONObject), W.a.A0(jSONObjectOptJSONObject), 8192, null));
                }
                i8++;
                str7 = str4;
                str6 = str3;
                str8 = str5;
                i7 = 0;
            }
            responseD.close();
            return arrayList;
        } catch (Throwable th) {
            try {
                throw th;
            } catch (Throwable th2) {
                android.support.v4.media.session.b.j(responseD, th);
                throw th2;
            }
        }
    }
}
