package f3;

import E3.v;
import S4.p;
import V4.C;
import androidx.media3.common.MimeTypes;
import com.google.android.gms.common.internal.ImagesContract;
import com.revenuecat.purchases.common.Constants;
import com.revenuecat.purchases.ui.revenuecatui.components.SensibleDefaults;
import java.io.EOFException;
import java.util.Locale;
import kotlin.jvm.internal.o;
import m3.AbstractC2467b;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.json.JSONArray;
import org.json.JSONObject;
import w4.C3137A;
import w4.m;

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

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final /* synthetic */ W.d f16427c;

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

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

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

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public C2044i(String str, W.d dVar, Long l, String str2, String str3, A4.d dVar2) {
        super(2, dVar2);
        this.f16426b = str;
        this.f16427c = dVar;
        this.f16428d = l;
        this.f16429e = str2;
        this.f16430f = str3;
    }

    /* JADX WARN: Type inference failed for: r6v6, types: [java.lang.Object, java.util.Map] */
    public static final C2043h d(JSONArray jSONArray, String str) {
        String str2;
        if (str == null || p.Q0(str)) {
            return null;
        }
        int length = jSONArray.length();
        for (int i6 = 0; i6 < length; i6++) {
            JSONObject jSONObjectOptJSONObject = jSONArray.optJSONObject(i6);
            if (jSONObjectOptJSONObject != null) {
                String strOptString = jSONObjectOptJSONObject.optString("lang", "");
                OkHttpClient okHttpClient = AbstractC2045j.f16431a;
                o.e(strOptString);
                String string = p.o1(str).toString();
                Locale US = Locale.US;
                String strO = com.google.android.recaptcha.internal.a.o(US, "US", string, US, "toLowerCase(...)");
                if (!p.Q0(strO) && !strO.equals("none")) {
                    String string2 = p.o1(strOptString).toString();
                    o.g(US, "US");
                    String lowerCase = string2.toLowerCase(US);
                    o.g(lowerCase, "toLowerCase(...)");
                    if (lowerCase.length() != 2 && lowerCase.length() == 3 && (str2 = (String) AbstractC2045j.f16432b.get(lowerCase)) != null) {
                        lowerCase = str2;
                    }
                    String strJ1 = p.j1('-', strO, strO);
                    if (lowerCase.equals(strO) || lowerCase.equals(strJ1)) {
                        String strP = A0.a.p(jSONObjectOptJSONObject, ImagesContract.URL, "", "optString(...)");
                        if (!p.Q0(strP)) {
                            String strP2 = A0.a.p(jSONObjectOptJSONObject, "id", "", "optString(...)");
                            if (p.Q0(strP2)) {
                                strP2 = Z0.o.l("stremio-", i6);
                            }
                            return new C2043h(strP2, strP);
                        }
                    }
                }
            }
        }
        return null;
    }

    @Override // C4.a
    public final A4.d create(Object obj, A4.d dVar) {
        C2044i c2044i = new C2044i(this.f16426b, this.f16427c, this.f16428d, this.f16429e, this.f16430f, dVar);
        c2044i.f16425a = obj;
        return c2044i;
    }

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

    /* JADX WARN: Removed duplicated region for block: B:63:0x0129  */
    /* JADX WARN: Removed duplicated region for block: B:74:? A[RETURN, SYNTHETIC] */
    @Override // C4.a
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object invokeSuspend(Object obj) throws EOFException {
        String strN;
        HttpUrl httpUrlA;
        Object objD;
        Response responseD;
        String strD;
        JSONArray jSONArrayOptJSONArray;
        B4.a aVar = B4.a.f492a;
        AbstractC2467b.l(obj);
        String string = p.o1(this.f16426b).toString();
        boolean zQ0 = p.Q0(string);
        String str = MimeTypes.BASE_TYPE_VIDEO;
        if (zQ0) {
            string = MimeTypes.BASE_TYPE_VIDEO;
        }
        HttpUrl.Companion companion = HttpUrl.f22023k;
        OkHttpClient okHttpClient = AbstractC2045j.f16431a;
        String string2 = p.o1(string).toString();
        if (!p.Q0(string2)) {
            str = string2;
        }
        String strM1 = p.m1(SensibleDefaults.X_OFFSET, str);
        Long l = this.f16428d;
        long j4 = 0;
        if (l != null) {
            long jLongValue = l.longValue();
            if (jLongValue >= 0) {
                j4 = jLongValue;
            }
        }
        W.d dVar = this.f16427c;
        if (dVar instanceof C2042g) {
            C2042g c2042g = (C2042g) dVar;
            StringBuilder sb = new StringBuilder("https://opensubtitles-v3.strem.io/subtitles/series/");
            sb.append(c2042g.f16420h);
            sb.append(Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR);
            sb.append(c2042g.f16421i);
            sb.append(Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR);
            sb.append(c2042g.f16422j);
            sb.append("/filename=");
            sb.append(strM1);
            sb.append("&videoSize=");
            strN = Z0.o.k(j4, ".json", sb);
        } else {
            if (!(dVar instanceof C2041f)) {
                throw new v();
            }
            C2041f c2041f = (C2041f) dVar;
            StringBuilder sb2 = new StringBuilder("https://opensubtitles-v3.strem.io/subtitles/movie/");
            A0.a.w(sb2, c2041f.f16418h, "/filename=", strM1, "&videoSize=");
            sb2.append(j4);
            sb2.append("&videoHash=");
            strN = com.google.android.recaptcha.internal.a.n(sb2, c2041f.f16419i, ".json");
        }
        companion.getClass();
        o.h(strN, "<this>");
        try {
            HttpUrl.Builder builder = new HttpUrl.Builder();
            builder.b(null, strN);
            httpUrlA = builder.a();
        } catch (IllegalArgumentException unused) {
            httpUrlA = null;
        }
        if (httpUrlA != null) {
            Request.Builder builder2 = new Request.Builder();
            builder2.f22131a = httpUrlA;
            builder2.d();
            Request requestB = builder2.b();
            String str2 = this.f16429e;
            String str3 = this.f16430f;
            try {
                responseD = AbstractC2045j.f16431a.a(requestB).d();
                try {
                    ResponseBody responseBody = responseD.f22146j;
                    strD = responseBody != null ? responseBody.d() : null;
                    if (strD == null) {
                        strD = "";
                    }
                } finally {
                }
            } catch (Throwable th) {
                objD = AbstractC2467b.d(th);
            }
            if (responseD.c() && !p.Q0(strD) && (jSONArrayOptJSONArray = new JSONObject(strD).optJSONArray("subtitles")) != null) {
                objD = d(jSONArrayOptJSONArray, str2);
                if (objD == null) {
                    objD = d(jSONArrayOptJSONArray, str3);
                }
                responseD.close();
                if (objD instanceof m) {
                    return objD;
                }
                return null;
            }
            responseD.close();
            objD = AbstractC2467b.d(th);
            if (objD instanceof m) {
            }
        }
        return null;
    }
}
