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

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.media3.exoplayer.rtsp.SessionDescription;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

/* JADX INFO: loaded from: classes2.dex */
public final class qe1 {

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final i8.c f15719h;

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

    @VisibleForTesting
    public qe1(qy0 qy0Var, q7.a aVar, String str, String str2, Context context, @Nullable fb1 fb1Var, @Nullable gb1 gb1Var, i8.c cVar, tg tgVar) {
        this.f15712a = qy0Var;
        this.f15713b = aVar.f32212a;
        this.f15714c = str;
        this.f15715d = str2;
        this.f15716e = context;
        this.f15717f = fb1Var;
        this.f15718g = gb1Var;
        this.f15719h = cVar;
        this.f15720i = tgVar;
    }

    public static String zzd(String str, String str2, @Nullable String str3) {
        if (true == TextUtils.isEmpty(str3)) {
            str3 = "";
        }
        return str.replaceAll(str2, str3);
    }

    /* JADX INFO: Access modifiers changed from: private */
    @Nullable
    public static String zzg(@Nullable String str) {
        return TextUtils.isEmpty(str) ? "" : q7.g.b() ? "fakeForAdDebugLog" : str;
    }

    public final List zza(eb1 eb1Var, @Nullable wa1 wa1Var, List list) {
        return zzb(eb1Var, wa1Var, false, "", "", list, null, null);
    }

    /* JADX WARN: Removed duplicated region for block: B:46:0x0134  */
    /* JADX WARN: Removed duplicated region for block: B:56:0x015b  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final List zzb(eb1 eb1Var, @Nullable wa1 wa1Var, boolean z10, @Nullable String str, @Nullable String str2, List list, @Nullable td0 td0Var, @Nullable s20 s20Var) {
        long j10;
        ArrayList arrayList = new ArrayList();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            String strZzd = zzd(zzd(zzd((String) it.next(), "@gw_adlocid@", ((ib1) eb1Var.f10745a.f10402b).f12455d), "@gw_adnetrefresh@", true != z10 ? SessionDescription.SUPPORTED_SDP_VERSION : "1"), "@gw_sdkver@", this.f15713b);
            if (wa1Var != null) {
                String strZzd2 = zzd(zzd(zzd(strZzd, "@gw_qdata@", wa1Var.f18015w), "@gw_adnetid@", wa1Var.f18013v), "@gw_allocid@", wa1Var.f18011u);
                HashMap map = wa1Var.f18004q0;
                boolean z11 = wa1Var.T;
                Context context = this.f15716e;
                strZzd = ni.e(strZzd2, context, z11, map);
                tn tnVar = ao.Me;
                n7.y yVar = n7.y.f31093e;
                if (((Boolean) yVar.f31096c.b(tnVar)).booleanValue() && wa1Var.f17979e == 4) {
                    p7.r0 r0Var = m7.n.C.f30464c;
                    strZzd = zzd(strZzd, "@gw_aps@", true != p7.r0.f(context) ? SessionDescription.SUPPORTED_SDP_VERSION : "1");
                }
                if (((Boolean) yVar.f31096c.b(ao.Xe)).booleanValue() && s20Var != null) {
                    int i10 = s20Var.f16277a;
                    String strZzd3 = zzd(strZzd, "@gw_is@", i10 >= 0 ? Integer.toString(i10) : "");
                    int i11 = s20Var.f16278b;
                    String strZzd4 = zzd(strZzd3, "@gw_fis@", i11 >= 0 ? Integer.toString(i11) : "");
                    int i12 = s20Var.f16279c;
                    strZzd = zzd(strZzd4, "@gw_sfis@", i12 >= 0 ? Integer.toString(i12) : "");
                }
            }
            qy0 qy0Var = this.f15712a;
            String strZzd5 = zzd(strZzd, "@gw_adnetstatus@", qy0Var.d());
            synchronized (qy0Var) {
                j10 = qy0Var.f15895h;
            }
            String strZzd6 = zzd(zzd(zzd(strZzd5, "@gw_ttr@", Long.toString(j10, 10)), "@gw_seqnum@", this.f15714c), "@gw_sessid@", this.f15715d);
            tn tnVar2 = ao.Ve;
            n7.y yVar2 = n7.y.f31093e;
            if (((Boolean) yVar2.f31096c.b(tnVar2)).booleanValue()) {
                if (td0Var != null) {
                    AtomicLong atomicLong = td0Var.f16861a;
                    strZzd6 = atomicLong.get() > 0 ? zzd(strZzd6, "@gw_placement_id@", Long.toString(atomicLong.get(), 10)) : zzd(strZzd6, "@gw_placement_id@", "");
                }
            }
            boolean z12 = false;
            if (((Boolean) yVar2.f31096c.b(ao.f8915j4)).booleanValue() && !TextUtils.isEmpty(str)) {
                z12 = true;
            }
            boolean z13 = true;
            boolean z14 = !TextUtils.isEmpty(str2);
            if (z12) {
                z13 = z14;
            } else {
                if (z14) {
                }
                arrayList.add(strZzd6);
            }
            if (this.f15720i.a(Uri.parse(strZzd6))) {
                Uri.Builder builderBuildUpon = Uri.parse(strZzd6).buildUpon();
                if (z12) {
                    builderBuildUpon = builderBuildUpon.appendQueryParameter("ms", str);
                }
                if (z13) {
                    builderBuildUpon = builderBuildUpon.appendQueryParameter("attok", str2);
                }
                strZzd6 = builderBuildUpon.build().toString();
            }
            arrayList.add(strZzd6);
        }
        return arrayList;
    }
}
