package com.strmr.ps.ui.usercollection;

import android.util.Log;
import androidx.credentials.exceptions.publickeycredential.DomExceptionUtils;
import androidx.lifecycle.ak;
import androidx.media3.container.NalUnitUtil;
import androidx.media3.exoplayer.rtsp.SessionDescription;
import androidx.media3.exoplayer.upstream.CmcdData;
import androidx.media3.extractor.text.ttml.TtmlNode;
import com.strmr.ps.data.model.ContentItem;
import com.strmr.ps.data.model.UserCollectionItem;
import com.strmr.ps.data.model.UserCollectionLink;
import com.strmr.ps.data.model.tmdb.TMDBFindResponse;
import com.strmr.ps.data.model.tmdb.TMDBFindResult;
import com.strmr.ps.data.remote.AddonCatalogClient;
import com.strmr.ps.data.remote.api.MdbListApiService;
import com.strmr.ps.data.remote.api.TMDBApiService;
import com.strmr.ps.data.remote.api.TraktApiService;
import com.strmr.ps.data.remote.model.mdblist.MdbListApiResponse;
import com.strmr.ps.data.repository.PreferencesRepository;
import com.strmr.ps.data.repository.WatchStatusRepository;
import dagger.hilt.android.lifecycle.HiltViewModel;
import defpackage.af1;
import defpackage.bk1;
import defpackage.bka;
import defpackage.ck1;
import defpackage.cr4;
import defpackage.dg8;
import defpackage.dy4;
import defpackage.e6a;
import defpackage.fi7;
import defpackage.gw2;
import defpackage.ik2;
import defpackage.kp6;
import defpackage.lo9;
import defpackage.n41;
import defpackage.n89;
import defpackage.pw7;
import defpackage.q07;
import defpackage.q78;
import defpackage.qo9;
import defpackage.re6;
import defpackage.s38;
import defpackage.tp5;
import defpackage.vw2;
import defpackage.vx8;
import defpackage.wj0;
import defpackage.x78;
import defpackage.xf8;
import defpackage.xja;
import defpackage.y41;
import defpackage.zw8;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CancellationException;
import javax.inject.Inject;
import kotlin.Metadata;
import kotlin.coroutines.jvm.internal.Boxing;
import kotlin.coroutines.jvm.internal.ContinuationImpl;

