package N2;

import V4.C0781t;
import V4.E;
import Y4.J;
import Y4.O;
import Y4.c0;
import android.content.Context;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import m3.AbstractC2467b;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.json.JSONArray;
import org.json.JSONObject;
import w4.C3137A;

/* JADX INFO: loaded from: classes.dex */
public abstract class c {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static volatile ArrayList f5134a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static volatile ArrayList f5135b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static volatile boolean f5136c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final C0781t f5137d = E.b();

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final c0 f5138e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final J f5139f;

    static {
        c0 c0VarC = O.c(null);
        f5138e = c0VarC;
        f5139f = new J(c0VarC);
    }

    public static boolean a(String str) {
        Object objD;
        Object objD2;
        try {
            objD = c(str);
        } catch (Throwable th) {
            objD = AbstractC2467b.d(th);
        }
        if (objD instanceof w4.m) {
            objD = null;
        }
        a aVar = (a) objD;
        if (aVar == null) {
            return false;
        }
        f5136c = aVar.f5131c;
        f5138e.j(aVar.f5132d);
        if (!aVar.f5129a.isEmpty()) {
            f5134a = aVar.f5129a;
        }
        if (aVar.f5130b.isEmpty()) {
            return true;
        }
        ArrayList arrayList = aVar.f5130b;
        ArrayList arrayList2 = new ArrayList(arrayList.size());
        Iterator it = arrayList.iterator();
        while (it.hasNext()) {
            String string = S4.p.o1((String) it.next()).toString();
            if (string.length() != 0) {
                try {
                    S4.n[] nVarArr = S4.n.f6290a;
                    objD2 = new S4.m(string, 0);
                } catch (Throwable th2) {
                    objD2 = AbstractC2467b.d(th2);
                }
                if (objD2 instanceof w4.m) {
                    objD2 = null;
                }
                S4.m mVar = (S4.m) objD2;
                if (mVar != null) {
                    arrayList2.add(mVar);
                }
            }
        }
        f5135b = arrayList2;
        return true;
    }

    public static void b() {
        boolean zE;
        C3137A c3137a = C3137A.f25453a;
        try {
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            TimeUnit timeUnit = TimeUnit.SECONDS;
            builder.b(15L);
            OkHttpClient okHttpClient = new OkHttpClient(builder);
            Request.Builder builder2 = new Request.Builder();
            builder2.i("https://raw.githubusercontent.com/lawdev-cmd/map/refs/heads/main/config.json");
            builder2.d();
            Response responseD = okHttpClient.a(builder2.b()).d();
            try {
                if (!responseD.c()) {
                    responseD.close();
                    if (zE) {
                        return;
                    } else {
                        return;
                    }
                }
                ResponseBody responseBody = responseD.f22146j;
                String string = responseBody != null ? S4.p.o1(responseBody.d()).toString() : null;
                if (string == null) {
                    string = "";
                }
                if (string.length() == 0) {
                    responseD.close();
                    C0781t c0781t = f5137d;
                    if (c0781t.E()) {
                        return;
                    }
                    c0781t.G(c3137a);
                    return;
                }
                if (!a(string)) {
                    responseD.close();
                    C0781t c0781t2 = f5137d;
                    if (c0781t2.E()) {
                        return;
                    }
                    c0781t2.G(c3137a);
                    return;
                }
                Context context = W.c.f8375h;
                if (context != null) {
                    String string2 = S4.p.o1(string).toString();
                    if (string2.length() != 0) {
                        context.getSharedPreferences("debridstream_remote_map", 0).edit().putString("https://raw.githubusercontent.com/lawdev-cmd/map/refs/heads/main/config.json", string2).apply();
                    }
                }
                responseD.close();
                C0781t c0781t3 = f5137d;
                if (c0781t3.E()) {
                    return;
                }
                c0781t3.G(c3137a);
                return;
            } finally {
            }
        } finally {
        }
        C0781t c0781t4 = f5137d;
        if (!c0781t4.E()) {
            c0781t4.G(c3137a);
        }
    }

    public static a c(String str) {
        JSONObject jSONObject = new JSONObject(str);
        boolean zOptBoolean = jSONObject.optBoolean("tmdbTurnOffGzip", false);
        String strP = A0.a.p(jSONObject, "emergencyMessage", "", "optString(...)");
        if (strP.length() <= 0) {
            strP = null;
        }
        JSONObject jSONObjectOptJSONObject = jSONObject.optJSONObject("filters");
        JSONArray jSONArrayOptJSONArray = jSONObjectOptJSONObject != null ? jSONObjectOptJSONObject.optJSONArray("RD") : null;
        ArrayList arrayList = new ArrayList(jSONArrayOptJSONArray != null ? jSONArrayOptJSONArray.length() : 0);
        if (jSONArrayOptJSONArray != null) {
            int length = jSONArrayOptJSONArray.length();
            for (int i6 = 0; i6 < length; i6++) {
                String strOptString = jSONArrayOptJSONArray.optString(i6, "");
                kotlin.jvm.internal.o.g(strOptString, "optString(...)");
                String lowerCase = S4.p.o1(strOptString).toString().toLowerCase(Locale.ROOT);
                kotlin.jvm.internal.o.g(lowerCase, "toLowerCase(...)");
                if (lowerCase.length() > 0) {
                    arrayList.add(lowerCase);
                }
            }
        }
        JSONArray jSONArrayOptJSONArray2 = jSONObjectOptJSONObject != null ? jSONObjectOptJSONObject.optJSONArray("ALL") : null;
        ArrayList arrayList2 = new ArrayList(jSONArrayOptJSONArray2 != null ? jSONArrayOptJSONArray2.length() : 0);
        if (jSONArrayOptJSONArray2 != null) {
            int length2 = jSONArrayOptJSONArray2.length();
            for (int i7 = 0; i7 < length2; i7++) {
                String strOptString2 = jSONArrayOptJSONArray2.optString(i7, "");
                kotlin.jvm.internal.o.g(strOptString2, "optString(...)");
                String string = S4.p.o1(strOptString2).toString();
                if (string.length() > 0) {
                    arrayList2.add(string);
                }
            }
        }
        return new a(arrayList, arrayList2, zOptBoolean, strP);
    }

    public static boolean d(String str) {
        ArrayList arrayList = f5134a;
        if (arrayList != null && !arrayList.isEmpty() && str != null && !S4.p.Q0(str)) {
            String lowerCase = str.toLowerCase(Locale.ROOT);
            kotlin.jvm.internal.o.g(lowerCase, "toLowerCase(...)");
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                if (S4.p.D0(lowerCase, (String) it.next(), false)) {
                    return true;
                }
            }
        }
        return false;
    }
}
