package com.strmr.ps.ui.ratings;

import android.util.Log;
import com.strmr.ps.data.repository.EpisodeRating;
import com.strmr.ps.data.repository.EpisodeRatingsRepository;
import defpackage.ak1;
import defpackage.ck1;
import defpackage.e6a;
import defpackage.gr3;
import defpackage.ig6;
import defpackage.ks7;
import defpackage.s38;
import defpackage.vx8;
import defpackage.y41;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
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.ratings.EpisodeRatingsViewModel$loadRatings$1", f = "EpisodeRatingsViewModel.kt", i = {}, l = {41}, m = "invokeSuspend", n = {}, s = {})
@vx8
final class d extends SuspendLambda implements gr3<ak1, Continuation<? super e6a>, Object> {
    public int a;
    public final /* synthetic */ EpisodeRatingsViewModel b;
    public final /* synthetic */ String c;
    public final /* synthetic */ int d;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public d(EpisodeRatingsViewModel episodeRatingsViewModel, String str, int i, Continuation continuation) {
        super(2, continuation);
        this.b = episodeRatingsViewModel;
        this.c = str;
        this.d = i;
    }

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

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

    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    public final Object invokeSuspend(Object obj) {
        int i;
        int i2;
        EpisodeRatingsViewModel episodeRatingsViewModel = this.b;
        ig6 ig6Var = episodeRatingsViewModel.b;
        ck1 ck1Var = ck1.a;
        int i3 = this.a;
        e6a e6aVar = e6a.a;
        try {
            if (i3 == 0) {
                s38.b(obj);
                ig6Var.setValue(new ks7(null, null, 0, null, 30));
                EpisodeRatingsRepository episodeRatingsRepository = episodeRatingsViewModel.a;
                String str = this.c;
                int i4 = this.d;
                this.a = 1;
                obj = episodeRatingsRepository.getRatings(str, i4, this);
                if (obj == ck1Var) {
                    return ck1Var;
                }
            } else {
                if (i3 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                s38.b(obj);
            }
            List list = (List) obj;
            if (list.isEmpty()) {
                ig6Var.setValue(new ks7("No ratings available", null, 0, null, 29));
                return e6aVar;
            }
            List list2 = list;
            ArrayList arrayList = new ArrayList(y41.p(list2, 10));
            Iterator it = list2.iterator();
            while (it.hasNext()) {
                arrayList.add(Boxing.boxInt(((EpisodeRating) it.next()).getSeason()));
            }
            List listBi = y41.bi(y41.t(arrayList));
            Iterator it2 = listBi.iterator();
            Integer num = null;
            if (it2.hasNext()) {
                int iIntValue = ((Number) it2.next()).intValue();
                List list3 = list;
                if ((list3 instanceof Collection) && list3.isEmpty()) {
                    i = 0;
                } else {
                    Iterator it3 = list3.iterator();
                    i = 0;
                    while (it3.hasNext()) {
                        if (((EpisodeRating) it3.next()).getSeason() == iIntValue && (i = i + 1) < 0) {
                            y41.bl();
                            throw null;
                        }
                    }
                }
                Integer numBoxInt = Boxing.boxInt(i);
                while (it2.hasNext()) {
                    int iIntValue2 = ((Number) it2.next()).intValue();
                    List list4 = list;
                    if ((list4 instanceof Collection) && list4.isEmpty()) {
                        i2 = 0;
                    } else {
                        Iterator it4 = list4.iterator();
                        i2 = 0;
                        while (it4.hasNext()) {
                            if (((EpisodeRating) it4.next()).getSeason() == iIntValue2 && (i2 = i2 + 1) < 0) {
                                y41.bl();
                                throw null;
                            }
                        }
                    }
                    Integer numBoxInt2 = Boxing.boxInt(i2);
                    if (numBoxInt.compareTo(numBoxInt2) < 0) {
                        numBoxInt = numBoxInt2;
                    }
                }
                num = numBoxInt;
            }
            ig6Var.setValue(new ks7(null, list, num != null ? num.intValue() : 0, listBi, 3));
            return e6aVar;
        } catch (Exception e) {
            Log.e("EpisodeRatingsVM", "Failed to load ratings", e);
            ig6Var.setValue(new ks7("Failed to load ratings", null, 0, null, 29));
            return e6aVar;
        }
    }
}
