package com.strmr.ps.data.service;

import com.strmr.ps.data.model.debrid.DebridFileItem;
import com.strmr.ps.data.model.debrid.VideoExtensions;
import com.strmr.ps.data.repository.DebridLibraryRepository;
import defpackage.ak1;
import defpackage.bk1;
import defpackage.ck1;
import defpackage.cr4;
import defpackage.e6a;
import defpackage.gr3;
import defpackage.kq;
import defpackage.q38;
import defpackage.s38;
import defpackage.u75;
import defpackage.vo2;
import defpackage.vv1;
import defpackage.vx8;
import defpackage.y41;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.ExecutorService;
import kotlin.Metadata;
import kotlin.coroutines.Continuation;
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\u0004\u0018\u00010\u0001*\u00020\u0000H\n¢\u0006\u0004\b\u0002\u0010\u0003"}, d2 = {"Lak1;", "Lu75;", "<anonymous>", "(Lak1;)Lu75;"}, k = 3, mv = {2, 0, 0})
@DebugMetadata(c = "com.strmr.ps.data.service.DebridLibrarySearchService$drillIntoFolders$2$1$1", f = "DebridLibrarySearchService.kt", i = {}, l = {254, 274, 302}, m = "invokeSuspend", n = {}, s = {})
@vx8
final class o extends SuspendLambda implements gr3<ak1, Continuation<? super u75>, Object> {
    public int a;
    public final /* synthetic */ DebridLibrarySearchService b;
    public final /* synthetic */ String c;
    public final /* synthetic */ DebridFileItem.Folder d;
    public final /* synthetic */ boolean e;
    public final /* synthetic */ int f;
    public final /* synthetic */ int i;
    public final /* synthetic */ String j;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public o(int i, int i2, DebridFileItem.Folder folder, DebridLibrarySearchService debridLibrarySearchService, String str, String str2, Continuation continuation, boolean z) {
        super(2, continuation);
        this.b = debridLibrarySearchService;
        this.c = str;
        this.d = folder;
        this.e = z;
        this.f = i;
        this.i = i2;
        this.j = str2;
    }

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

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

