package Y2;

import S4.w;
import V2.C0715r2;
import V2.C0736x;
import V4.D0;
import V4.E;
import V4.N;
import a.AbstractC0872a;
import android.content.Context;
import android.os.SystemClock;
import com.debridstream.tv.data.home.HomeMetaItem;
import com.revenuecat.purchases.common.diagnostics.DiagnosticsEntry;
import e3.AbstractC1966c;
import g3.u;
import j$.time.LocalDate;
import j$.time.ZoneId;
import java.io.EOFException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import m3.AbstractC2467b;
import n2.AbstractC2612c;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import x4.AbstractC3251q;
import x4.AbstractC3252r;
import x4.y;

/* JADX INFO: loaded from: classes.dex */
public final class o {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final a5.c f8777b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final ConcurrentHashMap.KeySetView f8778c;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final OkHttpClient f8779a;

    static {
        D0 d0D = E.d();
        c5.e eVar = N.f8259a;
        f8777b = E.c(AbstractC0872a.a0(d0D, c5.d.f13466b));
        f8778c = ConcurrentHashMap.newKeySet();
    }

    public o() {
        OkHttpClient client = (OkHttpClient) AbstractC1966c.f16270a.getValue();
        kotlin.jvm.internal.o.h(client, "client");
        this.f8779a = client;
    }

