package com.strmr.ps.data.repository;

import com.google.android.gms.measurement.api.AppMeasurementSdk;
import com.strmr.ps.data.local.dao.InstalledAddonDao;
import com.strmr.ps.data.local.entity.InstalledAddonEntity;
import com.strmr.ps.data.model.addon.StremioCatalog;
import com.strmr.ps.data.model.addon.StremioManifest;
import com.strmr.ps.data.service.StremioAddonService;
import defpackage.ak1;
import defpackage.ck1;
import defpackage.e6a;
import defpackage.fi7;
import defpackage.gr3;
import defpackage.io6;
import defpackage.kq;
import defpackage.q38;
import defpackage.s38;
import defpackage.vx8;
import defpackage.y41;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import kotlin.Metadata;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.jvm.internal.DebugMetadata;
import kotlin.coroutines.jvm.internal.SuspendLambda;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000\u0010\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\u0010\u0003\u001a\b\u0012\u0004\u0012\u00020\u00020\u0001*\u00020\u0000H\n¢\u0006\u0004\b\u0003\u0010\u0004"}, d2 = {"Lak1;", "Lq38;", "Lcom/strmr/ps/data/local/entity/InstalledAddonEntity;", "<anonymous>", "(Lak1;)Lq38;"}, k = 3, mv = {2, 0, 0})
@DebugMetadata(c = "com.strmr.ps.data.repository.AddonRepository$refreshAddon$2", f = "AddonRepository.kt", i = {1, 2, 2}, l = {579, 582, 614}, m = "invokeSuspend", n = {"existing", "existing", "updated"}, s = {"L$0", "L$0", "L$1"})
@vx8
public final class AddonRepository$refreshAddon$2 extends SuspendLambda implements gr3<ak1, Continuation<? super q38<? extends InstalledAddonEntity>>, Object> {
    final /* synthetic */ String $instanceId;
    Object L$0;
    Object L$1;
    int label;
    final /* synthetic */ AddonRepository this$0;

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

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

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

