package f3;

import S4.p;
import V4.C;
import java.io.EOFException;
import java.io.IOException;
import kotlin.jvm.internal.o;
import m3.AbstractC2467b;
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.JSONObject;
import w4.C3137A;

/* JADX INFO: renamed from: f3.b, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C2037b extends C4.j implements J4.e {

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

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

    public static final Response d(RequestBody$Companion$toRequestBody$2 requestBody$Companion$toRequestBody$2, int i6) throws EOFException {
        OkHttpClient okHttpClient = C2039d.f16416a;
        Request.Builder builder = new Request.Builder();
        builder.i("https://api.opensubtitles.com/api/v1/download");
        builder.f(C2039d.a(i6).e());
        builder.h(requestBody$Companion$toRequestBody$2);
        return okHttpClient.a(builder.b()).d();
    }

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

    @Override // J4.e
    public final Object invoke(Object obj, Object obj2) {
        return ((C2037b) 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 string = new JSONObject().put("file_id", this.f16412a).toString();
        o.g(string, "toString(...)");
        RequestBody.Companion companion = RequestBody.f22136a;
        MediaType.f22043d.getClass();
        MediaType mediaTypeA = MediaType.Companion.a("application/json");
        companion.getClass();
        RequestBody$Companion$toRequestBody$2 requestBody$Companion$toRequestBody$2A = RequestBody.Companion.a(string, mediaTypeA);
        Response responseD = d(requestBody$Companion$toRequestBody$2A, 0);
        try {
            String str = null;
            if (responseD.c()) {
                ResponseBody responseBody = responseD.f22146j;
                if (responseBody != null) {
                    String strOptString = new JSONObject(responseBody.d()).optString("link", "");
                    o.e(strOptString);
                    if (!p.Q0(strOptString)) {
                        str = strOptString;
                    }
                }
                responseD.close();
                return str;
            }
            int i6 = responseD.f22143d;
            if (i6 != 406 && i6 != 429) {
                responseD.close();
                return null;
            }
            responseD.close();
            int size = AbstractC2040e.f16417a.size();
            for (int i7 = 1; i7 < size; i7++) {
                responseD = d(requestBody$Companion$toRequestBody$2A, i7);
                try {
                    if (responseD.c()) {
                        ResponseBody responseBody2 = responseD.f22146j;
                        if (responseBody2 != null) {
                            String strOptString2 = new JSONObject(responseBody2.d()).optString("link", "");
                            o.e(strOptString2);
                            if (!p.Q0(strOptString2)) {
                                str = strOptString2;
                            }
                        }
                        responseD.close();
                        return str;
                    }
                    responseD.close();
                } finally {
                }
            }
            return null;
        } finally {
            try {
                throw th;
            } finally {
            }
        }
    }
}