/* JADX INFO: loaded from: classes3.dex */
@HiltViewModel
@Metadata(d1 = {"\u00004\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0007\b\u0007\u0018\u00002\u00020\u0001:\u0003\u0012\u0013\u0014BA\b\u0007\u0012\u0006\u0010\u0003\u001a\u00020\u0002\u0012\u0006\u0010\u0005\u001a\u00020\u0004\u0012\u0006\u0010\u0007\u001a\u00020\u0006\u0012\u0006\u0010\t\u001a\u00020\b\u0012\u0006\u0010\u000b\u001a\u00020\n\u0012\u0006\u0010\r\u001a\u00020\f\u0012\u0006\u0010\u000f\u001a\u00020\u000e¢\u0006\u0004\b\u0010\u0010\u0011¨\u0006\u0015"}, d2 = {"Lcom/strmr/ps/ui/usercollection/UserCollectionContentViewModel;", "Lxja;", "Lcom/strmr/ps/data/remote/api/TraktApiService;", "traktApiService", "Lcom/strmr/ps/data/remote/api/MdbListApiService;", "mdbListApiService", "Lcom/strmr/ps/data/remote/api/TMDBApiService;", "tmdbApiService", "Lcom/strmr/ps/data/repository/WatchStatusRepository;", "watchStatusRepository", "Lcom/strmr/ps/data/repository/PreferencesRepository;", "preferencesRepository", "Lcom/strmr/ps/data/remote/AddonCatalogClient;", "addonCatalogClient", "Landroidx/lifecycle/ak;", "savedStateHandle", "<init>", "(Lcom/strmr/ps/data/remote/api/TraktApiService;Lcom/strmr/ps/data/remote/api/MdbListApiService;Lcom/strmr/ps/data/remote/api/TMDBApiService;Lcom/strmr/ps/data/repository/WatchStatusRepository;Lcom/strmr/ps/data/repository/PreferencesRepository;Lcom/strmr/ps/data/remote/AddonCatalogClient;Landroidx/lifecycle/ak;)V", "b", "c", CmcdData.OBJECT_TYPE_AUDIO_ONLY, "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
@vx8
public final class UserCollectionContentViewModel extends xja {
    public static final pw7 u = new pw7("/catalog/[^/]+/[^/]+\\.json$");
    public final TraktApiService a;
    public final MdbListApiService b;
    public final TMDBApiService c;
    public final WatchStatusRepository d;
    public final PreferencesRepository e;
    public final AddonCatalogClient f;
    public final String g;
    public final UserCollectionItem h;
    public final re6 i;
    public final re6 j;
    public final re6 k;
    public final re6 l;
    public final re6 m;
    public final re6 n;
    public final re6 o;
    public final re6 p;
    public final ArrayList q;
    public boolean r;
    public final xf8 s;
    public final String t;

    @Metadata(d1 = {"\u0000\u0010\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\b\u0086\u0003\u0018\u00002\u00020\u0001R\u0014\u0010\u0003\u001a\u00020\u00028\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u0003\u0010\u0004¨\u0006\u0005"}, d2 = {"Lcom/strmr/ps/ui/usercollection/UserCollectionContentViewModel$a;", "", "Lpw7;", "STREMIO_CATALOG_REGEX", "Lpw7;", "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public static final class a {
    }

    @Metadata(d1 = {"\u0000\n\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\b\u0002\u0018\u00002\u00020\u0001¨\u0006\u0002"}, d2 = {"Lcom/strmr/ps/ui/usercollection/UserCollectionContentViewModel$b;", "", "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public static final class b {
        public final UserCollectionLink a;
        public final int b;
        public String c;
        public String d;
        public final String e;
        public final String f;
        public int g = 1;
        public int h = 1;
        public boolean i = true;
        public boolean j = true;
        public final ArrayList k = new ArrayList();
        public final ArrayList l = new ArrayList();
        public final ArrayList m = new ArrayList();
        public final ArrayList n = new ArrayList();

        public b(UserCollectionLink userCollectionLink, int i, String str, String str2, String str3, String str4) {
            this.a = userCollectionLink;
            this.b = i;
            this.c = str;
            this.d = str2;
            this.e = str3;
            this.f = str4;
        }

        public final boolean a() {
            if (!this.l.isEmpty() || !this.m.isEmpty() || !this.n.isEmpty()) {
                return true;
            }
            if (this.e != null) {
                return this.i || this.j;
            }
            return false;
        }
    }

    @Metadata(d1 = {"\u0000\n\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\b\u0082\b\u0018\u00002\u00020\u0001¨\u0006\u0002"}, d2 = {"Lcom/strmr/ps/ui/usercollection/UserCollectionContentViewModel$c;", "", "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public static final /* data */ class c {
        public final String a;
        public final String b;
        public final String c;

        public c(String str, String str2, String str3) {
            cr4.g(str, "baseUrl");
            cr4.g(str2, SessionDescription.ATTR_TYPE);
            cr4.g(str3, "catalogId");
            this.a = str;
            this.b = str2;
            this.c = str3;
        }

        public final boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!(obj instanceof c)) {
                return false;
            }
            c cVar = (c) obj;
            return cr4.b(this.a, cVar.a) && cr4.b(this.b, cVar.b) && cr4.b(this.c, cVar.c);
        }

        public final int hashCode() {
            return this.c.hashCode() + vw2.b(this.a.hashCode() * 31, 31, this.b);
        }

        public final String toString() {
            return n41.s(fi7.m("StremioCatalogLink(baseUrl=", this.a, ", type=", this.b, ", catalogId="), this.c, ")");
        }
    }

    @Metadata(k = 3, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public /* synthetic */ class d {
        static {
            int[] iArr = new int[com.strmr.ps.ui.usercollection.a.values().length];
            try {
                iArr[0] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                com.strmr.ps.ui.usercollection.a aVar = com.strmr.ps.ui.usercollection.a.a;
                iArr[1] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                com.strmr.ps.ui.usercollection.a aVar2 = com.strmr.ps.ui.usercollection.a.a;
                iArr[2] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                com.strmr.ps.ui.usercollection.a aVar3 = com.strmr.ps.ui.usercollection.a.a;
                iArr[3] = 4;
            } catch (NoSuchFieldError unused4) {
            }
        }
    }

    @Inject
    public UserCollectionContentViewModel(@kp6 TraktApiService traktApiService, @kp6 MdbListApiService mdbListApiService, @kp6 TMDBApiService tMDBApiService, @kp6 WatchStatusRepository watchStatusRepository, @kp6 PreferencesRepository preferencesRepository, @kp6 AddonCatalogClient addonCatalogClient, @kp6 ak akVar) {
        UserCollectionItem userCollectionItem;
        String id;
        cr4.g(traktApiService, "traktApiService");
        cr4.g(mdbListApiService, "mdbListApiService");
        cr4.g(tMDBApiService, "tmdbApiService");
        cr4.g(watchStatusRepository, "watchStatusRepository");
        cr4.g(preferencesRepository, "preferencesRepository");
        cr4.g(addonCatalogClient, "addonCatalogClient");
        cr4.g(akVar, "savedStateHandle");
        this.a = traktApiService;
        this.b = mdbListApiService;
        this.c = tMDBApiService;
        this.d = watchStatusRepository;
        this.e = preferencesRepository;
        this.f = addonCatalogClient;
        String str = (String) akVar.b("collection_title");
        this.g = str == null ? "Collection" : str;
        String str2 = (String) akVar.b("collection_item_json");
        if (str2 != null) {
            try {
                dy4.a aVar = dy4.d;
                aVar.getClass();
                userCollectionItem = (UserCollectionItem) aVar.c(UserCollectionItem.INSTANCE.serializer(), str2);
            } catch (Exception e) {
                Log.e("UserCollectionVM", "Failed to parse collection item JSON", e);
                userCollectionItem = null;
            }
        } else {
            userCollectionItem = null;
        }
        this.h = userCollectionItem;
        re6 re6Var = new re6(ik2.a);
        this.i = re6Var;
        this.j = re6Var;
        re6 re6Var2 = new re6(Boolean.FALSE);
        this.k = re6Var2;
        this.l = re6Var2;
        re6 re6Var3 = new re6();
        this.m = re6Var3;
        this.n = re6Var3;
        re6 re6Var4 = new re6();
        this.o = re6Var4;
        this.p = re6Var4;
        this.q = new ArrayList();
        this.s = dg8.a(8);
        this.t = (userCollectionItem == null || (id = userCollectionItem.getId()) == null) ? "unknown" : id;
        PreferencesRepository preferencesRepository2 = this.e;
        if (userCollectionItem != null) {
            userCollectionItem.getName();
            userCollectionItem.getResolvedLinks().size();
            List<UserCollectionLink> resolvedLinks = userCollectionItem.getResolvedLinks();
            ArrayList arrayList = new ArrayList(y41.p(resolvedLinks, 10));
            Iterator<T> it = resolvedLinks.iterator();
            while (it.hasNext()) {
                arrayList.add(((UserCollectionLink) it.next()).getUrl());
            }
            arrayList.toString();
            int i = 0;
            for (Object obj : userCollectionItem.getResolvedLinks()) {
                int i2 = i + 1;
                if (i < 0) {
                    y41.bm();
                    throw null;
                }
                UserCollectionLink userCollectionLink = (UserCollectionLink) obj;
                String strS = s(i);
                q07 q07VarP = i(userCollectionLink.getUrl()) == com.strmr.ps.ui.usercollection.a.a ? p(userCollectionLink.getUrl()) : null;
                this.q.add(new b(userCollectionLink, i, preferencesRepository2.getSubPageSortBy(strS), preferencesRepository2.getSubPageSortHow(strS), q07VarP != null ? (String) q07VarP.a : null, q07VarP != null ? (String) q07VarP.b : null));
                i = i2;
            }
        }
        if (this.h == null) {
            this.m.l("Collection item not found");
        } else {
            wj0.c(bka.a(this), null, null, new v(this, null), 3);
        }
    }

    public static final ArrayList b(UserCollectionContentViewModel userCollectionContentViewModel, List list) {
        int iMin = Math.min(20, list.size());
        ArrayList arrayList = new ArrayList(list.subList(0, iMin));
        list.subList(0, iMin).clear();
        return arrayList;
    }

    /* JADX WARN: Removed duplicated region for block: B:8:0x0018  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final Object c(UserCollectionContentViewModel userCollectionContentViewModel, String str, String str2, int i, ContinuationImpl continuationImpl) {
        m mVar;
        String str3;
        String str4;
        int i2;
        if (continuationImpl instanceof m) {
            mVar = (m) continuationImpl;
            int i3 = mVar.f;
            if ((i3 & Integer.MIN_VALUE) != 0) {
                mVar.f = i3 - Integer.MIN_VALUE;
            } else {
                mVar = new m(userCollectionContentViewModel, continuationImpl);
            }
        }
        m mVar2 = mVar;
        Object listMovies$default = mVar2.d;
        ck1 ck1Var = ck1.a;
        int i4 = mVar2.f;
        try {
            if (i4 == 0) {
                s38.b(listMovies$default);
                try {
                    TraktApiService traktApiService = userCollectionContentViewModel.a;
                    mVar2.a = str;
                    mVar2.b = str2;
                    mVar2.c = i;
                    mVar2.f = 1;
                    listMovies$default = TraktApiService.DefaultImpls.getListMovies$default(traktApiService, str, str2, null, null, i, 20, null, null, null, mVar2, 460, null);
                    if (listMovies$default == ck1Var) {
                        return ck1Var;
                    }
                    str3 = str;
                    str4 = str2;
                    i2 = i;
                } catch (Exception e) {
                    e = e;
                    str3 = str;
                    str4 = str2;
                    i2 = i;
                    StringBuilder sbM = fi7.m("Error fetching Trakt movies ", str3, DomExceptionUtils.SEPARATOR, str4, " page ");
                    sbM.append(i2);
                    Log.e("UserCollectionVM", sbM.toString(), e);
                    return ik2.a;
                }
            } else {
                if (i4 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                i2 = mVar2.c;
                str4 = mVar2.b;
                str3 = mVar2.a;
                try {
                    s38.b(listMovies$default);
                } catch (Exception e2) {
                    e = e2;
                    StringBuilder sbM2 = fi7.m("Error fetching Trakt movies ", str3, DomExceptionUtils.SEPARATOR, str4, " page ");
                    sbM2.append(i2);
                    Log.e("UserCollectionVM", sbM2.toString(), e);
                    return ik2.a;
                }
            }
            return (List) listMovies$default;
        } catch (CancellationException e3) {
            throw e3;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:8:0x0018  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final Object d(UserCollectionContentViewModel userCollectionContentViewModel, String str, String str2, int i, ContinuationImpl continuationImpl) {
        n nVar;
        String str3;
        String str4;
        int i2;
        if (continuationImpl instanceof n) {
            nVar = (n) continuationImpl;
            int i3 = nVar.f;
            if ((i3 & Integer.MIN_VALUE) != 0) {
                nVar.f = i3 - Integer.MIN_VALUE;
            } else {
                nVar = new n(userCollectionContentViewModel, continuationImpl);
            }
        }
        n nVar2 = nVar;
        Object listShows$default = nVar2.d;
        ck1 ck1Var = ck1.a;
        int i4 = nVar2.f;
        try {
            if (i4 == 0) {
                s38.b(listShows$default);
                try {
                    TraktApiService traktApiService = userCollectionContentViewModel.a;
                    nVar2.a = str;
                    nVar2.b = str2;
                    nVar2.c = i;
                    nVar2.f = 1;
                    listShows$default = TraktApiService.DefaultImpls.getListShows$default(traktApiService, str, str2, null, null, i, 20, null, null, null, nVar2, 460, null);
                    if (listShows$default == ck1Var) {
                        return ck1Var;
                    }
                    str3 = str;
                    str4 = str2;
                    i2 = i;
                } catch (Exception e) {
                    e = e;
                    str3 = str;
                    str4 = str2;
                    i2 = i;
                    StringBuilder sbM = fi7.m("Error fetching Trakt shows ", str3, DomExceptionUtils.SEPARATOR, str4, " page ");
                    sbM.append(i2);
                    Log.e("UserCollectionVM", sbM.toString(), e);
                    return ik2.a;
                }
            } else {
                if (i4 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                i2 = nVar2.c;
                str4 = nVar2.b;
                str3 = nVar2.a;
                try {
                    s38.b(listShows$default);
                } catch (Exception e2) {
                    e = e2;
                    StringBuilder sbM2 = fi7.m("Error fetching Trakt shows ", str3, DomExceptionUtils.SEPARATOR, str4, " page ");
                    sbM2.append(i2);
                    Log.e("UserCollectionVM", sbM2.toString(), e);
                    return ik2.a;
                }
            }
            return (List) listShows$default;
        } catch (CancellationException e3) {
            throw e3;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0016  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final Object e(UserCollectionContentViewModel userCollectionContentViewModel, int i, String str, Integer num, ContinuationImpl continuationImpl) {
        p pVar;
        Exception exc;
        userCollectionContentViewModel.getClass();
        if (continuationImpl instanceof p) {
            pVar = (p) continuationImpl;
            int i2 = pVar.d;
            if ((i2 & Integer.MIN_VALUE) != 0) {
                pVar.d = i2 - Integer.MIN_VALUE;
            } else {
                pVar = new p(userCollectionContentViewModel, continuationImpl);
            }
        }
        Object objB = pVar.b;
        ck1 ck1Var = ck1.a;
        int i3 = pVar.d;
        try {
            if (i3 == 0) {
                s38.b(objB);
                try {
                } catch (lo9 unused) {
                } catch (Exception e) {
                    e = e;
                    exc = e;
                    Log.w("UserCollectionVM", "Failed to fetch movie details for TMDB ID: " + i, exc);
                    return null;
                }
                try {
                    q qVar = new q(userCollectionContentViewModel, i, str, num, null);
                    pVar.a = i;
                    pVar.d = 1;
                    objB = qo9.b(10000L, qVar, pVar);
                    if (objB == ck1Var) {
                        return ck1Var;
                    }
                    i = i;
                } catch (lo9 unused2) {
                    i = i;
                    n41.x(i, "Timeout fetching movie details for TMDB ID: ", "UserCollectionVM");
                    return null;
                } catch (Exception e2) {
                    exc = e2;
                    i = i;
                    Log.w("UserCollectionVM", "Failed to fetch movie details for TMDB ID: " + i, exc);
                    return null;
                }
            } else {
                if (i3 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                i = pVar.a;
                try {
                    s38.b(objB);
                } catch (lo9 unused3) {
                    n41.x(i, "Timeout fetching movie details for TMDB ID: ", "UserCollectionVM");
                    return null;
                } catch (Exception e3) {
                    e = e3;
                    exc = e;
                    Log.w("UserCollectionVM", "Failed to fetch movie details for TMDB ID: " + i, exc);
                    return null;
                }
            }
            return (ContentItem) objB;
        } catch (CancellationException e4) {
            throw e4;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0016  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final Object f(UserCollectionContentViewModel userCollectionContentViewModel, int i, String str, Integer num, ContinuationImpl continuationImpl) {
        r rVar;
        Exception exc;
        userCollectionContentViewModel.getClass();
        if (continuationImpl instanceof r) {
            rVar = (r) continuationImpl;
            int i2 = rVar.d;
            if ((i2 & Integer.MIN_VALUE) != 0) {
                rVar.d = i2 - Integer.MIN_VALUE;
            } else {
                rVar = new r(userCollectionContentViewModel, continuationImpl);
            }
        }
        Object objB = rVar.b;
        ck1 ck1Var = ck1.a;
        int i3 = rVar.d;
        try {
            if (i3 == 0) {
                s38.b(objB);
                try {
                } catch (lo9 unused) {
                } catch (Exception e) {
                    e = e;
                    exc = e;
                    Log.w("UserCollectionVM", "Failed to fetch show details for TMDB ID: " + i, exc);
                    return null;
                }
                try {
                    s sVar = new s(userCollectionContentViewModel, i, str, num, null);
                    rVar.a = i;
                    rVar.d = 1;
                    objB = qo9.b(10000L, sVar, rVar);
                    if (objB == ck1Var) {
                        return ck1Var;
                    }
                    i = i;
                } catch (lo9 unused2) {
                    i = i;
                    n41.x(i, "Timeout fetching show details for TMDB ID: ", "UserCollectionVM");
                    return null;
                } catch (Exception e2) {
                    exc = e2;
                    i = i;
                    Log.w("UserCollectionVM", "Failed to fetch show details for TMDB ID: " + i, exc);
                    return null;
                }
            } else {
                if (i3 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                i = rVar.a;
                try {
                    s38.b(objB);
                } catch (lo9 unused3) {
                    n41.x(i, "Timeout fetching show details for TMDB ID: ", "UserCollectionVM");
                    return null;
                } catch (Exception e3) {
                    e = e3;
                    exc = e;
                    Log.w("UserCollectionVM", "Failed to fetch show details for TMDB ID: " + i, exc);
                    return null;
                }
            }
            return (ContentItem) objB;
        } catch (CancellationException e4) {
            throw e4;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:8:0x0017  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final Object g(UserCollectionContentViewModel userCollectionContentViewModel, String str, boolean z, ContinuationImpl continuationImpl) {
        ad adVar;
        TMDBFindResult tMDBFindResult;
        TMDBFindResult tMDBFindResult2;
        userCollectionContentViewModel.getClass();
        if (continuationImpl instanceof ad) {
            adVar = (ad) continuationImpl;
            int i = adVar.d;
            if ((i & Integer.MIN_VALUE) != 0) {
                adVar.d = i - Integer.MIN_VALUE;
            } else {
                adVar = new ad(userCollectionContentViewModel, continuationImpl);
            }
        }
        ad adVar2 = adVar;
        Object objFindByExternalId$default = adVar2.b;
        ck1 ck1Var = ck1.a;
        int i2 = adVar2.d;
        if (i2 == 0) {
            s38.b(objFindByExternalId$default);
            if (str == null || !n89.ar(str, TtmlNode.TAG_TT, false)) {
                return null;
            }
            TMDBApiService tMDBApiService = userCollectionContentViewModel.c;
            adVar2.a = z;
            adVar2.d = 1;
            objFindByExternalId$default = TMDBApiService.DefaultImpls.findByExternalId$default(tMDBApiService, str, null, adVar2, 2, null);
            if (objFindByExternalId$default == ck1Var) {
                return ck1Var;
            }
        } else {
            if (i2 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            z = adVar2.a;
            s38.b(objFindByExternalId$default);
        }
        TMDBFindResponse tMDBFindResponse = (TMDBFindResponse) objFindByExternalId$default;
        if (z) {
            List<TMDBFindResult> movieResults = tMDBFindResponse.getMovieResults();
            if (movieResults == null || (tMDBFindResult2 = (TMDBFindResult) y41.ab(movieResults)) == null) {
                return null;
            }
            return Boxing.boxInt(tMDBFindResult2.getId());
        }
        List<TMDBFindResult> tvResults = tMDBFindResponse.getTvResults();
        if (tvResults == null || (tMDBFindResult = (TMDBFindResult) y41.ab(tvResults)) == null) {
            return null;
        }
        return Boxing.boxInt(tMDBFindResult.getId());
    }

    public static com.strmr.ps.ui.usercollection.a i(String str) {
        return n89.l(str, "trakt.tv/users/", false) ? com.strmr.ps.ui.usercollection.a.a : n89.l(str, "mdblist.com/lists/", false) ? com.strmr.ps.ui.usercollection.a.b : (n89.ar(str, "https://", false) && u.a(str)) ? com.strmr.ps.ui.usercollection.a.c : com.strmr.ps.ui.usercollection.a.d;
    }

    public static q07 o(String str) {
        tp5 tp5VarB = new pw7("mdblist\\.com/lists/([^/]+)/([^/?]+)").b(str);
        if (tp5VarB != null) {
            return new q07(tp5VarB.b().get(1), tp5VarB.b().get(2));
        }
        return null;
    }

    public static q07 p(String str) {
        tp5 tp5VarB = new pw7("trakt\\.tv/users/([^/]+)/lists/([^/?]+)").b(str);
        if (tp5VarB != null) {
            return new q07(tp5VarB.b().get(1), tp5VarB.b().get(2));
        }
        return null;
    }

    public final void h(String str, String str2, String str3) {
        ContentItem contentItem;
        Object next;
        af1 af1Var;
        List list;
        if (str == null) {
            return;
        }
        Iterator it = this.q.iterator();
        while (true) {
            contentItem = null;
            if (!it.hasNext()) {
                next = null;
                break;
            } else {
                next = it.next();
                if (cr4.b(s(((b) next).b), str)) {
                    break;
                }
            }
        }
        b bVar = (b) next;
        if (bVar == null) {
            return;
        }
        bVar.c = str2;
        bVar.d = str3;
        this.e.setSubPageSort(str, str2, str3);
        q();
        List list2 = (List) this.i.e();
        if (list2 != null && (af1Var = (af1) y41.ab(list2)) != null && (list = af1Var.b) != null) {
            contentItem = (ContentItem) y41.ab(list);
        }
        this.o.l(contentItem);
    }

    /* JADX WARN: Code restructure failed: missing block: B:47:0x00bc, code lost:
    
        if (r12 == r3) goto L48;
     */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0017  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object j(String str, ContinuationImpl continuationImpl) {
        o oVar;
        if (continuationImpl instanceof o) {
            oVar = (o) continuationImpl;
            int i = oVar.d;
            if ((i & Integer.MIN_VALUE) != 0) {
                oVar.d = i - Integer.MIN_VALUE;
            } else {
                oVar = new o(this, continuationImpl);
            }
        }
        Object listItemsByApiId = oVar.b;
        ck1 ck1Var = ck1.a;
        int i2 = oVar.d;
        ik2 ik2Var = ik2.a;
        try {
            try {
                if (i2 != 0) {
                    if (i2 == 1) {
                        str = oVar.a;
                        s38.b(listItemsByApiId);
                        return (List) listItemsByApiId;
                    }
                    if (i2 != 2) {
                        throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                    }
                    String str2 = oVar.a;
                    s38.b(listItemsByApiId);
                    MdbListApiResponse mdbListApiResponse = (MdbListApiResponse) listItemsByApiId;
                    return y41.au(mdbListApiResponse.getShows(), mdbListApiResponse.getMovies());
                }
                s38.b(listItemsByApiId);
                q07 q07VarO = o(str);
                MdbListApiService mdbListApiService = this.b;
                if (q07VarO != null) {
                    String str3 = (String) q07VarO.a;
                    String str4 = (String) q07VarO.b;
                    oVar.a = str;
                    oVar.d = 1;
                    listItemsByApiId = mdbListApiService.getListItems(str3, str4, oVar);
                    if (listItemsByApiId == ck1Var) {
                    }
                    return (List) listItemsByApiId;
                }
                tp5 tp5VarB = new pw7("mdblist\\.com/lists/(\\d+)/?$").b(str);
                String str5 = tp5VarB != null ? (String) tp5VarB.b().get(1) : null;
                if (str5 == null || n89.ab("")) {
                    return ik2Var;
                }
                oVar.a = str5;
                oVar.d = 2;
                listItemsByApiId = mdbListApiService.getListItemsByApiId("https://api.mdblist.com/lists/" + str5 + "/items?apikey=", oVar);
                return ck1Var;
            } catch (CancellationException e) {
                throw e;
            } catch (Exception e2) {
                Log.e("UserCollectionVM", "Error fetching MDBList " + str, e2);
                return ik2Var;
            }
        } catch (CancellationException e3) {
            throw e3;
        } catch (Exception e4) {
            Log.w("UserCollectionVM", "MDBList numeric ID " + str + " fetch failed", e4);
            return ik2Var;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object k(b bVar, ContinuationImpl continuationImpl) {
        w wVar;
        UserCollectionContentViewModel userCollectionContentViewModel;
        UserCollectionContentViewModel userCollectionContentViewModel2;
        if (continuationImpl instanceof w) {
            wVar = (w) continuationImpl;
            int i = wVar.e;
            if ((i & Integer.MIN_VALUE) != 0) {
                wVar.e = i - Integer.MIN_VALUE;
            } else {
                wVar = new w(this, continuationImpl);
            }
        }
        Object objJ = wVar.c;
        ck1 ck1Var = ck1.a;
        int i2 = wVar.e;
        e6a e6aVar = e6a.a;
        if (i2 == 0) {
            s38.b(objJ);
            String url = bVar.a.getUrl();
            wVar.a = this;
            wVar.b = bVar;
            wVar.e = 1;
            objJ = j(url, wVar);
            if (objJ != ck1Var) {
                userCollectionContentViewModel = this;
            }
            return ck1Var;
        }
        if (i2 != 1) {
            if (i2 != 2) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            bVar = (b) wVar.b;
            userCollectionContentViewModel2 = wVar.a;
            s38.b(objJ);
            List list = (List) objJ;
            bVar.k.addAll(list);
            userCollectionContentViewModel2.q();
            list.size();
            bVar.m.size();
            bVar.a.getUrl();
            return e6aVar;
        }
        bVar = (b) wVar.b;
        userCollectionContentViewModel = wVar.a;
        s38.b(objJ);
        List list2 = (List) objJ;
        if (list2.isEmpty()) {
            return e6aVar;
        }
        List list3 = list2;
        List listBk = y41.bk(list3, 20);
        if (list2.size() > 20) {
            bVar.m.addAll(y41.u(list3, 20));
        }
        wVar.a = userCollectionContentViewModel;
        wVar.b = bVar;
        wVar.e = 2;
        userCollectionContentViewModel.getClass();
        objJ = bk1.c(new g(listBk, userCollectionContentViewModel, null), wVar);
        if (objJ != ck1Var) {
            userCollectionContentViewModel2 = userCollectionContentViewModel;
            List list4 = (List) objJ;
            bVar.k.addAll(list4);
            userCollectionContentViewModel2.q();
            list4.size();
            bVar.m.size();
            bVar.a.getUrl();
            return e6aVar;
        }
        return ck1Var;
    }

    public final void l() {
        if (this.r || cr4.b(this.k.e(), Boolean.TRUE)) {
            return;
        }
        ArrayList arrayList = this.q;
        if (arrayList == null || !arrayList.isEmpty()) {
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                if (((b) it.next()).a()) {
                    this.r = true;
                    wj0.c(bka.a(this), null, null, new x(this, null), 3);
                    return;
                }
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:8:0x0014  */
    /* JADX WARN: Type inference failed for: r12v12, types: [java.lang.Object, java.util.List] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object m(b bVar, ContinuationImpl continuationImpl) {
        y yVar;
        UserCollectionContentViewModel userCollectionContentViewModel;
        b bVar2;
        List list;
        if (continuationImpl instanceof y) {
            yVar = (y) continuationImpl;
            int i = yVar.f;
            if ((i & Integer.MIN_VALUE) != 0) {
                yVar.f = i - Integer.MIN_VALUE;
            } else {
                yVar = new y(this, continuationImpl);
            }
        }
        y yVar2 = yVar;
        Object objB = yVar2.d;
        ck1 ck1Var = ck1.a;
        int i2 = yVar2.f;
        e6a e6aVar = e6a.a;
        if (i2 == 0) {
            s38.b(objB);
            tp5 tp5VarB = new pw7("(.+)/catalog/([^/]+)/([^/]+)\\.json").b(bVar.a.getUrl());
            c cVar = tp5VarB != null ? new c((String) tp5VarB.b().get(1), (String) tp5VarB.b().get(2), (String) tp5VarB.b().get(3)) : null;
            if (cVar != null) {
                String str = cVar.a;
                String str2 = cVar.b;
                String str3 = cVar.c;
                yVar2.a = this;
                yVar2.b = bVar;
                yVar2.f = 1;
                objB = this.f.b(str, str2, str3, 0, yVar2);
                if (objB != ck1Var) {
                    userCollectionContentViewModel = this;
                }
                return ck1Var;
            }
            return e6aVar;
        }
        if (i2 != 1) {
            if (i2 != 2) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            list = yVar2.c;
            bVar2 = (b) yVar2.b;
            userCollectionContentViewModel = yVar2.a;
            s38.b(objB);
            bVar2.k.removeAll(y41.br(list));
            bVar2.k.addAll((List) objB);
            userCollectionContentViewModel.q();
            return e6aVar;
        }
        bVar = (b) yVar2.b;
        userCollectionContentViewModel = yVar2.a;
        s38.b(objB);
        ?? r12 = ((gw2) objB).a;
        if (!r12.isEmpty()) {
            Iterable iterable = (Iterable) r12;
            List listBk = y41.bk(iterable, 20);
            if (r12.size() > 20) {
                bVar.n.addAll(y41.u(iterable, 20));
            }
            bVar.k.addAll(listBk);
            userCollectionContentViewModel.q();
            listBk.size();
            bVar.n.size();
            yVar2.a = userCollectionContentViewModel;
            yVar2.b = bVar;
            yVar2.c = listBk;
            yVar2.f = 2;
            objB = bk1.c(new i(listBk, userCollectionContentViewModel, null), yVar2);
            if (objB != ck1Var) {
                bVar2 = bVar;
                list = listBk;
                bVar2.k.removeAll(y41.br(list));
                bVar2.k.addAll((List) objB);
                userCollectionContentViewModel.q();
                return e6aVar;
            }
            return ck1Var;
        }
        return e6aVar;
    }

    /* JADX WARN: Removed duplicated region for block: B:8:0x0014  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object n(b bVar, ContinuationImpl continuationImpl) {
        z zVar;
        String str;
        UserCollectionContentViewModel userCollectionContentViewModel;
        b bVar2;
        String str2;
        ArrayList arrayList;
        b bVar3;
        UserCollectionContentViewModel userCollectionContentViewModel2;
        if (continuationImpl instanceof z) {
            zVar = (z) continuationImpl;
            int i = zVar.j;
            if ((i & Integer.MIN_VALUE) != 0) {
                zVar.j = i - Integer.MIN_VALUE;
            } else {
                zVar = new z(this, continuationImpl);
            }
        }
        z zVar2 = zVar;
        Object objC = zVar2.f;
        ck1 ck1Var = ck1.a;
        int i2 = zVar2.j;
        e6a e6aVar = e6a.a;
        if (i2 == 0) {
            s38.b(objC);
            String str3 = bVar.e;
            if (str3 != null && (str = bVar.f) != null) {
                ArrayList arrayList2 = new ArrayList();
                ac acVar = new ac(this, str3, str, bVar, arrayList2, null);
                zVar2.a = this;
                zVar2.b = bVar;
                zVar2.c = str3;
                zVar2.d = str;
                zVar2.e = arrayList2;
                zVar2.j = 1;
                if (bk1.c(acVar, zVar2) != ck1Var) {
                    userCollectionContentViewModel = this;
                    bVar2 = bVar;
                    str2 = str3;
                    arrayList = arrayList2;
                }
                return ck1Var;
            }
            return e6aVar;
        }
        if (i2 != 1) {
            if (i2 != 2) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            bVar3 = (b) zVar2.b;
            userCollectionContentViewModel2 = zVar2.a;
            s38.b(objC);
            List list = (List) objC;
            bVar3.k.addAll(list);
            userCollectionContentViewModel2.q();
            list.size();
            bVar3.l.size();
            return e6aVar;
        }
        arrayList = zVar2.e;
        str = zVar2.d;
        String str4 = zVar2.c;
        b bVar4 = (b) zVar2.b;
        userCollectionContentViewModel = zVar2.a;
        s38.b(objC);
        str2 = str4;
        bVar2 = bVar4;
        if (!arrayList.isEmpty()) {
            List listBk = y41.bk(arrayList, 20);
            if (arrayList.size() > 20) {
                bVar2.l.addAll(y41.u(arrayList, 20));
            }
            zVar2.a = userCollectionContentViewModel;
            zVar2.b = bVar2;
            zVar2.c = str2;
            zVar2.d = str;
            zVar2.e = null;
            zVar2.j = 2;
            userCollectionContentViewModel.getClass();
            objC = bk1.c(new l(listBk, userCollectionContentViewModel, null), zVar2);
            if (objC != ck1Var) {
                bVar3 = bVar2;
                userCollectionContentViewModel2 = userCollectionContentViewModel;
                List list2 = (List) objC;
                bVar3.k.addAll(list2);
                userCollectionContentViewModel2.q();
                list2.size();
                bVar3.l.size();
            }
            return ck1Var;
        }
        return e6aVar;
    }

    public final void q() {
        List list;
        String name;
        String str;
        String str2;
        ArrayList arrayList = this.q;
        boolean z = arrayList.size() > 1;
        ArrayList<b> arrayList2 = new ArrayList();
        for (Object obj : arrayList) {
            if (!((b) obj).k.isEmpty()) {
                arrayList2.add(obj);
            }
        }
        ArrayList arrayList3 = new ArrayList(y41.p(arrayList2, 10));
        for (b bVar : arrayList2) {
            List listA = bVar.k;
            int i = bVar.b;
            UserCollectionLink userCollectionLink = bVar.a;
            String str3 = bVar.c;
            String str4 = bVar.d;
            if (str3 != null) {
                List list2 = zw8.a;
                if (str4 == null) {
                    str4 = "asc";
                }
                listA = zw8.a(str3, str4, listA);
            }
            if (z) {
                name = userCollectionLink.getName();
                if (name == null || n89.ab(name)) {
                    String url = userCollectionLink.getUrl();
                    if (n89.l(url, "trakt.tv/users/", false)) {
                        q07 q07VarP = p(url);
                        if (q07VarP == null || (str2 = (String) q07VarP.b) == null || (name = y41.ai(n89.ao(n89.an(str2, "-", " "), new String[]{" "}, 6), " ", null, null, new q78(27), 30)) == null) {
                            name = "Trakt List";
                        }
                    } else if (n89.l(url, "mdblist.com/lists/", false)) {
                        q07 q07VarO = o(url);
                        if (q07VarO == null || (str = (String) q07VarO.b) == null || (name = y41.ai(n89.ao(n89.an(str, "-", " "), new String[]{" "}, 6), " ", null, null, new q78(28), 30)) == null) {
                            name = "MDBList";
                        }
                    } else {
                        name = n41.k("List ", i + 1);
                    }
                }
            } else {
                name = this.g;
            }
            arrayList3.add(new af1(name, y41.bp(listA), x78.a, "user_collection", s(i), bVar.c, bVar.d, 560));
        }
        this.i.l(arrayList3);
        re6 re6Var = this.o;
        if (re6Var.e() == null) {
            af1 af1Var = (af1) y41.ab(arrayList3);
            re6Var.l((af1Var == null || (list = af1Var.b) == null) ? null : (ContentItem) y41.ab(list));
        }
        this.m.l(null);
    }

    public final void r() {
        for (b bVar : this.q) {
            bVar.k.clear();
            bVar.l.clear();
            bVar.m.clear();
            bVar.n.clear();
            bVar.g = 1;
            bVar.h = 1;
            bVar.i = true;
            bVar.j = true;
        }
        this.i.l(ik2.a);
        if (this.h == null) {
            this.m.l("Collection item not found");
        } else {
            wj0.c(bka.a(this), null, null, new v(this, null), 3);
        }
    }

    public final String s(int i) {
        return "user_collection:" + this.t + ":" + i;
    }
}
