package com.strmr.ps.data.service;

import android.util.Log;
import com.strmr.ps.data.model.addon.StremioManifest;
import com.strmr.ps.data.service.StremioAddonService;
import defpackage.ak1;
import defpackage.ck1;
import defpackage.cr4;
import defpackage.e6a;
import defpackage.gr3;
import defpackage.n89;
import defpackage.s38;
import defpackage.st;
import defpackage.vx8;
import java.io.IOException;
import java.net.URL;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
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\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;", "Lcom/strmr/ps/data/service/StremioAddonService$c;", "<anonymous>", "(Lak1;)Lcom/strmr/ps/data/service/StremioAddonService$c;"}, k = 3, mv = {2, 0, 0})
@DebugMetadata(c = "com.strmr.ps.data.service.StremioAddonService$validateAddon$2", f = "StremioAddonService.kt", i = {}, l = {}, m = "invokeSuspend", n = {}, s = {})
@vx8
final class y extends SuspendLambda implements gr3<ak1, Continuation<? super StremioAddonService.c>, Object> {
    public final /* synthetic */ String a;
    public final /* synthetic */ StremioAddonService b;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public y(String str, StremioAddonService stremioAddonService, Continuation continuation) {
        super(2, continuation);
        this.a = str;
        this.b = stremioAddonService;
    }

    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    public final Continuation create(Object obj, Continuation continuation) {
        return new y(this.a, this.b, continuation);
    }

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

    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    public final Object invokeSuspend(Object obj) {
        String name;
        String version;
        ck1 ck1Var = ck1.a;
        s38.b(obj);
        String str = this.a;
        StremioAddonService stremioAddonService = this.b;
        boolean z = true;
        String strConcat = null;
        try {
            String string = n89.bg(str).toString();
            if (n89.ar(string, "stremio://", false)) {
                string = n89.an(string, "stremio://", "https://");
            }
            if (!n89.ar(string, "http://", false) && !n89.ar(string, "https://", false)) {
                string = "https://".concat(string);
            }
            URL url = new URL(string);
            if (url.getHost() != null) {
                String host = url.getHost();
                cr4.d(host);
                if (!n89.ab(host)) {
                    String strBh = n89.bh(string, '/');
                    if (n89.r(strBh, "/configure", false)) {
                        strBh = n89.aj(strBh, "/configure");
                    }
                    strConcat = !n89.r(strBh, "/manifest.json", false) ? strBh.concat("/manifest.json") : strBh;
                }
            }
        } catch (Exception e) {
            Log.e("StremioAddonService", "Failed to normalize URL: " + str, e);
        }
        if (strConcat == null) {
            return new StremioAddonService.c.a("Invalid URL format. Please enter a valid addon URL.", StremioAddonService.b.a);
        }
        String strAj = n89.aj(strConcat, "/manifest.json");
        try {
            StremioManifest stremioManifestA = StremioAddonService.a(stremioAddonService, strConcat);
            try {
                String id = stremioManifestA.getId();
                if (id != null && !n89.ab(id) && (name = stremioManifestA.getName()) != null && !n89.ab(name) && (version = stremioManifestA.getVersion()) != null && !n89.ab(version)) {
                    List<String> resourceNames = stremioManifestA.getResourceNames();
                    Set setAo = st.ao(new String[]{"stream", "catalog", "meta", "subtitles", "search"});
                    List<Object> catalogs = stremioManifestA.getCatalogs();
                    if (catalogs != null && !catalogs.isEmpty()) {
                        z = false;
                    }
                    List<String> list = resourceNames;
                    if (!(list instanceof Collection) || !list.isEmpty()) {
                        Iterator<T> it = list.iterator();
                        while (it.hasNext()) {
                            if (setAo.contains((String) it.next())) {
                                break;
                            }
                        }
                    }
                    if (z) {
                        return new StremioAddonService.c.a("This addon does not provide any recognized resources (stream, catalog, meta, subtitles, or search).", StremioAddonService.b.d);
                    }
                    stremioManifestA.getName();
                    stremioManifestA.getId();
                    Objects.toString(resourceNames);
                    return new StremioAddonService.c.b(stremioManifestA, strAj);
                }
                return new StremioAddonService.c.a("Invalid manifest: missing required fields (id, name, or version).", StremioAddonService.b.c);
            } catch (NullPointerException e2) {
                Log.e("StremioAddonService", "NPE during manifest validation (likely Gson/R8 null safety bypass)", e2);
                return new StremioAddonService.c.a("Invalid manifest format. This may not be a valid Stremio addon.", StremioAddonService.b.c);
            }
        } catch (IOException e3) {
            Log.e("StremioAddonService", "Network error fetching manifest", e3);
            return new StremioAddonService.c.a("Unable to connect to addon. Please check the URL and try again.", StremioAddonService.b.b);
        } catch (Exception e4) {
            Log.e("StremioAddonService", "Error parsing manifest", e4);
            return new StremioAddonService.c.a("Invalid manifest format. This may not be a valid Stremio addon.", StremioAddonService.b.c);
        }
    }
}
