package g3;

import V2.AbstractC0696m3;
import android.content.Context;
import androidx.media3.exoplayer.upstream.CmcdData;
import androidx.media3.extractor.text.ttml.TtmlNode;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import m3.AbstractC2467b;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: renamed from: g3.b, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public abstract class AbstractC2060b {

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final LinkedHashMap f16470b = new LinkedHashMap();

    public static String a(int i6, String type) {
        kotlin.jvm.internal.o.h(type, "type");
        String lowerCase = S4.p.o1(type).toString().toLowerCase(Locale.ROOT);
        kotlin.jvm.internal.o.g(lowerCase, "toLowerCase(...)");
        if (S4.p.Q0(lowerCase)) {
            lowerCase = "movie";
        }
        return lowerCase + "-" + i6;
    }

    public static JSONObject b(JSONObject base, String profileKey) {
        String str;
        String str2;
        String str3;
        String str4;
        kotlin.jvm.internal.o.h(profileKey, "profileKey");
        kotlin.jvm.internal.o.h(base, "base");
        JSONObject jSONObject = new JSONObject(base.toString());
        String strOptString = jSONObject.optString("localStreamUrl", "");
        kotlin.jvm.internal.o.g(strOptString, "optString(...)");
        boolean zQ0 = S4.p.Q0(strOptString);
        String strOptString2 = jSONObject.optString("localLinkHash", "");
        kotlin.jvm.internal.o.g(strOptString2, "optString(...)");
        boolean zQ02 = S4.p.Q0(strOptString2);
        String strOptString3 = jSONObject.optString("localDebridProvider", "");
        kotlin.jvm.internal.o.g(strOptString3, "optString(...)");
        boolean zQ03 = S4.p.Q0(strOptString3);
        String strOptString4 = jSONObject.optString("imdbId", "");
        kotlin.jvm.internal.o.g(strOptString4, "optString(...)");
        boolean zQ04 = S4.p.Q0(strOptString4);
        if (!zQ0 && !zQ02 && !zQ03 && !zQ04) {
            return jSONObject;
        }
        synchronized (f16469a) {
            try {
                LinkedHashMap linkedHashMap = (LinkedHashMap) f16470b.get(profileKey);
                if (linkedHashMap != null) {
                    int iOptInt = base.optInt("tmdbId", 0);
                    String strOptString5 = base.optString("type", "movie");
                    kotlin.jvm.internal.o.g(strOptString5, "optString(...)");
                    C2059a c2059a = (C2059a) linkedHashMap.get(a(iOptInt, strOptString5));
                    if (c2059a != null) {
                        if (zQ0 && (str4 = c2059a.f16465a) != null) {
                            jSONObject.put("localStreamUrl", str4);
                        }
                        if (zQ02 && (str3 = c2059a.f16466b) != null) {
                            jSONObject.put("localLinkHash", str3);
                        }
                        if (zQ03 && (str2 = c2059a.f16467c) != null) {
                            jSONObject.put("localDebridProvider", str2);
                        }
                        if (zQ04 && (str = c2059a.f16468d) != null) {
                            jSONObject.put("imdbId", str);
                            jSONObject.put("localImdbId", c2059a.f16468d);
                        }
                        return jSONObject;
                    }
                }
                return jSONObject;
            } finally {
            }
        }
    }

    public static void c() throws JSONException {
        JSONObject jSONObject = new JSONObject();
        for (Map.Entry entry : f16470b.entrySet()) {
            String str = (String) entry.getKey();
            LinkedHashMap linkedHashMap = (LinkedHashMap) entry.getValue();
            if (!linkedHashMap.isEmpty()) {
                JSONObject jSONObject2 = new JSONObject();
                for (Map.Entry entry2 : linkedHashMap.entrySet()) {
                    String str2 = (String) entry2.getKey();
                    C2059a c2059a = (C2059a) entry2.getValue();
                    JSONObject jSONObject3 = new JSONObject();
                    String str3 = c2059a.f16465a;
                    if (str3 != null) {
                        jSONObject3.put("u", str3);
                    }
                    String str4 = c2059a.f16466b;
                    if (str4 != null) {
                        jSONObject3.put(CmcdData.Factory.STREAMING_FORMAT_HLS, str4);
                    }
                    String str5 = c2059a.f16467c;
                    if (str5 != null) {
                        jSONObject3.put(TtmlNode.TAG_P, str5);
                    }
                    String str6 = c2059a.f16468d;
                    if (str6 != null) {
                        jSONObject3.put(CmcdData.Factory.OBJECT_TYPE_INIT_SEGMENT, str6);
                    }
                    if (jSONObject3.length() > 0) {
                        jSONObject2.put(str2, jSONObject3);
                    }
                }
                if (jSONObject2.length() > 0) {
                    jSONObject.put(str, jSONObject2);
                }
            }
        }
        String string = jSONObject.toString();
        kotlin.jvm.internal.o.g(string, "toString(...)");
        Context context = AbstractC0696m3.f7937d0;
        if (context == null) {
            return;
        }
        context.getSharedPreferences("debridstream_app_progress_cache", 0).edit().putString("cw_playback_hints_by_profile_json", string).apply();
    }

    public static void d(String profileKey, String type, String str, String str2, int i6, String str3, String str4) {
        String string;
        String string2;
        kotlin.jvm.internal.o.h(profileKey, "profileKey");
        kotlin.jvm.internal.o.h(type, "type");
        if (i6 <= 0) {
            return;
        }
        String str5 = null;
        if (str == null || S4.p.Q0(str)) {
            str = null;
        }
        if (str2 == null || S4.p.Q0(str2)) {
            str2 = null;
        }
        if (str4 == null || (string = S4.p.o1(str4).toString()) == null || string.length() <= 0) {
            string = null;
        }
        if (str3 == null || (string2 = S4.p.o1(str3).toString()) == null || !S4.w.A0(string2, TtmlNode.TAG_TT, true)) {
            string2 = null;
        }
        if (str == null && str2 == null && string == null && string2 == null) {
            return;
        }
        synchronized (f16469a) {
            try {
                LinkedHashMap linkedHashMap = f16470b;
                Object linkedHashMap2 = linkedHashMap.get(profileKey);
                if (linkedHashMap2 == null) {
                    linkedHashMap2 = new LinkedHashMap();
                    linkedHashMap.put(profileKey, linkedHashMap2);
                }
                LinkedHashMap linkedHashMap3 = (LinkedHashMap) linkedHashMap2;
                C2059a c2059a = (C2059a) linkedHashMap3.get(a(i6, type));
                String strA = a(i6, type);
                if (str == null) {
                    str = c2059a != null ? c2059a.f16465a : null;
                }
                if (str2 == null) {
                    str2 = c2059a != null ? c2059a.f16466b : null;
                }
                if (string == null) {
                    string = c2059a != null ? c2059a.f16467c : null;
                }
                if (string2 != null) {
                    str5 = string2;
                } else if (c2059a != null) {
                    str5 = c2059a.f16468d;
                }
                linkedHashMap3.put(strA, new C2059a(str, str2, string, str5));
                c();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public static void e(int i6, String profileKey, String type) {
        kotlin.jvm.internal.o.h(profileKey, "profileKey");
        kotlin.jvm.internal.o.h(type, "type");
        if (i6 <= 0) {
            return;
        }
        synchronized (f16469a) {
            try {
                LinkedHashMap linkedHashMap = (LinkedHashMap) f16470b.get(profileKey);
                if (linkedHashMap != null) {
                }
                c();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public static String f(String str) {
        Object objD;
        try {
            JSONArray jSONArray = new JSONArray(str);
            JSONArray jSONArray2 = new JSONArray();
            int length = jSONArray.length();
            for (int i6 = 0; i6 < length; i6++) {
                JSONObject jSONObjectOptJSONObject = jSONArray.optJSONObject(i6);
                if (jSONObjectOptJSONObject != null) {
                    jSONArray2.put(g(jSONObjectOptJSONObject));
                }
            }
            objD = jSONArray2.toString();
        } catch (Throwable th) {
            objD = AbstractC2467b.d(th);
        }
        if (objD instanceof w4.m) {
            objD = "[]";
        }
        return (String) objD;
    }

    public static JSONObject g(JSONObject jo) {
        kotlin.jvm.internal.o.h(jo, "jo");
        JSONObject jSONObject = new JSONObject(jo.toString());
        jSONObject.remove("localStreamUrl");
        jSONObject.remove("localLinkHash");
        jSONObject.remove("localDebridProvider");
        jSONObject.remove("localImdbId");
        return jSONObject;
    }
}
