package V2;

import android.content.Context;
import android.content.SharedPreferences;
import com.debridstream.tv.ProfileViewModel;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import m3.AbstractC2467b;
import w4.C3137A;
import x4.AbstractC3251q;

/* JADX INFO: loaded from: classes.dex */
public final class U2 extends C4.j implements J4.e {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public int f7565a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public /* synthetic */ Object f7566b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final /* synthetic */ ProfileViewModel f7567c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final /* synthetic */ boolean f7568d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final /* synthetic */ A0 f7569e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final /* synthetic */ String f7570f;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final /* synthetic */ Context f7571j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final /* synthetic */ String f7572k;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public U2(ProfileViewModel profileViewModel, boolean z6, A0 a02, String str, Context context, String str2, A4.d dVar) {
        super(2, dVar);
        this.f7567c = profileViewModel;
        this.f7568d = z6;
        this.f7569e = a02;
        this.f7570f = str;
        this.f7571j = context;
        this.f7572k = str2;
    }

    @Override // C4.a
    public final A4.d create(Object obj, A4.d dVar) {
        U2 u22 = new U2(this.f7567c, this.f7568d, this.f7569e, this.f7570f, this.f7571j, this.f7572k, dVar);
        u22.f7566b = obj;
        return u22;
    }

    @Override // J4.e
    public final Object invoke(Object obj, Object obj2) {
        return ((U2) create((V4.C) obj, (A4.d) obj2)).invokeSuspend(C3137A.f25453a);
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:119:0x026a  */
    /* JADX WARN: Removed duplicated region for block: B:126:0x02af A[LOOP:2: B:109:0x0225->B:126:0x02af, LOOP_END] */
    /* JADX WARN: Removed duplicated region for block: B:143:0x02b8 A[EDGE_INSN: B:143:0x02b8->B:128:0x02b8 BREAK  A[LOOP:2: B:109:0x0225->B:126:0x02af], SYNTHETIC] */
    /* JADX WARN: Type inference failed for: r3v1 */
    @Override // C4.a
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object invokeSuspend(Object obj) {
        Object objD;
        String profileKey;
        Object obj2;
        Y4.c0 c0Var;
        Object value;
        T2 t22;
        String message;
        Y4.H h5;
        Y4.c0 c0Var2;
        Object value2;
        T2 t23;
        ArrayList arrayList;
        Object next;
        String str;
        Y4.c0 c0Var3;
        Object value3;
        Y4.c0 c0Var4;
        Object value4;
        Y4.c0 c0Var5;
        Object value5;
        B4.a aVar = B4.a.f492a;
        int i6 = this.f7565a;
        C3137A c3137a = C3137A.f25453a;
        ProfileViewModel profileViewModel = this.f7567c;
        try {
        } catch (Throwable th) {
            objD = AbstractC2467b.d(th);
            profileKey = i6;
        }
        if (i6 == 0) {
            AbstractC2467b.l(obj);
            T2 t24 = (T2) ((Y4.c0) profileViewModel._uiState).getValue();
            if (t24.f7543a.size() >= profileViewModel.allProfileKeys.size()) {
                Y4.H h6 = profileViewModel._uiState;
                do {
                    c0Var5 = (Y4.c0) h6;
                    value5 = c0Var5.getValue();
                } while (!c0Var5.i(value5, T2.a((T2) value5, null, false, false, null, null, false, false, "Maximum profiles reached", false, 0L, null, 1919)));
            } else {
                List list = t24.f7543a;
                boolean z6 = this.f7568d;
                if (!z6) {
                    Iterator it = profileViewModel.nonKidsOptionalKeys.iterator();
                    loop9: while (true) {
                        if (!it.hasNext()) {
                            next = null;
                            break;
                        }
                        next = it.next();
                        String str2 = (String) next;
                        if (!list.isEmpty()) {
                            Iterator it2 = list.iterator();
                            while (it2.hasNext()) {
                                if (kotlin.jvm.internal.o.c(((R2) it2.next()).f7513a, str2)) {
                                    break;
                                }
                            }
                            break loop9;
                        }
                        break;
                    }
                    str = (String) next;
                } else if (list.isEmpty()) {
                    str = "kids";
                } else {
                    Iterator it3 = list.iterator();
                    while (it3.hasNext()) {
                        if (kotlin.jvm.internal.o.c(((R2) it3.next()).f7513a, "kids")) {
                            str = null;
                            break;
                        }
                    }
                    str = "kids";
                }
                if (str == null) {
                    Y4.H h7 = profileViewModel._uiState;
                    do {
                        c0Var4 = (Y4.c0) h7;
                        value4 = c0Var4.getValue();
                    } while (!c0Var4.i(value4, T2.a((T2) value4, null, false, false, null, null, false, false, z6 ? "Kids profile already exists" : "No profile slots available", false, 0L, null, 1919)));
                } else {
                    Y4.H h8 = profileViewModel._uiState;
                    do {
                        c0Var3 = (Y4.c0) h8;
                        value3 = c0Var3.getValue();
                    } while (!c0Var3.i(value3, T2.a((T2) value3, null, true, false, "Saving profile…", null, false, false, null, false, 0L, null, 1909)));
                    A0 a02 = this.f7569e;
                    C0741y0 c0741y0 = C0741y0.f8184a;
                    String str3 = a02.f7249a;
                    this.f7566b = str;
                    this.f7565a = 1;
                    Object objD2 = C0741y0.d(str3, str, this);
                    i6 = str;
                    if (objD2 == aVar) {
                        return aVar;
                    }
                }
            }
            return c3137a;
        }
        if (i6 != 1) {
            throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
        }
        String str4 = (String) this.f7566b;
        AbstractC2467b.l(obj);
        i6 = str4;
        objD = c3137a;
        profileKey = i6;
        if (!(objD instanceof w4.m)) {
            String string = S4.p.o1(this.f7570f).toString();
            boolean zQ0 = S4.p.Q0(string);
            String str5 = string;
            if (zQ0) {
                str5 = profileKey;
            }
            Context context = this.f7571j;
            kotlin.jvm.internal.o.h(profileKey, "profileKey");
            String string2 = str5 != null ? S4.p.o1(str5).toString() : null;
            if (string2 == null) {
                string2 = "";
            }
            int i7 = 0;
            SharedPreferences.Editor editorEdit = context.getApplicationContext().getSharedPreferences("debridstream_profile_names", 0).edit();
            if (S4.p.Q0(string2)) {
                editorEdit.remove(profileKey);
            } else {
                editorEdit.putString(profileKey, string2);
            }
            editorEdit.apply();
            String str6 = this.f7572k;
            String string3 = str6 != null ? S4.p.o1(str6).toString() : null;
            String str7 = string3 != null ? string3 : "";
            if (str7.length() == 4) {
                int i8 = 0;
                while (true) {
                    if (i8 < str7.length()) {
                        if (!Character.isDigit(str7.charAt(i8))) {
                            break;
                        }
                        i8++;
                    } else if (!profileKey.equals("kids")) {
                        String string4 = S4.p.o1(str7).toString();
                        if (string4.length() == 4) {
                            int i9 = 0;
                            while (true) {
                                if (i9 >= string4.length()) {
                                    context.getApplicationContext().getSharedPreferences("debridstream_profile_pins", 0).edit().putString(profileKey, string4).apply();
                                    break;
                                }
                                if (!Character.isDigit(string4.charAt(i9))) {
                                    break;
                                }
                                i9++;
                            }
                        }
                    }
                }
                context.getApplicationContext().getSharedPreferences("debridstream_profile_pins", 0).edit().remove(profileKey).apply();
                h5 = profileViewModel._uiState;
                while (true) {
                    c0Var2 = (Y4.c0) h5;
                    value2 = c0Var2.getValue();
                    t23 = (T2) value2;
                    List list2 = t23.f7543a;
                    String string5 = context.getApplicationContext().getSharedPreferences("debridstream_profile_pins", i7).getString(profileKey, null);
                    ArrayList arrayListT0 = AbstractC3251q.t0(list2, new R2(profileKey, str5, string5 == null && string5.length() == 4));
                    HashSet hashSet = new HashSet();
                    arrayList = new ArrayList();
                    for (Object obj3 : arrayListT0) {
                        Object obj4 = objD;
                        if (hashSet.add(((R2) obj3).f7513a)) {
                            arrayList.add(obj3);
                        }
                        objD = obj4;
                    }
                    obj2 = objD;
                    if (!c0Var2.i(value2, T2.a(t23, AbstractC3251q.x0(new Q0.C(profileViewModel, 2), arrayList), false, false, "", null, false, false, null, false, 0L, null, 1908))) {
                        break;
                    }
                    objD = obj2;
                    i7 = 0;
                }
            } else {
                context.getApplicationContext().getSharedPreferences("debridstream_profile_pins", 0).edit().remove(profileKey).apply();
                h5 = profileViewModel._uiState;
                while (true) {
                    c0Var2 = (Y4.c0) h5;
                    value2 = c0Var2.getValue();
                    t23 = (T2) value2;
                    List list22 = t23.f7543a;
                    String string52 = context.getApplicationContext().getSharedPreferences("debridstream_profile_pins", i7).getString(profileKey, null);
                    ArrayList arrayListT02 = AbstractC3251q.t0(list22, new R2(profileKey, str5, string52 == null && string52.length() == 4));
                    HashSet hashSet2 = new HashSet();
                    arrayList = new ArrayList();
                    while (r2.hasNext()) {
                    }
                    obj2 = objD;
                    if (!c0Var2.i(value2, T2.a(t23, AbstractC3251q.x0(new Q0.C(profileViewModel, 2), arrayList), false, false, "", null, false, false, null, false, 0L, null, 1908))) {
                    }
                    objD = obj2;
                    i7 = 0;
                }
            }
            return c3137a;
        }
        obj2 = objD;
        Throwable thA = w4.n.a(obj2);
        if (thA != null) {
            Y4.H h9 = profileViewModel._uiState;
            do {
                c0Var = (Y4.c0) h9;
                value = c0Var.getValue();
                t22 = (T2) value;
                message = thA.getMessage();
                if (message == null) {
                    message = "Failed to add profile";
                }
            } while (!c0Var.i(value, T2.a(t22, null, false, false, "", null, false, false, message, false, 0L, null, 1909)));
        }
        return c3137a;
    }
}
