package com.strmr.ps.data.repository;

import androidx.credentials.CredentialOption;
import androidx.media3.container.NalUnitUtil;
import androidx.media3.exoplayer.rtsp.SessionDescription;
import androidx.media3.extractor.ts.TsExtractor;
import com.strmr.ps.data.local.dao.WatchStatusDao;
import com.strmr.ps.data.local.entity.TraktAccount;
import com.strmr.ps.data.local.entity.WatchStatusEntity;
import com.strmr.ps.data.model.ContentItem;
import defpackage.ak1;
import defpackage.ck1;
import defpackage.cr4;
import defpackage.e6a;
import defpackage.fg4;
import defpackage.gr3;
import defpackage.kg4;
import defpackage.kp6;
import defpackage.m02;
import defpackage.m12;
import defpackage.mg6;
import defpackage.pq6;
import defpackage.rg6;
import defpackage.s38;
import defpackage.sc2;
import defpackage.vx8;
import defpackage.wj0;
import defpackage.y41;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import javax.inject.Inject;
import javax.inject.Singleton;
import kotlin.Metadata;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.jvm.internal.ContinuationImpl;
import kotlin.coroutines.jvm.internal.DebugMetadata;
import kotlin.coroutines.jvm.internal.SuspendLambda;

/* JADX INFO: loaded from: classes3.dex */
@Singleton
@Metadata(d1 = {"\u0000^\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\u000e\n\u0002\b\u0002\n\u0002\u0010\b\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0006\n\u0002\b\u0003\n\u0002\u0010\t\n\u0002\b\u0003\n\u0002\u0010 \n\u0002\u0018\u0002\n\u0002\b\r\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\b\u0003\b\u0007\u0018\u00002\u00020\u0001B\u0019\b\u0007\u0012\u0006\u0010\u0003\u001a\u00020\u0002\u0012\u0006\u0010\u0005\u001a\u00020\u0004¢\u0006\u0004\b\u0006\u0010\u0007J\u0010\u0010\t\u001a\u00020\bH\u0082@¢\u0006\u0004\b\t\u0010\nJ\u001f\u0010\u000f\u001a\u00020\b2\u0006\u0010\f\u001a\u00020\u000b2\u0006\u0010\u000e\u001a\u00020\rH\u0002¢\u0006\u0004\b\u000f\u0010\u0010J\u0010\u0010\u0012\u001a\u00020\u0011H\u0096@¢\u0006\u0004\b\u0012\u0010\nJ!\u0010\u0014\u001a\u0004\u0018\u00010\u00132\u0006\u0010\f\u001a\u00020\u000b2\u0006\u0010\u000e\u001a\u00020\rH\u0016¢\u0006\u0004\b\u0014\u0010\u0015J2\u0010\u0019\u001a\u00020\u00112\u0006\u0010\f\u001a\u00020\u000b2\u0006\u0010\u000e\u001a\u00020\r2\b\u0010\u0016\u001a\u0004\u0018\u00010\u00132\u0006\u0010\u0018\u001a\u00020\u0017H\u0096@¢\u0006\u0004\b\u0019\u0010\u001aJ\u001e\u0010\u001e\u001a\u00020\u00112\f\u0010\u001d\u001a\b\u0012\u0004\u0012\u00020\u001c0\u001bH\u0096@¢\u0006\u0004\b\u001e\u0010\u001fJ\u0010\u0010 \u001a\u00020\u0011H\u0096@¢\u0006\u0004\b \u0010\nJ'\u0010!\u001a\u00020\u00112\u0006\u0010\f\u001a\u00020\u000b2\u0006\u0010\u000e\u001a\u00020\r2\u0006\u0010\u0016\u001a\u00020\u0013H\u0016¢\u0006\u0004\b!\u0010\"J\u001f\u0010#\u001a\u00020\u00112\u0006\u0010\f\u001a\u00020\u000b2\u0006\u0010\u000e\u001a\u00020\rH\u0016¢\u0006\u0004\b#\u0010$J\u0018\u0010&\u001a\u00020\u00112\u0006\u0010%\u001a\u00020\bH\u0096@¢\u0006\u0004\b&\u0010'R\u0014\u0010\u0003\u001a\u00020\u00028\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u0003\u0010(R\u0014\u0010\u0005\u001a\u00020\u00048\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u0005\u0010)R \u0010+\u001a\u000e\u0012\u0004\u0012\u00020\b\u0012\u0004\u0012\u00020\u001c0*8\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b+\u0010,R\u0018\u0010-\u001a\u0004\u0018\u00010\b8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b-\u0010.R\u0014\u00100\u001a\u00020/8\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b0\u00101¨\u00062"}, d2 = {"Lcom/strmr/ps/data/repository/WatchStatusRepository;", "Lkg4;", "Lcom/strmr/ps/data/local/dao/WatchStatusDao;", "watchStatusDao", "Lfg4;", "traktAccountRepository", "<init>", "(Lcom/strmr/ps/data/local/dao/WatchStatusDao;Lfg4;)V", "", "getActiveUserSlug", "(Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", "", "tmdbId", "Lcom/strmr/ps/data/model/ContentItem$ItemType;", "itemType", "cacheKey", "(ILcom/strmr/ps/data/model/ContentItem$ItemType;)Ljava/lang/String;", "Le6a;", "preload", "", "getProgress", "(ILcom/strmr/ps/data/model/ContentItem$ItemType;)Ljava/lang/Double;", "progress", "", "updatedAt", "upsert", "(ILcom/strmr/ps/data/model/ContentItem$ItemType;Ljava/lang/Double;JLkotlin/coroutines/Continuation;)Ljava/lang/Object;", "", "Lcom/strmr/ps/data/local/entity/WatchStatusEntity;", "items", "upsertAll", "(Ljava/util/List;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", "clear", "setProgressSync", "(ILcom/strmr/ps/data/model/ContentItem$ItemType;D)V", "removeProgressSync", "(ILcom/strmr/ps/data/model/ContentItem$ItemType;)V", "userSlug", "reloadForUser", "(Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", "Lcom/strmr/ps/data/local/dao/WatchStatusDao;", "Lfg4;", "Ljava/util/concurrent/ConcurrentHashMap;", "cache", "Ljava/util/concurrent/ConcurrentHashMap;", "loadedUserSlug", "Ljava/lang/String;", "Lmg6;", "mutex", "Lmg6;", "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
public final class WatchStatusRepository implements kg4 {

    @kp6
    private final ConcurrentHashMap<String, WatchStatusEntity> cache;

    @pq6
    private volatile String loadedUserSlug;

    @kp6
    private final mg6 mutex;

    @kp6
    private final fg4 traktAccountRepository;

    @kp6
    private final WatchStatusDao watchStatusDao;

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

        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) {
            }
            $EnumSwitchMapping$0 = iArr;
        }
    }

    /* JADX INFO: renamed from: com.strmr.ps.data.repository.WatchStatusRepository$clear$2, reason: invalid class name */
    @Metadata(d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\u0010\u0002\u001a\u00020\u0001*\u00020\u0000H\n¢\u0006\u0004\b\u0002\u0010\u0003"}, d2 = {"Lak1;", "Le6a;", "<anonymous>", "(Lak1;)V"}, k = 3, mv = {2, 0, 0})
    @DebugMetadata(c = "com.strmr.ps.data.repository.WatchStatusRepository$clear$2", f = "WatchStatusRepository.kt", i = {1, 1, 2, 2}, l = {119, TsExtractor.TS_PACKET_SIZE, 123}, m = "invokeSuspend", n = {"userSlug", "$this$withLock_u24default$iv", "userSlug", "$this$withLock_u24default$iv"}, s = {"L$0", "L$1", "L$0", "L$1"})
    @vx8
    public static final class AnonymousClass2 extends SuspendLambda implements gr3<ak1, Continuation<? super e6a>, Object> {
        Object L$0;
        Object L$1;
        Object L$2;
        int label;

        public AnonymousClass2(Continuation<? super AnonymousClass2> continuation) {
            super(2, continuation);
        }

        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        public final Continuation<e6a> create(Object obj, Continuation<?> continuation) {
            return WatchStatusRepository.this.new AnonymousClass2(continuation);
        }

        @Override // defpackage.gr3
        public final Object invoke(ak1 ak1Var, Continuation<? super e6a> continuation) {
            return ((AnonymousClass2) create(ak1Var, continuation)).invokeSuspend(e6a.a);
        }

        /* JADX WARN: Removed duplicated region for block: B:25:0x007e  */
        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        /*
            Code decompiled incorrectly, please refer to instructions dump.
        */
        public final Object invokeSuspend(Object obj) throws Throwable {
            String str;
            mg6 mg6Var;
            WatchStatusRepository watchStatusRepository;
            mg6 mg6Var2;
            Throwable th;
            WatchStatusDao watchStatusDao;
            ck1 ck1Var = ck1.a;
            int i = this.label;
            if (i == 0) {
                s38.b(obj);
                WatchStatusRepository watchStatusRepository2 = WatchStatusRepository.this;
                this.label = 1;
                obj = watchStatusRepository2.getActiveUserSlug(this);
                if (obj != ck1Var) {
                }
                return ck1Var;
            }
            if (i != 1) {
                if (i != 2) {
                    if (i != 3) {
                        throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                    }
                    mg6Var2 = (mg6) this.L$1;
                    try {
                        s38.b(obj);
                        mg6Var2.c(null);
                        return e6a.a;
                    } catch (Throwable th2) {
                        th = th2;
                        mg6Var2.c(null);
                        throw th;
                    }
                }
                watchStatusRepository = (WatchStatusRepository) this.L$2;
                mg6 mg6Var3 = (mg6) this.L$1;
                str = (String) this.L$0;
                s38.b(obj);
                mg6Var = mg6Var3;
                try {
                    watchStatusRepository.cache.clear();
                    watchStatusDao = watchStatusRepository.watchStatusDao;
                    this.L$0 = str;
                    this.L$1 = mg6Var;
                    this.L$2 = null;
                    this.label = 3;
                    if (watchStatusDao.clearForUser(str, this) != ck1Var) {
                        mg6Var2 = mg6Var;
                        mg6Var2.c(null);
                        return e6a.a;
                    }
                    return ck1Var;
                } catch (Throwable th3) {
                    mg6Var2 = mg6Var;
                    th = th3;
                    mg6Var2.c(null);
                    throw th;
                }
            }
            s38.b(obj);
            str = (String) obj;
            mg6Var = WatchStatusRepository.this.mutex;
            watchStatusRepository = WatchStatusRepository.this;
            this.L$0 = str;
            this.L$1 = mg6Var;
            this.L$2 = watchStatusRepository;
            this.label = 2;
            if (mg6Var.a(this) != ck1Var) {
                watchStatusRepository.cache.clear();
                watchStatusDao = watchStatusRepository.watchStatusDao;
                this.L$0 = str;
                this.L$1 = mg6Var;
                this.L$2 = null;
                this.label = 3;
                if (watchStatusDao.clearForUser(str, this) != ck1Var) {
                }
            }
            return ck1Var;
        }
    }

    /* JADX INFO: renamed from: com.strmr.ps.data.repository.WatchStatusRepository$getActiveUserSlug$1, reason: invalid class name */
    @Metadata(k = 3, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    @DebugMetadata(c = "com.strmr.ps.data.repository.WatchStatusRepository", f = "WatchStatusRepository.kt", i = {}, l = {35}, m = "getActiveUserSlug", n = {}, s = {})
    public static final class AnonymousClass1 extends ContinuationImpl {
        int label;
        /* synthetic */ Object result;

        public AnonymousClass1(Continuation<? super AnonymousClass1> continuation) {
            super(continuation);
        }

        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        @pq6
        public final Object invokeSuspend(@kp6 Object obj) {
            this.result = obj;
            this.label |= Integer.MIN_VALUE;
            return WatchStatusRepository.this.getActiveUserSlug(this);
        }
    }

    /* JADX INFO: renamed from: com.strmr.ps.data.repository.WatchStatusRepository$preload$2, reason: invalid class name and case insensitive filesystem */
    @Metadata(d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\u0010\u0002\u001a\u00020\u0001*\u00020\u0000H\n¢\u0006\u0004\b\u0002\u0010\u0003"}, d2 = {"Lak1;", "Le6a;", "<anonymous>", "(Lak1;)V"}, k = 3, mv = {2, 0, 0})
    @DebugMetadata(c = "com.strmr.ps.data.repository.WatchStatusRepository$preload$2", f = "WatchStatusRepository.kt", i = {1, 1, 2, 2}, l = {52, TsExtractor.TS_PACKET_SIZE, 55}, m = "invokeSuspend", n = {"userSlug", "$this$withLock_u24default$iv", "userSlug", "$this$withLock_u24default$iv"}, s = {"L$0", "L$1", "L$0", "L$1"})
    @vx8
    public static final class C05192 extends SuspendLambda implements gr3<ak1, Continuation<? super e6a>, Object> {
        Object L$0;
        Object L$1;
        Object L$2;
        int label;

        public C05192(Continuation<? super C05192> continuation) {
            super(2, continuation);
        }

        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        public final Continuation<e6a> create(Object obj, Continuation<?> continuation) {
            return WatchStatusRepository.this.new C05192(continuation);
        }

        @Override // defpackage.gr3
        public final Object invoke(ak1 ak1Var, Continuation<? super e6a> continuation) {
            return ((C05192) create(ak1Var, continuation)).invokeSuspend(e6a.a);
        }

        /* JADX WARN: Removed duplicated region for block: B:26:0x007d  */
        /* JADX WARN: Removed duplicated region for block: B:30:0x0098 A[Catch: all -> 0x0021, LOOP:0: B:28:0x0092->B:30:0x0098, LOOP_END, TryCatch #0 {all -> 0x0021, blocks: (B:8:0x001c, B:27:0x0082, B:28:0x0092, B:30:0x0098, B:31:0x00b6), top: B:38:0x001c }] */
        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        /*
            Code decompiled incorrectly, please refer to instructions dump.
        */
        public final Object invokeSuspend(Object obj) throws Throwable {
            String str;
            mg6 mg6Var;
            WatchStatusRepository watchStatusRepository;
            mg6 mg6Var2;
            Object allForUser;
            String str2;
            WatchStatusRepository watchStatusRepository2;
            ck1 ck1Var = ck1.a;
            int i = this.label;
            if (i == 0) {
                s38.b(obj);
                WatchStatusRepository watchStatusRepository3 = WatchStatusRepository.this;
                this.label = 1;
                obj = watchStatusRepository3.getActiveUserSlug(this);
                if (obj != ck1Var) {
                }
                return ck1Var;
            }
            if (i != 1) {
                if (i != 2) {
                    if (i != 3) {
                        throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                    }
                    watchStatusRepository2 = (WatchStatusRepository) this.L$2;
                    mg6Var2 = (mg6) this.L$1;
                    str2 = (String) this.L$0;
                    try {
                        s38.b(obj);
                        List<WatchStatusEntity> list = (List) obj;
                        watchStatusRepository2.cache.clear();
                        for (WatchStatusEntity watchStatusEntity : list) {
                            watchStatusRepository2.cache.put(watchStatusRepository2.cacheKey(watchStatusEntity.getTmdbId(), ContentItem.ItemType.valueOf(watchStatusEntity.getType())), watchStatusEntity);
                        }
                        watchStatusRepository2.loadedUserSlug = str2;
                        list.size();
                        mg6Var2.c(null);
                        return e6a.a;
                    } catch (Throwable th) {
                        th = th;
                        mg6Var2.c(null);
                        throw th;
                    }
                }
                watchStatusRepository = (WatchStatusRepository) this.L$2;
                mg6Var = (mg6) this.L$1;
                String str3 = (String) this.L$0;
                s38.b(obj);
                str = str3;
                try {
                    WatchStatusDao watchStatusDao = watchStatusRepository.watchStatusDao;
                    this.L$0 = str;
                    this.L$1 = mg6Var;
                    this.L$2 = watchStatusRepository;
                    this.label = 3;
                    allForUser = watchStatusDao.getAllForUser(str, this);
                    if (allForUser != ck1Var) {
                        str2 = str;
                        obj = allForUser;
                        watchStatusRepository2 = watchStatusRepository;
                        mg6Var2 = mg6Var;
                        List<WatchStatusEntity> list2 = (List) obj;
                        watchStatusRepository2.cache.clear();
                        while (r3.hasNext()) {
                        }
                        watchStatusRepository2.loadedUserSlug = str2;
                        list2.size();
                        mg6Var2.c(null);
                        return e6a.a;
                    }
                    return ck1Var;
                } catch (Throwable th2) {
                    th = th2;
                    mg6Var2 = mg6Var;
                    mg6Var2.c(null);
                    throw th;
                }
            }
            s38.b(obj);
            str = (String) obj;
            mg6 mg6Var3 = WatchStatusRepository.this.mutex;
            WatchStatusRepository watchStatusRepository4 = WatchStatusRepository.this;
            this.L$0 = str;
            this.L$1 = mg6Var3;
            this.L$2 = watchStatusRepository4;
            this.label = 2;
            if (mg6Var3.a(this) != ck1Var) {
                mg6Var = mg6Var3;
                watchStatusRepository = watchStatusRepository4;
                WatchStatusDao watchStatusDao2 = watchStatusRepository.watchStatusDao;
                this.L$0 = str;
                this.L$1 = mg6Var;
                this.L$2 = watchStatusRepository;
                this.label = 3;
                allForUser = watchStatusDao2.getAllForUser(str, this);
                if (allForUser != ck1Var) {
                }
            }
            return ck1Var;
        }
    }

    /* JADX INFO: renamed from: com.strmr.ps.data.repository.WatchStatusRepository$reloadForUser$2, reason: invalid class name and case insensitive filesystem */
    @Metadata(d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\u0010\u0002\u001a\u00020\u0001*\u00020\u0000H\n¢\u0006\u0004\b\u0002\u0010\u0003"}, d2 = {"Lak1;", "Le6a;", "<anonymous>", "(Lak1;)V"}, k = 3, mv = {2, 0, 0})
    @DebugMetadata(c = "com.strmr.ps.data.repository.WatchStatusRepository$reloadForUser$2", f = "WatchStatusRepository.kt", i = {0, 1}, l = {TsExtractor.TS_PACKET_SIZE, 167}, m = "invokeSuspend", n = {"$this$withLock_u24default$iv", "$this$withLock_u24default$iv"}, s = {"L$0", "L$0"})
    @vx8
    public static final class C05202 extends SuspendLambda implements gr3<ak1, Continuation<? super e6a>, Object> {
        final /* synthetic */ String $userSlug;
        Object L$0;
        Object L$1;
        Object L$2;
        int label;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public C05202(String str, Continuation<? super C05202> continuation) {
            super(2, continuation);
            this.$userSlug = str;
        }

        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        public final Continuation<e6a> create(Object obj, Continuation<?> continuation) {
            return WatchStatusRepository.this.new C05202(this.$userSlug, continuation);
        }

        @Override // defpackage.gr3
        public final Object invoke(ak1 ak1Var, Continuation<? super e6a> continuation) {
            return ((C05202) create(ak1Var, continuation)).invokeSuspend(e6a.a);
        }

        /* JADX WARN: Removed duplicated region for block: B:25:0x0085 A[Catch: all -> 0x001d, TRY_LEAVE, TryCatch #1 {all -> 0x001d, blocks: (B:7:0x0019, B:22:0x006f, B:23:0x007f, B:25:0x0085, B:26:0x008b, B:28:0x00a3), top: B:38:0x0019 }] */
        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        /*
            Code decompiled incorrectly, please refer to instructions dump.
        */
        public final Object invokeSuspend(Object obj) throws Throwable {
            mg6 mg6Var;
            WatchStatusRepository watchStatusRepository;
            String str;
            mg6 mg6Var2;
            Throwable th;
            String str2;
            WatchStatusRepository watchStatusRepository2;
            ck1 ck1Var = ck1.a;
            int i = this.label;
            try {
                if (i == 0) {
                    s38.b(obj);
                    mg6Var = WatchStatusRepository.this.mutex;
                    WatchStatusRepository watchStatusRepository3 = WatchStatusRepository.this;
                    String str3 = this.$userSlug;
                    this.L$0 = mg6Var;
                    this.L$1 = watchStatusRepository3;
                    this.L$2 = str3;
                    this.label = 1;
                    if (mg6Var.a(this) != ck1Var) {
                        watchStatusRepository = watchStatusRepository3;
                        str = str3;
                    }
                    return ck1Var;
                }
                if (i != 1) {
                    if (i != 2) {
                        throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                    }
                    str2 = (String) this.L$2;
                    watchStatusRepository2 = (WatchStatusRepository) this.L$1;
                    mg6Var2 = (mg6) this.L$0;
                    try {
                        s38.b(obj);
                        List<WatchStatusEntity> list = (List) obj;
                        watchStatusRepository2.cache.clear();
                        for (WatchStatusEntity watchStatusEntity : list) {
                            try {
                                watchStatusRepository2.cache.put(watchStatusRepository2.cacheKey(watchStatusEntity.getTmdbId(), ContentItem.ItemType.valueOf(watchStatusEntity.getType())), watchStatusEntity);
                            } catch (IllegalArgumentException unused) {
                            }
                        }
                        watchStatusRepository2.loadedUserSlug = str2;
                        list.size();
                        mg6Var2.c(null);
                        return e6a.a;
                    } catch (Throwable th2) {
                        th = th2;
                        mg6Var2.c(null);
                        throw th;
                    }
                }
                str = (String) this.L$2;
                watchStatusRepository = (WatchStatusRepository) this.L$1;
                mg6 mg6Var3 = (mg6) this.L$0;
                s38.b(obj);
                mg6Var = mg6Var3;
                WatchStatusDao watchStatusDao = watchStatusRepository.watchStatusDao;
                this.L$0 = mg6Var;
                this.L$1 = watchStatusRepository;
                this.L$2 = str;
                this.label = 2;
                Object allForUser = watchStatusDao.getAllForUser(str, this);
                if (allForUser != ck1Var) {
                    mg6Var2 = mg6Var;
                    obj = allForUser;
                    str2 = str;
                    watchStatusRepository2 = watchStatusRepository;
                    List<WatchStatusEntity> list2 = (List) obj;
                    watchStatusRepository2.cache.clear();
                    while (r3.hasNext()) {
                    }
                    watchStatusRepository2.loadedUserSlug = str2;
                    list2.size();
                    mg6Var2.c(null);
                    return e6a.a;
                }
                return ck1Var;
            } catch (Throwable th3) {
                mg6Var2 = mg6Var;
                th = th3;
                mg6Var2.c(null);
                throw th;
            }
        }
    }

    /* JADX INFO: renamed from: com.strmr.ps.data.repository.WatchStatusRepository$upsert$2, reason: invalid class name and case insensitive filesystem */
    @Metadata(d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\u0010\u0002\u001a\u00020\u0001*\u00020\u0000H\n¢\u0006\u0004\b\u0002\u0010\u0003"}, d2 = {"Lak1;", "Le6a;", "<anonymous>", "(Lak1;)V"}, k = 3, mv = {2, 0, 0})
    @DebugMetadata(c = "com.strmr.ps.data.repository.WatchStatusRepository$upsert$2", f = "WatchStatusRepository.kt", i = {1, 1, 1, 2}, l = {74, TsExtractor.TS_PACKET_SIZE, 86}, m = "invokeSuspend", n = {"userSlug", SessionDescription.ATTR_TYPE, "$this$withLock_u24default$iv", "$this$withLock_u24default$iv"}, s = {"L$0", "L$1", "L$2", "L$0"})
    @vx8
    public static final class C05212 extends SuspendLambda implements gr3<ak1, Continuation<? super e6a>, Object> {
        final /* synthetic */ ContentItem.ItemType $itemType;
        final /* synthetic */ Double $progress;
        final /* synthetic */ int $tmdbId;
        final /* synthetic */ long $updatedAt;
        int I$0;
        long J$0;
        Object L$0;
        Object L$1;
        Object L$2;
        Object L$3;
        Object L$4;
        Object L$5;
        int label;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public C05212(ContentItem.ItemType itemType, int i, Double d, long j, Continuation<? super C05212> continuation) {
            super(2, continuation);
            this.$itemType = itemType;
            this.$tmdbId = i;
            this.$progress = d;
            this.$updatedAt = j;
        }

        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        public final Continuation<e6a> create(Object obj, Continuation<?> continuation) {
            return WatchStatusRepository.this.new C05212(this.$itemType, this.$tmdbId, this.$progress, this.$updatedAt, continuation);
        }

        @Override // defpackage.gr3
        public final Object invoke(ak1 ak1Var, Continuation<? super e6a> continuation) {
            return ((C05212) create(ak1Var, continuation)).invokeSuspend(e6a.a);
        }

        /* JADX WARN: Removed duplicated region for block: B:32:0x00e7  */
        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        /*
            Code decompiled incorrectly, please refer to instructions dump.
        */
        public final Object invokeSuspend(Object obj) throws Throwable {
            Object activeUserSlug;
            String str;
            String str2;
            Double d;
            long j;
            WatchStatusRepository watchStatusRepository;
            ContentItem.ItemType itemType;
            mg6 mg6Var;
            int i;
            mg6 mg6Var2;
            WatchStatusDao watchStatusDao;
            List<WatchStatusEntity> listAm;
            ck1 ck1Var = ck1.a;
            int i2 = this.label;
            if (i2 == 0) {
                s38.b(obj);
                WatchStatusRepository watchStatusRepository2 = WatchStatusRepository.this;
                this.label = 1;
                activeUserSlug = watchStatusRepository2.getActiveUserSlug(this);
                if (activeUserSlug != ck1Var) {
                }
                return ck1Var;
            }
            if (i2 != 1) {
                if (i2 != 2) {
                    if (i2 != 3) {
                        throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                    }
                    mg6Var2 = (mg6) this.L$0;
                    try {
                        s38.b(obj);
                        mg6Var2.c(null);
                        return e6a.a;
                    } catch (Throwable th) {
                        th = th;
                        mg6Var2.c(null);
                        throw th;
                    }
                }
                long j2 = this.J$0;
                i = this.I$0;
                itemType = (ContentItem.ItemType) this.L$5;
                watchStatusRepository = (WatchStatusRepository) this.L$4;
                d = (Double) this.L$3;
                mg6Var = (mg6) this.L$2;
                str2 = (String) this.L$1;
                str = (String) this.L$0;
                s38.b(obj);
                j = j2;
                try {
                    String str3 = str;
                    WatchStatusEntity watchStatusEntity = new WatchStatusEntity(WatchStatusEntity.INSTANCE.generateKey(str, str2, i), str3, i, str2, d, j, null, null, null, null, null, 1984, null);
                    watchStatusRepository.cache.put(watchStatusRepository.cacheKey(i, itemType), watchStatusEntity);
                    watchStatusDao = watchStatusRepository.watchStatusDao;
                    listAm = y41.am(watchStatusEntity);
                    this.L$0 = mg6Var;
                    this.L$1 = null;
                    this.L$2 = null;
                    this.L$3 = null;
                    this.L$4 = null;
                    this.L$5 = null;
                    this.label = 3;
                    if (watchStatusDao.upsertAll(listAm, this) != ck1Var) {
                        mg6Var2 = mg6Var;
                        mg6Var2.c(null);
                        return e6a.a;
                    }
                    return ck1Var;
                } catch (Throwable th2) {
                    th = th2;
                    mg6Var2 = mg6Var;
                    mg6Var2.c(null);
                    throw th;
                }
            }
            s38.b(obj);
            activeUserSlug = obj;
            str = (String) activeUserSlug;
            str2 = this.$itemType == ContentItem.ItemType.TV_SHOW ? "TV_SHOW" : "MOVIE";
            mg6 mg6Var3 = WatchStatusRepository.this.mutex;
            int i3 = this.$tmdbId;
            d = this.$progress;
            long j3 = this.$updatedAt;
            WatchStatusRepository watchStatusRepository3 = WatchStatusRepository.this;
            ContentItem.ItemType itemType2 = this.$itemType;
            this.L$0 = str;
            this.L$1 = str2;
            this.L$2 = mg6Var3;
            this.L$3 = d;
            this.L$4 = watchStatusRepository3;
            this.L$5 = itemType2;
            this.I$0 = i3;
            this.J$0 = j3;
            this.label = 2;
            if (mg6Var3.a(this) != ck1Var) {
                j = j3;
                watchStatusRepository = watchStatusRepository3;
                itemType = itemType2;
                mg6Var = mg6Var3;
                i = i3;
                String str32 = str;
                WatchStatusEntity watchStatusEntity2 = new WatchStatusEntity(WatchStatusEntity.INSTANCE.generateKey(str, str2, i), str32, i, str2, d, j, null, null, null, null, null, 1984, null);
                watchStatusRepository.cache.put(watchStatusRepository.cacheKey(i, itemType), watchStatusEntity2);
                watchStatusDao = watchStatusRepository.watchStatusDao;
                listAm = y41.am(watchStatusEntity2);
                this.L$0 = mg6Var;
                this.L$1 = null;
                this.L$2 = null;
                this.L$3 = null;
                this.L$4 = null;
                this.L$5 = null;
                this.label = 3;
                if (watchStatusDao.upsertAll(listAm, this) != ck1Var) {
                }
            }
            return ck1Var;
        }
    }

    /* JADX INFO: renamed from: com.strmr.ps.data.repository.WatchStatusRepository$upsertAll$2, reason: invalid class name and case insensitive filesystem */
    @Metadata(d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\u0010\u0002\u001a\u00020\u0001*\u00020\u0000H\n¢\u0006\u0004\b\u0002\u0010\u0003"}, d2 = {"Lak1;", "Le6a;", "<anonymous>", "(Lak1;)V"}, k = 3, mv = {2, 0, 0})
    @DebugMetadata(c = "com.strmr.ps.data.repository.WatchStatusRepository$upsertAll$2", f = "WatchStatusRepository.kt", i = {0}, l = {TsExtractor.TS_PACKET_SIZE, 108, 111}, m = "invokeSuspend", n = {"$this$withLock_u24default$iv"}, s = {"L$0"})
    @vx8
    public static final class C05222 extends SuspendLambda implements gr3<ak1, Continuation<? super e6a>, Object> {
        final /* synthetic */ List<WatchStatusEntity> $items;
        Object L$0;
        Object L$1;
        Object L$2;
        int label;
        final /* synthetic */ WatchStatusRepository this$0;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public C05222(List<WatchStatusEntity> list, WatchStatusRepository watchStatusRepository, Continuation<? super C05222> continuation) {
            super(2, continuation);
            this.$items = list;
            this.this$0 = watchStatusRepository;
        }

        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        public final Continuation<e6a> create(Object obj, Continuation<?> continuation) {
            return new C05222(this.$items, this.this$0, continuation);
        }

        @Override // defpackage.gr3
        public final Object invoke(ak1 ak1Var, Continuation<? super e6a> continuation) {
            return ((C05222) create(ak1Var, continuation)).invokeSuspend(e6a.a);
        }

        /* JADX WARN: Code restructure failed: missing block: B:30:0x00b0, code lost:
        
            if (r12.upsertAll(r1, r11) == r0) goto L37;
         */
        /* JADX WARN: Removed duplicated region for block: B:35:0x00c9  */
        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        /*
            Code decompiled incorrectly, please refer to instructions dump.
        */
        public final Object invokeSuspend(Object obj) {
            mg6 mg6Var;
            WatchStatusRepository watchStatusRepository;
            List<WatchStatusEntity> list;
            WatchStatusRepository watchStatusRepository2;
            Iterator it;
            ck1 ck1Var = ck1.a;
            int i = this.label;
            e6a e6aVar = e6a.a;
            try {
                if (i == 0) {
                    s38.b(obj);
                    if (this.$items.isEmpty()) {
                        return e6aVar;
                    }
                    mg6Var = this.this$0.mutex;
                    List<WatchStatusEntity> list2 = this.$items;
                    watchStatusRepository = this.this$0;
                    this.L$0 = mg6Var;
                    this.L$1 = list2;
                    this.L$2 = watchStatusRepository;
                    this.label = 1;
                    if (mg6Var.a(this) != ck1Var) {
                        list = list2;
                    }
                    return ck1Var;
                }
                if (i != 1) {
                    if (i == 2) {
                        s38.b(obj);
                        this.$items.size();
                        return e6aVar;
                    }
                    if (i != 3) {
                        throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                    }
                    it = (Iterator) this.L$1;
                    watchStatusRepository2 = (WatchStatusRepository) this.L$0;
                    s38.b(obj);
                    while (it.hasNext()) {
                        List<WatchStatusEntity> list3 = (List) it.next();
                        WatchStatusDao watchStatusDao = watchStatusRepository2.watchStatusDao;
                        this.L$0 = watchStatusRepository2;
                        this.L$1 = it;
                        this.L$2 = null;
                        this.label = 3;
                        if (watchStatusDao.upsertAll(list3, this) == ck1Var) {
                            return ck1Var;
                        }
                    }
                    this.$items.size();
                    return e6aVar;
                }
                watchStatusRepository = (WatchStatusRepository) this.L$2;
                list = (List) this.L$1;
                mg6Var = (mg6) this.L$0;
                s38.b(obj);
                for (WatchStatusEntity watchStatusEntity : list) {
                    try {
                        watchStatusRepository.cache.put(watchStatusRepository.cacheKey(watchStatusEntity.getTmdbId(), ContentItem.ItemType.valueOf(watchStatusEntity.getType())), watchStatusEntity);
                    } catch (IllegalArgumentException unused) {
                    }
                }
                mg6Var.c(null);
                if (this.$items.size() > 500) {
                    ArrayList arrayListO = y41.o(this.$items, CredentialOption.PRIORITY_OIDC_OR_SIMILAR);
                    watchStatusRepository2 = this.this$0;
                    it = arrayListO.iterator();
                    while (it.hasNext()) {
                    }
                    this.$items.size();
                    return e6aVar;
                }
                WatchStatusDao watchStatusDao2 = this.this$0.watchStatusDao;
                List<WatchStatusEntity> list4 = this.$items;
                this.L$0 = null;
                this.L$1 = null;
                this.L$2 = null;
                this.label = 2;
            } catch (Throwable th) {
                mg6Var.c(null);
                throw th;
            }
        }
    }

    @Inject
    public WatchStatusRepository(@kp6 WatchStatusDao watchStatusDao, @kp6 fg4 fg4Var) {
        cr4.g(watchStatusDao, "watchStatusDao");
        cr4.g(fg4Var, "traktAccountRepository");
        this.watchStatusDao = watchStatusDao;
        this.traktAccountRepository = fg4Var;
        this.cache = new ConcurrentHashMap<>();
        this.mutex = rg6.a();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final String cacheKey(int tmdbId, ContentItem.ItemType itemType) {
        int i = WhenMappings.$EnumSwitchMapping$0[itemType.ordinal()];
        String str = "MOVIE";
        if (i != 1 && i == 2) {
            str = "TV_SHOW";
        }
        return str + "_" + tmdbId;
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object getActiveUserSlug(Continuation<? super String> continuation) {
        AnonymousClass1 anonymousClass1;
        String userSlug;
        if (continuation instanceof AnonymousClass1) {
            anonymousClass1 = (AnonymousClass1) continuation;
            int i = anonymousClass1.label;
            if ((i & Integer.MIN_VALUE) != 0) {
                anonymousClass1.label = i - Integer.MIN_VALUE;
            } else {
                anonymousClass1 = new AnonymousClass1(continuation);
            }
        }
        Object activeAccount = anonymousClass1.result;
        ck1 ck1Var = ck1.a;
        int i2 = anonymousClass1.label;
        if (i2 == 0) {
            s38.b(activeAccount);
            fg4 fg4Var = this.traktAccountRepository;
            anonymousClass1.label = 1;
            activeAccount = fg4Var.getActiveAccount(anonymousClass1);
            if (activeAccount == ck1Var) {
                return ck1Var;
            }
        } else {
            if (i2 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            s38.b(activeAccount);
        }
        TraktAccount traktAccount = (TraktAccount) activeAccount;
        return (traktAccount == null || (userSlug = traktAccount.getUserSlug()) == null) ? "" : userSlug;
    }

    @pq6
    public Object clear(@kp6 Continuation<? super e6a> continuation) throws Throwable {
        m12 m12Var = sc2.a;
        Object objF = wj0.f(m02.c, new AnonymousClass2(null), continuation);
        return objF == ck1.a ? objF : e6a.a;
    }

    @Override // defpackage.kg4
    @pq6
    public Double getProgress(int tmdbId, @kp6 ContentItem.ItemType itemType) {
        cr4.g(itemType, "itemType");
        WatchStatusEntity watchStatusEntity = this.cache.get(cacheKey(tmdbId, itemType));
        if (watchStatusEntity != null) {
            return watchStatusEntity.getProgress();
        }
        return null;
    }

    @pq6
    public Object preload(@kp6 Continuation<? super e6a> continuation) throws Throwable {
        m12 m12Var = sc2.a;
        Object objF = wj0.f(m02.c, new C05192(null), continuation);
        return objF == ck1.a ? objF : e6a.a;
    }

    @pq6
    public Object reloadForUser(@kp6 String str, @kp6 Continuation<? super e6a> continuation) throws Throwable {
        m12 m12Var = sc2.a;
        Object objF = wj0.f(m02.c, new C05202(str, null), continuation);
        return objF == ck1.a ? objF : e6a.a;
    }

    @Override // defpackage.kg4
    public void removeProgressSync(int tmdbId, @kp6 ContentItem.ItemType itemType) {
        cr4.g(itemType, "itemType");
        synchronized (this.cache) {
            this.cache.remove(cacheKey(tmdbId, itemType));
        }
    }

    @Override // defpackage.kg4
    public void setProgressSync(int tmdbId, @kp6 ContentItem.ItemType itemType, double progress) {
        cr4.g(itemType, "itemType");
        synchronized (this.cache) {
            try {
                String str = this.loadedUserSlug;
                if (str == null) {
                    return;
                }
                String str2 = itemType == ContentItem.ItemType.TV_SHOW ? "TV_SHOW" : "MOVIE";
                this.cache.put(cacheKey(tmdbId, itemType), new WatchStatusEntity(WatchStatusEntity.INSTANCE.generateKey(str, str2, tmdbId), str, tmdbId, str2, Double.valueOf(progress), System.currentTimeMillis(), null, null, null, null, null, 1984, null));
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Override // defpackage.kg4
    @pq6
    public Object upsert(int i, @kp6 ContentItem.ItemType itemType, @pq6 Double d, long j, @kp6 Continuation<? super e6a> continuation) throws Throwable {
        m12 m12Var = sc2.a;
        Object objF = wj0.f(m02.c, new C05212(itemType, i, d, j, null), continuation);
        return objF == ck1.a ? objF : e6a.a;
    }

    @pq6
    public Object upsertAll(@kp6 List<WatchStatusEntity> list, @kp6 Continuation<? super e6a> continuation) throws Throwable {
        m12 m12Var = sc2.a;
        Object objF = wj0.f(m02.c, new C05222(list, this, null), continuation);
        return objF == ck1.a ? objF : e6a.a;
    }
}