    /* JADX WARN: Removed duplicated region for block: B:32:0x0094 A[Catch: Exception -> 0x0025, TryCatch #1 {Exception -> 0x0025, blocks: (B:8:0x0020, B:80:0x018c, B:15:0x0034, B:30:0x008e, B:32:0x0094, B:34:0x009e, B:35:0x00a9, B:37:0x00af, B:39:0x00b7, B:47:0x00d0, B:40:0x00ba, B:42:0x00be, B:44:0x00ca, B:48:0x00d4, B:64:0x011e, B:66:0x0124, B:67:0x0128, B:69:0x012f, B:72:0x0137, B:75:0x013f, B:76:0x0143, B:52:0x00e5, B:81:0x01bb, B:83:0x01bf, B:85:0x01d6, B:86:0x01db, B:16:0x003b, B:22:0x0056, B:24:0x005a, B:26:0x0077, B:19:0x0044), top: B:94:0x0010 }] */
    /* JADX WARN: Removed duplicated region for block: B:45:0x00cd  */
    /* JADX WARN: Removed duplicated region for block: B:56:0x00f7 A[Catch: Exception -> 0x0102, TRY_LEAVE, TryCatch #2 {Exception -> 0x0102, blocks: (B:54:0x00ea, B:56:0x00f7), top: B:95:0x00ea }] */
    /* JADX WARN: Removed duplicated region for block: B:58:0x0102  */
    /* JADX WARN: Removed duplicated region for block: B:61:0x0112 A[Catch: Exception -> 0x011d, TRY_LEAVE, TryCatch #0 {Exception -> 0x011d, blocks: (B:59:0x0103, B:61:0x0112), top: B:92:0x0103 }] */
    /* JADX WARN: Removed duplicated region for block: B:63:0x011d  */
    /* JADX WARN: Removed duplicated region for block: B:66:0x0124 A[Catch: Exception -> 0x0025, TryCatch #1 {Exception -> 0x0025, blocks: (B:8:0x0020, B:80:0x018c, B:15:0x0034, B:30:0x008e, B:32:0x0094, B:34:0x009e, B:35:0x00a9, B:37:0x00af, B:39:0x00b7, B:47:0x00d0, B:40:0x00ba, B:42:0x00be, B:44:0x00ca, B:48:0x00d4, B:64:0x011e, B:66:0x0124, B:67:0x0128, B:69:0x012f, B:72:0x0137, B:75:0x013f, B:76:0x0143, B:52:0x00e5, B:81:0x01bb, B:83:0x01bf, B:85:0x01d6, B:86:0x01db, B:16:0x003b, B:22:0x0056, B:24:0x005a, B:26:0x0077, B:19:0x0044), top: B:94:0x0010 }] */
    /* JADX WARN: Removed duplicated region for block: B:69:0x012f A[Catch: Exception -> 0x0025, TryCatch #1 {Exception -> 0x0025, blocks: (B:8:0x0020, B:80:0x018c, B:15:0x0034, B:30:0x008e, B:32:0x0094, B:34:0x009e, B:35:0x00a9, B:37:0x00af, B:39:0x00b7, B:47:0x00d0, B:40:0x00ba, B:42:0x00be, B:44:0x00ca, B:48:0x00d4, B:64:0x011e, B:66:0x0124, B:67:0x0128, B:69:0x012f, B:72:0x0137, B:75:0x013f, B:76:0x0143, B:52:0x00e5, B:81:0x01bb, B:83:0x01bf, B:85:0x01d6, B:86:0x01db, B:16:0x003b, B:22:0x0056, B:24:0x005a, B:26:0x0077, B:19:0x0044), top: B:94:0x0010 }] */
    /* JADX WARN: Removed duplicated region for block: B:72:0x0137 A[Catch: Exception -> 0x0025, TryCatch #1 {Exception -> 0x0025, blocks: (B:8:0x0020, B:80:0x018c, B:15:0x0034, B:30:0x008e, B:32:0x0094, B:34:0x009e, B:35:0x00a9, B:37:0x00af, B:39:0x00b7, B:47:0x00d0, B:40:0x00ba, B:42:0x00be, B:44:0x00ca, B:48:0x00d4, B:64:0x011e, B:66:0x0124, B:67:0x0128, B:69:0x012f, B:72:0x0137, B:75:0x013f, B:76:0x0143, B:52:0x00e5, B:81:0x01bb, B:83:0x01bf, B:85:0x01d6, B:86:0x01db, B:16:0x003b, B:22:0x0056, B:24:0x005a, B:26:0x0077, B:19:0x0044), top: B:94:0x0010 }] */
    /* JADX WARN: Removed duplicated region for block: B:75:0x013f A[Catch: Exception -> 0x0025, TryCatch #1 {Exception -> 0x0025, blocks: (B:8:0x0020, B:80:0x018c, B:15:0x0034, B:30:0x008e, B:32:0x0094, B:34:0x009e, B:35:0x00a9, B:37:0x00af, B:39:0x00b7, B:47:0x00d0, B:40:0x00ba, B:42:0x00be, B:44:0x00ca, B:48:0x00d4, B:64:0x011e, B:66:0x0124, B:67:0x0128, B:69:0x012f, B:72:0x0137, B:75:0x013f, B:76:0x0143, B:52:0x00e5, B:81:0x01bb, B:83:0x01bf, B:85:0x01d6, B:86:0x01db, B:16:0x003b, B:22:0x0056, B:24:0x005a, B:26:0x0077, B:19:0x0044), top: B:94:0x0010 }] */
    /* JADX WARN: Removed duplicated region for block: B:79:0x018b  */
    /* JADX WARN: Removed duplicated region for block: B:81:0x01bb A[Catch: Exception -> 0x0025, TryCatch #1 {Exception -> 0x0025, blocks: (B:8:0x0020, B:80:0x018c, B:15:0x0034, B:30:0x008e, B:32:0x0094, B:34:0x009e, B:35:0x00a9, B:37:0x00af, B:39:0x00b7, B:47:0x00d0, B:40:0x00ba, B:42:0x00be, B:44:0x00ca, B:48:0x00d4, B:64:0x011e, B:66:0x0124, B:67:0x0128, B:69:0x012f, B:72:0x0137, B:75:0x013f, B:76:0x0143, B:52:0x00e5, B:81:0x01bb, B:83:0x01bf, B:85:0x01d6, B:86:0x01db, B:16:0x003b, B:22:0x0056, B:24:0x005a, B:26:0x0077, B:19:0x0044), top: B:94:0x0010 }] */
    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object invokeSuspend(Object obj) {
        Object objA;
        Object addonByInstanceId;
        Object objB;
        InstalledAddonEntity installedAddonEntity;
        StremioAddonService.c cVar;
        String capabilities;
        String json;
        String json2;
        String version;
        String description;
        InstalledAddonEntity installedAddonEntityCopy$default;
        InstalledAddonDao installedAddonDao;
        InstalledAddonEntity installedAddonEntity2;
        List resources;
        List<StremioCatalog> parsedCatalogs;
        String str;
        InstalledAddonEntity installedAddonEntity3;
        ck1 ck1Var = ck1.a;
        int i = this.label;
        try {
        } catch (Exception e) {
            ExecutorService executorService = kq.a;
            kq.a("AddonRepository", fi7.h("Failed to refresh addon ", this.$instanceId, ": ", e.getMessage()), null);
            objA = s38.a(e);
        }
        if (i == 0) {
            s38.b(obj);
            InstalledAddonDao installedAddonDao2 = this.this$0.addonDao;
            String str2 = this.$instanceId;
            this.label = 1;
            addonByInstanceId = installedAddonDao2.getAddonByInstanceId(str2, this);
            if (addonByInstanceId == ck1Var) {
            }
            return ck1Var;
        }
        if (i != 1) {
            if (i != 2) {
                if (i != 3) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                InstalledAddonEntity installedAddonEntity4 = (InstalledAddonEntity) this.L$1;
                installedAddonEntity2 = (InstalledAddonEntity) this.L$0;
                s38.b(obj);
                installedAddonEntity3 = installedAddonEntity4;
                this.this$0.triggerCloudSync();
                ExecutorService executorService2 = kq.a;
                kq.h("AddonRepository", "Refreshed addon: " + installedAddonEntity2.getName() + " (" + this.$instanceId + ")");
                objA = installedAddonEntity3;
                return new q38(objA);
            }
            InstalledAddonEntity installedAddonEntity5 = (InstalledAddonEntity) this.L$0;
            s38.b(obj);
            installedAddonEntity = installedAddonEntity5;
            objB = obj;
            cVar = (StremioAddonService.c) objB;
            if (cVar instanceof StremioAddonService.c.b) {
                if (cVar instanceof StremioAddonService.c.a) {
                    return new q38(s38.a(new IllegalStateException("Manifest validation failed: ".concat(((StremioAddonService.c.a) cVar).a))));
                }
                throw new io6();
            }
            StremioManifest stremioManifest = ((StremioAddonService.c.b) cVar).a;
            List<Object> resources2 = stremioManifest.getResources();
            if (resources2 == null) {
                capabilities = installedAddonEntity.getCapabilities();
                String str3 = capabilities;
                parsedCatalogs = stremioManifest.getParsedCatalogs();
                if (parsedCatalogs.isEmpty()) {
                }
                resources = this.this$0.parseResources(stremioManifest);
                if (resources.isEmpty()) {
                }
                version = stremioManifest.getVersion();
                if (version == null) {
                }
                String str4 = version;
                description = stremioManifest.getDescription();
                if (description == null) {
                }
                String str5 = description;
                if (json == null) {
                }
                String str6 = json;
                if (json2 == null) {
                }
                installedAddonEntityCopy$default = InstalledAddonEntity.copy$default(installedAddonEntity, null, null, 0, null, str4, str5, null, null, null, false, null, str3, null, null, 0, false, false, null, 0L, null, str6, json2, false, null, null, null, null, null, 265287631, null);
                installedAddonDao = this.this$0.addonDao;
                this.L$0 = installedAddonEntity;
                this.L$1 = installedAddonEntityCopy$default;
                this.label = 3;
                if (installedAddonDao.upsertAddon(installedAddonEntityCopy$default, this) != ck1Var) {
                }
                return ck1Var;
            }
            ArrayList arrayList = new ArrayList();
            for (Object obj2 : resources2) {
                if (obj2 instanceof String) {
                    str = (String) obj2;
                } else if (obj2 instanceof Map) {
                    Object obj3 = ((Map) obj2).get(AppMeasurementSdk.ConditionalUserProperty.NAME);
                    str = obj3 instanceof String ? (String) obj3 : null;
                }
                if (str != null) {
                    arrayList.add(str);
                }
            }
            capabilities = y41.ai(arrayList, ",", null, null, null, 62);
            if (capabilities == null) {
                capabilities = installedAddonEntity.getCapabilities();
            }
            String str32 = capabilities;
            try {
                parsedCatalogs = stremioManifest.getParsedCatalogs();
            } catch (Exception unused) {
            }
            json = parsedCatalogs.isEmpty() ? this.this$0.gson.toJson(parsedCatalogs) : null;
            try {
                resources = this.this$0.parseResources(stremioManifest);
            } catch (Exception unused2) {
            }
            json2 = resources.isEmpty() ? this.this$0.gson.toJson(resources) : null;
            version = stremioManifest.getVersion();
            if (version == null) {
                version = installedAddonEntity.getVersion();
            }
            String str42 = version;
            description = stremioManifest.getDescription();
            if (description == null) {
                description = installedAddonEntity.getDescription();
            }
            String str52 = description;
            if (json == null) {
                json = installedAddonEntity.getCatalogsJson();
            }
            String str62 = json;
            if (json2 == null) {
                json2 = installedAddonEntity.getResourcesJson();
            }
            installedAddonEntityCopy$default = InstalledAddonEntity.copy$default(installedAddonEntity, null, null, 0, null, str42, str52, null, null, null, false, null, str32, null, null, 0, false, false, null, 0L, null, str62, json2, false, null, null, null, null, null, 265287631, null);
            installedAddonDao = this.this$0.addonDao;
            this.L$0 = installedAddonEntity;
            this.L$1 = installedAddonEntityCopy$default;
            this.label = 3;
            if (installedAddonDao.upsertAddon(installedAddonEntityCopy$default, this) != ck1Var) {
                installedAddonEntity2 = installedAddonEntity;
                installedAddonEntity3 = installedAddonEntityCopy$default;
                this.this$0.triggerCloudSync();
                ExecutorService executorService22 = kq.a;
                kq.h("AddonRepository", "Refreshed addon: " + installedAddonEntity2.getName() + " (" + this.$instanceId + ")");
                objA = installedAddonEntity3;
            }
            return ck1Var;
            return new q38(objA);
        }
        s38.b(obj);
        addonByInstanceId = obj;
        InstalledAddonEntity installedAddonEntity6 = (InstalledAddonEntity) addonByInstanceId;
        if (installedAddonEntity6 == null) {
            return new q38(s38.a(new IllegalStateException("Addon not found: " + this.$instanceId)));
        }
        StremioAddonService stremioAddonService = this.this$0.stremioAddonService;
        String manifestUrl = installedAddonEntity6.getManifestUrl();
        this.L$0 = installedAddonEntity6;
        this.label = 2;
        objB = stremioAddonService.b(manifestUrl, this);
        if (objB == ck1Var) {
            return ck1Var;
        }
        installedAddonEntity = installedAddonEntity6;
        cVar = (StremioAddonService.c) objB;
        if (cVar instanceof StremioAddonService.c.b) {
        }
    }
}
