package i3;

import android.content.Context;
import com.google.android.gms.auth.api.proxy.AuthApiStatusCodes;
import com.google.firebase.sessions.settings.RemoteSettings;
import com.revenuecat.purchases.subscriberattributes.SubscriberAttributeKt;
import d3.L1;
import e3.AbstractC1964a;
import j$.time.Instant;
import j$.time.LocalDate;
import j$.time.ZoneOffset;
import j$.time.format.DateTimeFormatter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import m3.AbstractC2467b;
import okhttp3.Headers;
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.JSONException;
import org.json.JSONObject;
import w4.C3137A;
import x4.AbstractC3251q;

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final AtomicLong f18885e = new AtomicLong(0);

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final AtomicLong f18886f = new AtomicLong(0);

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final ConcurrentHashMap f18887g = new ConcurrentHashMap();

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

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public volatile EnumC2273g f18891c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public volatile AtomicInteger f18892d;

    static {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        TimeUnit timeUnit = TimeUnit.SECONDS;
        builder.b(30L);
        AbstractC1964a.b(builder);
        f18888h = new OkHttpClient(builder);
    }

    public J(Context appContext) {
        kotlin.jvm.internal.o.h(appContext, "appContext");
        Context applicationContext = appContext.getApplicationContext();
        kotlin.jvm.internal.o.g(applicationContext, "getApplicationContext(...)");
        this.f18889a = new K(applicationContext);
        this.f18890b = e5.d.a();
        this.f18891c = EnumC2273g.f18931a;
    }

    public static final Response C(J j4, J4.c cVar, kotlin.jvm.internal.C c6) {
        AtomicInteger atomicInteger = j4.f18892d;
        if (atomicInteger != null) {
            atomicInteger.incrementAndGet();
        }
        int iOrdinal = j4.f18891c.ordinal();
        if (iOrdinal != 0) {
            if (iOrdinal == 1) {
                AbstractC2271e.f18922a.incrementAndGet();
            } else if (iOrdinal == 2) {
                AbstractC2271e.f18923b.incrementAndGet();
            } else if (iOrdinal == 3) {
                AbstractC2271e.f18924c.incrementAndGet();
            } else {
                if (iOrdinal != 4) {
                    throw new E3.v();
                }
                AbstractC2271e.f18925d.incrementAndGet();
            }
        }
        return f18888h.a((Request) cVar.invoke(c6.f20115a)).d();
    }

    public static /* synthetic */ Object F(J j4, String str, int i6, C4.j jVar, int i7) {
        if ((i7 & 4) != 0) {
            i6 = Integer.MAX_VALUE;
        }
        return j4.E(str, 100, i6, jVar);
    }

    public static String L(JSONObject jSONObject) {
        String strOptString = jSONObject.optString("certification", "");
        if (kotlin.jvm.internal.o.c(strOptString, "null")) {
            return "";
        }
        kotlin.jvm.internal.o.e(strOptString);
        return strOptString;
    }

    public static Instant M(String str) {
        if (str == null) {
            return null;
        }
        try {
            if (S4.p.Q0(str)) {
                return null;
            }
            return Instant.parse(str);
        } catch (Exception unused) {
            return null;
        }
    }

    public static Instant N(JSONObject jSONObject) {
        Instant instantM = M(jSONObject.optString("paused_at"));
        Instant instantM2 = M(jSONObject.optString("updated_at"));
        if (instantM == null || instantM2 == null) {
            if (instantM == null) {
                if (instantM2 == null) {
                    return null;
                }
                return instantM2;
            }
        } else if (instantM2.isAfter(instantM)) {
            return instantM2;
        }
        return instantM;
    }

    public static Object O(boolean z6, C4.c cVar) {
        long j4;
        long jCurrentTimeMillis;
        long jMax;
        Object objL;
        long j6 = z6 ? 2000L : 350L;
        AtomicLong atomicLong = z6 ? f18886f : f18885e;
        do {
            j4 = atomicLong.get();
            jCurrentTimeMillis = System.currentTimeMillis();
            jMax = Math.max(jCurrentTimeMillis, j4 + j6);
        } while (!atomicLong.compareAndSet(j4, jMax));
        long j7 = jMax - jCurrentTimeMillis;
        return (j7 <= 0 || (objL = V4.E.l(j7, cVar)) != B4.a.f492a) ? C3137A.f25453a : objL;
    }

    public static final boolean a(J j4, JSONObject jSONObject, Instant instant, String str, String[] strArr) {
        j4.getClass();
        JSONObject jSONObjectOptJSONObject = jSONObject.optJSONObject(str);
        if (jSONObjectOptJSONObject == null) {
            return true;
        }
        for (String str2 : strArr) {
            Instant instantM = M(jSONObjectOptJSONObject.optString(str2));
            if (instantM != null && instantM.isAfter(instant)) {
                return true;
            }
        }
        return false;
    }

    public static final AtomicInteger b(J j4, EnumC2273g enumC2273g) {
        j4.getClass();
        AtomicInteger atomicInteger = new AtomicInteger(0);
        j4.f18891c = enumC2273g;
        j4.f18892d = atomicInteger;
        return atomicInteger;
    }

    public static final JSONObject c(J j4, JSONObject jSONObject, JSONObject jSONObject2) throws JSONException {
        JSONArray jSONArrayOptJSONArray;
        JSONArray jSONArrayOptJSONArray2;
        JSONArray jSONArrayOptJSONArray3;
        j4.getClass();
        JSONObject jSONObjectOptJSONObject = jSONObject.optJSONObject("ids");
        if (jSONObjectOptJSONObject == null) {
            jSONObjectOptJSONObject = new JSONObject();
        }
        int iOptInt = jSONObjectOptJSONObject.optInt("trakt");
        JSONObject jSONObjectOptJSONObject2 = jSONObject.optJSONObject("images");
        String strOptString = null;
        String strZ = z((jSONObjectOptJSONObject2 == null || (jSONArrayOptJSONArray3 = jSONObjectOptJSONObject2.optJSONArray("poster")) == null) ? null : jSONArrayOptJSONArray3.optString(0));
        String strZ2 = z((jSONObjectOptJSONObject2 == null || (jSONArrayOptJSONArray2 = jSONObjectOptJSONObject2.optJSONArray("fanart")) == null) ? null : jSONArrayOptJSONArray2.optString(0));
        if (jSONObjectOptJSONObject2 != null && (jSONArrayOptJSONArray = jSONObjectOptJSONObject2.optJSONArray("logo")) != null) {
            strOptString = jSONArrayOptJSONArray.optString(0);
        }
        String strZ3 = z(strOptString);
        String str = strZ2 == null ? strZ : strZ2;
        String strA = g3.u.a(strZ == null ? strZ2 : strZ, false);
        if (S4.p.Q0(strA)) {
            strA = strZ == null ? strZ2 == null ? "" : strZ2 : strZ;
        }
        String strA2 = g3.u.a(str, true);
        if (S4.p.Q0(strA2)) {
            if (strZ2 != null) {
                strZ = strZ2;
            } else if (strZ == null) {
                strZ = "";
            }
            strA2 = strZ;
        }
        JSONObject jSONObjectPut = new JSONObject().put(SubscriberAttributeKt.JSON_NAME_KEY, "movie:" + iOptInt).put("tmdbId", jSONObjectOptJSONObject.optInt("tmdb")).put("traktId", iOptInt).put("imdbId", jSONObjectOptJSONObject.optString("imdb")).put("type", "movie").put("title", jSONObject.optString("title")).put("overview", jSONObject.optString("overview")).put("year", String.valueOf(jSONObject.optInt("year"))).put("rating", jSONObject.optDouble("rating")).put("popularity", jSONObject.optDouble("votes")).put("certificate", L(jSONObject)).put("runTime", jSONObject.optInt("runtime")).put("progress", (int) jSONObject2.optDouble("progress")).put("posterUrl", strA).put("backdropUrl", strA2).put("logo", strZ3);
        JSONArray jSONArrayOptJSONArray4 = jSONObject.optJSONArray("genres");
        if (jSONArrayOptJSONArray4 == null) {
            jSONArrayOptJSONArray4 = new JSONArray();
        }
        JSONObject jSONObjectPut2 = jSONObjectPut.put("genres", jSONArrayOptJSONArray4);
        Instant instantN = N(jSONObject2);
        if (instantN == null && (instantN = M(jSONObject2.optString("paused_at"))) == null) {
            instantN = Instant.EPOCH;
        }
        String string = instantN.toString();
        kotlin.jvm.internal.o.g(string, "toString(...)");
        JSONObject jSONObjectPut3 = jSONObjectPut2.put("sortDate", string);
        kotlin.jvm.internal.o.g(jSONObjectPut3, "put(...)");
        return jSONObjectPut3;
    }

    public static final JSONObject d(J j4, JSONObject jSONObject, JSONObject jSONObject2, JSONObject jSONObject3) throws JSONException {
        JSONArray jSONArrayOptJSONArray;
        JSONArray jSONArrayOptJSONArray2;
        JSONArray jSONArrayOptJSONArray3;
        j4.getClass();
        JSONObject jSONObjectOptJSONObject = jSONObject.optJSONObject("ids");
        if (jSONObjectOptJSONObject == null) {
            jSONObjectOptJSONObject = new JSONObject();
        }
        int iOptInt = jSONObjectOptJSONObject.optInt("trakt");
        JSONObject jSONObjectOptJSONObject2 = jSONObject.optJSONObject("images");
        String strOptString = null;
        String strZ = z((jSONObjectOptJSONObject2 == null || (jSONArrayOptJSONArray3 = jSONObjectOptJSONObject2.optJSONArray("poster")) == null) ? null : jSONArrayOptJSONArray3.optString(0));
        String strZ2 = z((jSONObjectOptJSONObject2 == null || (jSONArrayOptJSONArray2 = jSONObjectOptJSONObject2.optJSONArray("fanart")) == null) ? null : jSONArrayOptJSONArray2.optString(0));
        if (jSONObjectOptJSONObject2 != null && (jSONArrayOptJSONArray = jSONObjectOptJSONObject2.optJSONArray("logo")) != null) {
            strOptString = jSONArrayOptJSONArray.optString(0);
        }
        String strZ3 = z(strOptString);
        String strA = g3.u.a(strZ, false);
        if (S4.p.Q0(strA)) {
            if (strZ == null) {
                strZ = "";
            }
            strA = strZ;
        }
        String strA2 = g3.u.a(strZ2, true);
        if (S4.p.Q0(strA2)) {
            if (strZ2 == null) {
                strZ2 = "";
            }
            strA2 = strZ2;
        }
        JSONObject jSONObjectPut = new JSONObject().put(SubscriberAttributeKt.JSON_NAME_KEY, "tv:" + iOptInt).put("tmdbId", jSONObjectOptJSONObject.optInt("tmdb")).put("traktId", iOptInt).put("imdbId", jSONObjectOptJSONObject.optString("imdb")).put("type", "tv").put("title", jSONObject.optString("title")).put("overview", jSONObject.optString("overview")).put("year", String.valueOf(jSONObject.optInt("year"))).put("rating", jSONObject.optDouble("rating")).put("popularity", jSONObject.optDouble("votes")).put("certificate", L(jSONObject)).put("runTime", jSONObject.optInt("runtime")).put("progress", (int) jSONObject3.optDouble("progress")).put("season", jSONObject2.optInt("season")).put("episode", jSONObject2.optInt("number")).put("posterUrl", strA).put("backdropUrl", strA2).put("logo", strZ3);
        JSONArray jSONArrayOptJSONArray4 = jSONObject.optJSONArray("genres");
        if (jSONArrayOptJSONArray4 == null) {
            jSONArrayOptJSONArray4 = new JSONArray();
        }
        JSONObject jSONObjectPut2 = jSONObjectPut.put("genres", jSONArrayOptJSONArray4);
        Instant instantN = N(jSONObject3);
        if (instantN == null && (instantN = M(jSONObject3.optString("paused_at"))) == null) {
            instantN = Instant.EPOCH;
        }
        String string = instantN.toString();
        kotlin.jvm.internal.o.g(string, "toString(...)");
        JSONObject jSONObjectPut3 = jSONObjectPut2.put("sortDate", string);
        kotlin.jvm.internal.o.g(jSONObjectPut3, "put(...)");
        return jSONObjectPut3;
    }

    public static final JSONObject e(J j4, JSONObject jSONObject, JSONObject jSONObject2, Instant instant) throws JSONException {
        JSONArray jSONArrayOptJSONArray;
        JSONArray jSONArrayOptJSONArray2;
        JSONArray jSONArrayOptJSONArray3;
        j4.getClass();
        JSONObject jSONObjectOptJSONObject = jSONObject.optJSONObject("ids");
        if (jSONObjectOptJSONObject == null) {
            jSONObjectOptJSONObject = new JSONObject();
        }
        int iOptInt = jSONObjectOptJSONObject.optInt("trakt");
        JSONObject jSONObjectOptJSONObject2 = jSONObject.optJSONObject("images");
        String strOptString = null;
        String strZ = z((jSONObjectOptJSONObject2 == null || (jSONArrayOptJSONArray3 = jSONObjectOptJSONObject2.optJSONArray("poster")) == null) ? null : jSONArrayOptJSONArray3.optString(0));
        String strZ2 = z((jSONObjectOptJSONObject2 == null || (jSONArrayOptJSONArray2 = jSONObjectOptJSONObject2.optJSONArray("fanart")) == null) ? null : jSONArrayOptJSONArray2.optString(0));
        if (jSONObjectOptJSONObject2 != null && (jSONArrayOptJSONArray = jSONObjectOptJSONObject2.optJSONArray("logo")) != null) {
            strOptString = jSONArrayOptJSONArray.optString(0);
        }
        String strZ3 = z(strOptString);
        String strA = g3.u.a(strZ, false);
        if (S4.p.Q0(strA)) {
            if (strZ == null) {
                strZ = "";
            }
            strA = strZ;
        }
        String strA2 = g3.u.a(strZ2, true);
        if (S4.p.Q0(strA2)) {
            if (strZ2 == null) {
                strZ2 = "";
            }
            strA2 = strZ2;
        }
        JSONObject jSONObjectPut = new JSONObject().put(SubscriberAttributeKt.JSON_NAME_KEY, "tv:" + iOptInt).put("tmdbId", jSONObjectOptJSONObject.optInt("tmdb")).put("traktId", iOptInt).put("imdbId", jSONObjectOptJSONObject.optString("imdb")).put("type", "tv").put("title", jSONObject.optString("title")).put("overview", jSONObject.optString("overview")).put("year", String.valueOf(jSONObject.optInt("year"))).put("rating", jSONObject.optDouble("rating")).put("popularity", jSONObject.optDouble("votes")).put("certificate", L(jSONObject)).put("runTime", jSONObject.optInt("runtime")).put("progress", 0).put("season", jSONObject2.optInt("season")).put("episode", jSONObject2.optInt("number")).put("posterUrl", strA).put("backdropUrl", strA2).put("logo", strZ3);
        JSONArray jSONArrayOptJSONArray4 = jSONObject.optJSONArray("genres");
        if (jSONArrayOptJSONArray4 == null) {
            jSONArrayOptJSONArray4 = new JSONArray();
        }
        JSONObject jSONObjectPut2 = jSONObjectPut.put("genres", jSONArrayOptJSONArray4).put("sortDate", instant.toString());
        kotlin.jvm.internal.o.g(jSONObjectPut2, "put(...)");
        return jSONObjectPut2;
    }

    public static final JSONObject f(J j4, int i6, String str, Integer num, Integer num2) throws JSONException {
        if (i6 <= 0) {
            return null;
        }
        String lowerCase = S4.p.o1(str).toString().toLowerCase(Locale.ROOT);
        kotlin.jvm.internal.o.g(lowerCase, "toLowerCase(...)");
        if (S4.p.Q0(lowerCase)) {
            lowerCase = "movie";
        }
        JSONObject jSONObject = new JSONObject();
        if (i6 > 0) {
            jSONObject.put("tmdb", i6);
        }
        if (!lowerCase.equals("tv") && !lowerCase.equals("show")) {
            return new JSONObject().put("movies", new JSONArray().put(new JSONObject().put("ids", jSONObject)));
        }
        JSONObject jSONObjectPut = new JSONObject().put("ids", jSONObject);
        if (num != null && num.intValue() > 0 && num2 != null && num2.intValue() > 0) {
            jSONObjectPut.put("seasons", new JSONArray().put(new JSONObject().put("number", num.intValue()).put("episodes", new JSONArray().put(new JSONObject().put("number", num2.intValue())))));
        } else if (num != null && num.intValue() > 0) {
            jSONObjectPut.put("seasons", new JSONArray().put(new JSONObject().put("number", num.intValue())));
        }
        return new JSONObject().put("shows", new JSONArray().put(jSONObjectPut));
    }

    public static final JSONArray g(J j4, JSONArray jSONArray, boolean z6) {
        String strM;
        j4.getClass();
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        int length = jSONArray.length();
        for (int i6 = 0; i6 < length; i6++) {
            JSONObject jSONObjectOptJSONObject = jSONArray.optJSONObject(i6);
            if (jSONObjectOptJSONObject != null) {
                if (z6) {
                    int iOptInt = jSONObjectOptJSONObject.optInt("tmdbId", 0);
                    strM = iOptInt > 0 ? Z0.o.l("m-", iOptInt) : A0.a.m("hid-", jSONObjectOptJSONObject.optLong("historyId", 0L));
                } else {
                    int iOptInt2 = jSONObjectOptJSONObject.optInt("tmdbId", 0);
                    int iOptInt3 = jSONObjectOptJSONObject.optInt("season", 0);
                    int iOptInt4 = jSONObjectOptJSONObject.optInt("episode", 0);
                    if (iOptInt2 <= 0 || iOptInt3 <= 0 || iOptInt4 <= 0) {
                        strM = A0.a.m("hid-", jSONObjectOptJSONObject.optLong("historyId", 0L));
                    } else {
                        StringBuilder sbQ = com.google.android.recaptcha.internal.a.q(iOptInt2, iOptInt3, "t-", "-", "-");
                        sbQ.append(iOptInt4);
                        strM = sbQ.toString();
                    }
                }
                if (!kotlin.jvm.internal.o.c(strM, "hid-0")) {
                    JSONObject jSONObject = (JSONObject) linkedHashMap.get(strM);
                    if (jSONObject == null) {
                        linkedHashMap.put(strM, jSONObjectOptJSONObject);
                    } else {
                        Instant instantM = M(jSONObject.optString("watched_at"));
                        if (instantM == null) {
                            instantM = Instant.EPOCH;
                        }
                        Instant instantM2 = M(jSONObjectOptJSONObject.optString("watched_at"));
                        if (instantM2 == null) {
                            instantM2 = Instant.EPOCH;
                        }
                        if (instantM2.isAfter(instantM)) {
                            linkedHashMap.put(strM, jSONObjectOptJSONObject);
                        }
                    }
                }
            }
        }
        JSONArray jSONArray2 = new JSONArray();
        Collection collectionValues = linkedHashMap.values();
        kotlin.jvm.internal.o.g(collectionValues, "<get-values>(...)");
        Iterator it = R4.n.c0(new R4.j(1, AbstractC3251q.W(collectionValues), new C2279m(j4, 0)), AuthApiStatusCodes.AUTH_API_INVALID_CREDENTIALS).iterator();
        while (it.hasNext()) {
            jSONArray2.put((JSONObject) it.next());
        }
        return jSONArray2;
    }

    public static final void h(J j4, EnumC2273g enumC2273g, AtomicInteger atomicInteger) {
        j4.getClass();
        atomicInteger.get();
        int iOrdinal = enumC2273g.ordinal();
        if (iOrdinal != 0) {
            if (iOrdinal == 1) {
                AbstractC2271e.f18922a.get();
            } else if (iOrdinal == 2) {
                AbstractC2271e.f18923b.get();
            } else if (iOrdinal == 3) {
                AbstractC2271e.f18924c.get();
            } else {
                if (iOrdinal != 4) {
                    throw new E3.v();
                }
                AbstractC2271e.f18925d.get();
            }
        }
        j4.f18891c = EnumC2273g.f18931a;
        j4.f18892d = null;
    }

    public static final void i(J j4, JSONArray jSONArray, Set set, Map map) throws JSONException {
        Instant instant;
        j4.getClass();
        int length = jSONArray.length();
        for (int i6 = 0; i6 < length; i6++) {
            JSONObject jSONObject = jSONArray.getJSONObject(i6);
            JSONObject jSONObjectOptJSONObject = jSONObject.optJSONObject("show");
            if (jSONObjectOptJSONObject != null) {
                JSONObject jSONObjectOptJSONObject2 = jSONObjectOptJSONObject.optJSONObject("ids");
                int iOptInt = jSONObjectOptJSONObject2 != null ? jSONObjectOptJSONObject2.optInt("trakt", 0) : 0;
                if (iOptInt != 0) {
                    set.add(Integer.valueOf(iOptInt));
                    Instant instantM = M(jSONObject.optString("first_aired"));
                    if (instantM != null && ((instant = (Instant) map.get(Integer.valueOf(iOptInt))) == null || instantM.isAfter(instant))) {
                        map.put(Integer.valueOf(iOptInt), instantM);
                    }
                }
            }
        }
    }

    public static final Object j(J j4, w wVar) {
        return j4.K("https://api.trakt.tv/sync/last_activities", wVar);
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0016  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final Object k(J j4, int i6, C4.c cVar) {
        p pVar;
        Object objD;
        j4.getClass();
        if (cVar instanceof p) {
            pVar = (p) cVar;
            int i7 = pVar.f18981c;
            if ((i7 & Integer.MIN_VALUE) != 0) {
                pVar.f18981c = i7 - Integer.MIN_VALUE;
            } else {
                pVar = new p(j4, cVar);
            }
        }
        Object objJ = pVar.f18979a;
        Object obj = B4.a.f492a;
        int i8 = pVar.f18981c;
        try {
            if (i8 == 0) {
                AbstractC2467b.l(objJ);
                String str = LocalDate.now(ZoneOffset.UTC).minusDays(i6).format(DateTimeFormatter.ISO_DATE);
                int i9 = i6 + 1;
                if (i9 > 31) {
                    i9 = 31;
                }
                String str2 = "https://api.trakt.tv/calendars/my/shows/" + str + RemoteSettings.FORWARD_SLASH_STRING + i9;
                pVar.f18981c = 1;
                objJ = j4.J(str2, pVar);
                if (objJ == obj) {
                    return obj;
                }
            } else {
                if (i8 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                AbstractC2467b.l(objJ);
            }
            objD = (JSONArray) objJ;
        } catch (Throwable th) {
            objD = AbstractC2467b.d(th);
        }
        return w4.n.a(objD) == null ? objD : new JSONArray();
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0016  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final Object l(J j4, Instant instant, C4.c cVar) {
        q qVar;
        Object objD;
        j4.getClass();
        if (cVar instanceof q) {
            qVar = (q) cVar;
            int i6 = qVar.f18984c;
            if ((i6 & Integer.MIN_VALUE) != 0) {
                qVar.f18984c = i6 - Integer.MIN_VALUE;
            } else {
                qVar = new q(j4, cVar);
            }
        }
        Object objJ = qVar.f18982a;
        Object obj = B4.a.f492a;
        int i7 = qVar.f18984c;
        try {
            if (i7 == 0) {
                AbstractC2467b.l(objJ);
                ZoneOffset zoneOffset = ZoneOffset.UTC;
                LocalDate localDateMinusDays = LocalDate.ofInstant(instant, zoneOffset).minusDays(1L);
                int iV = L4.a.v((int) ((LocalDate.now(zoneOffset).toEpochDay() - localDateMinusDays.toEpochDay()) + 1), 1, 14);
                String str = "https://api.trakt.tv/calendars/my/shows/" + localDateMinusDays.format(DateTimeFormatter.ISO_DATE) + RemoteSettings.FORWARD_SLASH_STRING + iV;
                qVar.f18984c = 1;
                objJ = j4.J(str, qVar);
                if (objJ == obj) {
                    return obj;
                }
            } else {
                if (i7 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                AbstractC2467b.l(objJ);
            }
            objD = (JSONArray) objJ;
        } catch (Throwable th) {
            objD = AbstractC2467b.d(th);
        }
        return w4.n.a(objD) == null ? objD : new JSONArray();
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0018  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final Object m(J j4, int i6, C4.c cVar) {
        s sVar;
        Object objD;
        j4.getClass();
        if (cVar instanceof s) {
            sVar = (s) cVar;
            int i7 = sVar.f18997c;
            if ((i7 & Integer.MIN_VALUE) != 0) {
                sVar.f18997c = i7 - Integer.MIN_VALUE;
            } else {
                sVar = new s(j4, cVar);
            }
        }
        Object objK = sVar.f18995a;
        Object obj = B4.a.f492a;
        int i8 = sVar.f18997c;
        try {
            if (i8 == 0) {
                AbstractC2467b.l(objK);
                sVar.f18997c = 1;
                objK = j4.K("https://api.trakt.tv/shows/" + i6 + "/progress/watched?hidden=false&specials=false", sVar);
                if (objK == obj) {
                    return obj;
                }
            } else {
                if (i8 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                AbstractC2467b.l(objK);
            }
            objD = (JSONObject) objK;
        } catch (Throwable th) {
            objD = AbstractC2467b.d(th);
        }
        if (objD instanceof w4.m) {
            return null;
        }
        return objD;
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0015  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final Object n(J j4, int i6, C4.c cVar) {
        t tVar;
        Object objD;
        if (cVar instanceof t) {
            tVar = (t) cVar;
            int i7 = tVar.f19000c;
            if ((i7 & Integer.MIN_VALUE) != 0) {
                tVar.f19000c = i7 - Integer.MIN_VALUE;
            } else {
                tVar = new t(j4, cVar);
            }
        }
        Object objK = tVar.f18998a;
        Object obj = B4.a.f492a;
        int i8 = tVar.f19000c;
        try {
            if (i8 == 0) {
                AbstractC2467b.l(objK);
                tVar.f19000c = 1;
                objK = j4.K("https://api.trakt.tv/shows/" + i6 + "?extended=full,images", tVar);
                if (objK == obj) {
                    return obj;
                }
            } else {
                if (i8 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                AbstractC2467b.l(objK);
            }
            objD = (JSONObject) objK;
        } catch (Throwable th) {
            objD = AbstractC2467b.d(th);
        }
        if (objD instanceof w4.m) {
            return null;
        }
        return objD;
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0016  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final Object o(J j4, C4.c cVar) {
        z zVar;
        Object objD;
        j4.getClass();
        if (cVar instanceof z) {
            zVar = (z) cVar;
            int i6 = zVar.f19066c;
            if ((i6 & Integer.MIN_VALUE) != 0) {
                zVar.f19066c = i6 - Integer.MIN_VALUE;
            } else {
                zVar = new z(j4, cVar);
            }
        }
        Object objJ = zVar.f19064a;
        Object obj = B4.a.f492a;
        int i7 = zVar.f19066c;
        try {
            if (i7 == 0) {
                AbstractC2467b.l(objJ);
                zVar.f19066c = 1;
                objJ = j4.J("https://api.trakt.tv/sync/watched/shows", zVar);
                if (objJ == obj) {
                    return obj;
                }
            } else {
                if (i7 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                AbstractC2467b.l(objJ);
            }
            objD = (JSONArray) objJ;
        } catch (Throwable th) {
            objD = AbstractC2467b.d(th);
        }
        return w4.n.a(objD) == null ? objD : new JSONArray();
    }

    public static final LinkedHashSet p(J j4, JSONArray jSONArray, Instant instant) throws JSONException {
        JSONObject jSONObjectOptJSONObject;
        j4.getClass();
        LinkedHashSet linkedHashSet = new LinkedHashSet();
        int length = jSONArray.length();
        for (int i6 = 0; i6 < length; i6++) {
            JSONObject jSONObject = jSONArray.getJSONObject(i6);
            Instant instantM = M(jSONObject.optString("last_watched_at"));
            if (instantM != null && instantM.isAfter(instant) && (jSONObjectOptJSONObject = jSONObject.optJSONObject("show")) != null) {
                JSONObject jSONObjectOptJSONObject2 = jSONObjectOptJSONObject.optJSONObject("ids");
                int iOptInt = jSONObjectOptJSONObject2 != null ? jSONObjectOptJSONObject2.optInt("trakt", 0) : 0;
                if (iOptInt != 0) {
                    linkedHashSet.add(Integer.valueOf(iOptInt));
                }
            }
        }
        return linkedHashSet;
    }

    public static final LinkedHashMap q(J j4, JSONArray jSONArray) throws JSONException {
        Instant instantM;
        Instant instant;
        j4.getClass();
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        int length = jSONArray.length();
        for (int i6 = 0; i6 < length; i6++) {
            JSONObject jSONObject = jSONArray.getJSONObject(i6);
            JSONObject jSONObjectOptJSONObject = jSONObject.optJSONObject("show");
            if (jSONObjectOptJSONObject != null) {
                JSONObject jSONObjectOptJSONObject2 = jSONObjectOptJSONObject.optJSONObject("ids");
                int iOptInt = jSONObjectOptJSONObject2 != null ? jSONObjectOptJSONObject2.optInt("trakt", 0) : 0;
                if (iOptInt != 0 && (instantM = M(jSONObject.optString("last_watched_at"))) != null && ((instant = (Instant) linkedHashMap.get(Integer.valueOf(iOptInt))) == null || instantM.isAfter(instant))) {
                    linkedHashMap.put(Integer.valueOf(iOptInt), instantM);
                }
            }
        }
        return linkedHashMap;
    }

    public static final LinkedHashMap r(J j4, JSONArray jSONArray) {
        JSONObject jSONObjectOptJSONObject;
        JSONObject jSONObjectOptJSONObject2;
        Instant instantM;
        j4.getClass();
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        LinkedHashMap linkedHashMap2 = new LinkedHashMap();
        int length = jSONArray.length();
        for (int i6 = 0; i6 < length; i6++) {
            JSONObject jSONObjectOptJSONObject3 = jSONArray.optJSONObject(i6);
            if (jSONObjectOptJSONObject3 != null && (jSONObjectOptJSONObject = jSONObjectOptJSONObject3.optJSONObject("show")) != null) {
                JSONObject jSONObjectOptJSONObject4 = jSONObjectOptJSONObject.optJSONObject("ids");
                int iOptInt = jSONObjectOptJSONObject4 != null ? jSONObjectOptJSONObject4.optInt("trakt", 0) : 0;
                if (iOptInt != 0 && (jSONObjectOptJSONObject2 = jSONObjectOptJSONObject3.optJSONObject("episode")) != null && (instantM = M(jSONObjectOptJSONObject3.optString("watched_at"))) != null) {
                    N n6 = new N(jSONObjectOptJSONObject2.optInt("season"), jSONObjectOptJSONObject2.optInt("number"));
                    Instant instant = (Instant) linkedHashMap.get(Integer.valueOf(iOptInt));
                    if (instant == null || instantM.isAfter(instant)) {
                        linkedHashMap.put(Integer.valueOf(iOptInt), instantM);
                        linkedHashMap2.put(Integer.valueOf(iOptInt), n6);
                    }
                }
            }
        }
        LinkedHashMap linkedHashMap3 = new LinkedHashMap();
        for (Map.Entry entry : linkedHashMap.entrySet()) {
            int iIntValue = ((Number) entry.getKey()).intValue();
            Instant instant2 = (Instant) entry.getValue();
            N n7 = (N) linkedHashMap2.get(Integer.valueOf(iIntValue));
            if (n7 != null) {
                linkedHashMap3.put(Integer.valueOf(iIntValue), new w4.k(instant2, n7));
            }
        }
        return linkedHashMap3;
    }

    public static final JSONArray s(J j4, String str) {
        j4.getClass();
        if (str == null || S4.p.Q0(str)) {
            return new JSONArray();
        }
        try {
            return new JSONArray(str);
        } catch (Exception unused) {
            return new JSONArray();
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:107:0x0363  */
    /* JADX WARN: Removed duplicated region for block: B:114:0x0366 A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final JSONArray t(J j4, JSONArray jSONArray, boolean z6) throws JSONException {
        int i6;
        int i7;
        JSONObject jSONObjectOptJSONObject;
        JSONObject jSONObjectOptJSONObject2;
        int i8;
        String strOptString;
        String str;
        String str2;
        JSONArray jSONArrayOptJSONArray;
        JSONArray jSONArrayOptJSONArray2;
        JSONArray jSONArrayOptJSONArray3;
        JSONObject jSONObject;
        JSONArray jSONArray2;
        JSONObject jSONObjectOptJSONObject3;
        String str3;
        JSONArray jSONArrayOptJSONArray4;
        JSONArray jSONArrayOptJSONArray5;
        JSONArray jSONArrayOptJSONArray6;
        j4.getClass();
        JSONArray jSONArray3 = new JSONArray();
        int length = jSONArray.length();
        int i9 = 0;
        while (i9 < length) {
            JSONObject jSONObjectOptJSONObject4 = jSONArray.optJSONObject(i9);
            if (jSONObjectOptJSONObject4 == null) {
                i6 = length;
                i7 = i9;
                jSONArray2 = jSONArray3;
            } else {
                i6 = length;
                i7 = i9;
                JSONArray jSONArray4 = jSONArray3;
                JSONObject jSONObjectPut = null;
                strOptString = null;
                String strOptString2 = null;
                jSONObjectPut = null;
                jSONObjectPut = null;
                jSONObjectPut = null;
                strOptString = null;
                String strOptString3 = null;
                jSONObjectPut = null;
                jSONObjectPut = null;
                jSONObjectPut = null;
                if (z6) {
                    JSONObject jSONObjectOptJSONObject5 = jSONObjectOptJSONObject4.optJSONObject("movie");
                    if (jSONObjectOptJSONObject5 != null && (jSONObjectOptJSONObject3 = jSONObjectOptJSONObject5.optJSONObject("ids")) != null && jSONObjectOptJSONObject3.optInt("trakt", 0) != 0) {
                        JSONObject jSONObjectOptJSONObject6 = jSONObjectOptJSONObject5.optJSONObject("images");
                        String strZ = z((jSONObjectOptJSONObject6 == null || (jSONArrayOptJSONArray6 = jSONObjectOptJSONObject6.optJSONArray("poster")) == null) ? null : jSONArrayOptJSONArray6.optString(0));
                        String strZ2 = z((jSONObjectOptJSONObject6 == null || (jSONArrayOptJSONArray5 = jSONObjectOptJSONObject6.optJSONArray("fanart")) == null) ? null : jSONArrayOptJSONArray5.optString(0));
                        if (jSONObjectOptJSONObject6 != null && (jSONArrayOptJSONArray4 = jSONObjectOptJSONObject6.optJSONArray("logo")) != null) {
                            strOptString2 = jSONArrayOptJSONArray4.optString(0);
                        }
                        String strZ3 = z(strOptString2);
                        JSONArray jSONArray5 = new JSONArray();
                        JSONArray jSONArrayOptJSONArray7 = jSONObjectOptJSONObject5.optJSONArray("genres");
                        if (jSONArrayOptJSONArray7 != null) {
                            str3 = "logo";
                            int i10 = 0;
                            for (int length2 = jSONArrayOptJSONArray7.length(); i10 < length2; length2 = length2) {
                                jSONArray5.put(jSONArrayOptJSONArray7.getString(i10));
                                i10++;
                            }
                        } else {
                            str3 = "logo";
                        }
                        JSONObject jSONObjectPut2 = new JSONObject().put("historyId", jSONObjectOptJSONObject4.optLong("id", 0L)).put("watched_at", jSONObjectOptJSONObject4.optString("watched_at")).put("type", "movie").put("tmdbId", jSONObjectOptJSONObject3.optInt("tmdb")).put("traktId", jSONObjectOptJSONObject3.optInt("trakt")).put("imdbId", jSONObjectOptJSONObject3.optString("imdb")).put("title", jSONObjectOptJSONObject5.optString("title")).put("overview", jSONObjectOptJSONObject5.optString("overview")).put("year", String.valueOf(jSONObjectOptJSONObject5.optInt("year"))).put("rating", jSONObjectOptJSONObject5.optDouble("rating")).put("popularity", jSONObjectOptJSONObject5.optDouble("votes")).put("certificate", L(jSONObjectOptJSONObject5)).put("runTime", jSONObjectOptJSONObject5.optInt("runtime"));
                        String strA = g3.u.a(strZ, false);
                        if (S4.p.Q0(strA)) {
                            if (strZ == null) {
                                strZ = "";
                            }
                            strA = strZ;
                        }
                        JSONObject jSONObjectPut3 = jSONObjectPut2.put("posterUrl", strA);
                        String strA2 = g3.u.a(strZ2, true);
                        if (S4.p.Q0(strA2)) {
                            strA2 = strZ2 == null ? "" : strZ2;
                        }
                        jSONObjectPut = jSONObjectPut3.put("backdropUrl", strA2).put(str3, strZ3).put("genres", jSONArray5);
                    }
                } else {
                    JSONObject jSONObjectOptJSONObject7 = jSONObjectOptJSONObject4.optJSONObject("episode");
                    if (jSONObjectOptJSONObject7 != null && (jSONObjectOptJSONObject = jSONObjectOptJSONObject4.optJSONObject("show")) != null && (jSONObjectOptJSONObject2 = jSONObjectOptJSONObject.optJSONObject("ids")) != null && jSONObjectOptJSONObject2.optInt("trakt", 0) != 0) {
                        JSONObject jSONObjectOptJSONObject8 = jSONObjectOptJSONObject7.optJSONObject("ids");
                        if (jSONObjectOptJSONObject8 == null) {
                            jSONObjectOptJSONObject8 = new JSONObject();
                        }
                        JSONObject jSONObjectOptJSONObject9 = jSONObjectOptJSONObject.optJSONObject("images");
                        if (jSONObjectOptJSONObject9 == null || (jSONArrayOptJSONArray3 = jSONObjectOptJSONObject9.optJSONArray("poster")) == null) {
                            i8 = 0;
                            strOptString = null;
                        } else {
                            i8 = 0;
                            strOptString = jSONArrayOptJSONArray3.optString(0);
                        }
                        String strZ4 = z(strOptString);
                        String strZ5 = z((jSONObjectOptJSONObject9 == null || (jSONArrayOptJSONArray2 = jSONObjectOptJSONObject9.optJSONArray("fanart")) == null) ? null : jSONArrayOptJSONArray2.optString(i8));
                        if (jSONObjectOptJSONObject9 != null && (jSONArrayOptJSONArray = jSONObjectOptJSONObject9.optJSONArray("logo")) != null) {
                            strOptString3 = jSONArrayOptJSONArray.optString(i8);
                        }
                        String strZ6 = z(strOptString3);
                        JSONArray jSONArray6 = new JSONArray();
                        JSONArray jSONArrayOptJSONArray8 = jSONObjectOptJSONObject.optJSONArray("genres");
                        if (jSONArrayOptJSONArray8 != null) {
                            str2 = "genres";
                            str = strZ6;
                            int i11 = 0;
                            for (int length3 = jSONArrayOptJSONArray8.length(); i11 < length3; length3 = length3) {
                                jSONArray6.put(jSONArrayOptJSONArray8.getString(i11));
                                i11++;
                            }
                        } else {
                            str = strZ6;
                            str2 = "genres";
                        }
                        JSONObject jSONObjectPut4 = new JSONObject().put("historyId", jSONObjectOptJSONObject4.optLong("id", 0L)).put("watched_at", jSONObjectOptJSONObject4.optString("watched_at")).put("type", "tv").put("tmdbId", jSONObjectOptJSONObject2.optInt("tmdb")).put("traktId", jSONObjectOptJSONObject2.optInt("trakt")).put("episodeTmdbId", jSONObjectOptJSONObject8.optInt("tmdb")).put("imdbId", jSONObjectOptJSONObject2.optString("imdb")).put("title", jSONObjectOptJSONObject.optString("title")).put("overview", jSONObjectOptJSONObject.optString("overview")).put("year", String.valueOf(jSONObjectOptJSONObject.optInt("year"))).put("rating", jSONObjectOptJSONObject.optDouble("rating")).put("popularity", jSONObjectOptJSONObject.optDouble("votes")).put("certificate", L(jSONObjectOptJSONObject)).put("runTime", jSONObjectOptJSONObject.optInt("runtime")).put("season", jSONObjectOptJSONObject7.optInt("season")).put("episode", jSONObjectOptJSONObject7.optInt("number")).put("episodeTitle", jSONObjectOptJSONObject7.optString("title"));
                        String strA3 = g3.u.a(strZ4, false);
                        if (S4.p.Q0(strA3)) {
                            strA3 = strZ4 == null ? "" : strZ4;
                        }
                        JSONObject jSONObjectPut5 = jSONObjectPut4.put("posterUrl", strA3);
                        String strA4 = g3.u.a(strZ5, true);
                        if (S4.p.Q0(strA4)) {
                            if (strZ5 == null) {
                                strZ5 = "";
                            }
                            strA4 = strZ5;
                        }
                        jSONObjectPut = jSONObjectPut5.put("backdropUrl", strA4).put("logo", str).put(str2, jSONArray6);
                    }
                    jSONObject = jSONObjectPut;
                    jSONArray2 = jSONArray4;
                    if (jSONObject == null) {
                        jSONArray2.put(jSONObject);
                    }
                }
                jSONObject = jSONObjectPut;
                jSONArray2 = jSONArray4;
                if (jSONObject == null) {
                }
            }
            i9 = i7 + 1;
            jSONArray3 = jSONArray2;
            length = i6;
        }
        return jSONArray3;
    }

    public static final JSONArray u(J j4, JSONArray jSONArray, JSONArray jSONArray2) {
        j4.getClass();
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        int length = jSONArray.length();
        for (int i6 = 0; i6 < length; i6++) {
            JSONObject jSONObjectOptJSONObject = jSONArray.optJSONObject(i6);
            if (jSONObjectOptJSONObject != null) {
                long jOptLong = jSONObjectOptJSONObject.optLong("historyId", 0L);
                if (jOptLong != 0) {
                    linkedHashMap.put(Long.valueOf(jOptLong), jSONObjectOptJSONObject);
                }
            }
        }
        int length2 = jSONArray2.length();
        for (int i7 = 0; i7 < length2; i7++) {
            JSONObject jSONObjectOptJSONObject2 = jSONArray2.optJSONObject(i7);
            if (jSONObjectOptJSONObject2 != null) {
                long jOptLong2 = jSONObjectOptJSONObject2.optLong("historyId", 0L);
                if (jOptLong2 != 0) {
                    linkedHashMap.put(Long.valueOf(jOptLong2), jSONObjectOptJSONObject2);
                }
            }
        }
        Collection collectionValues = linkedHashMap.values();
        kotlin.jvm.internal.o.g(collectionValues, "<get-values>(...)");
        List listX0 = AbstractC3251q.x0(new C2279m(j4, 2), collectionValues);
        JSONArray jSONArray3 = new JSONArray();
        Iterator it = listX0.iterator();
        while (it.hasNext()) {
            jSONArray3.put((JSONObject) it.next());
        }
        return jSONArray3;
    }

    public static final void v(J j4, Map map, LinkedHashMap linkedHashMap) {
        JSONObject jSONObjectOptJSONObject;
        j4.getClass();
        if (linkedHashMap.isEmpty() || map.isEmpty()) {
            return;
        }
        ArrayList arrayList = new ArrayList();
        for (Map.Entry entry : map.entrySet()) {
            int iIntValue = ((Number) entry.getKey()).intValue();
            JSONObject jSONObject = (JSONObject) entry.getValue();
            w4.k kVar = (w4.k) linkedHashMap.get(Integer.valueOf(iIntValue));
            if (kVar != null) {
                Instant instant = (Instant) kVar.f25469a;
                N n6 = (N) kVar.f25470b;
                Instant instantN = N(jSONObject);
                if (instantN != null && instant.isAfter(instantN) && (jSONObjectOptJSONObject = jSONObject.optJSONObject("episode")) != null) {
                    int iOptInt = jSONObjectOptJSONObject.optInt("season");
                    int iOptInt2 = jSONObjectOptJSONObject.optInt("number");
                    n6.getClass();
                    int i6 = kotlin.jvm.internal.o.i(n6.f18906a, iOptInt);
                    if (i6 == 0) {
                        i6 = kotlin.jvm.internal.o.i(n6.f18907b, iOptInt2);
                    }
                    if (i6 >= 0) {
                        arrayList.add(Integer.valueOf(iIntValue));
                        jSONObject.optJSONObject("show");
                    }
                }
            }
        }
        Iterator it = arrayList.iterator();
        while (it.hasNext()) {
            map.remove(Integer.valueOf(((Number) it.next()).intValue()));
        }
    }

    public static final Instant w(J j4, String... strArr) {
        j4.getClass();
        ArrayList arrayList = new ArrayList();
        for (String str : strArr) {
            Instant instantM = M(str);
            if (instantM != null) {
                arrayList.add(instantM);
            }
        }
        Instant instant = (Instant) AbstractC3251q.p0(arrayList);
        if (instant == null) {
            return null;
        }
        ZoneOffset zoneOffset = ZoneOffset.UTC;
        return instant.atZone(zoneOffset).o().atStartOfDay(zoneOffset).toInstant();
    }

    public static RequestBody$Companion$toRequestBody$2 x(JSONObject jSONObject) {
        RequestBody.Companion companion = RequestBody.f22136a;
        MediaType.f22043d.getClass();
        MediaType mediaTypeB = MediaType.Companion.b("application/json");
        String string = jSONObject.toString();
        kotlin.jvm.internal.o.g(string, "toString(...)");
        companion.getClass();
        return RequestBody.Companion.a(string, mediaTypeB);
    }

    public static Headers y(String str) {
        Headers.Builder builder = new Headers.Builder();
        builder.a("Content-Type", "application/json");
        String str2 = W.d.f8386g;
        if (str2 == null) {
            str2 = "DebridStream/3.0.0";
        }
        builder.a("User-Agent", str2);
        builder.a("trakt-api-version", "2");
        builder.a("trakt-api-key", "cb6c23120401d06808b06c16c78d0e54e1953df0fce43d921280c708619c1b34");
        builder.a("Authorization", "Bearer " + str);
        return builder.e();
    }

    public static String z(String str) {
        if (str == null || str.length() == 0) {
            return null;
        }
        return S4.w.A0(str, "https://", false) ? str : S4.w.A0(str, "http://", false) ? "https://".concat(S4.p.X0(str, "http://")) : "https://".concat(str);
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0017  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object A(C4.c cVar) throws Throwable {
        C2280n c2280n;
        e5.a aVar;
        J j4;
        e5.a aVar2;
        String string;
        if (cVar instanceof C2280n) {
            c2280n = (C2280n) cVar;
            int i6 = c2280n.f18969e;
            if ((i6 & Integer.MIN_VALUE) != 0) {
                c2280n.f18969e = i6 - Integer.MIN_VALUE;
            } else {
                c2280n = new C2280n(this, cVar);
            }
        }
        Object objP = c2280n.f18967c;
        B4.a aVar3 = B4.a.f492a;
        int i7 = c2280n.f18969e;
        try {
            if (i7 == 0) {
                AbstractC2467b.l(objP);
                K k6 = this.f18889a;
                String string2 = k6.f18895a.getString("access", null);
                long j6 = k6.f18895a.getLong("expires", 0L);
                if (string2 != null && System.currentTimeMillis() < j6) {
                    return string2;
                }
                c2280n.f18965a = this;
                e5.c cVar2 = this.f18890b;
                c2280n.f18966b = cVar2;
                c2280n.f18969e = 1;
                if (cVar2.e(c2280n) != aVar3) {
                    aVar = cVar2;
                    j4 = this;
                }
                return aVar3;
            }
            if (i7 != 1) {
                if (i7 != 2) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                aVar2 = (e5.a) c2280n.f18965a;
                try {
                    AbstractC2467b.l(objP);
                    string = (String) objP;
                    aVar = aVar2;
                    ((e5.c) aVar).g(null);
                    return string;
                } catch (Throwable th) {
                    th = th;
                    ((e5.c) aVar2).g(null);
                    throw th;
                }
            }
            aVar = c2280n.f18966b;
            j4 = (J) c2280n.f18965a;
            AbstractC2467b.l(objP);
            string = j4.f18889a.f18895a.getString("access", null);
            long j7 = j4.f18889a.f18895a.getLong("expires", 0L);
            if (string != null && System.currentTimeMillis() < j7) {
                ((e5.c) aVar).g(null);
                return string;
            }
            c2280n.f18965a = aVar;
            c2280n.f18966b = null;
            c2280n.f18969e = 2;
            objP = j4.P(c2280n);
            if (objP != aVar3) {
                aVar2 = aVar;
                string = (String) objP;
                aVar = aVar2;
                ((e5.c) aVar).g(null);
                return string;
            }
            return aVar3;
        } catch (Throwable th2) {
            th = th2;
            aVar2 = aVar;
            ((e5.c) aVar2).g(null);
            throw th;
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:53:0x014a, code lost:
    
        if (V4.E.l(((long) r9) * 1000, r3) == r4) goto L54;
     */
    /* JADX WARN: Removed duplicated region for block: B:36:0x00ea  */
    /* JADX WARN: Removed duplicated region for block: B:40:0x0102  */
    /* JADX WARN: Removed duplicated region for block: B:45:0x0116 A[ADDED_TO_REGION] */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0019  */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:53:0x014a -> B:55:0x014d). Please report as a decompilation issue!!! */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object B(J4.c cVar, boolean z6, boolean z7, C4.c cVar2) throws Exception {
        o oVar;
        boolean z8;
        J j4;
        J4.c cVar3;
        boolean z9;
        boolean z10;
        J4.c cVar4;
        kotlin.jvm.internal.C c6;
        kotlin.jvm.internal.C c7;
        Response responseC;
        boolean z11;
        kotlin.jvm.internal.C c8;
        kotlin.jvm.internal.C c9;
        J4.c cVar5;
        J j6;
        int i6;
        kotlin.jvm.internal.C c10;
        J4.c cVar6;
        J j7;
        Integer numB0;
        boolean z12 = z6;
        if (cVar2 instanceof o) {
            oVar = (o) cVar2;
            int i7 = oVar.f18978m;
            if ((i7 & Integer.MIN_VALUE) != 0) {
                oVar.f18978m = i7 - Integer.MIN_VALUE;
            } else {
                oVar = new o(this, cVar2);
            }
        }
        Object objP = oVar.f18977k;
        B4.a aVar = B4.a.f492a;
        int i8 = oVar.f18978m;
        if (i8 == 0) {
            AbstractC2467b.l(objP);
            oVar.f18970a = this;
            oVar.f18971b = cVar;
            oVar.f18974e = z12;
            z8 = z7;
            oVar.f18975f = z8;
            oVar.f18978m = 1;
            if (O(z12, oVar) != aVar) {
                j4 = this;
                cVar3 = cVar;
            }
            return aVar;
        }
        if (i8 == 1) {
            boolean z13 = oVar.f18975f;
            boolean z14 = oVar.f18974e;
            cVar3 = oVar.f18971b;
            J j8 = oVar.f18970a;
            AbstractC2467b.l(objP);
            z8 = z13;
            z12 = z14;
            j4 = j8;
        } else {
            if (i8 == 2) {
                z10 = oVar.f18975f;
                z9 = oVar.f18974e;
                c7 = oVar.f18973d;
                c6 = oVar.f18972c;
                cVar4 = oVar.f18971b;
                j4 = oVar.f18970a;
                AbstractC2467b.l(objP);
                c7.f20115a = objP;
                responseC = C(j4, cVar4, c6);
                if (responseC.f22143d == 401 && z10) {
                    responseC.close();
                    oVar.f18970a = j4;
                    oVar.f18971b = cVar4;
                    oVar.f18972c = c6;
                    oVar.f18973d = c6;
                    oVar.f18974e = z9;
                    oVar.f18978m = 3;
                    objP = j4.P(oVar);
                    if (objP != aVar) {
                        z11 = z9;
                        c8 = c6;
                        c8.f20115a = objP;
                        oVar.f18970a = j4;
                        oVar.f18971b = cVar4;
                        oVar.f18972c = c6;
                        oVar.f18973d = null;
                        oVar.f18978m = 4;
                        j4.getClass();
                        if (O(z11, oVar) != aVar) {
                        }
                    }
                    return aVar;
                }
                i6 = 0;
                c10 = c6;
                cVar6 = cVar4;
                j7 = j4;
                if (responseC.f22143d == 429) {
                }
                return responseC;
            }
            if (i8 == 3) {
                z11 = oVar.f18974e;
                c8 = oVar.f18973d;
                kotlin.jvm.internal.C c11 = oVar.f18972c;
                J4.c cVar7 = oVar.f18971b;
                J j9 = oVar.f18970a;
                AbstractC2467b.l(objP);
                cVar4 = cVar7;
                j4 = j9;
                c6 = c11;
                c8.f20115a = objP;
                oVar.f18970a = j4;
                oVar.f18971b = cVar4;
                oVar.f18972c = c6;
                oVar.f18973d = null;
                oVar.f18978m = 4;
                j4.getClass();
                if (O(z11, oVar) != aVar) {
                    c9 = c6;
                    cVar5 = cVar4;
                    j6 = j4;
                    responseC = C(j6, cVar5, c9);
                    c6 = c9;
                    cVar4 = cVar5;
                    j4 = j6;
                    i6 = 0;
                    c10 = c6;
                    cVar6 = cVar4;
                    j7 = j4;
                    if (responseC.f22143d == 429) {
                    }
                    return responseC;
                }
                return aVar;
            }
            if (i8 == 4) {
                c9 = oVar.f18972c;
                cVar5 = oVar.f18971b;
                j6 = oVar.f18970a;
                AbstractC2467b.l(objP);
                responseC = C(j6, cVar5, c9);
                c6 = c9;
                cVar4 = cVar5;
                j4 = j6;
                i6 = 0;
                c10 = c6;
                cVar6 = cVar4;
                j7 = j4;
                if (responseC.f22143d == 429) {
                }
                return responseC;
            }
            if (i8 != 5) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            i6 = oVar.f18976j;
            c10 = oVar.f18972c;
            cVar6 = oVar.f18971b;
            j7 = oVar.f18970a;
            AbstractC2467b.l(objP);
            long jCurrentTimeMillis = System.currentTimeMillis();
            f18885e.set(jCurrentTimeMillis);
            f18886f.set(jCurrentTimeMillis);
            i6++;
            responseC = C(j7, cVar6, c10);
            if (responseC.f22143d == 429 || i6 >= 5) {
                return responseC;
            }
            String strB = Response.b("Retry-After", responseC);
            int iV = (strB == null || (numB0 = S4.w.B0(strB)) == null) ? 60 : L4.a.v(numB0.intValue(), 1, 300);
            responseC.close();
            oVar.f18970a = j7;
            oVar.f18971b = cVar6;
            oVar.f18972c = c10;
            oVar.f18973d = null;
            oVar.f18976j = i6;
            oVar.f18978m = 5;
        }
        kotlin.jvm.internal.C c12 = new kotlin.jvm.internal.C();
        oVar.f18970a = j4;
        oVar.f18971b = cVar3;
        oVar.f18972c = c12;
        oVar.f18973d = c12;
        oVar.f18974e = z12;
        oVar.f18975f = z8;
        oVar.f18978m = 2;
        Object objA = j4.A(oVar);
        if (objA != aVar) {
            boolean z15 = z8;
            z9 = z12;
            z10 = z15;
            cVar4 = cVar3;
            c6 = c12;
            objP = objA;
            c7 = c6;
            c7.f20115a = objP;
            responseC = C(j4, cVar4, c6);
            if (responseC.f22143d == 401) {
                responseC.close();
                oVar.f18970a = j4;
                oVar.f18971b = cVar4;
                oVar.f18972c = c6;
                oVar.f18973d = c6;
                oVar.f18974e = z9;
                oVar.f18978m = 3;
                objP = j4.P(oVar);
                if (objP != aVar) {
                }
            }
            i6 = 0;
            c10 = c6;
            cVar6 = cVar4;
            j7 = j4;
            if (responseC.f22143d == 429) {
            }
            return responseC;
        }
        return aVar;
    }

    public final Object D(String str, Instant instant, int i6, C4.j jVar) throws UnsupportedEncodingException {
        if (instant != null) {
            String str2 = S4.p.D0(str, "?", false) ? "&" : "?";
            str = str + str2 + "start_at=" + URLEncoder.encode(instant.toString(), S4.a.f6257a.name());
        }
        return F(this, str, i6, jVar, 2);
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:28:0x00ab A[Catch: all -> 0x00b0, TryCatch #0 {all -> 0x00b0, blocks: (B:26:0x00a7, B:28:0x00ab, B:34:0x00b9, B:36:0x00bf, B:38:0x00cd, B:39:0x00d8, B:41:0x00e0, B:43:0x00e6, B:45:0x00ed, B:44:0x00eb, B:47:0x00ff, B:48:0x0131), top: B:54:0x00a7 }] */
    /* JADX WARN: Removed duplicated region for block: B:31:0x00b4  */
    /* JADX WARN: Removed duplicated region for block: B:33:0x00b7  */
    /* JADX WARN: Removed duplicated region for block: B:36:0x00bf A[Catch: all -> 0x00b0, TryCatch #0 {all -> 0x00b0, blocks: (B:26:0x00a7, B:28:0x00ab, B:34:0x00b9, B:36:0x00bf, B:38:0x00cd, B:39:0x00d8, B:41:0x00e0, B:43:0x00e6, B:45:0x00ed, B:44:0x00eb, B:47:0x00ff, B:48:0x0131), top: B:54:0x00a7 }] */
    /* JADX WARN: Removed duplicated region for block: B:47:0x00ff A[Catch: all -> 0x00b0, TRY_ENTER, TryCatch #0 {all -> 0x00b0, blocks: (B:26:0x00a7, B:28:0x00ab, B:34:0x00b9, B:36:0x00bf, B:38:0x00cd, B:39:0x00d8, B:41:0x00e0, B:43:0x00e6, B:45:0x00ed, B:44:0x00eb, B:47:0x00ff, B:48:0x0131), top: B:54:0x00a7 }] */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0017  */
    /* JADX WARN: Type inference failed for: r15v1, types: [org.json.JSONArray] */
    /* JADX WARN: Type inference failed for: r5v10 */
    /* JADX WARN: Type inference failed for: r5v4 */
    /* JADX WARN: Type inference failed for: r5v5, types: [int] */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:24:0x009d -> B:25:0x00a4). Please report as a decompilation issue!!! */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object E(String str, int i6, int i7, C4.c cVar) throws Exception {
        r rVar;
        J j4;
        kotlin.jvm.internal.A a6;
        String str2;
        int i8;
        J j6;
        kotlin.jvm.internal.A a7;
        String str3;
        JSONArray jSONArray;
        int i9;
        int i10;
        Integer numB0;
        if (cVar instanceof r) {
            rVar = (r) cVar;
            int i11 = rVar.f18994n;
            if ((i11 & Integer.MIN_VALUE) != 0) {
                rVar.f18994n = i11 - Integer.MIN_VALUE;
                j4 = this;
            } else {
                j4 = this;
                rVar = new r(j4, cVar);
            }
        }
        Object obj = rVar.l;
        B4.a aVar = B4.a.f492a;
        int i12 = rVar.f18994n;
        boolean z6 = true;
        z6 = true;
        boolean z7 = false;
        if (i12 == 0) {
            AbstractC2467b.l(obj);
            JSONArray jSONArray2 = new JSONArray();
            kotlin.jvm.internal.A a8 = new kotlin.jvm.internal.A();
            a8.f20113a = 1;
            a6 = new kotlin.jvm.internal.A();
            a6.f20113a = 1;
            str2 = S4.p.D0(str, "?", false) ? "&" : "?";
            i8 = i6;
            j6 = j4;
            a7 = a8;
            str3 = str;
            jSONArray = jSONArray2;
            i9 = i7;
            String str4 = str2;
            i10 = a7.f20113a;
            if (i10 <= a6.f20113a) {
            }
            return jSONArray;
        }
        if (i12 != 1) {
            throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
        }
        int i13 = rVar.f18992k;
        int i14 = rVar.f18991j;
        str2 = rVar.f18990f;
        kotlin.jvm.internal.A a9 = rVar.f18989e;
        kotlin.jvm.internal.A a10 = rVar.f18988d;
        JSONArray jSONArray3 = rVar.f18987c;
        String str5 = rVar.f18986b;
        J j7 = rVar.f18985a;
        AbstractC2467b.l(obj);
        a7 = a10;
        str3 = str5;
        j6 = j7;
        i8 = i14;
        a6 = a9;
        Response response = (Response) obj;
        try {
            ResponseBody responseBody = response.f22146j;
            String strD = responseBody == null ? responseBody.d() : null;
            if (strD == null) {
                strD = "";
            }
            if (!response.c()) {
                ?? jSONArray4 = new JSONArray(strD);
                int length = jSONArray4.length();
                boolean z8 = z6;
                for (?? r5 = z7; r5 < length; r5++) {
                    jSONArray3.put(jSONArray4.get(r5));
                }
                String strB = Response.b("X-Pagination-Page-Count", response);
                a6.f20113a = (strB == null || (numB0 = S4.w.B0(strB)) == null) ? a6.f20113a : numB0.intValue();
                response.close();
                a7.f20113a++;
                z6 = z8 ? 1 : 0;
                i9 = i13;
                jSONArray = jSONArray3;
                z7 = false;
                String str42 = str2;
                i10 = a7.f20113a;
                if (i10 <= a6.f20113a || i10 > i9) {
                    return jSONArray;
                }
                L1 l12 = new L1(str3, str42, a7, i8, j6);
                rVar.f18985a = j6;
                rVar.f18986b = str3;
                rVar.f18987c = jSONArray;
                rVar.f18988d = a7;
                rVar.f18989e = a6;
                rVar.f18990f = str42;
                rVar.f18991j = i8;
                rVar.f18992k = i9;
                rVar.f18994n = z6 ? 1 : 0;
                Object objB = j6.B(l12, z7, z6, rVar);
                if (objB == aVar) {
                    return aVar;
                }
                JSONArray jSONArray5 = jSONArray;
                i13 = i9;
                obj = objB;
                str2 = str42;
                jSONArray3 = jSONArray5;
                Response response2 = (Response) obj;
                ResponseBody responseBody2 = response2.f22146j;
                if (responseBody2 == null) {
                }
                if (strD == null) {
                }
                if (!response2.c()) {
                    throw new RuntimeException("GET " + str3 + " failed page=" + a7.f20113a + ": HTTP " + response2.f22143d + " " + strD);
                }
            }
        } finally {
        }
    }

    public final Object G(C4.j jVar) {
        c5.e eVar = V4.N.f8259a;
        Object objH = V4.E.H(c5.d.f13466b, new w(this, null), jVar);
        return objH == B4.a.f492a ? objH : C3137A.f25453a;
    }

    public final Object H(C4.j jVar) {
        c5.e eVar = V4.N.f8259a;
        Object objH = V4.E.H(c5.d.f13466b, new x(this, null), jVar);
        return objH == B4.a.f492a ? objH : C3137A.f25453a;
    }

    public final Object I(C4.c cVar) {
        c5.e eVar = V4.N.f8259a;
        return V4.E.H(c5.d.f13466b, new y(this, null), cVar);
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0015  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object J(String str, C4.c cVar) throws Exception {
        C2264A c2264a;
        if (cVar instanceof C2264A) {
            c2264a = (C2264A) cVar;
            int i6 = c2264a.f18839d;
            if ((i6 & Integer.MIN_VALUE) != 0) {
                c2264a.f18839d = i6 - Integer.MIN_VALUE;
            } else {
                c2264a = new C2264A(this, cVar);
            }
        }
        Object objB = c2264a.f18837b;
        Object obj = B4.a.f492a;
        int i7 = c2264a.f18839d;
        if (i7 == 0) {
            AbstractC2467b.l(objB);
            J4.c c2272f = new C2272f(str, this, 1);
            c2264a.f18836a = str;
            c2264a.f18839d = 1;
            objB = B(c2272f, false, true, c2264a);
            if (objB == obj) {
                return obj;
            }
        } else {
            if (i7 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            str = c2264a.f18836a;
            AbstractC2467b.l(objB);
        }
        Response response = (Response) objB;
        try {
            ResponseBody responseBody = response.f22146j;
            String strD = responseBody != null ? responseBody.d() : null;
            if (strD == null) {
                strD = "";
            }
            if (response.c()) {
                JSONArray jSONArray = new JSONArray(strD);
                response.close();
                return jSONArray;
            }
            throw new RuntimeException("GET " + str + " failed: HTTP " + response.f22143d + " " + strD);
        } finally {
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0015  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object K(String str, C4.c cVar) throws Exception {
        C2265B c2265b;
        if (cVar instanceof C2265B) {
            c2265b = (C2265B) cVar;
            int i6 = c2265b.f18843d;
            if ((i6 & Integer.MIN_VALUE) != 0) {
                c2265b.f18843d = i6 - Integer.MIN_VALUE;
            } else {
                c2265b = new C2265B(this, cVar);
            }
        }
        Object objB = c2265b.f18841b;
        Object obj = B4.a.f492a;
        int i7 = c2265b.f18843d;
        if (i7 == 0) {
            AbstractC2467b.l(objB);
            J4.c c2272f = new C2272f(str, this, 0);
            c2265b.f18840a = str;
            c2265b.f18843d = 1;
            objB = B(c2272f, false, true, c2265b);
            if (objB == obj) {
                return obj;
            }
        } else {
            if (i7 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            str = c2265b.f18840a;
            AbstractC2467b.l(objB);
        }
        Response response = (Response) objB;
        try {
            ResponseBody responseBody = response.f22146j;
            String strD = responseBody != null ? responseBody.d() : null;
            if (strD == null) {
                strD = "";
            }
            if (response.c()) {
                JSONObject jSONObject = new JSONObject(strD);
                response.close();
                return jSONObject;
            }
            throw new RuntimeException("GET " + str + " failed: HTTP " + response.f22143d + " " + strD);
        } finally {
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0015  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object P(C4.c cVar) throws Exception {
        C2266C c2266c;
        J j4;
        String str;
        if (cVar instanceof C2266C) {
            c2266c = (C2266C) cVar;
            int i6 = c2266c.f18848e;
            if ((i6 & Integer.MIN_VALUE) != 0) {
                c2266c.f18848e = i6 - Integer.MIN_VALUE;
            } else {
                c2266c = new C2266C(this, cVar);
            }
        }
        Object obj = c2266c.f18846c;
        B4.a aVar = B4.a.f492a;
        int i7 = c2266c.f18848e;
        if (i7 == 0) {
            AbstractC2467b.l(obj);
            String string = this.f18889a.f18895a.getString("refresh", null);
            if (string == null) {
                throw new Exception("No refresh token");
            }
            c2266c.f18844a = this;
            c2266c.f18845b = string;
            c2266c.f18848e = 1;
            if (O(true, c2266c) == aVar) {
                return aVar;
            }
            j4 = this;
            str = string;
        } else {
            if (i7 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            str = c2266c.f18845b;
            j4 = c2266c.f18844a;
            AbstractC2467b.l(obj);
        }
        JSONObject jSONObject = new JSONObject();
        jSONObject.put("refresh_token", str);
        j4.getClass();
        jSONObject.put("client_id", "cb6c23120401d06808b06c16c78d0e54e1953df0fce43d921280c708619c1b34");
        jSONObject.put("grant_type", "refresh_token");
        Request.Builder builder = new Request.Builder();
        builder.i("https://api.trakt.tv/oauth/token");
        builder.a("Content-Type", "application/json");
        String str2 = W.d.f8386g;
        if (str2 == null) {
            str2 = "DebridStream/3.0.0";
        }
        builder.a("User-Agent", str2);
        builder.h(x(jSONObject));
        Response responseD = f18888h.a(builder.b()).d();
        try {
            if (!responseD.c()) {
                throw new IOException("Refresh failed " + responseD.f22143d);
            }
            ResponseBody responseBody = responseD.f22146j;
            kotlin.jvm.internal.o.e(responseBody);
            JSONObject jSONObject2 = new JSONObject(responseBody.d());
            String string2 = jSONObject2.getString("access_token");
            String string3 = jSONObject2.getString("refresh_token");
            long j6 = jSONObject2.getLong("expires_in");
            K k6 = j4.f18889a;
            kotlin.jvm.internal.o.e(string2);
            kotlin.jvm.internal.o.e(string3);
            k6.b((j6 * ((long) 1000)) + System.currentTimeMillis(), string2, string3);
            responseD.close();
            return string2;
        } finally {
        }
    }
}
