package com.strmr.ps.ui.details;

import android.util.Log;
import androidx.media3.container.NalUnitUtil;
import com.google.android.gms.actions.SearchIntents;
import com.strmr.ps.data.local.entity.PlaybackProgress;
import com.strmr.ps.data.model.ContentItem;
import com.strmr.ps.data.model.tmdb.TMDBSearchResponse;
import com.strmr.ps.data.model.tmdb.TMDBSearchResult;
import com.strmr.ps.data.remote.RateLimiter;
import com.strmr.ps.data.remote.api.TMDBApiService;
import defpackage.ak1;
import defpackage.ck1;
import defpackage.cr4;
import defpackage.e6a;
import defpackage.gr3;
import defpackage.n89;
import defpackage.s38;
import defpackage.vx8;
import defpackage.y41;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CancellationException;
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\u0010\b\n\u0002\b\u0002\u0010\u0002\u001a\u00020\u0001*\u00020\u0000H\n¢\u0006\u0004\b\u0002\u0010\u0003"}, d2 = {"Lak1;", "", "<anonymous>", "(Lak1;)I"}, k = 3, mv = {2, 0, 0})
@DebugMetadata(c = "com.strmr.ps.ui.details.DetailsFragment$searchTmdbByTitle$2", f = "DetailsFragment.kt", i = {0}, l = {1041, 1042}, m = "invokeSuspend", n = {SearchIntents.EXTRA_QUERY}, s = {"L$0"})
@vx8
final class af extends SuspendLambda implements gr3<ak1, Continuation<? super Integer>, Object> {
    public String a;
    public int b;
    public final /* synthetic */ String c;
    public final /* synthetic */ String d;
    public final /* synthetic */ DetailsFragment e;
    public final /* synthetic */ ContentItem.ItemType f;

    @Metadata(k = 3, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public /* synthetic */ class a {
        public static final /* synthetic */ int[] a;

        static {
            int[] iArr = new int[ContentItem.ItemType.values().length];
            try {
                iArr[ContentItem.ItemType.MOVIE.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                iArr[ContentItem.ItemType.TV_SHOW.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            a = iArr;
        }
    }

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public af(String str, String str2, DetailsFragment detailsFragment, ContentItem.ItemType itemType, Continuation continuation) {
        super(2, continuation);
        this.c = str;
        this.d = str2;
        this.e = detailsFragment;
        this.f = itemType;
    }

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

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

    /* JADX WARN: Code restructure failed: missing block: B:28:0x0072, code lost:
    
        if (r0 == r7) goto L29;
     */
    /* JADX WARN: Removed duplicated region for block: B:52:0x00c0 A[Catch: Exception -> 0x0017, CancellationException -> 0x001a, TryCatch #2 {CancellationException -> 0x001a, Exception -> 0x0017, blocks: (B:6:0x0012, B:30:0x0075, B:37:0x008e, B:39:0x0094, B:40:0x009a, B:42:0x00a0, B:46:0x00b3, B:57:0x00d7, B:50:0x00ba, B:52:0x00c0, B:53:0x00c8, B:55:0x00ce, B:15:0x0027, B:27:0x005e, B:19:0x0032, B:22:0x0039, B:24:0x004f), top: B:63:0x000c }] */
    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object invokeSuspend(Object obj) {
        String str;
        Object objMultiSearch$default;
        List<TMDBSearchResult> results;
        Object next;
        ck1 ck1Var = ck1.a;
        int i = this.b;
        int id = 0;
        DetailsFragment detailsFragment = this.e;
        String str2 = this.d;
        TMDBSearchResult tMDBSearchResult = null;
        try {
        } catch (CancellationException e) {
            throw e;
        } catch (Exception e2) {
            Log.w("DetailsFragment", "Failed to search TMDB for '" + str2 + "': " + e2.getMessage());
        }
        if (i == 0) {
            s38.b(obj);
            String str3 = this.c;
            if (str3 == null || n89.ab(str3)) {
                str = str2;
            } else {
                str = str2 + " " + str3;
            }
            RateLimiter rateLimiterT = detailsFragment.t();
            this.a = str;
            this.b = 1;
            if (rateLimiterT.a(this) == ck1Var) {
            }
            return ck1Var;
        }
        if (i != 1) {
            if (i != 2) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            s38.b(obj);
            objMultiSearch$default = obj;
            TMDBSearchResponse tMDBSearchResponse = (TMDBSearchResponse) objMultiSearch$default;
            int i2 = a.a[this.f.ordinal()];
            String str4 = i2 != 1 ? i2 != 2 ? null : "tv" : PlaybackProgress.TYPE_MOVIE;
            if (str4 != null) {
                List<TMDBSearchResult> results2 = tMDBSearchResponse.getResults();
                if (results2 != null) {
                    Iterator<T> it = results2.iterator();
                    while (true) {
                        if (!it.hasNext()) {
                            next = null;
                            break;
                        }
                        next = it.next();
                        if (cr4.b(((TMDBSearchResult) next).getMediaType(), str4)) {
                            break;
                        }
                    }
                    TMDBSearchResult tMDBSearchResult2 = (TMDBSearchResult) next;
                    if (tMDBSearchResult2 == null) {
                        results = tMDBSearchResponse.getResults();
                        if (results != null) {
                            tMDBSearchResult = (TMDBSearchResult) y41.ab(results);
                        }
                    } else {
                        tMDBSearchResult = tMDBSearchResult2;
                    }
                } else {
                    results = tMDBSearchResponse.getResults();
                    if (results != null) {
                    }
                }
                return Boxing.boxInt(id);
            }
            List<TMDBSearchResult> results3 = tMDBSearchResponse.getResults();
            if (results3 != null) {
                tMDBSearchResult = (TMDBSearchResult) y41.ab(results3);
            }
            if (tMDBSearchResult != null) {
                id = tMDBSearchResult.getId();
            }
            return Boxing.boxInt(id);
        }
        str = this.a;
        s38.b(obj);
        TMDBApiService tMDBApiServiceU = detailsFragment.u();
        this.a = null;
        this.b = 2;
        objMultiSearch$default = TMDBApiService.DefaultImpls.multiSearch$default(tMDBApiServiceU, str, 1, false, this, 4, null);
    }
}
