package com.strmr.ps.ui.base;

import android.util.Log;
import com.strmr.ps.data.model.ContentItem;
import com.strmr.ps.domain.ContentLoaderUseCase;
import defpackage.ak1;
import defpackage.ck1;
import defpackage.dw7;
import defpackage.e6a;
import defpackage.gf1;
import defpackage.gr3;
import defpackage.i68;
import defpackage.mg6;
import defpackage.s38;
import defpackage.vx8;
import defpackage.y41;
import defpackage.zw8;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import kotlin.Metadata;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.jvm.internal.Boxing;
import kotlin.coroutines.jvm.internal.DebugMetadata;
import kotlin.coroutines.jvm.internal.SuspendLambda;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\u0010\u0002\u001a\u00020\u0001*\u00020\u0000H\n¢\u0006\u0004\b\u0002\u0010\u0003"}, d2 = {"Lak1;", "Le6a;", "<anonymous>", "(Lak1;)V"}, k = 3, mv = {2, 0, 0})
@DebugMetadata(c = "com.strmr.ps.ui.base.BaseContentViewModel$requestNextPage$1", f = "BaseContentViewModel.kt", i = {0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2}, l = {881, 469, 899}, m = "invokeSuspend", n = {"$this$withLock_u24default$iv", "state", "allNewFiltered", "page", "apiHasMore", "paginationSort", "extraPagesLoaded", "state", "allNewFiltered", "page", "apiHasMore", "$this$withLock_u24default$iv"}, s = {"L$0", "L$0", "L$1", "L$2", "L$3", "L$4", "I$0", "L$0", "L$1", "L$2", "L$3", "L$4"})
@vx8
final class u extends SuspendLambda implements gr3<ak1, Continuation<? super e6a>, Object> {
    public Object a;
    public Object b;
    public dw7.f c;
    public dw7.a d;
    public Object e;
    public BaseContentViewModel f;
    public int i;
    public int j;
    public final /* synthetic */ BaseContentViewModel k;
    public final /* synthetic */ int l;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public u(BaseContentViewModel baseContentViewModel, int i, Continuation continuation) {
        super(2, continuation);
        this.k = baseContentViewModel;
        this.l = i;
    }

    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    public final Continuation create(Object obj, Continuation continuation) {
        return new u(this.k, this.l, continuation);
    }

    @Override // defpackage.gr3
    public final Object invoke(Object obj, Object obj2) {
        return ((u) create((ak1) obj, (Continuation) obj2)).invokeSuspend(e6a.a);
    }

