package com.google.android.gms.internal.ads;

import android.content.Context;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.SystemClock;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes2.dex */
public final class c61 implements n61 {

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public static final u11 f9764k = new u11(new JSONArray().toString(), new Bundle(), "", 1);

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final dv1 f9765a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final ScheduledExecutorService f9766b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final h01 f9767c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final Context f9768d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final ib1 f9769e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final f01 f9770f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final lq0 f9771g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final hs0 f9772h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final int f9773i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final String f9774j;

    public c61(a30 a30Var, ScheduledExecutorService scheduledExecutorService, String str, h01 h01Var, Context context, ib1 ib1Var, f01 f01Var, lq0 lq0Var, hs0 hs0Var, int i10) {
        this.f9765a = a30Var;
        this.f9766b = scheduledExecutorService;
        this.f9774j = str;
        this.f9767c = h01Var;
        this.f9768d = context;
        this.f9769e = ib1Var;
        this.f9770f = f01Var;
        this.f9771g = lq0Var;
        this.f9772h = hs0Var;
        this.f9773i = i10;
    }

    @Nullable
    private final Bundle zzg(String str) {
        Bundle bundle = this.f9769e.f12452a.f30905m;
        if (bundle != null) {
            return bundle.getBundle(str);
        }
        return null;
    }

