package X2;

import S4.p;
import V4.C;
import com.google.firebase.sessions.settings.RemoteSettings;
import java.io.IOException;
import java.util.Locale;
import kotlin.jvm.internal.o;
import m1.AbstractC2464a;
import m3.AbstractC2467b;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.json.JSONObject;
import w4.C3137A;
import x4.y;

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

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

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

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

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

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public c(int i6, A4.d dVar, f fVar, String str, String str2) {
        super(2, dVar);
        this.f8562a = str;
        this.f8563b = fVar;
        this.f8564c = i6;
        this.f8565d = str2;
    }

    @Override // C4.a
    public final A4.d create(Object obj, A4.d dVar) {
        int i6 = this.f8564c;
        String str = this.f8565d;
        return new c(i6, dVar, this.f8563b, this.f8562a, str);
    }

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

    @Override // C4.a
    public final Object invokeSuspend(Object obj) throws IOException {
        B4.a aVar = B4.a.f492a;
        AbstractC2467b.l(obj);
        String str = this.f8562a;
        if (!o.c(str, "movie") && !o.c(str, "tv")) {
            throw new IllegalArgumentException("type must be movie or tv");
        }
        String strQ = Z0.o.q(AbstractC2464a.b(str, RemoteSettings.FORWARD_SLASH_STRING), this.f8564c, "/watch/providers");
        f fVar = this.f8563b;
        fVar.getClass();
        String str2 = "https://api.themoviedb.org/3/" + strQ + (p.D0(strQ, "?", false) ? "&" : "?") + "api_key=" + fVar.f8573b;
        Request.Builder builder = new Request.Builder();
        builder.i(str2);
        Response responseD = fVar.f8572a.a(builder.b()).d();
        try {
            if (!responseD.c()) {
                throw new Exception("TMDB " + responseD.f22143d);
            }
            ResponseBody responseBody = responseD.f22146j;
            if (responseBody == null) {
                throw new Exception("Empty TMDB response");
            }
            JSONObject jSONObject = new JSONObject(responseBody.d());
            responseD.close();
            JSONObject jSONObjectOptJSONObject = jSONObject.optJSONObject("results");
            y yVar = y.f26065a;
            if (jSONObjectOptJSONObject == null) {
                return new l(yVar, yVar, yVar);
            }
            String upperCase = p.o1(this.f8565d).toString().toUpperCase(Locale.ROOT);
            o.g(upperCase, "toUpperCase(...)");
            if (p.Q0(upperCase)) {
                upperCase = "US";
            }
            JSONObject jSONObjectOptJSONObject2 = jSONObjectOptJSONObject.optJSONObject(upperCase);
            return jSONObjectOptJSONObject2 == null ? new l(yVar, yVar, yVar) : new l(f.d(fVar, jSONObjectOptJSONObject2.optJSONArray("flatrate")), f.d(fVar, jSONObjectOptJSONObject2.optJSONArray("rent")), f.d(fVar, jSONObjectOptJSONObject2.optJSONArray("buy")));
        } finally {
        }
    }
}
