package Y2;

import V4.C;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Set;
import m3.AbstractC2467b;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import w4.C3137A;
import x4.AbstractC3250p;
import x4.y;

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

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

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

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public n(String str, o oVar, A4.d dVar) {
        super(2, dVar);
        this.f8775a = str;
        this.f8776b = oVar;
    }

    @Override // C4.a
    public final A4.d create(Object obj, A4.d dVar) {
        return new n(this.f8775a, this.f8776b, dVar);
    }

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

    @Override // C4.a
    public final Object invokeSuspend(Object obj) throws IOException {
        List listF;
        B4.a aVar = B4.a.f492a;
        AbstractC2467b.l(obj);
        String string = S4.p.o1(this.f8775a).toString();
        int length = string.length();
        List listF2 = y.f26065a;
        if (length < 2) {
            return new w4.k(listF2, listF2);
        }
        String strEncode = URLEncoder.encode(string, StandardCharsets.UTF_8.name());
        String strO = Z0.o.o("https://api.trakt.tv/search/movie?query=", strEncode, "&extended=full,images");
        String strO2 = Z0.o.o("https://api.trakt.tv/search/show?query=", strEncode, "&extended=full,images");
        Request.Builder builder = new Request.Builder();
        builder.i(strO);
        builder.a("Content-Type", "application/json");
        String str = W.d.f8386g;
        if (str == null) {
            str = "DebridStream/3.0.0";
        }
        builder.a("User-Agent", str);
        builder.a("trakt-api-key", "cb6c23120401d06808b06c16c78d0e54e1953df0fce43d921280c708619c1b34");
        builder.a("trakt-api-version", "2");
        Request requestB = builder.b();
        Request.Builder builder2 = new Request.Builder();
        builder2.i(strO2);
        builder2.a("Content-Type", "application/json");
        String str2 = W.d.f8386g;
        builder2.a("User-Agent", str2 != null ? str2 : "DebridStream/3.0.0");
        builder2.a("trakt-api-key", "cb6c23120401d06808b06c16c78d0e54e1953df0fce43d921280c708619c1b34");
        builder2.a("trakt-api-version", "2");
        Request requestB2 = builder2.b();
        Set setY = AbstractC3250p.Y(new String[]{"g", "pg", "pg-13"});
        Set setY2 = AbstractC3250p.Y(new String[]{"tv-y", "tv-y7", "tv-g", "tv-pg"});
        Response responseD = this.f8776b.f8779a.a(requestB).d();
        o oVar = this.f8776b;
        try {
            String str3 = "";
            if (responseD.c()) {
                ResponseBody responseBody = responseD.f22146j;
                String strD = responseBody != null ? responseBody.d() : null;
                if (strD == null) {
                    strD = "";
                }
                listF = o.f(oVar, strD, "movie", setY);
            } else {
                listF = listF2;
            }
            responseD.close();
            responseD = this.f8776b.f8779a.a(requestB2).d();
            o oVar2 = this.f8776b;
            try {
                if (responseD.c()) {
                    ResponseBody responseBody2 = responseD.f22146j;
                    String strD2 = responseBody2 != null ? responseBody2.d() : null;
                    if (strD2 != null) {
                        str3 = strD2;
                    }
                    listF2 = o.f(oVar2, str3, "tv", setY2);
                }
                responseD.close();
                return new w4.k(listF, listF2);
            } finally {
            }
        } finally {
            try {
                throw th;
            } finally {
            }
        }
    }
}