    /* JADX WARN: Code restructure failed: missing block: B:106:0x0258, code lost:
    
        if (r0 == r2) goto L107;
     */
    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object invokeSuspend(Object obj) {
        Object obj2;
        Object objM101getFolderContents0E7RQCE;
        Object objC;
        Object next;
        Object objC2;
        ck1 ck1Var = ck1.a;
        int i = this.a;
        DebridLibrarySearchService debridLibrarySearchService = this.b;
        String str = this.c;
        DebridFileItem.Folder folder = this.d;
        Object obj3 = null;
        try {
            if (i == 0) {
                s38.b(obj);
                DebridLibraryRepository debridLibraryRepository = debridLibrarySearchService.a;
                this.a = 1;
                objM101getFolderContents0E7RQCE = debridLibraryRepository.m101getFolderContents0E7RQCE(str, folder, this);
                if (objM101getFolderContents0E7RQCE == ck1Var) {
                }
                return ck1Var;
            }
            if (i != 1) {
                if (i == 2) {
                    s38.b(obj);
                    objC2 = obj;
                    return y41.ab((List) objC2);
                }
                if (i != 3) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                s38.b(obj);
                objC = obj;
                return y41.ab((List) objC);
            }
            s38.b(obj);
            objM101getFolderContents0E7RQCE = ((q38) obj).a;
            if (objM101getFolderContents0E7RQCE instanceof q38.b) {
                ExecutorService executorService = kq.a;
                String name = folder.getName();
                Throwable thA = q38.a(objM101getFolderContents0E7RQCE);
                kq.h("DebridLibrarySearch", "[" + str + "] Drill-in FAILED for '" + name + "': " + (thA != null ? thA.getMessage() : null));
                return null;
            }
            if (objM101getFolderContents0E7RQCE instanceof q38.b) {
                objM101getFolderContents0E7RQCE = null;
            }
            List list = (List) objM101getFolderContents0E7RQCE;
            if (list == null) {
                return null;
            }
            ArrayList arrayList = new ArrayList();
            for (Object obj4 : list) {
                obj2 = obj3;
                try {
                    if (obj4 instanceof DebridFileItem.File) {
                        arrayList.add(obj4);
                    }
                    obj3 = obj2;
                } catch (Exception e) {
                    e = e;
                    ExecutorService executorService2 = kq.a;
                    kq.h("DebridLibrarySearch", "Failed to drill into folder " + folder.getName() + ": " + e.getMessage());
                    return obj2;
                }
            }
            Object obj5 = obj3;
            ArrayList arrayList2 = new ArrayList();
            for (Object obj6 : arrayList) {
                if (VideoExtensions.INSTANCE.isVideoFile(((DebridFileItem.File) obj6).getName())) {
                    arrayList2.add(obj6);
                }
            }
            ArrayList arrayList3 = new ArrayList();
            for (Object obj7 : list) {
                if (obj7 instanceof DebridFileItem.Folder) {
                    arrayList3.add(obj7);
                }
            }
            ExecutorService executorService3 = kq.a;
            kq.h("DebridLibrarySearch", "[" + str + "] Drill '" + folder.getName() + "': " + arrayList2.size() + " videos, " + arrayList3.size() + " subfolders");
            if (this.e) {
                Iterator it = arrayList2.iterator();
                if (it.hasNext()) {
                    next = it.next();
                    if (it.hasNext()) {
                        long size = ((DebridFileItem.File) next).getSize();
                        do {
                            Object next2 = it.next();
                            long size2 = ((DebridFileItem.File) next2).getSize();
                            if (size < size2) {
                                next = next2;
                                size = size2;
                            }
                        } while (it.hasNext());
                    }
                } else {
                    next = obj5;
                }
                DebridFileItem.File file = (DebridFileItem.File) next;
                if (file != null) {
                    return new u75(file);
                }
                if (arrayList3.isEmpty()) {
                    return obj5;
                }
                String str2 = this.c;
                boolean z = this.e;
                int i2 = this.f;
                int i3 = this.i;
                this.a = 2;
                objC2 = bk1.c(new r(arrayList3, z, i2, debridLibrarySearchService, str2, i3, null, null), this);
                if (objC2 == ck1Var) {
                }
                return y41.ab((List) objC2);
            }
            boolean zIsEmpty = arrayList2.isEmpty();
            int i4 = this.i;
            int i5 = this.f;
            if (!zIsEmpty) {
                ArrayList arrayList4 = new ArrayList(y41.p(arrayList2, 10));
                Iterator it2 = arrayList2.iterator();
                while (it2.hasNext()) {
                    arrayList4.add(((DebridFileItem.File) it2.next()).getName());
                }
                String strC = vo2.c(arrayList4, i5, i4, this.j);
                if (strC != null) {
                    for (Object obj8 : arrayList2) {
                        if (cr4.b(((DebridFileItem.File) obj8).getName(), strC)) {
                            return new u75((DebridFileItem.File) obj8);
                        }
                    }
                    throw new NoSuchElementException("Collection contains no element matching the predicate.");
                }
            }
            if (arrayList3.isEmpty()) {
                ExecutorService executorService4 = kq.a;
                kq.h("DebridLibrarySearch", "No episode S" + i5 + "E" + i4 + " found in folder: " + folder.getName());
                return obj5;
            }
            ExecutorService executorService5 = kq.a;
            kq.h("DebridLibrarySearch", "No episode at top level of '" + folder.getName() + "', checking " + arrayList3.size() + " subfolders");
            List listBj = y41.bj(new vv1(i5), arrayList3);
            String str3 = this.c;
            boolean z2 = this.e;
            int i6 = this.f;
            int i7 = this.i;
            String str4 = this.j;
            this.a = 3;
            objC = bk1.c(new r(listBj, z2, i6, debridLibrarySearchService, str3, i7, str4, null), this);
            return ck1Var;
        } catch (Exception e2) {
            e = e2;
            obj2 = null;
        }
    }
}
