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.ck1;
import defpackage.cr4;
import defpackage.e6a;
import defpackage.gr3;
import defpackage.kq;
import defpackage.q38;
import defpackage.s38;
import defpackage.tv1;
import defpackage.u75;
import defpackage.vo2;
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$drillSubfolders$2$1$1", f = "DebridLibrarySearchService.kt", i = {}, l = {342}, m = "invokeSuspend", n = {}, s = {})
@vx8
final class q extends SuspendLambda implements gr3<ak1, Continuation<? super u75>, Object> {
    public int a;
    public final /* synthetic */ boolean b;
    public final /* synthetic */ DebridFileItem.Folder c;
    public final /* synthetic */ int d;
    public final /* synthetic */ DebridLibrarySearchService e;
    public final /* synthetic */ String 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 q(int i, int i2, DebridFileItem.Folder folder, DebridLibrarySearchService debridLibrarySearchService, String str, String str2, Continuation continuation, boolean z) {
        super(2, continuation);
        this.b = z;
        this.c = folder;
        this.d = i;
        this.e = debridLibrarySearchService;
        this.f = str;
        this.i = i2;
        this.j = str2;
    }

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

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

    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    public final Object invokeSuspend(Object obj) {
        Object objM101getFolderContents0E7RQCE;
        Object next;
        int i = this.i;
        ck1 ck1Var = ck1.a;
        int i2 = this.a;
        boolean z = this.b;
        int i3 = this.d;
        DebridFileItem.Folder folder = this.c;
        try {
            if (i2 == 0) {
                s38.b(obj);
                if (!z && !tv1.a(i3, folder.getName()) && !tv1.b(i3, folder.getName())) {
                    return null;
                }
                DebridLibraryRepository debridLibraryRepository = this.e.a;
                String str = this.f;
                this.a = 1;
                objM101getFolderContents0E7RQCE = debridLibraryRepository.m101getFolderContents0E7RQCE(str, folder, this);
                if (objM101getFolderContents0E7RQCE == ck1Var) {
                    return ck1Var;
                }
            } else {
                if (i2 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                s38.b(obj);
                objM101getFolderContents0E7RQCE = ((q38) obj).a;
            }
            if (objM101getFolderContents0E7RQCE instanceof q38.b) {
                objM101getFolderContents0E7RQCE = null;
            }
            List list = (List) objM101getFolderContents0E7RQCE;
            if (list != null) {
                ArrayList arrayList = new ArrayList();
                for (Object obj2 : list) {
                    if (obj2 instanceof DebridFileItem.File) {
                        arrayList.add(obj2);
                    }
                }
                ArrayList arrayList2 = new ArrayList();
                for (Object obj3 : arrayList) {
                    if (VideoExtensions.INSTANCE.isVideoFile(((DebridFileItem.File) obj3).getName())) {
                        arrayList2.add(obj3);
                    }
                }
                if (!arrayList2.isEmpty()) {
                    if (z) {
                        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 = null;
                        }
                        DebridFileItem.File file = (DebridFileItem.File) next;
                        if (file != null) {
                            return new u75(file);
                        }
                    } else {
                        ArrayList arrayList3 = new ArrayList(y41.p(arrayList2, 10));
                        Iterator it2 = arrayList2.iterator();
                        while (it2.hasNext()) {
                            arrayList3.add(((DebridFileItem.File) it2.next()).getName());
                        }
                        String strC = vo2.c(arrayList3, i3, i, this.j);
                        if (strC != null) {
                            for (Object obj4 : arrayList2) {
                                if (cr4.b(((DebridFileItem.File) obj4).getName(), strC)) {
                                    DebridFileItem.File file2 = (DebridFileItem.File) obj4;
                                    ExecutorService executorService = kq.a;
                                    kq.h("DebridLibrarySearch", "Found S" + i3 + "E" + i + " in subfolder '" + folder.getName() + "': " + file2.getName());
                                    return new u75(file2);
                                }
                            }
                            throw new NoSuchElementException("Collection contains no element matching the predicate.");
                        }
                    }
                }
            }
            return null;
        } catch (Exception e) {
            ExecutorService executorService2 = kq.a;
            kq.h("DebridLibrarySearch", "Failed to drill subfolder " + folder.getName() + ": " + e.getMessage());
            return null;
        }
    }
}
