package Y2;

import V4.C;
import android.os.SystemClock;
import com.google.firebase.sessions.settings.RemoteSettings;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import m3.AbstractC2467b;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.json.JSONArray;
import org.json.JSONObject;
import q.AbstractC2761L;
import w4.C3137A;
import x4.y;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public /* synthetic */ Object f8758a;

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

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

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

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

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

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

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public k(String str, int i6, boolean z6, int i7, boolean z7, o oVar, A4.d dVar) {
        super(2, dVar);
        this.f8759b = str;
        this.f8760c = i6;
        this.f8761d = z6;
        this.f8762e = i7;
        this.f8763f = z7;
        this.f8764j = oVar;
    }

    @Override // C4.a
    public final A4.d create(Object obj, A4.d dVar) {
        k kVar = new k(this.f8759b, this.f8760c, this.f8761d, this.f8762e, this.f8763f, this.f8764j, dVar);
        kVar.f8758a = obj;
        return kVar;
    }

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

    @Override // C4.a
    public final Object invokeSuspend(Object obj) throws IOException {
        Object objD;
        B4.a aVar = B4.a.f492a;
        AbstractC2467b.l(obj);
        String string = S4.p.o1(this.f8759b).toString();
        boolean zQ0 = S4.p.Q0(string);
        y yVar = y.f26065a;
        if (zQ0) {
            return q.c(new t(this.f8760c, yVar, false), this.f8761d, null, null);
        }
        if (this.f8760c < 1) {
            throw new IllegalArgumentException("Trakt page must be >= 1");
        }
        int i6 = this.f8762e;
        if (1 > i6 || i6 >= 101) {
            throw new IllegalArgumentException("Trakt limit must be 1..100");
        }
        String str = this.f8763f ? "shows" : "movies";
        String strEncode = URLEncoder.encode(string, StandardCharsets.UTF_8.name());
        int i7 = this.f8760c;
        int i8 = this.f8762e;
        StringBuilder sbI = AbstractC2761L.i("https://api.trakt.tv/", str, RemoteSettings.FORWARD_SLASH_STRING, strEncode, "/related?extended=full,images&page=");
        sbI.append(i7);
        sbI.append("&limit=");
        sbI.append(i8);
        String string2 = sbI.toString();
        Request.Builder builder = new Request.Builder();
        builder.i(string2);
        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-key", "cb6c23120401d06808b06c16c78d0e54e1953df0fce43d921280c708619c1b34");
        builder.a("trakt-api-version", "2");
        Request requestB = builder.b();
        SystemClock.elapsedRealtime();
        Response responseD = this.f8764j.f8779a.a(requestB).d();
        int i9 = this.f8760c;
        boolean z6 = this.f8761d;
        o oVar = this.f8764j;
        boolean z7 = this.f8763f;
        int i10 = this.f8762e;
        try {
            SystemClock.elapsedRealtime();
            if (!responseD.c()) {
                t tVarC = q.c(new t(i9, yVar, false), z6, null, null);
                responseD.close();
                return tVarC;
            }
            SystemClock.elapsedRealtime();
            ResponseBody responseBody = responseD.f22146j;
            String strD = responseBody != null ? responseBody.d() : "";
            SystemClock.elapsedRealtime();
            SystemClock.elapsedRealtime();
            try {
                objD = new JSONArray(strD);
            } catch (Throwable th) {
                objD = AbstractC2467b.d(th);
            }
            if (w4.n.a(objD) != null) {
                SystemClock.elapsedRealtime();
                t tVarC2 = q.c(new t(i9, yVar, false), z6, null, null);
                responseD.close();
                return tVarC2;
            }
            JSONArray jSONArray = (JSONArray) objD;
            ArrayList arrayList = new ArrayList(jSONArray.length());
            int length = jSONArray.length();
            for (int i11 = 0; i11 < length; i11++) {
                JSONObject jSONObjectOptJSONObject = jSONArray.optJSONObject(i11);
                if (jSONObjectOptJSONObject != null) {
                    String str3 = z7 ? "tv" : "movie";
                    oVar.getClass();
                    arrayList.add(o.s(jSONObjectOptJSONObject, str3));
                }
            }
            SystemClock.elapsedRealtime();
            boolean zB = q.b(responseD, i10, arrayList.size());
            SystemClock.elapsedRealtime();
            t tVarC3 = q.c(new t(i9, arrayList, zB), z6, null, null);
            responseD.close();
            return tVarC3;
        } finally {
        }
    }
}