    /* JADX WARN: Removed duplicated region for block: B:38:0x008d  */
    /* JADX WARN: Removed duplicated region for block: B:61:0x005a A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0016  */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:55:0x00d1 -> B:57:0x00d4). Please report as a decompilation issue!!! */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final Object a(o oVar, String str, C4.c cVar) throws EOFException {
        b bVar;
        o oVar2;
        int i6;
        Request requestB;
        oVar.getClass();
        if (cVar instanceof b) {
            bVar = (b) cVar;
            int i7 = bVar.f8721f;
            if ((i7 & Integer.MIN_VALUE) != 0) {
                bVar.f8721f = i7 - Integer.MIN_VALUE;
            } else {
                bVar = new b(oVar, cVar);
            }
        }
        Object obj = bVar.f8719d;
        B4.a aVar = B4.a.f492a;
        int i8 = bVar.f8721f;
        if (i8 == 0) {
            AbstractC2467b.l(obj);
            Request.Builder builder = new Request.Builder();
            builder.i(str);
            builder.a("Accept", "application/json");
            oVar2 = oVar;
            i6 = 0;
            requestB = builder.b();
            if (i6 < 3) {
            }
            return null;
        }
        if (i8 != 1) {
            throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
        }
        i6 = bVar.f8718c;
        Request request = bVar.f8717b;
        o oVar3 = bVar.f8716a;
        AbstractC2467b.l(obj);
        requestB = request;
        oVar2 = oVar3;
        i6++;
        if (i6 < 3) {
            try {
            } catch (IOException e6) {
                oVar2.getClass();
                while (e != null) {
                }
            }
            Response responseD = oVar2.f8779a.a(requestB).d();
            try {
                if (responseD.c()) {
                    ResponseBody responseBody = responseD.f22146j;
                    String strD = responseBody != null ? responseBody.d() : null;
                    responseD.close();
                    return strD;
                }
                responseD.close();
            } finally {
            }
            oVar2.getClass();
            for (e = e6; e != null; e = e.getCause()) {
                String message = e.getMessage();
                if (message == null) {
                    message = "";
                }
                if (S4.p.D0(message, "gzip finished without exhausting source", true) || S4.p.D0(message, "unexpected end of stream", true) || S4.p.D0(message, "Connection reset", true) || S4.p.D0(message, "stream was reset", true)) {
                    if (i6 != 2) {
                        bVar.f8716a = oVar2;
                        bVar.f8717b = requestB;
                        bVar.f8718c = i6;
                        bVar.f8721f = 1;
                        if (E.l(((long) (i6 + 1)) * 80, bVar) == aVar) {
                            return aVar;
                        }
                        i6++;
                        if (i6 < 3) {
                        }
                    }
                }
            }
        }
        return null;
    }

    public static final HomeMetaItem b(o oVar, JSONObject jSONObject) {
        oVar.getClass();
        int iOptInt = jSONObject.optInt("id", 0);
        String str = null;
        if (iOptInt != 0) {
            String strOptString = jSONObject.optString("title");
            kotlin.jvm.internal.o.e(strOptString);
            if (strOptString.length() != 0) {
                String strOptString2 = jSONObject.optString("poster_path", "");
                kotlin.jvm.internal.o.e(strOptString2);
                if (!q.a(strOptString2)) {
                    strOptString2 = null;
                }
                String strConcat = strOptString2 != null ? "https://image.tmdb.org/t/p/w500".concat(strOptString2) : null;
                String strOptString3 = jSONObject.optString("backdrop_path", "");
                kotlin.jvm.internal.o.e(strOptString3);
                if (!q.a(strOptString3)) {
                    strOptString3 = null;
                }
                String strConcat2 = strOptString3 != null ? "https://image.tmdb.org/t/p/w780".concat(strOptString3) : null;
                String strOptString4 = jSONObject.optString("release_date", "");
                kotlin.jvm.internal.o.e(strOptString4);
                String strM1 = S4.p.m1(4, strOptString4);
                if (strM1.length() == 4 && Character.isDigit(strM1.charAt(0))) {
                    str = strM1;
                }
                String str2 = str == null ? "" : str;
                String str3 = strConcat2;
                String strValueOf = String.valueOf(iOptInt);
                String strOptString5 = jSONObject.optString("overview");
                kotlin.jvm.internal.o.g(strOptString5, "optString(...)");
                return new HomeMetaItem(strValueOf, iOptInt, "movie", strOptString, strOptString5, str2, jSONObject.optDouble("vote_average", 0.0d), strConcat, str3 == null ? strConcat : str3, null, W0.e.i(""), 0, "", null, W.a.z0(jSONObject), W.a.A0(jSONObject), 8192, null);
            }
        }
        return null;
    }

    public static final HomeMetaItem c(o oVar, JSONObject jSONObject) {
        oVar.getClass();
        int iOptInt = jSONObject.optInt("id", 0);
        String str = null;
        if (iOptInt != 0) {
            String strOptString = jSONObject.optString(DiagnosticsEntry.NAME_KEY);
            kotlin.jvm.internal.o.e(strOptString);
            if (strOptString.length() != 0) {
                String strOptString2 = jSONObject.optString("poster_path", "");
                kotlin.jvm.internal.o.e(strOptString2);
                if (!q.a(strOptString2)) {
                    strOptString2 = null;
                }
                String strConcat = strOptString2 != null ? "https://image.tmdb.org/t/p/w500".concat(strOptString2) : null;
                String strOptString3 = jSONObject.optString("backdrop_path", "");
                kotlin.jvm.internal.o.e(strOptString3);
                if (!q.a(strOptString3)) {
                    strOptString3 = null;
                }
                String strConcat2 = strOptString3 != null ? "https://image.tmdb.org/t/p/w780".concat(strOptString3) : null;
                String strOptString4 = jSONObject.optString("first_air_date", "");
                kotlin.jvm.internal.o.e(strOptString4);
                String strM1 = S4.p.m1(4, strOptString4);
                if (strM1.length() == 4 && Character.isDigit(strM1.charAt(0))) {
                    str = strM1;
                }
                String str2 = str == null ? "" : str;
                String str3 = strConcat2;
                String strValueOf = String.valueOf(iOptInt);
                String strOptString5 = jSONObject.optString("overview");
                kotlin.jvm.internal.o.g(strOptString5, "optString(...)");
                return new HomeMetaItem(strValueOf, iOptInt, "tv", strOptString, strOptString5, str2, jSONObject.optDouble("vote_average", 0.0d), strConcat, str3 == null ? strConcat : str3, null, W0.e.i(""), 0, "", null, W.a.z0(jSONObject), W.a.A0(jSONObject), 8192, null);
            }
        }
        return null;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r0v1 */
    /* JADX WARN: Type inference failed for: r0v3 */
    /* JADX WARN: Type inference failed for: r0v4 */
    /* JADX WARN: Type inference failed for: r5v1, types: [w4.m] */
    /* JADX WARN: Type inference failed for: r5v2, types: [java.lang.Object] */
    /* JADX WARN: Type inference failed for: r5v4, types: [java.util.ArrayList] */
    public static final List d(o oVar, String str, boolean z6) {
        ?? D3;
        JSONArray jSONArrayOptJSONArray;
        s sVarU;
        oVar.getClass();
        boolean zQ0 = S4.p.Q0(str);
        y yVar = y.f26065a;
        if (!zQ0) {
            try {
                jSONArrayOptJSONArray = new JSONObject(str).optJSONArray("results");
            } catch (Throwable th) {
                D3 = AbstractC2467b.d(th);
            }
            if (jSONArrayOptJSONArray != null) {
                D3 = new ArrayList(jSONArrayOptJSONArray.length());
                int length = jSONArrayOptJSONArray.length();
                for (int i6 = 0; i6 < length; i6++) {
                    JSONObject jSONObjectOptJSONObject = jSONArrayOptJSONArray.optJSONObject(i6);
                    if (jSONObjectOptJSONObject != null && (sVarU = u(jSONObjectOptJSONObject, z6)) != null) {
                        D3.add(sVarU);
                    }
                }
                ?? r02 = yVar;
                if (w4.n.a(D3) == null) {
                    r02 = D3;
                }
                return (List) r02;
            }
        }
        return yVar;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r0v1 */
    /* JADX WARN: Type inference failed for: r0v3 */
    /* JADX WARN: Type inference failed for: r0v4 */
    /* JADX WARN: Type inference failed for: r5v1, types: [w4.m] */
    /* JADX WARN: Type inference failed for: r5v2, types: [java.lang.Object] */
    /* JADX WARN: Type inference failed for: r5v4, types: [java.util.ArrayList] */
    public static final List e(o oVar, String str, boolean z6) {
        ?? D3;
        JSONArray jSONArrayOptJSONArray;
        s sVarV;
        oVar.getClass();
        boolean zQ0 = S4.p.Q0(str);
        y yVar = y.f26065a;
        if (!zQ0) {
            try {
                jSONArrayOptJSONArray = new JSONObject(str).optJSONArray("results");
            } catch (Throwable th) {
                D3 = AbstractC2467b.d(th);
            }
            if (jSONArrayOptJSONArray != null) {
                D3 = new ArrayList(jSONArrayOptJSONArray.length());
                int length = jSONArrayOptJSONArray.length();
                for (int i6 = 0; i6 < length; i6++) {
                    JSONObject jSONObjectOptJSONObject = jSONArrayOptJSONArray.optJSONObject(i6);
                    if (jSONObjectOptJSONObject != null && (sVarV = v(jSONObjectOptJSONObject, z6)) != null) {
                        D3.add(sVarV);
                    }
                }
                ?? r02 = yVar;
                if (w4.n.a(D3) == null) {
                    r02 = D3;
                }
                return (List) r02;
            }
        }
        return yVar;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r10v1, types: [w4.m] */
    /* JADX WARN: Type inference failed for: r10v2, types: [java.lang.Object] */
    /* JADX WARN: Type inference failed for: r10v3, types: [java.util.ArrayList] */
    /* JADX WARN: Type inference failed for: r12v0, types: [java.util.Set] */
    /* JADX WARN: Type inference failed for: r1v1 */
    /* JADX WARN: Type inference failed for: r1v3 */
    /* JADX WARN: Type inference failed for: r1v4 */
    public static final List f(o oVar, String str, String str2, Set set) {
        ?? D3;
        oVar.getClass();
        boolean zQ0 = S4.p.Q0(str);
        y yVar = y.f26065a;
        if (zQ0) {
            return yVar;
        }
        try {
            JSONArray jSONArray = new JSONArray(str);
            D3 = new ArrayList(jSONArray.length());
            int length = jSONArray.length();
            for (int i6 = 0; i6 < length; i6++) {
                JSONObject jSONObjectOptJSONObject = jSONArray.optJSONObject(i6);
                if (jSONObjectOptJSONObject != null) {
                    JSONObject jSONObjectOptJSONObject2 = str2.equals("movie") ? jSONObjectOptJSONObject.optJSONObject("movie") : jSONObjectOptJSONObject.optJSONObject("show");
                    if (jSONObjectOptJSONObject2 != null) {
                        String strOptString = jSONObjectOptJSONObject2.optString("certification", "");
                        kotlin.jvm.internal.o.g(strOptString, "optString(...)");
                        String lowerCase = S4.p.o1(strOptString).toString().toLowerCase(Locale.ROOT);
                        kotlin.jvm.internal.o.g(lowerCase, "toLowerCase(...)");
                        if (lowerCase.length() != 0 && set.contains(lowerCase)) {
                            D3.add(new s(s(jSONObjectOptJSONObject2, str2), jSONObjectOptJSONObject2.optDouble("votes", 0.0d)));
                        }
                    }
                }
            }
        } catch (Throwable th) {
            D3 = AbstractC2467b.d(th);
        }
        ?? r12 = yVar;
        if (w4.n.a(D3) == null) {
            r12 = D3;
        }
        return (List) r12;
    }

    public static String g(int i6, int i7, String str, String str2) {
        StringBuilder sbS = Z0.o.s(i6, "https://api.trakt.tv/users/", str, "/items?extended=full,images&page=", "&limit=");
        sbS.append(i7);
        String string = sbS.toString();
        return (str2 == null || S4.p.Q0(str2) || str2.equalsIgnoreCase("random")) ? string : Z0.o.B(string, "&sort_how=", str2);
    }

    /* JADX WARN: Removed duplicated region for block: B:31:0x0063  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static t h(String str, int i6, int i7, Response response, List list) throws JSONException {
        boolean zB = q.b(response, i7, list.size());
        t tVar = new t(i6, list, zB);
        if (!response.c()) {
            t tVarW0 = W.b.w0(str);
            if (tVarW0 != null) {
                return tVarW0;
            }
        } else if (list.isEmpty() && i6 == 1) {
            t tVarW02 = W.b.w0(str);
            if (tVarW02 != null) {
                return tVarW02;
            }
        } else if (list.isEmpty() && i6 > 1) {
            String strB = Response.b("X-Pagination-Page", response);
            Integer numB0 = strB != null ? w.B0(strB) : null;
            String strB2 = Response.b("X-Pagination-Page-Count", response);
            Integer numB02 = strB2 != null ? w.B0(strB2) : null;
            if (numB0 == null || numB02 == null || numB0.intValue() <= numB02.intValue()) {
                t tVarW03 = W.b.w0(str);
                if (tVarW03 != null) {
                    return tVarW03;
                }
            }
        } else if (i6 != 1 || list.isEmpty()) {
            t tVarW04 = W.b.w0(str);
            if (tVarW04 != null) {
                return tVarW04;
            }
        } else {
            Context context = W.b.f8366i;
            if (context != null) {
                JSONObject jSONObject = new JSONObject();
                String string = LocalDate.now(ZoneId.systemDefault()).toString();
                kotlin.jvm.internal.o.g(string, "toString(...)");
                JSONObject jSONObjectPut = jSONObject.put("day", string).put("page", i6).put("hasNext", zB);
                JSONArray jSONArray = new JSONArray();
                Iterator it = list.iterator();
                while (it.hasNext()) {
                    HomeMetaItem homeMetaItem = (HomeMetaItem) it.next();
                    JSONObject jSONObjectPut2 = new JSONObject().put("id", homeMetaItem.getId()).put("tmdbId", homeMetaItem.getTmdbId()).put("type", homeMetaItem.getType()).put("title", homeMetaItem.getTitle()).put("overview", homeMetaItem.getOverview()).put("year", homeMetaItem.getYear()).put("rating", homeMetaItem.getRating());
                    Object posterUrl = homeMetaItem.getPosterUrl();
                    if (posterUrl == null) {
                        posterUrl = JSONObject.NULL;
                    }
                    JSONObject jSONObjectPut3 = jSONObjectPut2.put("posterUrl", posterUrl);
                    Object backdropUrl = homeMetaItem.getBackdropUrl();
                    if (backdropUrl == null) {
                        backdropUrl = JSONObject.NULL;
                    }
                    JSONObject jSONObjectPut4 = jSONObjectPut3.put("backdropUrl", backdropUrl);
                    Object logoUrl = homeMetaItem.getLogoUrl();
                    if (logoUrl == null) {
                        logoUrl = JSONObject.NULL;
                    }
                    JSONObject jSONObjectPut5 = jSONObjectPut4.put("logoUrl", logoUrl).put("certificate", homeMetaItem.getCertificate()).put("runTimeMinutes", homeMetaItem.getRunTimeMinutes()).put("primaryGenre", homeMetaItem.getPrimaryGenre()).put("secondaryGenre", homeMetaItem.getSecondaryGenre());
                    JSONArray jSONArray2 = new JSONArray();
                    Iterator<T> it2 = homeMetaItem.getOriginCountries().iterator();
                    while (it2.hasNext()) {
                        jSONArray2.put((String) it2.next());
                    }
                    JSONObject jSONObjectPut6 = jSONObjectPut5.put("originCountries", jSONArray2).put("originalLanguage", homeMetaItem.getOriginalLanguage());
                    kotlin.jvm.internal.o.g(jSONObjectPut6, "put(...)");
                    jSONArray.put(jSONObjectPut6);
                }
                context.getSharedPreferences("debridstream_trakt_catalog_list_cache", 0).edit().putString(str, jSONObjectPut.put("items", jSONArray).toString()).apply();
                return tVar;
            }
        }
        return tVar;
    }

    public static String j(JSONArray jSONArray, int i6) {
        if (jSONArray != null && jSONArray.length() > i6) {
            String strOptString = jSONArray.optString(i6, "");
            if (strOptString == null) {
                strOptString = "";
            }
            if (!S4.p.Q0(strOptString)) {
                List listC1 = S4.p.c1(w.x0(strOptString, '-', ' '), new char[]{' '}, 6);
                ArrayList arrayList = new ArrayList();
                for (Object obj : listC1) {
                    if (!S4.p.Q0((String) obj)) {
                        arrayList.add(obj);
                    }
                }
                return AbstractC3251q.m0(arrayList, " ", null, null, new C0736x(23), 30);
            }
        }
        return "";
    }

    public static Object q(o oVar, String str, String str2, int i6, boolean z6, Integer num, Integer num2, C4.c cVar, int i7) {
        Integer num3 = (i7 & 32) != 0 ? null : num;
        Integer num4 = (i7 & 64) != 0 ? null : num2;
        oVar.getClass();
        c5.e eVar = N.f8259a;
        return E.H(c5.d.f13466b, new j(i6, 50, str, oVar, str2, z6, num3, num4, null), cVar);
    }

    public static Object r(o oVar, String str, C0715r2 c0715r2) {
        oVar.getClass();
        c5.e eVar = N.f8259a;
        return E.H(c5.d.f13466b, new k(str, 1, false, 50, false, oVar, null), c0715r2);
    }

    public static HomeMetaItem s(JSONObject jSONObject, String str) {
        String str2;
        String str3;
        String string;
        JSONObject jSONObjectOptJSONObject = jSONObject.optJSONObject("images");
        if (jSONObjectOptJSONObject == null) {
            jSONObjectOptJSONObject = new JSONObject();
        }
        String strT = t(jSONObjectOptJSONObject, "poster");
        String strT2 = t(jSONObjectOptJSONObject, "fanart");
        if (strT2 == null) {
            strT2 = strT;
        }
        if (strT != null) {
            String strA = u.a(strT, false);
            if (!S4.p.Q0(strA)) {
                strT = strA;
            }
            str2 = strT;
        } else {
            str2 = null;
        }
        if (strT2 != null) {
            String strA2 = u.a(strT2, true);
            if (!S4.p.Q0(strA2)) {
                strT2 = strA2;
            }
            str3 = strT2;
        } else {
            str3 = null;
        }
        String strT3 = t(jSONObjectOptJSONObject, "logo");
        if (strT3 == null) {
            strT3 = t(jSONObjectOptJSONObject, "clearlogo");
        }
        String str4 = strT3;
        JSONObject jSONObjectOptJSONObject2 = jSONObject.optJSONObject("ids");
        if (jSONObjectOptJSONObject2 == null) {
            jSONObjectOptJSONObject2 = new JSONObject();
        }
        int iOptInt = jSONObjectOptJSONObject2.optInt("tmdb");
        Integer numValueOf = Integer.valueOf(iOptInt);
        if (iOptInt == 0) {
            numValueOf = null;
        }
        if (numValueOf == null || (string = numValueOf.toString()) == null) {
            int iOptInt2 = jSONObjectOptJSONObject2.optInt("trakt");
            Integer numValueOf2 = Integer.valueOf(iOptInt2);
            if (iOptInt2 == 0) {
                numValueOf2 = null;
            }
            string = numValueOf2 != null ? numValueOf2.toString() : jSONObject.optString("title");
        }
        String str5 = string;
        int iOptInt3 = jSONObject.optInt("year", 0);
        String strValueOf = iOptInt3 > 0 ? String.valueOf(iOptInt3) : "";
        JSONArray jSONArrayOptJSONArray = jSONObject.optJSONArray("genres");
        String strJ = j(jSONArrayOptJSONArray, 0);
        String strJ2 = j(jSONArrayOptJSONArray, 1);
        String upperCase = A0.a.p(jSONObject, "country", "", "optString(...)").toUpperCase(Locale.ROOT);
        kotlin.jvm.internal.o.g(upperCase, "toUpperCase(...)");
        String str6 = upperCase.length() == 2 ? upperCase : null;
        List listQ = str6 != null ? AbstractC2612c.q(str6) : y.f26065a;
        kotlin.jvm.internal.o.e(str5);
        String strOptString = jSONObject.optString("title");
        kotlin.jvm.internal.o.g(strOptString, "optString(...)");
        String strOptString2 = jSONObject.optString("overview");
        kotlin.jvm.internal.o.g(strOptString2, "optString(...)");
        double dOptDouble = jSONObject.optDouble("rating", 0.0d);
        String strOptString3 = jSONObject.optString("certification", "");
        kotlin.jvm.internal.o.g(strOptString3, "optString(...)");
        String raw = S4.p.o1(strOptString3).toString();
        kotlin.jvm.internal.o.h(raw, "raw");
        return new HomeMetaItem(str5, iOptInt, str, strOptString, strOptString2, strValueOf, dOptDouble, str2, str3, str4, W0.e.i(raw), jSONObject.optInt("runtime", 0), strJ, strJ2, listQ, "");
    }

    public static final String t(JSONObject jSONObject, String str) {
        String strOptString;
        JSONArray jSONArrayOptJSONArray = jSONObject.optJSONArray(str);
        if (jSONArrayOptJSONArray == null || jSONArrayOptJSONArray.length() == 0 || (strOptString = jSONArrayOptJSONArray.optString(0)) == null || strOptString.length() == 0) {
            return null;
        }
        return !w.A0(strOptString, "http", false) ? "https://".concat(strOptString) : strOptString;
    }

    public static s u(JSONObject jSONObject, boolean z6) {
        int iOptInt;
        String strOptString = jSONObject.optString("original_language", "");
        String str = null;
        if ((z6 && (kotlin.jvm.internal.o.c(strOptString, "ja") || kotlin.jvm.internal.o.c(strOptString, "ko"))) || (iOptInt = jSONObject.optInt("id", 0)) == 0) {
            return null;
        }
        String strOptString2 = jSONObject.optString("poster_path", "");
        kotlin.jvm.internal.o.e(strOptString2);
        if (strOptString2.length() <= 0) {
            strOptString2 = null;
        }
        String strConcat = strOptString2 != null ? "https://image.tmdb.org/t/p/w500".concat(strOptString2) : null;
        String strOptString3 = jSONObject.optString("backdrop_path", "");
        kotlin.jvm.internal.o.e(strOptString3);
        if (strOptString3.length() <= 0) {
            strOptString3 = null;
        }
        String strConcat2 = strOptString3 != null ? "https://image.tmdb.org/t/p/w780".concat(strOptString3) : null;
        String strOptString4 = jSONObject.optString("release_date", "");
        kotlin.jvm.internal.o.e(strOptString4);
        String strM1 = S4.p.m1(4, strOptString4);
        if (strM1.length() == 4 && Character.isDigit(strM1.charAt(0))) {
            str = strM1;
        }
        String str2 = str == null ? "" : str;
        String strValueOf = String.valueOf(iOptInt);
        String strOptString5 = jSONObject.optString("title");
        kotlin.jvm.internal.o.g(strOptString5, "optString(...)");
        String strOptString6 = jSONObject.optString("overview");
        kotlin.jvm.internal.o.g(strOptString6, "optString(...)");
        return new s(new HomeMetaItem(strValueOf, iOptInt, "movie", strOptString5, strOptString6, str2, jSONObject.optDouble("vote_average", 0.0d), strConcat, strConcat2 == null ? strConcat : strConcat2, null, W0.e.i(""), 0, "", null, W.a.z0(jSONObject), W.a.A0(jSONObject), 8192, null), jSONObject.optDouble("popularity", 0.0d));
    }

    public static s v(JSONObject jSONObject, boolean z6) {
        int iOptInt;
        String strOptString = jSONObject.optString("original_language", "");
        String str = null;
        if ((z6 && (kotlin.jvm.internal.o.c(strOptString, "ja") || kotlin.jvm.internal.o.c(strOptString, "ko"))) || (iOptInt = jSONObject.optInt("id", 0)) == 0) {
            return null;
        }
        String strOptString2 = jSONObject.optString("poster_path", "");
        kotlin.jvm.internal.o.e(strOptString2);
        if (strOptString2.length() <= 0) {
            strOptString2 = null;
        }
        String strConcat = strOptString2 != null ? "https://image.tmdb.org/t/p/w500".concat(strOptString2) : null;
        String strOptString3 = jSONObject.optString("backdrop_path", "");
        kotlin.jvm.internal.o.e(strOptString3);
        if (strOptString3.length() <= 0) {
            strOptString3 = null;
        }
        String strConcat2 = strOptString3 != null ? "https://image.tmdb.org/t/p/w780".concat(strOptString3) : null;
        String strOptString4 = jSONObject.optString("first_air_date", "");
        kotlin.jvm.internal.o.e(strOptString4);
        String strM1 = S4.p.m1(4, strOptString4);
        if (strM1.length() == 4 && Character.isDigit(strM1.charAt(0))) {
            str = strM1;
        }
        String str2 = str == null ? "" : str;
        String strValueOf = String.valueOf(iOptInt);
        String strOptString5 = jSONObject.optString(DiagnosticsEntry.NAME_KEY);
        kotlin.jvm.internal.o.g(strOptString5, "optString(...)");
        String strOptString6 = jSONObject.optString("overview");
        kotlin.jvm.internal.o.g(strOptString6, "optString(...)");
        return new s(new HomeMetaItem(strValueOf, iOptInt, "tv", strOptString5, strOptString6, str2, jSONObject.optDouble("vote_average", 0.0d), strConcat, strConcat2 == null ? strConcat : strConcat2, null, W0.e.i(""), 0, "", null, W.a.z0(jSONObject), W.a.A0(jSONObject), 8192, null), jSONObject.optDouble("popularity", 0.0d));
    }

    /* JADX WARN: Removed duplicated region for block: B:34:0x009b A[Catch: all -> 0x0045, TryCatch #1 {all -> 0x0045, blocks: (B:7:0x0039, B:9:0x003f, B:12:0x0048, B:14:0x004c, B:20:0x005e, B:22:0x0064, B:24:0x0076, B:34:0x009b, B:35:0x009e, B:32:0x0095, B:36:0x00a1, B:37:0x00a6, B:19:0x005a, B:16:0x0053), top: B:49:0x0039, outer: #0, inners: #2 }] */
    /* JADX WARN: Removed duplicated region for block: B:56:0x009e A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final t i(int i6, int i7, String str, String str2) throws EOFException {
        Object objD;
        t tVarH;
        HomeMetaItem homeMetaItemS;
        y yVar = y.f26065a;
        Request.Builder builder = new Request.Builder();
        builder.i(str);
        builder.a("Content-Type", "application/json");
        String str3 = W.d.f8386g;
        if (str3 == null) {
            str3 = "DebridStream/3.0.0";
        }
        builder.a("User-Agent", str3);
        builder.a("trakt-api-key", "cb6c23120401d06808b06c16c78d0e54e1953df0fce43d921280c708619c1b34");
        builder.a("trakt-api-version", "2");
        try {
            Response responseD = this.f8779a.a(builder.b()).d();
            try {
                if (responseD.c()) {
                    ResponseBody responseBody = responseD.f22146j;
                    try {
                        objD = new JSONArray(responseBody != null ? responseBody.d() : "");
                    } catch (Throwable th) {
                        objD = AbstractC2467b.d(th);
                    }
                    if (w4.n.a(objD) == null) {
                        JSONArray jSONArray = (JSONArray) objD;
                        ArrayList arrayList = new ArrayList(jSONArray.length());
                        int length = jSONArray.length();
                        for (int i8 = 0; i8 < length; i8++) {
                            JSONObject jSONObject = jSONArray.getJSONObject(i8);
                            kotlin.jvm.internal.o.e(jSONObject);
                            String str4 = "movie";
                            JSONObject jSONObjectOptJSONObject = jSONObject.optJSONObject("movie");
                            JSONObject jSONObjectOptJSONObject2 = jSONObject.optJSONObject("show");
                            if (jSONObjectOptJSONObject == null) {
                                if (jSONObjectOptJSONObject2 == null) {
                                    homeMetaItemS = null;
                                }
                                if (homeMetaItemS == null) {
                                    arrayList.add(homeMetaItemS);
                                }
                            } else {
                                jSONObjectOptJSONObject2 = jSONObjectOptJSONObject;
                            }
                            if (jSONObjectOptJSONObject == null) {
                                str4 = "tv";
                            }
                            homeMetaItemS = s(jSONObjectOptJSONObject2, str4);
                            if (homeMetaItemS == null) {
                            }
                        }
                        tVarH = h(str2, i6, i7, responseD, arrayList);
                    } else {
                        tVarH = h(str2, i6, i7, responseD, yVar);
                    }
                } else {
                    tVarH = h(str2, i6, i7, responseD, yVar);
                }
                responseD.close();
                return tVarH;
            } finally {
            }
        } catch (Throwable unused) {
            t tVarW0 = W.b.w0(str2);
            return tVarW0 == null ? new t(i6, yVar, false) : tVarW0;
        }
    }

    public final Object k(String str, int i6, boolean z6, C4.c cVar) {
        c5.e eVar = N.f8259a;
        return E.H(c5.d.f13466b, new d(str, z6, i6, this, null), cVar);
    }

    public final Object m(String str, int i6, boolean z6, C4.c cVar) {
        c5.e eVar = N.f8259a;
        return E.H(c5.d.f13466b, new h(str, z6, i6, this, null), cVar);
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0016  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object o(String str, String str2, int i6, int i7, boolean z6, Integer num, Integer num2, C4.c cVar) {
        i iVar;
        o oVar;
        String str3;
        if (cVar instanceof i) {
            iVar = (i) cVar;
            int i8 = iVar.f8749e;
            if ((i8 & Integer.MIN_VALUE) != 0) {
                iVar.f8749e = i8 - Integer.MIN_VALUE;
            } else {
                iVar = new i(this, cVar);
            }
        }
        Object objH = iVar.f8747c;
        B4.a aVar = B4.a.f492a;
        int i9 = iVar.f8749e;
        if (i9 == 0) {
            AbstractC2467b.l(objH);
            iVar.f8745a = this;
            iVar.f8746b = str2;
            iVar.f8749e = 1;
            c5.e eVar = N.f8259a;
            objH = E.H(c5.d.f13466b, new j(i6, i7, str, this, str2, z6, num, num2, null), iVar);
            if (objH == aVar) {
                return aVar;
            }
            oVar = this;
            str3 = str2;
        } else {
            if (i9 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            str3 = iVar.f8746b;
            oVar = iVar.f8745a;
            AbstractC2467b.l(objH);
        }
        List list = ((t) objH).f8789a;
        if (!w.t0(str3, "random", true) || list.size() < 2) {
            return list;
        }
        ArrayList arrayList = new ArrayList(list);
        oVar.getClass();
        if (arrayList.size() >= 2) {
            int iElapsedRealtimeNanos = (int) (SystemClock.elapsedRealtimeNanos() ^ (((long) arrayList.size()) * 2654435769L));
            if (iElapsedRealtimeNanos == 0) {
                iElapsedRealtimeNanos = 1831565813;
            }
            for (int iA = AbstractC3252r.A(arrayList); iA > 0; iA--) {
                int i10 = iElapsedRealtimeNanos ^ (iElapsedRealtimeNanos << 13);
                int i11 = i10 ^ (i10 >>> 17);
                iElapsedRealtimeNanos = i11 ^ (i11 << 5);
                int i12 = (Integer.MAX_VALUE & iElapsedRealtimeNanos) % (iA + 1);
                if (iA != i12) {
                    Object obj = arrayList.get(iA);
                    arrayList.set(iA, arrayList.get(i12));
                    arrayList.set(i12, obj);
                }
            }
        }
        return arrayList;
    }
}
