package j3;

import S4.w;
import android.os.Build;
import androidx.media3.extractor.text.ttml.TtmlNode;
import com.revenuecat.purchases.common.diagnostics.DiagnosticsEntry;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import k3.C2381a;
import okhttp3.OkHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import x4.AbstractC3250p;
import x4.AbstractC3251q;
import x4.y;

/* JADX INFO: renamed from: j3.e, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C2353e {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final OkHttpClient f19561a;

    public C2353e() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        TimeUnit timeUnit = TimeUnit.SECONDS;
        builder.c(20L);
        builder.d(60L);
        this.f19561a = new OkHttpClient(builder);
    }

    public static C2381a a(JSONObject jSONObject) {
        Object next;
        Object next2;
        C2352d c2352d;
        Object next3;
        if (jSONObject.optBoolean("draft") || jSONObject.optBoolean("prerelease")) {
            throw new IllegalStateException("Latest release is draft/prerelease");
        }
        String strOptString = jSONObject.optString("tag_name");
        kotlin.jvm.internal.o.e(strOptString);
        if (S4.p.Q0(strOptString)) {
            strOptString = null;
        }
        if (strOptString == null) {
            strOptString = jSONObject.optString(DiagnosticsEntry.NAME_KEY);
            kotlin.jvm.internal.o.e(strOptString);
            if (S4.p.Q0(strOptString)) {
                strOptString = null;
            }
            if (strOptString == null) {
                throw new IllegalStateException("Release has no tag/name");
            }
        }
        JSONArray jSONArrayOptJSONArray = jSONObject.optJSONArray("assets");
        if (jSONArrayOptJSONArray == null) {
            jSONArrayOptJSONArray = new JSONArray();
        }
        List list = AbstractC2349a.f19554a;
        ArrayList arrayList = new ArrayList();
        int length = jSONArrayOptJSONArray.length();
        for (int i6 = 0; i6 < length; i6++) {
            JSONObject jSONObjectOptJSONObject = jSONArrayOptJSONArray.optJSONObject(i6);
            if (jSONObjectOptJSONObject != null) {
                String strOptString2 = jSONObjectOptJSONObject.optString(DiagnosticsEntry.NAME_KEY);
                kotlin.jvm.internal.o.e(strOptString2);
                if (w.s0(strOptString2, ".apk", true)) {
                    String strOptString3 = jSONObjectOptJSONObject.optString("browser_download_url");
                    kotlin.jvm.internal.o.e(strOptString3);
                    if (!S4.p.Q0(strOptString3)) {
                        long jOptLong = jSONObjectOptJSONObject.optLong("size");
                        Long lValueOf = Long.valueOf(jOptLong);
                        if (jOptLong <= 0) {
                            lValueOf = null;
                        }
                        arrayList.add(new C2352d(strOptString2, strOptString3, lValueOf));
                    }
                }
            }
        }
        if (arrayList.isEmpty()) {
            c2352d = null;
        } else if (arrayList.size() == 1) {
            c2352d = (C2352d) AbstractC3251q.e0(arrayList);
        } else {
            String[] strArr = Build.SUPPORTED_ABIS;
            List listX = strArr != null ? AbstractC3250p.X(strArr) : null;
            if (listX == null) {
                listX = y.f26065a;
            }
            Iterator it = listX.iterator();
            while (true) {
                if (it.hasNext()) {
                    String str = (String) it.next();
                    Iterator it2 = arrayList.iterator();
                    while (true) {
                        if (!it2.hasNext()) {
                            next3 = null;
                            break;
                        }
                        next3 = it2.next();
                        String str2 = ((C2352d) next3).f19558a;
                        kotlin.jvm.internal.o.e(str);
                        if (S4.p.D0(str2, str, true)) {
                            break;
                        }
                    }
                    C2352d c2352d2 = (C2352d) next3;
                    if (c2352d2 != null) {
                        c2352d = c2352d2;
                        break;
                    }
                } else {
                    Iterator it3 = arrayList.iterator();
                    while (true) {
                        if (!it3.hasNext()) {
                            next = null;
                            break;
                        }
                        next = it3.next();
                        String lowerCase = ((C2352d) next).f19558a.toLowerCase(Locale.ROOT);
                        kotlin.jvm.internal.o.g(lowerCase, "toLowerCase(...)");
                        if (S4.p.D0(lowerCase, "universal", false) || S4.p.D0(lowerCase, TtmlNode.COMBINE_ALL, false)) {
                            break;
                        }
                    }
                    C2352d c2352d3 = (C2352d) next;
                    if (c2352d3 != null) {
                        c2352d = c2352d3;
                    } else {
                        Iterator it4 = arrayList.iterator();
                        loop4: while (true) {
                            if (!it4.hasNext()) {
                                next2 = null;
                                break;
                            }
                            next2 = it4.next();
                            C2352d c2352d4 = (C2352d) next2;
                            List list2 = AbstractC2349a.f19554a;
                            if (list2 != null && list2.isEmpty()) {
                                break;
                            }
                            Iterator it5 = list2.iterator();
                            while (it5.hasNext()) {
                                if (S4.p.D0(c2352d4.f19558a, (String) it5.next(), true)) {
                                    break;
                                }
                            }
                            break loop4;
                        }
                        c2352d = (C2352d) next2;
                        if (c2352d == null) {
                            c2352d = (C2352d) AbstractC3251q.e0(arrayList);
                        }
                    }
                }
            }
        }
        if (c2352d == null) {
            throw new IllegalStateException("No APK asset found in release");
        }
        String strOptString4 = jSONObject.optString(DiagnosticsEntry.NAME_KEY);
        kotlin.jvm.internal.o.e(strOptString4);
        if (S4.p.Q0(strOptString4)) {
            strOptString4 = null;
        }
        String str3 = strOptString4 == null ? strOptString : strOptString4;
        String strOptString5 = jSONObject.optString(TtmlNode.TAG_BODY);
        kotlin.jvm.internal.o.g(strOptString5, "optString(...)");
        String strOptString6 = jSONObject.optString("html_url");
        kotlin.jvm.internal.o.e(strOptString6);
        return new C2381a(c2352d.f19560c, strOptString, str3, strOptString5, !S4.p.Q0(strOptString6) ? strOptString6 : null, c2352d.f19558a, c2352d.f19559b);
    }
}
