package V2;

import com.google.android.gms.common.internal.ImagesContract;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.Source;
import com.google.firebase.sessions.settings.RemoteSettings;
import com.revenuecat.purchases.common.diagnostics.DiagnosticsEntry;
import com.revenuecat.purchases.subscriberattributes.SubscriberAttributeKt;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import m3.AbstractC2467b;
import n2.AbstractC2612c;
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.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import w4.C3137A;
import x4.AbstractC3251q;
import x4.AbstractC3252r;

/* JADX INFO: loaded from: classes.dex */
public final class L3 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final L3 f7428a = new L3();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final w4.p f7429b = m1.g.k(new B2.f(20));

    public static final ArrayList a(List list) {
        Object objD;
        ArrayList arrayList = new ArrayList();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            try {
                w4.k kVarR = r(m((String) it.next()));
                objD = Boolean.valueOf(arrayList.addAll(k((List) kVarR.f25469a, (List) kVarR.f25470b)));
            } catch (Throwable th) {
                objD = AbstractC2467b.d(th);
            }
            w4.n.a(objD);
        }
        return arrayList;
    }

    public static final List b(DocumentSnapshot documentSnapshot) {
        Map<String, Object> data = documentSnapshot.getData();
        if (data == null) {
            Object obj = documentSnapshot.get("addOns");
            if (obj == null) {
                obj = documentSnapshot.get("addons");
            }
            return AbstractC3251q.Z(o(obj));
        }
        for (String str : AbstractC3252r.B("addOns", "addons", "addonUrls", "addon_urls", "AddOns")) {
            if (data.containsKey(str)) {
                List listO = o(data.get(str));
                if (!listO.isEmpty()) {
                    return AbstractC3251q.Z(listO);
                }
            }
        }
        LinkedHashSet linkedHashSet = new LinkedHashSet();
        l(data, linkedHashSet);
        List listC0 = AbstractC3251q.C0(linkedHashSet);
        return !listC0.isEmpty() ? listC0 : x4.y.f26065a;
    }

    public static final void c(List urls, List addons) {
        Y4.c0 c0Var = C3.f7279a;
        kotlin.jvm.internal.o.h(urls, "urls");
        Y4.c0 c0Var2 = C3.f7283e;
        c0Var2.getClass();
        c0Var2.k(null, urls);
        kotlin.jvm.internal.o.h(addons, "addons");
        Y4.c0 c0Var3 = C3.f7279a;
        c0Var3.getClass();
        c0Var3.k(null, urls);
        Y4.c0 c0Var4 = C3.f7281c;
        c0Var4.getClass();
        c0Var4.k(null, addons);
    }

    public static final void d(List list, List list2) {
        char c6 = 0;
        if (list.isEmpty() && list2.isEmpty()) {
            throw new IllegalArgumentException("The URL does not contain valid DebridStream add-ons");
        }
        Iterator it = list.iterator();
        while (it.hasNext()) {
            JSONObject jSONObject = (JSONObject) it.next();
            String strP = A0.a.p(jSONObject, DiagnosticsEntry.NAME_KEY, "", "optString(...)");
            if (strP.length() <= 0) {
                throw new IllegalArgumentException("Each add-on must include a non-empty name");
            }
            String strOptString = jSONObject.optString("type", "");
            kotlin.jvm.internal.o.g(strOptString, "optString(...)");
            if (S4.p.o1(strOptString).toString().length() <= 0) {
                throw new IllegalArgumentException(Z0.o.o("Add-on \"", strP, "\" must include a type (e.g. json_imdb)").toString());
            }
            String strP2 = A0.a.p(jSONObject, "itemType", "", "optString(...)");
            if (strP2.length() <= 0) {
                throw new IllegalArgumentException(Z0.o.o("Add-on \"", strP, "\" must include itemType (e.g. info_hash or http_url)").toString());
            }
            String strP3 = A0.a.p(jSONObject, ImagesContract.URL, "", "optString(...)");
            if (!S4.w.A0(strP3, "https://", true)) {
                throw new IllegalArgumentException(Z0.o.o("Add-on \"", strP, "\" must use an https:// stream template url").toString());
            }
            if (!S4.p.D0(strP3, "%searchPattern", false)) {
                throw new IllegalArgumentException(Z0.o.o("Add-on \"", strP, "\" url must contain the %searchPattern placeholder").toString());
            }
            v(jSONObject);
            String strOptString2 = jSONObject.optString("jsonResultsKey", "streams");
            kotlin.jvm.internal.o.g(strOptString2, "optString(...)");
            if (S4.p.o1(strOptString2).toString().length() <= 0) {
                throw new IllegalArgumentException(Z0.o.o("Add-on \"", strP, "\" must set a non-empty jsonResultsKey").toString());
            }
            if (strP2.equals("http_url")) {
                String strOptString3 = jSONObject.optString("httpUrlTargetKey", "");
                kotlin.jvm.internal.o.g(strOptString3, "optString(...)");
                if (S4.p.o1(strOptString3).toString().length() <= 0) {
                    throw new IllegalArgumentException(Z0.o.o("Add-on \"", strP, "\" with itemType http_url must include httpUrlTargetKey").toString());
                }
            }
        }
        Iterator it2 = list2.iterator();
        while (it2.hasNext()) {
            JSONObject jSONObject2 = (JSONObject) it2.next();
            if (!kotlin.jvm.internal.o.c(jSONObject2.optString("type", ""), "reg_hash")) {
                throw new IllegalArgumentException("Registered add-on entries must set type to reg_hash");
            }
            String strP4 = A0.a.p(jSONObject2, DiagnosticsEntry.NAME_KEY, "", "optString(...)");
            if (strP4.length() <= 0) {
                throw new IllegalArgumentException("Each reg_hash add-on must include a non-empty name");
            }
            String strP5 = A0.a.p(jSONObject2, ImagesContract.URL, "", "optString(...)");
            char c7 = c6;
            char[] cArr = new char[1];
            cArr[c7] = '/';
            if (!S4.w.A0(S4.p.q1(strP5, cArr), "https://", true)) {
                throw new IllegalArgumentException(Z0.o.o("Add-on \"", strP4, "\" must use an https:// base url for registration").toString());
            }
            String strOptString4 = jSONObject2.optString("mode", "");
            kotlin.jvm.internal.o.g(strOptString4, "optString(...)");
            if (S4.p.o1(strOptString4).toString().length() <= 0) {
                throw new IllegalArgumentException(Z0.o.o("Add-on \"", strP4, "\" (reg_hash) must include mode (e.g. encrypt-user-data)").toString());
            }
            v(jSONObject2);
            String strOptString5 = jSONObject2.optString("itemType", "");
            kotlin.jvm.internal.o.g(strOptString5, "optString(...)");
            String string = S4.p.o1(strOptString5).toString();
            if (string.length() <= 0) {
                throw new IllegalArgumentException(Z0.o.o("Add-on \"", strP4, "\" (reg_hash) must include itemType").toString());
            }
            if (string.equals("http_url")) {
                String strOptString6 = jSONObject2.optString("httpUrlTargetKey", "");
                kotlin.jvm.internal.o.g(strOptString6, "optString(...)");
                if (S4.p.o1(strOptString6).toString().length() <= 0) {
                    throw new IllegalArgumentException(Z0.o.o("Add-on \"", strP4, "\" (reg_hash) with itemType http_url must include httpUrlTargetKey").toString());
                }
            }
            c6 = c7;
        }
    }

    public static JSONObject f(String str, String str2) throws JSONException {
        JSONObject jSONObjectG = g(str, str2);
        JSONObject jSONObject = new JSONObject();
        jSONObject.put("streaming_providers", new JSONArray().put(jSONObjectG));
        jSONObject.put("streaming_provider", g(str, str2));
        jSONObject.put("selected_catalogs", new JSONArray());
        jSONObject.put("selected_resolutions", n(AbstractC3252r.B("4k", "2160p", "1440p", "1080p", "720p", "576p", "480p", "360p", "240p", null)));
        jSONObject.put("enable_catalogs", true);
        jSONObject.put("enable_imdb_metadata", false);
        jSONObject.put("max_size", "inf");
        jSONObject.put("min_size", 0);
        jSONObject.put("max_streams_per_resolution", 10);
        jSONObject.put("nudity_filter", new JSONArray().put("Severe"));
        jSONObject.put("certification_filter", new JSONArray().put("Adults+"));
        jSONObject.put("language_sorting", n(AbstractC3252r.B("English", "Tamil", "Hindi", "Malayalam", "Kannada", "Telugu", "Chinese", "Russian", "Arabic", "Japanese", "Korean", "Taiwanese", "Latino", "French", "Spanish", "Portuguese", "Italian", "German", "Ukrainian", "Polish", "Czech", "Thai", "Indonesian", "Vietnamese", "Dutch", "Bengali", "Turkish", "Greek", "Swedish", "Romanian", "Hungarian", "Finnish", "Norwegian", "Danish", "Hebrew", "Lithuanian", "Punjabi", "Marathi", "Gujarati", "Bhojpuri", "Nepali", "Urdu", "Tagalog", "Filipino", "Malay", "Mongolian", "Armenian", "Georgian", null)));
        jSONObject.put("quality_filter", n(AbstractC3252r.B("BluRay/UHD", "WEB/HD", "DVD/TV/SAT", "CAM/Screener", "Unknown")));
        jSONObject.put("hdr_filter", n(AbstractC3252r.B("HDR10", "HDR10+", "Dolby Vision", "HLG", "SDR", "Unknown")));
        jSONObject.put("live_search_streams", false);
        jSONObject.put("include_anime", true);
        jSONObject.put("enable_telegram_streams", false);
        jSONObject.put("enable_acestream_streams", false);
        jSONObject.put("max_streams", 25);
        jSONObject.put("stream_type_grouping", "separate");
        jSONObject.put("stream_type_order", n(AbstractC3252r.B("torrent", "usenet", "telegram", "http", "acestream", "youtube")));
        jSONObject.put("provider_grouping", "separate");
        jSONObject.put("stream_name_filter_mode", "disabled");
        jSONObject.put("stream_name_filter_patterns", new JSONArray());
        jSONObject.put("stream_name_filter_use_regex", false);
        JSONArray jSONArray = new JSONArray();
        for (w4.k kVar : AbstractC3252r.B(new w4.k("cached", "desc"), new w4.k("resolution", "desc"), new w4.k("quality", "desc"), new w4.k("language", "desc"), new w4.k("size", "desc"), new w4.k("seeders", "desc"), new w4.k("created_at", "desc"))) {
            String str3 = (String) kVar.f25469a;
            String str4 = (String) kVar.f25470b;
            JSONObject jSONObject2 = new JSONObject();
            jSONObject2.put(SubscriberAttributeKt.JSON_NAME_KEY, str3);
            jSONObject2.put("direction", str4);
            jSONArray.put(jSONObject2);
        }
        jSONObject.put("torrent_sorting_priority", jSONArray);
        jSONObject.put("telegram_config", JSONObject.NULL);
        return jSONObject;
    }

    public static JSONObject g(String str, String str2) throws JSONException {
        JSONObject jSONObject = new JSONObject();
        jSONObject.put(DiagnosticsEntry.NAME_KEY, "Provider");
        jSONObject.put("service", str);
        jSONObject.put("token", str2);
        jSONObject.put("enable_watchlist_catalogs", true);
        Object obj = JSONObject.NULL;
        jSONObject.put("qbittorrent_config", obj);
        jSONObject.put("only_show_cached_streams", true);
        jSONObject.put("use_mediaflow", true);
        jSONObject.put("sabnzbd_config", obj);
        jSONObject.put("nzbget_config", obj);
        jSONObject.put("nzbdav_config", obj);
        jSONObject.put("easynews_config", obj);
        jSONObject.put("priority", 0);
        jSONObject.put("enabled", true);
        return jSONObject;
    }

    public static LinkedHashMap i() {
        Map map = N2.q.f5245b;
        C0639b1 c0639b1 = ((n3) ((Y4.c0) u3.f8112c.f8871a).getValue()).f7976f;
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        j(map, linkedHashMap, "realdebrid", c0639b1.f7699a);
        j(map, linkedHashMap, "premiumize", c0639b1.f7700b);
        j(map, linkedHashMap, "offcloud", c0639b1.f7701c);
        j(map, linkedHashMap, "alldebrid", c0639b1.f7702d);
        j(map, linkedHashMap, "torbox", c0639b1.f7703e);
        return linkedHashMap;
    }

    public static final void j(Map map, LinkedHashMap linkedHashMap, String str, boolean z6) {
        if (z6) {
            String str2 = (String) map.get(str);
            String string = str2 != null ? S4.p.o1(str2).toString() : null;
            if (string == null) {
                string = "";
            }
            if (string.length() > 0) {
                linkedHashMap.put(str, string);
            }
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:43:0x00e0 A[PHI: r1
      0x00e0: PHI (r1v19 java.lang.String) = (r1v18 java.lang.String), (r1v22 java.lang.String) binds: [B:40:0x00cf, B:42:0x00de] A[DONT_GENERATE, DONT_INLINE]] */
    /* JADX WARN: Type inference failed for: r33v0 */
    /* JADX WARN: Type inference failed for: r33v1, types: [java.lang.String] */
    /* JADX WARN: Type inference failed for: r34v0 */
    /* JADX WARN: Type inference failed for: r34v1 */
    /* JADX WARN: Type inference failed for: r34v2 */
    /* JADX WARN: Type inference failed for: r34v3 */
    /* JADX WARN: Type inference failed for: r4v0, types: [java.util.ArrayList] */
    /* JADX WARN: Type inference failed for: r4v1, types: [java.util.ArrayList] */
    /* JADX WARN: Type inference failed for: r4v10, types: [java.lang.String] */
    /* JADX WARN: Type inference failed for: r4v11 */
    /* JADX WARN: Type inference failed for: r4v12 */
    /* JADX WARN: Type inference failed for: r4v13 */
    /* JADX WARN: Type inference failed for: r4v14 */
    /* JADX WARN: Type inference failed for: r4v15 */
    /* JADX WARN: Type inference failed for: r4v16 */
    /* JADX WARN: Type inference failed for: r4v17 */
    /* JADX WARN: Type inference failed for: r4v18 */
    /* JADX WARN: Type inference failed for: r4v19 */
    /* JADX WARN: Type inference failed for: r4v2 */
    /* JADX WARN: Type inference failed for: r4v20 */
    /* JADX WARN: Type inference failed for: r4v21, types: [java.util.ArrayList] */
    /* JADX WARN: Type inference failed for: r4v22 */
    /* JADX WARN: Type inference failed for: r4v23 */
    /* JADX WARN: Type inference failed for: r4v24 */
    /* JADX WARN: Type inference failed for: r4v25 */
    /* JADX WARN: Type inference failed for: r4v26 */
    /* JADX WARN: Type inference failed for: r4v3 */
    /* JADX WARN: Type inference failed for: r4v6 */
    /* JADX WARN: Type inference failed for: r4v7 */
    /* JADX WARN: Type inference failed for: r4v8 */
    /* JADX WARN: Type inference failed for: r4v9 */
    /* JADX WARN: Type inference failed for: r7v20, types: [java.lang.StringBuilder] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static ArrayList k(List list, List list2) {
        String str;
        String str2;
        String str3;
        String str4;
        boolean z6;
        String str5;
        String str6;
        String str7;
        boolean z7;
        ?? r42;
        Object objD;
        JSONObject jSONObjectF;
        D3 d32;
        int i6;
        ?? r34;
        D3 d33;
        ?? r43;
        T2.c cVar;
        ?? arrayList = new ArrayList();
        LinkedHashMap linkedHashMapI = i();
        Iterator it = list.iterator();
        while (true) {
            boolean zHasNext = it.hasNext();
            str = "httpUrlTargetKey";
            str2 = "itemType";
            str3 = "optString(...)";
            str4 = ImagesContract.URL;
            z6 = false;
            str5 = "streams";
            if (!zHasNext) {
                break;
            }
            JSONObject jSONObject = (JSONObject) it.next();
            String strP = A0.a.p(jSONObject, ImagesContract.URL, "", "optString(...)");
            if (strP.length() == 0) {
                cVar = null;
            } else {
                String strOptString = jSONObject.optString("jsonResultsKey", "streams");
                String str8 = S4.p.Q0(strOptString) ? "streams" : strOptString;
                String strOptString2 = jSONObject.optString(DiagnosticsEntry.NAME_KEY, "Addon");
                String str9 = S4.p.Q0(strOptString2) ? "Addon" : strOptString2;
                String strOptString3 = jSONObject.optString("sizeAttr", "");
                kotlin.jvm.internal.o.e(strOptString3);
                String str10 = strOptString3.length() > 0 ? strOptString3 : null;
                String strOptString4 = jSONObject.optString("sizePattern", "");
                kotlin.jvm.internal.o.e(strOptString4);
                str6 = strOptString4.length() > 0 ? strOptString4 : null;
                String strOptString5 = jSONObject.optString("torrentNameTargetKey", "");
                kotlin.jvm.internal.o.e(strOptString5);
                String str11 = strOptString5.length() > 0 ? strOptString5 : null;
                String strOptString6 = jSONObject.optString("itemType", "");
                kotlin.jvm.internal.o.e(strOptString6);
                String str12 = strOptString6.length() > 0 ? strOptString6 : null;
                String strOptString7 = jSONObject.optString("httpUrlTargetKey", "");
                kotlin.jvm.internal.o.e(strOptString7);
                String str13 = strOptString7.length() > 0 ? strOptString7 : null;
                String strOptString8 = jSONObject.optString("registeredDebridProvider", "");
                kotlin.jvm.internal.o.e(strOptString8);
                if (strOptString8.length() <= 0) {
                    strOptString8 = null;
                }
                if (strOptString8 == null) {
                    strOptString8 = jSONObject.optString("debridProvider", "");
                    kotlin.jvm.internal.o.e(strOptString8);
                    String str14 = strOptString8.length() > 0 ? strOptString8 : null;
                    cVar = new T2.c(strP, str8, str9, str10, str6, str11, str12, str13, str14);
                }
            }
            if (cVar != null) {
                arrayList.add(cVar);
            }
        }
        Iterator it2 = list2.iterator();
        while (it2.hasNext()) {
            LinkedHashMap linkedHashMap = linkedHashMapI;
            JSONObject jSONObject2 = (JSONObject) it2.next();
            String strOptString9 = jSONObject2.optString(str4, "");
            kotlin.jvm.internal.o.g(strOptString9, str3);
            Iterator it3 = it2;
            String str15 = str3;
            char[] cArr = new char[1];
            cArr[z6 ? 1 : 0] = '/';
            String strQ1 = S4.p.q1(strOptString9, cArr);
            String strOptString10 = jSONObject2.optString("mode", "");
            if (strQ1.length() != 0) {
                kotlin.jvm.internal.o.e(strOptString10);
                if (strOptString10.length() != 0) {
                    String str16 = str4;
                    String strB = Z0.o.B(strQ1, RemoteSettings.FORWARD_SLASH_STRING, strOptString10);
                    arrayList = arrayList;
                    for (Map.Entry entry : linkedHashMap.entrySet()) {
                        ?? r342 = arrayList;
                        String str17 = (String) entry.getKey();
                        String str18 = str;
                        try {
                            jSONObjectF = f(str17, (String) entry.getValue());
                            str7 = str2;
                            d32 = null;
                            i6 = 1;
                            r42 = str17;
                            r34 = r342;
                        } catch (Throwable th) {
                            th = th;
                            str6 = str5;
                            str7 = str2;
                            z7 = z6 ? 1 : 0;
                            r42 = r342;
                        }
                        while (i6 < 9) {
                            try {
                                try {
                                    String str19 = strQ1 + RemoteSettings.FORWARD_SLASH_STRING + p(jSONObjectF, strB) + "/stream/%searchPattern.json";
                                    String strOptString11 = jSONObject2.optString("jsonResultsKey", str5);
                                    String str20 = S4.p.Q0(strOptString11) ? str5 : strOptString11;
                                    String strOptString12 = jSONObject2.optString(DiagnosticsEntry.NAME_KEY, "Mediafusion");
                                    if (S4.p.Q0(strOptString12)) {
                                        strOptString12 = "Mediafusion";
                                    }
                                    String str21 = strOptString12 + " (" + r42 + ")";
                                    String strOptString13 = jSONObject2.optString("sizeAttr", "");
                                    kotlin.jvm.internal.o.e(strOptString13);
                                    String str22 = strOptString13.length() > 0 ? strOptString13 : null;
                                    String strOptString14 = jSONObject2.optString("sizePattern", "");
                                    kotlin.jvm.internal.o.e(strOptString14);
                                    String str23 = strOptString14.length() > 0 ? strOptString14 : null;
                                    String strOptString15 = jSONObject2.optString("torrentNameTargetKey", "");
                                    kotlin.jvm.internal.o.e(strOptString15);
                                    String str24 = strOptString15.length() > 0 ? strOptString15 : null;
                                    try {
                                        String strOptString16 = jSONObject2.optString(str7, "");
                                        kotlin.jvm.internal.o.e(strOptString16);
                                        String str25 = strOptString16.length() > 0 ? strOptString16 : null;
                                        try {
                                            String strOptString17 = jSONObject2.optString(str18, "");
                                            kotlin.jvm.internal.o.e(strOptString17);
                                            T2.c cVar2 = new T2.c(str19, str20, str21, str22, str23, str24, str25, strOptString17.length() > 0 ? strOptString17 : null, r42);
                                            r42 = r34;
                                            try {
                                                objD = Boolean.valueOf(r42.add(cVar2));
                                                str6 = str5;
                                                str7 = str7;
                                                str18 = str18;
                                                z7 = z6 ? 1 : 0;
                                            } catch (Throwable th2) {
                                                th = th2;
                                                str6 = str5;
                                                str7 = str7;
                                                str18 = str18;
                                                r43 = r42;
                                                z7 = z6 ? 1 : 0;
                                                r42 = r43;
                                                objD = AbstractC2467b.d(th);
                                            }
                                        } catch (Throwable th3) {
                                            th = th3;
                                            r42 = r34;
                                        }
                                    } catch (Throwable th4) {
                                        th = th4;
                                        r43 = r34;
                                        str6 = str5;
                                        str7 = str7;
                                    }
                                } catch (D3 e6) {
                                    ?? r33 = r42;
                                    r42 = r34;
                                    str6 = str5;
                                    try {
                                        if (!e6.f7294a || i6 >= 8) {
                                            throw e6;
                                        }
                                        Thread.sleep(800L);
                                        i6++;
                                        d32 = e6;
                                        r34 = r42;
                                        str5 = str6;
                                        r42 = r33;
                                    } catch (Throwable th5) {
                                        th = th5;
                                        z7 = z6 ? 1 : 0;
                                        objD = AbstractC2467b.d(th);
                                        w4.n.a(objD);
                                        z6 = z7;
                                        str = str18;
                                        str5 = str6;
                                        str2 = str7;
                                        arrayList = r42;
                                    }
                                }
                            } catch (Throwable th6) {
                                th = th6;
                                r43 = r34;
                                str6 = str5;
                            }
                            w4.n.a(objD);
                            z6 = z7;
                            str = str18;
                            str5 = str6;
                            str2 = str7;
                            arrayList = r42;
                        }
                        str6 = str5;
                        r42 = r34;
                        if (d32 != null) {
                            d33 = d32;
                            z7 = z6 ? 1 : 0;
                        } else {
                            try {
                                z7 = z6 ? 1 : 0;
                                try {
                                    d33 = new D3("Register failed", z7);
                                } catch (Throwable th7) {
                                    th = th7;
                                    objD = AbstractC2467b.d(th);
                                    w4.n.a(objD);
                                    z6 = z7;
                                    str = str18;
                                    str5 = str6;
                                    str2 = str7;
                                    arrayList = r42;
                                }
                            } catch (Throwable th8) {
                                th = th8;
                                z7 = z6 ? 1 : 0;
                                objD = AbstractC2467b.d(th);
                                w4.n.a(objD);
                                z6 = z7;
                                str = str18;
                                str5 = str6;
                                str2 = str7;
                                arrayList = r42;
                            }
                        }
                        throw d33;
                    }
                    it2 = it3;
                    linkedHashMapI = linkedHashMap;
                    str3 = str15;
                    str4 = str16;
                }
            }
            it2 = it3;
            linkedHashMapI = linkedHashMap;
            str3 = str15;
        }
        return arrayList;
    }

    public static void l(Object obj, LinkedHashSet linkedHashSet) {
        if (obj != null) {
            if (obj instanceof String) {
                String str = (String) obj;
                String string = S4.p.o1(str).toString();
                if (S4.w.A0(string, "https://", true) && S4.w.s0(string, ".json", true)) {
                    linkedHashSet.add(S4.p.o1(str).toString());
                    return;
                }
                return;
            }
            if (obj instanceof List) {
                Iterator it = ((Iterable) obj).iterator();
                while (it.hasNext()) {
                    l(it.next(), linkedHashSet);
                }
            } else if (obj instanceof Map) {
                Iterator it2 = ((Map) obj).values().iterator();
                while (it2.hasNext()) {
                    l(it2.next(), linkedHashSet);
                }
            }
        }
    }

    public static String m(String str) throws IOException {
        Request.Builder builder = new Request.Builder();
        builder.i(str);
        builder.d();
        Response responseD = ((OkHttpClient) f7429b.getValue()).a(builder.b()).d();
        try {
            if (!responseD.c()) {
                throw new IllegalArgumentException(("HTTP " + responseD.f22143d).toString());
            }
            ResponseBody responseBody = responseD.f22146j;
            String strD = responseBody != null ? responseBody.d() : null;
            if (strD == null) {
                strD = "";
            }
            responseD.close();
            return strD;
        } catch (Throwable th) {
            try {
                throw th;
            } catch (Throwable th2) {
                android.support.v4.media.session.b.j(responseD, th);
                throw th2;
            }
        }
    }

    public static JSONArray n(List list) {
        JSONArray jSONArray = new JSONArray();
        for (Object obj : list) {
            if (obj == null) {
                obj = JSONObject.NULL;
            }
            jSONArray.put(obj);
        }
        return jSONArray;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:108:0x016c  */
    /* JADX WARN: Removed duplicated region for block: B:143:0x010a A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:146:0x009a A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:155:0x018e A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:167:0x011e A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:28:0x0066  */
    /* JADX WARN: Removed duplicated region for block: B:67:0x00e8  */
    /* JADX WARN: Type inference failed for: r0v0, types: [x4.y] */
    /* JADX WARN: Type inference failed for: r0v1, types: [java.util.List] */
    /* JADX WARN: Type inference failed for: r0v2, types: [java.util.ArrayList] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static List o(Object obj) {
        String string;
        String string2;
        String string3;
        String string4;
        String string5;
        ?? arrayList = x4.y.f26065a;
        if (obj != null) {
            if (obj instanceof String) {
                List listQ = AbstractC2612c.q(S4.p.o1((String) obj).toString());
                ArrayList arrayList2 = new ArrayList();
                for (Object obj2 : listQ) {
                    if (((String) obj2).length() > 0) {
                        arrayList2.add(obj2);
                    }
                }
                return arrayList2;
            }
            if (obj instanceof Map) {
                Map map = (Map) obj;
                Object obj3 = map.get(ImagesContract.URL);
                String str = obj3 instanceof String ? (String) obj3 : null;
                if (str == null || (string4 = S4.p.o1(str).toString()) == null) {
                    Object obj4 = map.get("registerUrl");
                    String str2 = obj4 instanceof String ? (String) obj4 : null;
                    if (str2 == null || (string4 = S4.p.o1(str2).toString()) == null || string4.length() <= 0) {
                        string4 = null;
                    }
                } else {
                    if (string4.length() <= 0) {
                        string4 = null;
                    }
                    if (string4 == null) {
                    }
                }
                if (string4 != null) {
                    return AbstractC2612c.q(string4);
                }
                ArrayList arrayList3 = new ArrayList();
                for (Object obj5 : map.values()) {
                    if (obj5 instanceof String) {
                        String string6 = S4.p.o1((String) obj5).toString();
                        if (string6.length() <= 0) {
                            string6 = null;
                        }
                        if (string6 != null) {
                            arrayList3.add(string6);
                        }
                    } else if (obj5 instanceof Map) {
                        Map map2 = (Map) obj5;
                        Object obj6 = map2.get(ImagesContract.URL);
                        String str3 = obj6 instanceof String ? (String) obj6 : null;
                        if (str3 == null || (string5 = S4.p.o1(str3).toString()) == null) {
                            Object obj7 = map2.get("registerUrl");
                            String str4 = obj7 instanceof String ? (String) obj7 : null;
                            if (str4 == null || (string5 = S4.p.o1(str4).toString()) == null || string5.length() <= 0) {
                                string5 = null;
                            }
                            if (string5 == null) {
                                arrayList3.add(string5);
                            }
                        } else {
                            if (string5.length() <= 0) {
                                string5 = null;
                            }
                            if (string5 == null) {
                            }
                            if (string5 == null) {
                            }
                        }
                    }
                }
                return arrayList3;
            }
            if (obj instanceof List) {
                arrayList = new ArrayList();
                for (Object obj8 : (List) obj) {
                    if (obj8 instanceof String) {
                        String string7 = S4.p.o1((String) obj8).toString();
                        if (string7.length() <= 0) {
                            string7 = null;
                        }
                        if (string7 != null) {
                            arrayList.add(string7);
                        }
                    } else if (obj8 instanceof Map) {
                        Map map3 = (Map) obj8;
                        Object obj9 = map3.get(ImagesContract.URL);
                        String str5 = obj9 instanceof String ? (String) obj9 : null;
                        if (str5 == null || (string = S4.p.o1(str5).toString()) == null) {
                            Object obj10 = map3.get("registerUrl");
                            String str6 = obj10 instanceof String ? (String) obj10 : null;
                            if (str6 == null || (string = S4.p.o1(str6).toString()) == null || string.length() <= 0) {
                                string = null;
                            }
                            if (string == null) {
                                arrayList.add(string);
                            }
                        } else {
                            if (string.length() <= 0) {
                                string = null;
                            }
                            if (string == null) {
                            }
                            if (string == null) {
                            }
                        }
                    } else if (obj8 != null && (string2 = obj8.toString()) != null && (string3 = S4.p.o1(string2).toString()) != null) {
                        if (string3.length() <= 0) {
                            string3 = null;
                        }
                        if (string3 != null) {
                            arrayList.add(string3);
                        }
                    }
                }
            }
        }
        return arrayList;
    }

    public static String p(JSONObject jSONObject, String str) throws IOException {
        Object objD;
        RequestBody.Companion companion = RequestBody.f22136a;
        String string = jSONObject.toString();
        kotlin.jvm.internal.o.g(string, "toString(...)");
        MediaType.f22043d.getClass();
        MediaType mediaTypeB = MediaType.Companion.b("application/json; charset=utf-8");
        companion.getClass();
        RequestBody$Companion$toRequestBody$2 requestBody$Companion$toRequestBody$2A = RequestBody.Companion.a(string, mediaTypeB);
        Request.Builder builder = new Request.Builder();
        builder.i(str);
        builder.h(requestBody$Companion$toRequestBody$2A);
        builder.e("Content-Type", "application/json");
        Response responseD = ((OkHttpClient) f7429b.getValue()).a(builder.b()).d();
        try {
            ResponseBody responseBody = responseD.f22146j;
            String strD = responseBody != null ? responseBody.d() : null;
            String str2 = strD;
            if (strD == null) {
                str2 = "";
            }
            boolean zC = responseD.c();
            boolean zD0 = true;
            int i6 = responseD.f22143d;
            if (!zC) {
                try {
                    objD = new JSONObject(str2).optString("message", str2);
                } catch (Throwable th) {
                    objD = AbstractC2467b.d(th);
                }
                String str3 = (String) (objD instanceof w4.m ? str2 : objD);
                String str4 = "Register failed: " + i6 + " " + str3;
                kotlin.jvm.internal.o.e(str3);
                if (i6 != 429) {
                    zD0 = S4.p.D0(str3, "too many request", true);
                }
                throw new D3(str4, zD0);
            }
            JSONObject jSONObject2 = new JSONObject(str2);
            if (kotlin.jvm.internal.o.c(jSONObject2.optString("status"), "success")) {
                String strOptString = jSONObject2.optString("encrypted_str", "");
                kotlin.jvm.internal.o.e(strOptString);
                if (strOptString.length() == 0) {
                    throw new D3("Missing encrypted_str", false);
                }
                responseD.close();
                return strOptString;
            }
            String strOptString2 = jSONObject2.optString("message", str2);
            String str5 = S4.p.Q0(strOptString2) ? str2 : strOptString2;
            String strConcat = "Register returned non-success: ".concat(str5);
            if (i6 != 429) {
                zD0 = S4.p.D0(str5, "too many request", true);
            }
            throw new D3(strConcat, zD0);
        } finally {
        }
    }

    public static w4.k r(String str) throws JSONException {
        Object jSONObject;
        JSONArray jSONArrayPut;
        String string = S4.p.o1(str).toString();
        if (S4.w.A0(string, "[", false)) {
            jSONObject = new JSONArray(string);
        } else {
            if (!S4.w.A0(string, "{", false)) {
                throw new IllegalStateException("Invalid JSON");
            }
            jSONObject = new JSONObject(string);
        }
        if (jSONObject instanceof JSONArray) {
            jSONArrayPut = (JSONArray) jSONObject;
        } else {
            if (!(jSONObject instanceof JSONObject)) {
                throw new IllegalStateException("Invalid JSON root");
            }
            JSONObject jSONObject2 = (JSONObject) jSONObject;
            if (jSONObject2.has("data")) {
                Object obj = jSONObject2.get("data");
                if (obj instanceof JSONArray) {
                    jSONArrayPut = (JSONArray) obj;
                } else {
                    if (!(obj instanceof JSONObject)) {
                        throw new IllegalStateException("Invalid data field");
                    }
                    jSONArrayPut = new JSONArray().put(obj);
                    kotlin.jvm.internal.o.g(jSONArrayPut, "put(...)");
                }
            } else {
                jSONArrayPut = new JSONArray().put(jSONObject);
                kotlin.jvm.internal.o.g(jSONArrayPut, "put(...)");
            }
        }
        ArrayList arrayList = new ArrayList();
        ArrayList arrayList2 = new ArrayList();
        int length = jSONArrayPut.length();
        for (int i6 = 0; i6 < length; i6++) {
            JSONObject jSONObjectOptJSONObject = jSONArrayPut.optJSONObject(i6);
            if (jSONObjectOptJSONObject != null) {
                if (kotlin.jvm.internal.o.c(jSONObjectOptJSONObject.optString("type"), "reg_hash")) {
                    arrayList2.add(jSONObjectOptJSONObject);
                } else {
                    arrayList.add(jSONObjectOptJSONObject);
                }
            }
        }
        return new w4.k(arrayList, arrayList2);
    }

    public static Object s(String str, Source source, C4.c cVar) {
        boolean zQ0 = S4.p.Q0(str);
        C3137A c3137a = C3137A.f25453a;
        if (zQ0) {
            C3.a();
            return c3137a;
        }
        c5.e eVar = V4.N.f8259a;
        Object objH = V4.E.H(c5.d.f13466b, new K3(str, source, null), cVar);
        return objH == B4.a.f492a ? objH : c3137a;
    }

    public static String t(Throwable th) {
        String message = th.getMessage();
        String string = message != null ? S4.p.o1(message).toString() : null;
        if (string == null) {
            string = "";
        }
        return (string.equals("Not signed in") || S4.w.A0(string, "This add-on must be registered with a debrid service", false) || S4.w.A0(string, "Only one third-party add-on is allowed", false)) ? string : "Invalid Addon";
    }

    public static Object u(String url) {
        Object objD;
        kotlin.jvm.internal.o.h(url, "url");
        try {
            String string = S4.p.o1(url).toString();
            if (string.length() <= 0) {
                throw new IllegalArgumentException("Please enter an add-on URL");
            }
            if (!S4.w.A0(string, "https://", true)) {
                throw new IllegalArgumentException("URL must start with https://");
            }
            try {
                objD = new URI(string);
            } catch (Throwable th) {
                objD = AbstractC2467b.d(th);
            }
            if (w4.n.a(objD) != null) {
                throw new IllegalArgumentException("Invalid URL");
            }
            URI uri = (URI) objD;
            String host = uri.getHost();
            if (host == null || S4.p.Q0(host)) {
                throw new IllegalArgumentException("Invalid URL: missing host");
            }
            String path = uri.getPath();
            if (path == null) {
                path = "";
            }
            if (S4.w.s0(path, ".json", true)) {
                return C3137A.f25453a;
            }
            throw new IllegalArgumentException("Addon manifest URL must point to a JSON file (path should end with .json)");
        } catch (Throwable th2) {
            return AbstractC2467b.d(th2);
        }
    }

    public static void v(JSONObject jSONObject) {
        JSONObject jSONObjectOptJSONObject = jSONObject.optJSONObject("searchPattern");
        if (jSONObjectOptJSONObject == null) {
            throw new IllegalArgumentException("Each add-on must include a searchPattern object");
        }
        String strP = A0.a.p(jSONObjectOptJSONObject, "movie", "", "optString(...)");
        String strP2 = A0.a.p(jSONObjectOptJSONObject, "tv", "", "optString(...)");
        if (strP.length() <= 0 || strP2.length() <= 0) {
            throw new IllegalArgumentException("Each add-on must define searchPattern.movie and searchPattern.tv");
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object e(String str, String str2, C4.c cVar) {
        E3 e32;
        if (cVar instanceof E3) {
            e32 = (E3) cVar;
            int i6 = e32.f7307c;
            if ((i6 & Integer.MIN_VALUE) != 0) {
                e32.f7307c = i6 - Integer.MIN_VALUE;
            } else {
                e32 = new E3(this, cVar);
            }
        }
        Object objH = e32.f7305a;
        B4.a aVar = B4.a.f492a;
        int i7 = e32.f7307c;
        if (i7 == 0) {
            AbstractC2467b.l(objH);
            c5.e eVar = V4.N.f8259a;
            c5.d dVar = c5.d.f13466b;
            F3 f32 = new F3(str, str2, null);
            e32.f7307c = 1;
            objH = V4.E.H(dVar, f32, e32);
            if (objH == aVar) {
                return aVar;
            }
        } else {
            if (i7 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            AbstractC2467b.l(objH);
        }
        return ((w4.n) objH).f25472a;
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object h(String str, C4.c cVar) {
        G3 g32;
        if (cVar instanceof G3) {
            g32 = (G3) cVar;
            int i6 = g32.f7339c;
            if ((i6 & Integer.MIN_VALUE) != 0) {
                g32.f7339c = i6 - Integer.MIN_VALUE;
            } else {
                g32 = new G3(this, cVar);
            }
        }
        Object objH = g32.f7337a;
        B4.a aVar = B4.a.f492a;
        int i7 = g32.f7339c;
        if (i7 == 0) {
            AbstractC2467b.l(objH);
            c5.e eVar = V4.N.f8259a;
            c5.d dVar = c5.d.f13466b;
            H3 h32 = new H3(str, null);
            g32.f7339c = 1;
            objH = V4.E.H(dVar, h32, g32);
            if (objH == aVar) {
                return aVar;
            }
        } else {
            if (i7 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            AbstractC2467b.l(objH);
        }
        return ((w4.n) objH).f25472a;
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object q(String str, String str2, C4.c cVar) {
        I3 i32;
        if (cVar instanceof I3) {
            i32 = (I3) cVar;
            int i6 = i32.f7369c;
            if ((i6 & Integer.MIN_VALUE) != 0) {
                i32.f7369c = i6 - Integer.MIN_VALUE;
            } else {
                i32 = new I3(this, cVar);
            }
        }
        Object objH = i32.f7367a;
        B4.a aVar = B4.a.f492a;
        int i7 = i32.f7369c;
        if (i7 == 0) {
            AbstractC2467b.l(objH);
            c5.e eVar = V4.N.f8259a;
            c5.d dVar = c5.d.f13466b;
            J3 j32 = new J3(str, str2, null);
            i32.f7369c = 1;
            objH = V4.E.H(dVar, j32, i32);
            if (objH == aVar) {
                return aVar;
            }
        } else {
            if (i7 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            AbstractC2467b.l(objH);
        }
        return ((w4.n) objH).f25472a;
    }
}