    public final uu1 a() {
        Bundle bundle;
        String str;
        HashMap map;
        Map mapA;
        tn tnVar = ao.f8858fc;
        n7.y yVar = n7.y.f31093e;
        String lowerCase = ((Boolean) yVar.f31096c.b(tnVar)).booleanValue() ? this.f9769e.f12455d.toLowerCase(Locale.ROOT) : this.f9769e.f12455d;
        if (((Boolean) yVar.f31096c.b(ao.f8764a2)).booleanValue()) {
            hs0 hs0Var = this.f9772h;
            synchronized (hs0Var) {
                bundle = new Bundle(hs0Var.f12254a);
            }
        } else {
            bundle = new Bundle();
        }
        Bundle bundle2 = bundle;
        if (((Boolean) yVar.f31096c.b(ao.T4)).booleanValue()) {
            p20 p20VarT = m7.n.C.f30469h.g().t();
            char c6 = (TextUtils.isEmpty(p20VarT.f15240e) || p20VarT.f15242g == null) ? (char) 3 : p20VarT.b() ? (char) 1 : (char) 2;
            str = c6 != 1 ? c6 != 2 ? "EMPTY" : "INVALID" : "VALID";
        } else {
            str = "";
        }
        String str2 = str;
        ArrayList arrayList = new ArrayList();
        if (((Boolean) yVar.f31096c.b(ao.f8913j2)).booleanValue()) {
            h01 h01Var = this.f9767c;
            String str3 = this.f9774j;
            synchronized (h01Var) {
                try {
                    zzgui zzguiVarA = h01Var.a(str3, lowerCase);
                    zzgui zzguiVarI = h01Var.i(lowerCase);
                    map = new HashMap();
                    Iterator it = zzguiVarA.entrySet().iterator();
                    while (it.hasNext()) {
                        Map.Entry entry = (Map.Entry) it.next();
                        String str4 = (String) entry.getKey();
                        if (zzguiVarI.containsKey(str4)) {
                            k01 k01Var = (k01) zzguiVarI.get(str4);
                            List list = (List) entry.getValue();
                            map.put(str4, new k01(str4, k01Var.f13059b, k01Var.f13060c, k01Var.f13061d, (list == null || list.isEmpty()) ? new Bundle() : (Bundle) list.get(0)));
                        }
                    }
                    ct1 ct1VarC = zzguiVarI.entrySet().c();
                    while (ct1VarC.hasNext()) {
                        Map.Entry entry2 = (Map.Entry) ct1VarC.next();
                        String str5 = (String) entry2.getKey();
                        if (!map.containsKey(str5) && ((k01) entry2.getValue()).f13061d) {
                            map.put(str5, (k01) entry2.getValue());
                        }
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
            b(arrayList, map);
        } else {
            h01 h01Var2 = this.f9767c;
            Iterator it2 = h01Var2.a(this.f9774j, lowerCase).entrySet().iterator();
            while (it2.hasNext()) {
                Map.Entry entry3 = (Map.Entry) it2.next();
                String str6 = (String) entry3.getKey();
                arrayList.add(c(str6, (List) entry3.getValue(), zzg(str6), true, true));
            }
            synchronized (h01Var2) {
                mapA = TextUtils.isEmpty(m7.n.C.f30469h.g().t().f15240e) ? zzgwf.f19797g : zzgui.a(h01Var2.f11970b);
            }
            b(arrayList, mapA);
        }
        return ih1.x1(arrayList).c(new ae0(bundle2, str2, arrayList), this.f9765a);
    }

    public final void b(ArrayList arrayList, Map map) {
        Iterator it = map.entrySet().iterator();
        while (it.hasNext()) {
            k01 k01Var = (k01) ((Map.Entry) it.next()).getValue();
            String str = k01Var.f13058a;
            arrayList.add(c(str, Collections.singletonList(k01Var.f13062e), zzg(str), k01Var.f13059b, k01Var.f13060c));
        }
    }

    public final wu1 c(final String str, final List list, final Bundle bundle, final boolean z10, final boolean z11) {
        pu1 pu1Var = new pu1() { // from class: com.google.android.gms.internal.ads.y51
            /* JADX WARN: Removed duplicated region for block: B:71:0x004f A[EXC_TOP_SPLITTER, SYNTHETIC] */
            @Override // com.google.android.gms.internal.ads.pu1
            /*
                Code decompiled incorrectly, please refer to instructions dump.
            */
            public final com.google.common.util.concurrent.w zza() {
                ex exVar;
                ex exVarB;
                c61 c61Var = this.f18663a;
                String str2 = str;
                List list2 = list;
                Bundle bundle2 = bundle;
                boolean z12 = z10;
                boolean z13 = z11;
                c61Var.getClass();
                e30 e30Var = new e30();
                if (z13) {
                    if (((Boolean) n7.y.f31093e.f31096c.b(ao.f8781b2)).booleanValue()) {
                        try {
                            exVarB = c61Var.f9771g.b(str2);
                        } catch (RemoteException unused) {
                            p7.l0.h();
                            exVar = null;
                        }
                    } else {
                        f01 f01Var = c61Var.f9770f;
                        f01Var.getClass();
                        try {
                            f01Var.f10977a.put(str2, f01Var.f10978b.b(str2));
                        } catch (RemoteException unused2) {
                            p7.l0.h();
                        }
                        ConcurrentHashMap concurrentHashMap = f01Var.f10977a;
                        exVarB = concurrentHashMap.containsKey(str2) ? (ex) concurrentHashMap.get(str2) : null;
                    }
                    exVar = exVarB;
                }
                if (exVar == null) {
                    tn tnVar = ao.R1;
                    n7.y yVar = n7.y.f31093e;
                    if (!((Boolean) yVar.f31096c.b(tnVar)).booleanValue()) {
                        throw null;
                    }
                    int i10 = j01.f12671e;
                    synchronized (j01.class) {
                        try {
                            JSONObject jSONObject = new JSONObject();
                            try {
                                jSONObject.put("name", str2);
                                jSONObject.put("signal_error", "Adapter failed to instantiate");
                                if (((Boolean) yVar.f31096c.b(ao.X1)).booleanValue()) {
                                    jSONObject.put("signal_error_code", 1);
                                }
                                e30Var.zzc(jSONObject);
                            } catch (JSONException unused3) {
                            }
                        } catch (Throwable th) {
                            throw th;
                        }
                    }
                } else {
                    m7.n.C.f30472k.getClass();
                    j01 j01Var = new j01(str2, exVar, e30Var, SystemClock.elapsedRealtime());
                    tn tnVar2 = ao.W1;
                    n7.y yVar2 = n7.y.f31093e;
                    if (((Boolean) yVar2.f31096c.b(tnVar2)).booleanValue()) {
                        c61Var.f9766b.schedule(new z80(j01Var, 24), ((Long) yVar2.f31096c.b(ao.P1)).longValue(), TimeUnit.MILLISECONDS);
                    }
                    if (z12) {
                        if (((Boolean) yVar2.f31096c.b(ao.f8814d2)).booleanValue()) {
                            ((iu1) c61Var.f9765a).e(new a61(c61Var, exVar, bundle2, list2, j01Var, e30Var));
                        } else {
                            exVar.A1(new k8.b(c61Var.f9768d), c61Var.f9774j, bundle2, (Bundle) list2.get(0), c61Var.f9769e.f12454c, j01Var);
                        }
                    } else {
                        synchronized (j01Var) {
                            if (!j01Var.f12675d) {
                                try {
                                    if (((Boolean) yVar2.f31096c.b(ao.X1)).booleanValue()) {
                                        j01Var.f12673b.put("signal_error_code", 0);
                                    }
                                } catch (JSONException unused4) {
                                }
                                j01Var.f12672a.zzc(j01Var.f12673b);
                                j01Var.f12675d = true;
                            }
                        }
                    }
                }
                return e30Var;
            }
        };
        dv1 dv1Var = this.f9765a;
        wu1 wu1VarS = wu1.s(ih1.Q0(pu1Var, dv1Var));
        tn tnVar = ao.W1;
        n7.y yVar = n7.y.f31093e;
        if (!((Boolean) yVar.f31096c.b(tnVar)).booleanValue()) {
            wu1VarS = (wu1) ih1.Z0(wu1VarS, ((Long) yVar.f31096c.b(ao.P1)).longValue(), TimeUnit.MILLISECONDS, this.f9766b);
        }
        return ih1.U0(wu1VarS, Throwable.class, new ss(str, 3), dv1Var);
    }

    @Override // com.google.android.gms.internal.ads.n61
    public final com.google.common.util.concurrent.w zza() {
        int i10 = this.f9773i;
        u11 u11Var = f9764k;
        if (i10 == 2) {
            return ih1.k(u11Var);
        }
        ib1 ib1Var = this.f9769e;
        if (ib1Var.f12465n) {
            if (!Arrays.asList(((String) n7.y.f31093e.f31096c.b(ao.f8798c2)).split(",")).contains(com.google.android.gms.ads.nonagon.signalgeneration.b.zzb(com.google.android.gms.ads.nonagon.signalgeneration.b.zzc(ib1Var.f12452a)))) {
                return ih1.k(u11Var);
            }
        }
        return ih1.Q0(new pu1() { // from class: com.google.android.gms.internal.ads.b61
            @Override // com.google.android.gms.internal.ads.pu1
            public final /* synthetic */ com.google.common.util.concurrent.w zza() {
                return this.f9411a.a();
            }
        }, this.f9765a);
    }

    @Override // com.google.android.gms.internal.ads.n61
    public final int zzb() {
        return 32;
    }
}
