package i3;

import V4.D0;
import Y4.O;
import Y4.c0;
import a.AbstractC0872a;
import android.content.Context;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import m3.AbstractC2467b;
import org.json.JSONArray;
import org.json.JSONObject;
import s2.SharedPreferencesC2907b;
import w4.C3137A;
import x4.AbstractC3233J;
import x4.AbstractC3251q;
import x4.C3224A;

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

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final a5.c f18899c;

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

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

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

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

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

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

    static {
        D0 d0D = V4.E.d();
        c5.e eVar = V4.N.f8259a;
        f18899c = V4.E.c(AbstractC0872a.a0(d0D, c5.d.f13466b));
        C3224A c3224a = C3224A.f26032a;
        f18900d = O.c(c3224a);
        c0 c0VarC = O.c(c3224a);
        f18901e = c0VarC;
        f18902f = c0VarC;
        c0 c0VarC2 = O.c(c3224a);
        f18903g = c0VarC2;
        f18904h = c0VarC2;
        f18905i = O.c(c3224a);
    }

    public static final void a() {
        Object objD;
        Context context = f18897a;
        if (context == null) {
            return;
        }
        Object obj = C3224A.f26032a;
        try {
            Set<String> stringSet = context.getSharedPreferences("debridstream_trakt_watched_history", 0).getStringSet("watched_ids", null);
            objD = stringSet != null ? AbstractC3251q.H0(stringSet) : null;
            if (objD == null) {
                objD = obj;
            }
        } catch (Throwable th) {
            objD = AbstractC2467b.d(th);
        }
        if (w4.n.a(objD) == null) {
            obj = objD;
        }
        Set set = (Set) obj;
        Context applicationContext = context.getApplicationContext();
        kotlin.jvm.internal.o.g(applicationContext, "getApplicationContext(...)");
        Context applicationContext2 = applicationContext.getApplicationContext();
        kotlin.jvm.internal.o.g(applicationContext2, "getApplicationContext(...)");
        SharedPreferencesC2907b sharedPreferencesC2907b = (SharedPreferencesC2907b) W.c.D(applicationContext2);
        LinkedHashSet linkedHashSetS = AbstractC3233J.s(set, b(sharedPreferencesC2907b.getString("history_movie_payload", null), sharedPreferencesC2907b.getString("history_show_payload", null)));
        if (!linkedHashSetS.equals(set)) {
            c(context, linkedHashSetS);
        }
        e(linkedHashSetS);
    }

    public static Set b(String str, String str2) {
        int iOptInt;
        Object objD;
        C3224A c3224a = C3224A.f26032a;
        if ((str != null && !S4.p.Q0(str)) || (str2 != null && !S4.p.Q0(str2))) {
            HashSet hashSet = new HashSet();
            if (str != null) {
                try {
                    if (!S4.p.Q0(str)) {
                        JSONArray jSONArray = new JSONArray(str);
                        int length = jSONArray.length();
                        for (int i6 = 0; i6 < length; i6++) {
                            JSONObject jSONObjectOptJSONObject = jSONArray.optJSONObject(i6);
                            if (jSONObjectOptJSONObject != null && (iOptInt = jSONObjectOptJSONObject.optInt("tmdbId", 0)) > 0) {
                                hashSet.add("m-" + iOptInt);
                            }
                        }
                    }
                } catch (Throwable th) {
                    objD = AbstractC2467b.d(th);
                }
            }
            if (str2 != null && !S4.p.Q0(str2)) {
                JSONArray jSONArray2 = new JSONArray(str2);
                int length2 = jSONArray2.length();
                for (int i7 = 0; i7 < length2; i7++) {
                    JSONObject jSONObjectOptJSONObject2 = jSONArray2.optJSONObject(i7);
                    if (jSONObjectOptJSONObject2 != null) {
                        int iOptInt2 = jSONObjectOptJSONObject2.optInt("tmdbId", 0);
                        int iOptInt3 = jSONObjectOptJSONObject2.optInt("season", 0);
                        int iOptInt4 = jSONObjectOptJSONObject2.optInt("episode", 0);
                        if (iOptInt2 > 0 && iOptInt3 > 0 && iOptInt4 > 0) {
                            hashSet.add(g(iOptInt2, iOptInt3, iOptInt4));
                        }
                    }
                }
            }
            objD = C3137A.f25453a;
            if (w4.n.a(objD) == null) {
                return hashSet;
            }
        }
        return c3224a;
    }

    public static void c(Context context, Set set) {
        context.getSharedPreferences("debridstream_trakt_watched_history", 0).edit().putStringSet("watched_ids", set).apply();
    }

    public static void d(Set set) {
        Context context;
        if (set.isEmpty() || (context = f18897a) == null) {
            return;
        }
        synchronized (f18898b) {
            Set set2 = (Set) f18900d.getValue();
            Set setR = AbstractC3233J.r(set2, set);
            if (setR.equals(set2)) {
                return;
            }
            c(context, setR);
            e(setR);
        }
    }

    public static void e(Set set) {
        Set set2 = set;
        ArrayList arrayList = new ArrayList();
        Iterator it = set2.iterator();
        while (true) {
            if (!it.hasNext()) {
                break;
            }
            String str = (String) it.next();
            Integer numB0 = S4.w.A0(str, "m-", false) ? S4.w.B0(S4.p.X0(str, "m-")) : null;
            if (numB0 != null) {
                arrayList.add(numB0);
            }
        }
        Set setH0 = AbstractC3251q.H0(arrayList);
        ArrayList arrayList2 = new ArrayList();
        for (Object obj : set2) {
            if (S4.w.A0((String) obj, "t-", false)) {
                arrayList2.add(obj);
            }
        }
        Set setH02 = AbstractC3251q.H0(arrayList2);
        ArrayList arrayList3 = new ArrayList();
        Iterator it2 = setH02.iterator();
        while (it2.hasNext()) {
            String str2 = (String) AbstractC3251q.h0(0, S4.p.c1(S4.p.X0((String) it2.next(), "t-"), new char[]{'-'}, 6));
            Integer numB02 = str2 != null ? S4.w.B0(str2) : null;
            if (numB02 != null) {
                arrayList3.add(numB02);
            }
        }
        Set setH03 = AbstractC3251q.H0(arrayList3);
        c0 c0Var = f18900d;
        c0Var.getClass();
        c0Var.k(null, set);
        c0 c0Var2 = f18901e;
        c0Var2.getClass();
        c0Var2.k(null, setH0);
        c0 c0Var3 = f18903g;
        c0Var3.getClass();
        c0Var3.k(null, setH02);
        c0 c0Var4 = f18905i;
        c0Var4.getClass();
        c0Var4.k(null, setH03);
    }

    public static void f(Set set) {
        Context context;
        if (set.isEmpty() || (context = f18897a) == null) {
            return;
        }
        synchronized (f18898b) {
            Set set2 = (Set) f18900d.getValue();
            LinkedHashSet linkedHashSetS = AbstractC3233J.s(set2, set);
            if (linkedHashSetS.equals(set2)) {
                return;
            }
            c(context, linkedHashSetS);
            e(linkedHashSetS);
        }
    }

    public static String g(int i6, int i7, int i8) {
        if (i7 < 1) {
            i7 = 1;
        }
        if (i8 < 1) {
            i8 = 1;
        }
        StringBuilder sbQ = com.google.android.recaptcha.internal.a.q(i6, i7, "t-", "-", "-");
        sbQ.append(i8);
        return sbQ.toString();
    }
}