    /* JADX WARN: Finally extract failed */
    /* JADX WARN: Removed duplicated region for block: B:39:0x00e4 A[Catch: Exception -> 0x0030, TRY_ENTER, TryCatch #0 {Exception -> 0x0030, blocks: (B:8:0x002b, B:103:0x027c, B:105:0x0282, B:106:0x0285, B:51:0x0133, B:53:0x013d, B:86:0x01f6, B:54:0x0141, B:55:0x0156, B:57:0x015c, B:59:0x0168, B:61:0x0171, B:60:0x016d, B:62:0x0179, B:63:0x018a, B:65:0x0190, B:67:0x01a0, B:70:0x01ac, B:72:0x01b6, B:69:0x01a7, B:74:0x01be, B:78:0x01d5, B:80:0x01dd, B:85:0x01e5, B:39:0x00e4, B:42:0x00ea, B:35:0x00ad, B:90:0x0210, B:92:0x0219, B:94:0x023e, B:98:0x024e, B:100:0x0252, B:101:0x0256), top: B:119:0x0010, inners: #5 }] */
    /* JADX WARN: Removed duplicated region for block: B:40:0x00e7  */
    /* JADX WARN: Removed duplicated region for block: B:42:0x00ea A[Catch: Exception -> 0x0030, TRY_LEAVE, TryCatch #0 {Exception -> 0x0030, blocks: (B:8:0x002b, B:103:0x027c, B:105:0x0282, B:106:0x0285, B:51:0x0133, B:53:0x013d, B:86:0x01f6, B:54:0x0141, B:55:0x0156, B:57:0x015c, B:59:0x0168, B:61:0x0171, B:60:0x016d, B:62:0x0179, B:63:0x018a, B:65:0x0190, B:67:0x01a0, B:70:0x01ac, B:72:0x01b6, B:69:0x01a7, B:74:0x01be, B:78:0x01d5, B:80:0x01dd, B:85:0x01e5, B:39:0x00e4, B:42:0x00ea, B:35:0x00ad, B:90:0x0210, B:92:0x0219, B:94:0x023e, B:98:0x024e, B:100:0x0252, B:101:0x0256), top: B:119:0x0010, inners: #5 }] */
    /* JADX WARN: Removed duplicated region for block: B:44:0x00ef  */
    /* JADX WARN: Removed duplicated region for block: B:50:0x0128  */
    /* JADX WARN: Removed duplicated region for block: B:53:0x013d A[Catch: Exception -> 0x0030, TryCatch #0 {Exception -> 0x0030, blocks: (B:8:0x002b, B:103:0x027c, B:105:0x0282, B:106:0x0285, B:51:0x0133, B:53:0x013d, B:86:0x01f6, B:54:0x0141, B:55:0x0156, B:57:0x015c, B:59:0x0168, B:61:0x0171, B:60:0x016d, B:62:0x0179, B:63:0x018a, B:65:0x0190, B:67:0x01a0, B:70:0x01ac, B:72:0x01b6, B:69:0x01a7, B:74:0x01be, B:78:0x01d5, B:80:0x01dd, B:85:0x01e5, B:39:0x00e4, B:42:0x00ea, B:35:0x00ad, B:90:0x0210, B:92:0x0219, B:94:0x023e, B:98:0x024e, B:100:0x0252, B:101:0x0256), top: B:119:0x0010, inners: #5 }] */
    /* JADX WARN: Removed duplicated region for block: B:54:0x0141 A[Catch: Exception -> 0x0030, TryCatch #0 {Exception -> 0x0030, blocks: (B:8:0x002b, B:103:0x027c, B:105:0x0282, B:106:0x0285, B:51:0x0133, B:53:0x013d, B:86:0x01f6, B:54:0x0141, B:55:0x0156, B:57:0x015c, B:59:0x0168, B:61:0x0171, B:60:0x016d, B:62:0x0179, B:63:0x018a, B:65:0x0190, B:67:0x01a0, B:70:0x01ac, B:72:0x01b6, B:69:0x01a7, B:74:0x01be, B:78:0x01d5, B:80:0x01dd, B:85:0x01e5, B:39:0x00e4, B:42:0x00ea, B:35:0x00ad, B:90:0x0210, B:92:0x0219, B:94:0x023e, B:98:0x024e, B:100:0x0252, B:101:0x0256), top: B:119:0x0010, inners: #5 }] */
    /* JADX WARN: Removed duplicated region for block: B:92:0x0219 A[Catch: all -> 0x024b, TryCatch #5 {all -> 0x024b, blocks: (B:90:0x0210, B:92:0x0219, B:94:0x023e, B:98:0x024e, B:100:0x0252, B:101:0x0256), top: B:128:0x0210, outer: #0 }] */
    /* JADX WARN: Removed duplicated region for block: B:98:0x024e A[Catch: all -> 0x024b, TryCatch #5 {all -> 0x024b, blocks: (B:90:0x0210, B:92:0x0219, B:94:0x023e, B:98:0x024e, B:100:0x0252, B:101:0x0256), top: B:128:0x0210, outer: #0 }] */
    /* JADX WARN: Type inference failed for: r2v11, types: [java.lang.Object, java.util.List] */
    /* JADX WARN: Type inference failed for: r6v2, types: [java.lang.Object, java.util.List] */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:50:0x0128 -> B:51:0x0133). Please report as a decompilation issue!!! */
    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object invokeSuspend(Object obj) {
        gf1 gf1Var;
        int i;
        BaseContentViewModel baseContentViewModel;
        mg6 mg6Var;
        List arrayList;
        dw7.a aVar;
        dw7.f fVar;
        zw8.a aVarE;
        int i2;
        String str;
        int i3;
        String str2;
        int i4;
        char c;
        gf1 gf1Var2;
        List list;
        dw7.f fVar2;
        dw7.a aVar2;
        boolean z;
        boolean z2;
        Object objI;
        dw7.f fVar3;
        dw7.a aVar3;
        List list2;
        BaseContentViewModel baseContentViewModel2;
        boolean z3;
        Object obj2;
        BaseContentViewModel baseContentViewModel3 = this.k;
        mg6 mg6Var2 = baseContentViewModel3.j;
        ck1 ck1Var = ck1.a;
        int i5 = this.j;
        e6a e6aVar = e6a.a;
        try {
        } catch (Exception e) {
            e = e;
        }
        try {
            if (i5 == 0) {
                s38.b(obj);
                this.a = mg6Var2;
                this.b = baseContentViewModel3;
                i = this.l;
                this.i = i;
                this.j = 1;
                if (mg6Var2.a(this) != ck1Var) {
                    baseContentViewModel = baseContentViewModel3;
                    mg6Var = mg6Var2;
                }
                return ck1Var;
            }
            if (i5 != 1) {
                if (i5 != 2) {
                    if (i5 != 3) {
                        throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                    }
                    baseContentViewModel2 = this.f;
                    mg6Var2 = (mg6) this.e;
                    aVar3 = this.d;
                    fVar3 = this.c;
                    list2 = (List) this.b;
                    gf1Var = (gf1) this.a;
                    s38.b(obj);
                    try {
                        if (list2.isEmpty()) {
                            gf1Var.k.addAll(list2);
                            gf1Var.l = fVar3.a;
                            gf1Var.m = aVar3.a;
                            baseContentViewModel2.k();
                            int iIndexOf = baseContentViewModel2.k.indexOf(Boxing.boxInt(baseContentViewModel2.i.indexOf(gf1Var)));
                            if (iIndexOf >= 0) {
                                baseContentViewModel2.n.j(new i68(iIndexOf, list2));
                            }
                        } else {
                            if (aVar3.a) {
                                Log.w("BaseContentViewModel", "Page " + fVar3.a + " returned no usable items for row " + gf1Var.b);
                                z3 = false;
                                gf1Var.m = false;
                                obj2 = null;
                                mg6Var2.c(obj2);
                                gf1Var.n = z3;
                                return e6aVar;
                            }
                            gf1Var.m = false;
                        }
                        obj2 = null;
                        z3 = false;
                        mg6Var2.c(obj2);
                        gf1Var.n = z3;
                        return e6aVar;
                    } catch (Throwable th) {
                        mg6Var2.c(null);
                        throw th;
                    }
                }
                int i6 = this.i;
                zw8.a aVar4 = (zw8.a) this.e;
                aVar = this.d;
                dw7.f fVar4 = this.c;
                List list3 = (List) this.b;
                gf1 gf1Var3 = (gf1) this.a;
                try {
                    s38.b(obj);
                    int i7 = i6;
                    z2 = true;
                    fVar = fVar4;
                    gf1Var = gf1Var3;
                    c = 2;
                    z = false;
                    objI = obj;
                    ContentLoaderUseCase.b bVar = (ContentLoaderUseCase.b) objI;
                    if (bVar.a.isEmpty()) {
                        List list4 = gf1Var.k;
                        int i8 = gf1Var.g;
                        List<ContentItem> list5 = list4;
                        ArrayList arrayList2 = new ArrayList(y41.p(list5, 10));
                        for (ContentItem contentItem : list5) {
                            arrayList2.add(Boxing.boxInt(contentItem.isContent() ? contentItem.getTmdbId() : contentItem.getId()));
                        }
                        Set setBr = y41.br(arrayList2);
                        List list6 = bVar.a;
                        ArrayList arrayList3 = new ArrayList();
                        Iterator it = list6.iterator();
                        while (it.hasNext()) {
                            zw8.a aVar5 = aVar4;
                            Object next = it.next();
                            ContentItem contentItem2 = (ContentItem) next;
                            Iterator it2 = it;
                            if (!setBr.contains(Boxing.boxInt(contentItem2.isContent() ? contentItem2.getTmdbId() : contentItem2.getId()))) {
                                arrayList3.add(next);
                            }
                            aVar4 = aVar5;
                            it = it2;
                        }
                        zw8.a aVar6 = aVar4;
                        String str3 = gf1Var.c;
                        Set set = BaseContentViewModel.ae;
                        list3.addAll(baseContentViewModel3.g(str3, arrayList3));
                        aVar.a = bVar.b >= i8 ? z2 : false;
                        if (list3.size() < i8 && aVar.a && i7 < 5) {
                            try {
                                fVar.a++;
                                i2 = i7 + 1;
                                aVarE = aVar6;
                                arrayList = list3;
                                try {
                                    ContentLoaderUseCase contentLoaderUseCase = baseContentViewModel3.b;
                                    String str4 = gf1Var.c;
                                    String str5 = gf1Var.d;
                                    int i9 = fVar.a;
                                    String str6 = gf1Var.e;
                                    str = aVarE == null ? aVarE.a : null;
                                    if (aVarE == null) {
                                        i3 = i9;
                                        str2 = aVarE.b;
                                    } else {
                                        i3 = i9;
                                        str2 = null;
                                    }
                                    this.a = gf1Var;
                                    this.b = arrayList;
                                    this.c = fVar;
                                    this.d = aVar;
                                    this.e = aVarE;
                                    this.i = i2;
                                    zw8.a aVar7 = aVarE;
                                    this.j = 2;
                                    objI = contentLoaderUseCase.i(str4, str5, i3, false, str6, str, str2, 20, this);
                                    if (objI != ck1Var) {
                                        gf1Var = gf1Var2;
                                        fVar = fVar2;
                                        list3 = list;
                                        aVar = aVar2;
                                        i7 = i4;
                                        aVar4 = aVar7;
                                        ContentLoaderUseCase.b bVar2 = (ContentLoaderUseCase.b) objI;
                                        if (bVar2.a.isEmpty()) {
                                        }
                                    }
                                    return ck1Var;
                                } catch (Exception e2) {
                                    e = e2;
                                }
                            } catch (Exception e3) {
                                e = e3;
                                gf1Var = gf1Var2;
                                baseContentViewModel3.r.l("Failed to load more: " + e.getMessage());
                                gf1Var.n = false;
                                return e6aVar;
                            }
                            i4 = i2;
                            c = 2;
                            String str7 = str;
                            gf1Var2 = gf1Var;
                            list = arrayList;
                            fVar2 = fVar;
                            aVar2 = aVar;
                            z = false;
                            z2 = true;
                        }
                    } else {
                        aVar.a = z;
                    }
                    this.a = gf1Var;
                    this.b = list3;
                    this.c = fVar;
                    this.d = aVar;
                    this.e = mg6Var2;
                    this.f = baseContentViewModel3;
                    this.j = 3;
                    if (mg6Var2.a(this) != ck1Var) {
                        fVar3 = fVar;
                        aVar3 = aVar;
                        list2 = list3;
                        baseContentViewModel2 = baseContentViewModel3;
                        if (list2.isEmpty()) {
                        }
                        obj2 = null;
                        z3 = false;
                        mg6Var2.c(obj2);
                        gf1Var.n = z3;
                        return e6aVar;
                    }
                    return ck1Var;
                } catch (Exception e4) {
                    e = e4;
                    gf1Var = gf1Var3;
                    baseContentViewModel3.r.l("Failed to load more: " + e.getMessage());
                    gf1Var.n = false;
                    return e6aVar;
                }
            }
            i = this.i;
            baseContentViewModel = (BaseContentViewModel) this.b;
            mg6Var = (mg6) this.a;
            s38.b(obj);
            Integer num = (Integer) y41.af(i, baseContentViewModel.k);
            if (num == null) {
                mg6Var.c(null);
                return e6aVar;
            }
            gf1Var = (gf1) y41.af(num.intValue(), baseContentViewModel.i);
            mg6Var.c(null);
            if (gf1Var != null && !gf1Var.n && gf1Var.m) {
                gf1Var.n = true;
                arrayList = new ArrayList();
                dw7.f fVar5 = new dw7.f();
                fVar5.a = gf1Var.l + 1;
                aVar = new dw7.a();
                aVar.a = true;
                List list7 = zw8.a;
                fVar = fVar5;
                aVarE = zw8.e(gf1Var.c, gf1Var.h, gf1Var.i, gf1Var.e);
                i2 = 0;
                ContentLoaderUseCase contentLoaderUseCase2 = baseContentViewModel3.b;
                String str42 = gf1Var.c;
                String str52 = gf1Var.d;
                int i92 = fVar.a;
                String str62 = gf1Var.e;
                if (aVarE == null) {
                }
                if (aVarE == null) {
                }
                this.a = gf1Var;
                this.b = arrayList;
                this.c = fVar;
                this.d = aVar;
                this.e = aVarE;
                this.i = i2;
                zw8.a aVar72 = aVarE;
                this.j = 2;
                i4 = i2;
                c = 2;
                String str72 = str;
                gf1Var2 = gf1Var;
                list = arrayList;
                fVar2 = fVar;
                aVar2 = aVar;
                z = false;
                z2 = true;
                objI = contentLoaderUseCase2.i(str42, str52, i3, false, str62, str72, str2, 20, this);
                if (objI != ck1Var) {
                }
                return ck1Var;
            }
            return e6aVar;
        } catch (Throwable th2) {
            mg6Var.c(null);
            throw th2;
        }
    }
}
