package com.strmr.ps.ui.details;

import android.util.Log;
import androidx.media3.container.NalUnitUtil;
import androidx.media3.extractor.text.ttml.TtmlNode;
import com.strmr.ps.data.model.ContentItem;
import com.strmr.ps.data.model.tmdb.TMDBFindResponse;
import com.strmr.ps.data.model.tmdb.TMDBFindResult;
import com.strmr.ps.data.remote.RateLimiter;
import com.strmr.ps.data.remote.api.TMDBApiService;
import defpackage.ak1;
import defpackage.ck1;
import defpackage.e6a;
import defpackage.gr3;
import defpackage.n89;
import defpackage.s38;
import defpackage.vx8;
import defpackage.y41;
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$resolveTmdbIdFromImdb$2", f = "DetailsFragment.kt", i = {0}, l = {1076, 1077}, m = "invokeSuspend", n = {"normalizedId"}, s = {"L$0"})
@vx8
final class ac extends SuspendLambda implements gr3<ak1, Continuation<? super Integer>, Object> {
    public String a;
    public int b;
    public final /* synthetic */ String c;
    public final /* synthetic */ DetailsFragment d;
    public final /* synthetic */ ContentItem.ItemType e;

    @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 ac(String str, DetailsFragment detailsFragment, ContentItem.ItemType itemType, Continuation continuation) {
        super(2, continuation);
        this.c = str;
        this.d = detailsFragment;
        this.e = itemType;
    }

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

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

    /* JADX WARN: Can't wrap try/catch for region: R(10:0|2|71|(1:(1:(6:6|7|42|(1:(1:50))(2:55|(1:59))|66|67)(2:12|13))(2:14|15))(6:17|69|18|(4:20|34|(1:36)|41)(6:21|(2:22|(2:24|(1:74)(1:27))(3:73|28|(1:IC)(2:32|33)))|20|34|(0)(0)|41)|66|67)|16|37|38|72|39|(1:(0))) */
    /* JADX WARN: Code restructure failed: missing block: B:40:0x008f, code lost:
    
        if (r15 == r1) goto L41;
     */
    /* JADX WARN: Code restructure failed: missing block: B:51:0x00b6, code lost:
    
        r0 = e;
     */
    /* JADX WARN: Code restructure failed: missing block: B:53:0x00b9, code lost:
    
        r0 = e;
     */
    /* JADX WARN: Removed duplicated region for block: B:36:0x007c  */
    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object invokeSuspend(Object obj) {
        Exception exc;
        String str;
        RateLimiter rateLimiterT;
        ac acVar;
        TMDBFindResult tMDBFindResult;
        List<TMDBFindResult> tvResults;
        TMDBFindResult tMDBFindResult2;
        ck1 ck1Var = ck1.a;
        int i = this.b;
        DetailsFragment detailsFragment = this.d;
        int id = 0;
        String str2 = this.c;
        try {
        } catch (CancellationException e) {
            throw e;
        } catch (Exception e2) {
            exc = e2;
        }
        if (i == 0) {
            s38.b(obj);
            try {
                if (n89.ar(str2, TtmlNode.TAG_TT, false)) {
                    str = str2;
                    rateLimiterT = detailsFragment.t();
                    this.a = str;
                    this.b = 1;
                    if (rateLimiterT.a(this) != ck1Var) {
                    }
                    return ck1Var;
                }
                int i2 = 0;
                while (true) {
                    if (i2 >= str2.length()) {
                        int length = str2.length();
                        if (1 > length || length >= 11) {
                            break;
                        }
                        str = TtmlNode.TAG_TT + str2;
                    } else {
                        if (!Character.isDigit(str2.charAt(i2))) {
                            break;
                        }
                        i2++;
                    }
                }
                str = str2;
                rateLimiterT = detailsFragment.t();
                this.a = str;
                this.b = 1;
                if (rateLimiterT.a(this) != ck1Var) {
                }
                return ck1Var;
            } catch (CancellationException e3) {
                e = e3;
                throw e;
            } catch (Exception e4) {
                e = e4;
                exc = e;
                Log.e("DetailsFragment", "Failed to resolve TMDB ID from IMDB ID " + str2 + ": " + exc.getMessage());
                return Boxing.boxInt(id);
            }
            return Boxing.boxInt(id);
        }
        if (i != 1) {
            if (i != 2) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            s38.b(obj);
            acVar = this;
            TMDBFindResponse tMDBFindResponse = (TMDBFindResponse) obj;
            int i3 = a.a[acVar.e.ordinal()];
            if (i3 == 1) {
                List<TMDBFindResult> movieResults = tMDBFindResponse.getMovieResults();
                if (movieResults != null && (tMDBFindResult = (TMDBFindResult) y41.ab(movieResults)) != null) {
                    id = tMDBFindResult.getId();
                }
            } else if (i3 == 2 && (tvResults = tMDBFindResponse.getTvResults()) != null && (tMDBFindResult2 = (TMDBFindResult) y41.ab(tvResults)) != null) {
                id = tMDBFindResult2.getId();
            }
            return Boxing.boxInt(id);
        }
        str = this.a;
        s38.b(obj);
        String str3 = str;
        TMDBApiService tMDBApiServiceU = detailsFragment.u();
        this.a = null;
        this.b = 2;
        acVar = this;
        obj = TMDBApiService.DefaultImpls.findByExternalId$default(tMDBApiServiceU, str3, null, acVar, 2, null);
    }
}
