package V2;

import com.debridstream.tv.data.home.HomeMetaItem;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import m3.AbstractC2467b;
import org.json.JSONObject;
import w4.C3137A;
import x4.AbstractC3253s;

/* JADX INFO: renamed from: V2.m1, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C0694m1 extends C4.j implements J4.e {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public Y2.o f7896a;

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public ArrayList f7898c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public Iterator f7899d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public ArrayList f7900e;

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

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

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

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public C0694m1(Map map, String str, A4.d dVar) {
        super(2, dVar);
        this.f7902j = map;
        this.f7903k = str;
    }

    @Override // C4.a
    public final A4.d create(Object obj, A4.d dVar) {
        return new C0694m1(this.f7902j, this.f7903k, dVar);
    }

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

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Path cross not found for [B:102:0x0206, B:105:0x0215], limit reached: 132 */
    /* JADX WARN: Removed duplicated region for block: B:100:0x01fa  */
    /* JADX WARN: Removed duplicated region for block: B:111:0x023a  */
    /* JADX WARN: Removed duplicated region for block: B:67:0x013a  */
    /* JADX WARN: Type inference failed for: r10v15, types: [java.util.ArrayList] */
    /* JADX WARN: Type inference failed for: r10v16 */
    /* JADX WARN: Type inference failed for: r10v22 */
    /* JADX WARN: Type inference failed for: r10v8, types: [java.util.Collection] */
    /* JADX WARN: Type inference failed for: r10v9, types: [java.util.List] */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:111:0x023a -> B:112:0x023b). Please report as a decompilation issue!!! */
    @Override // C4.a
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object invokeSuspend(Object obj) throws Throwable {
        Object obj2;
        Object objK;
        Y2.o oVar;
        C0689l1 c0689l1;
        String string;
        Integer numB0;
        List list;
        Map map;
        Y2.o oVar2;
        ArrayList arrayList;
        Iterator it;
        HomeMetaItem homeMetaItem;
        B4.a aVar = B4.a.f492a;
        int i6 = this.f7901f;
        C3137A c3137a = C3137A.f25453a;
        String profileKey = this.f7903k;
        if (i6 == 0) {
            AbstractC2467b.l(obj);
            Map map2 = this.f7902j;
            if (!map2.containsKey("watchlist") || ((obj2 = map2.get("watchlist")) != null && !(obj2 instanceof List))) {
                return c3137a;
            }
            List list2 = obj2 instanceof List ? (List) obj2 : null;
            List<JSONObject> list3 = x4.y.f26065a;
            if (list2 == null) {
                list2 = list3;
            }
            Y2.o oVar3 = new Y2.o();
            C0707p1 c0707p1 = C0707p1.f8004a;
            Y4.c0 c0Var = C0706p0.f7996a;
            kotlin.jvm.internal.o.h(profileKey, "profileKey");
            List list4 = (List) ((Map) C0706p0.f7998c.getValue()).get(profileKey);
            ?? arrayList2 = list4;
            if (list4 == null) {
                arrayList2 = list3;
            }
            if (arrayList2.isEmpty()) {
                List list5 = (List) AbstractC0696m3.m().get(profileKey);
                if (list5 != null) {
                    list3 = list5;
                }
                arrayList2 = new ArrayList();
                for (JSONObject jSONObject : list3) {
                    Y4.c0 c0Var2 = C0706p0.f7996a;
                    HomeMetaItem homeMetaItemD = C0706p0.d(jSONObject);
                    if (homeMetaItemD != null) {
                        arrayList2.add(homeMetaItemD);
                    }
                }
            }
            LinkedHashMap linkedHashMap = new LinkedHashMap();
            Iterator it2 = arrayList2.iterator();
            while (true) {
                if (!it2.hasNext()) {
                    break;
                }
                HomeMetaItem homeMetaItem2 = (HomeMetaItem) it2.next();
                int tmdbId = homeMetaItem2.getTmdbId();
                if (tmdbId > 0) {
                    String lowerCase = S4.p.o1(homeMetaItem2.getType()).toString().toLowerCase(Locale.ROOT);
                    kotlin.jvm.internal.o.g(lowerCase, "toLowerCase(...)");
                    linkedHashMap.put(C0707p1.e(tmdbId, S4.p.Q0(lowerCase) ? "movie" : lowerCase), homeMetaItem2);
                }
            }
            ArrayList arrayList3 = new ArrayList();
            for (Object obj3 : list2) {
                if (obj3 == null || (string = obj3.toString()) == null || S4.w.A0(string, "t-kitsu:", false) || S4.w.A0(string, "m-kitsu:", false)) {
                    c0689l1 = null;
                } else {
                    boolean zA0 = S4.w.A0(string, "t-", false);
                    if ((S4.w.A0(string, "m-", false) || zA0) && (numB0 = S4.w.B0(S4.p.X0(S4.p.X0(string, "m-"), "t-"))) != null) {
                        int iIntValue = numB0.intValue();
                        String str = zA0 ? "tv" : "movie";
                        C0707p1 c0707p12 = C0707p1.f8004a;
                        String strE = C0707p1.e(iIntValue, str);
                        c0689l1 = new C0689l1(iIntValue, str, strE, (HomeMetaItem) linkedHashMap.get(strE));
                    }
                }
                if (c0689l1 != null) {
                    arrayList3.add(c0689l1);
                }
            }
            C0707p1 c0707p13 = C0707p1.f8004a;
            ArrayList<C0689l1> arrayList4 = new ArrayList();
            for (Object obj4 : arrayList3) {
                C0689l1 c0689l12 = (C0689l1) obj4;
                HomeMetaItem homeMetaItem3 = c0689l12.f7872d;
                if (homeMetaItem3 != null) {
                    C0707p1 c0707p14 = C0707p1.f8004a;
                    if (!C0707p1.d(homeMetaItem3, c0689l12.f7869a, c0689l12.f7870b)) {
                    }
                }
                arrayList4.add(obj4);
            }
            ArrayList arrayList5 = new ArrayList(AbstractC3253s.G(arrayList4, 10));
            for (C0689l1 c0689l13 : arrayList4) {
                arrayList5.add(new w4.k(new Integer(c0689l13.f7869a), c0689l13.f7870b));
            }
            this.f7896a = oVar3;
            this.f7897b = arrayList3;
            this.f7901f = 1;
            objK = arrayList5.isEmpty() ? x4.z.f26066a : V4.E.k(new C0703o1(arrayList5, oVar3, null), this);
            if (objK != aVar) {
                oVar = oVar3;
                list = arrayList3;
            }
            return aVar;
        }
        if (i6 != 1) {
            if (i6 != 2) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            arrayList = this.f7900e;
            it = this.f7899d;
            ArrayList arrayList6 = this.f7898c;
            map = (Map) this.f7897b;
            oVar2 = this.f7896a;
            AbstractC2467b.l(obj);
            Object objA = obj;
            arrayList.add(objA);
            arrayList = arrayList6;
            while (it.hasNext()) {
                C0689l1 c0689l14 = (C0689l1) it.next();
                HomeMetaItem homeMetaItem4 = c0689l14.f7872d;
                String str2 = c0689l14.f7871c;
                if (homeMetaItem4 != null) {
                    C0707p1 c0707p15 = C0707p1.f8004a;
                    if (C0707p1.d(homeMetaItem4, c0689l14.f7869a, c0689l14.f7870b)) {
                        homeMetaItem = c0689l14.f7872d;
                    }
                    C0707p1 c0707p16 = C0707p1.f8004a;
                    HomeMetaItem homeMetaItem5 = (HomeMetaItem) map.get(str2);
                    this.f7896a = oVar2;
                    this.f7897b = map;
                    this.f7898c = arrayList;
                    this.f7899d = it;
                    this.f7900e = arrayList;
                    this.f7901f = 2;
                    objA = C0707p1.a(oVar2, homeMetaItem, homeMetaItem5, this);
                    if (objA != aVar) {
                        arrayList6 = arrayList;
                        arrayList.add(objA);
                        arrayList = arrayList6;
                        while (it.hasNext()) {
                        }
                    }
                    return aVar;
                }
                homeMetaItem = (HomeMetaItem) map.get(str2);
                if (homeMetaItem != null) {
                    C0707p1 c0707p162 = C0707p1.f8004a;
                    HomeMetaItem homeMetaItem52 = (HomeMetaItem) map.get(str2);
                    this.f7896a = oVar2;
                    this.f7897b = map;
                    this.f7898c = arrayList;
                    this.f7899d = it;
                    this.f7900e = arrayList;
                    this.f7901f = 2;
                    objA = C0707p1.a(oVar2, homeMetaItem, homeMetaItem52, this);
                    if (objA != aVar) {
                    }
                    return aVar;
                }
            }
            Y4.c0 c0Var3 = C0706p0.f7996a;
            C0706p0.h(profileKey, arrayList);
            return c3137a;
        }
        List list6 = (List) this.f7897b;
        oVar = this.f7896a;
        AbstractC2467b.l(obj);
        list = list6;
        objK = obj;
        map = (Map) objK;
        oVar2 = oVar;
        arrayList = new ArrayList(list.size());
        it = list.iterator();
        while (it.hasNext()) {
        }
        Y4.c0 c0Var32 = C0706p0.f7996a;
        C0706p0.h(profileKey, arrayList);
        return c3137a;
    }
}
