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

import android.text.TextUtils;
import androidx.media3.exoplayer.rtsp.RtspHeaders;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPOutputStream;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes2.dex */
public final class jv0 implements rc1 {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final Pattern f13024d = Pattern.compile("([^;]+=[^;]+)(;\\s|$)", 2);

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

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

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

    public jv0(String str, md1 md1Var, id1 id1Var) {
        this.f13025a = str;
        this.f13027c = md1Var;
        this.f13026b = id1Var;
    }

    @Override // com.google.android.gms.internal.ads.rc1
    public final Object zza(Object obj) throws zzecr {
        String strJoin;
        zzecr zzecrVar;
        byte[] bArr;
        JSONObject jSONObject;
        String strConcat;
        iv0 iv0Var = (iv0) obj;
        int iOptInt = iv0Var.f12636a.optInt("http_timeout_millis", 60000);
        uz uzVar = iv0Var.f12637b;
        int i10 = uzVar.f17452g;
        id1 id1Var = this.f13026b;
        md1 md1Var = this.f13027c;
        int i11 = 1;
        strJoin = "";
        if (i10 != -2) {
            if (i10 == 1) {
                List list = uzVar.f17446a;
                if (list != null) {
                    strJoin = TextUtils.join(", ", list);
                    q7.j.b(strJoin);
                }
                zzecrVar = new zzecr(2, "Error building request URL: ".concat(String.valueOf(strJoin)));
            } else {
                zzecrVar = new zzecr(1);
            }
            id1Var.b(zzecrVar);
            id1Var.a(false);
            md1Var.a(id1Var);
            throw zzecrVar;
        }
        HashMap map = new HashMap();
        if (uzVar.f17450e) {
            String str = this.f13025a;
            if (!TextUtils.isEmpty(str)) {
                if (((Boolean) n7.y.f31093e.f31096c.b(ao.f8961m1)).booleanValue()) {
                    if (TextUtils.isEmpty(str)) {
                        strConcat = "";
                    } else {
                        Matcher matcher = f13024d.matcher(str);
                        strConcat = "";
                        while (matcher.find()) {
                            String strGroup = matcher.group(i11);
                            if (strGroup != null) {
                                Locale locale = Locale.ROOT;
                                if (strGroup.toLowerCase(locale).startsWith("id=") || strGroup.toLowerCase(locale).startsWith("ide=")) {
                                    if (!TextUtils.isEmpty(strConcat)) {
                                        strConcat = strConcat.concat("; ");
                                    }
                                    strConcat = strConcat.concat(strGroup);
                                }
                            }
                            i11 = 1;
                        }
                    }
                    if (!TextUtils.isEmpty(strConcat)) {
                        map.put("Cookie", strConcat);
                    }
                } else {
                    map.put("Cookie", str);
                }
            }
        }
        if (uzVar.f17449d && (jSONObject = iv0Var.f12636a) != null) {
            JSONObject jSONObjectOptJSONObject = jSONObject.optJSONObject("pii");
            if (jSONObjectOptJSONObject != null) {
                if (!TextUtils.isEmpty(jSONObjectOptJSONObject.optString("doritos", ""))) {
                    map.put("x-afma-drt-cookie", jSONObjectOptJSONObject.optString("doritos", ""));
                }
                if (!TextUtils.isEmpty(jSONObjectOptJSONObject.optString("doritos_v2", ""))) {
                    map.put("x-afma-drt-v2-cookie", jSONObjectOptJSONObject.optString("doritos_v2", ""));
                }
            } else {
                p7.l0.g("DSID signal does not exist.");
            }
        }
        byte[] byteArray = new byte[0];
        String str2 = uzVar.f17448c;
        if (TextUtils.isEmpty(str2)) {
            bArr = byteArray;
        } else {
            byte[] bytes = str2.getBytes(StandardCharsets.UTF_8);
            if (uzVar.f17457l) {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                try {
                    GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(byteArrayOutputStream);
                    try {
                        gZIPOutputStream.write(bytes);
                        gZIPOutputStream.finish();
                        byteArray = byteArrayOutputStream.toByteArray();
                        gZIPOutputStream.close();
                    } finally {
                    }
                } catch (IOException e10) {
                    p7.l0.h();
                    m7.n.C.f30469h.d("PrepareRequestFunction.apply", e10);
                    byteArray = null;
                }
                if (byteArray != null) {
                    map.put(RtspHeaders.CONTENT_ENCODING, "gzip");
                    bArr = byteArray;
                }
            }
            bArr = bytes;
        }
        String str3 = uzVar.f17458m;
        strJoin = TextUtils.isEmpty(str3) ? "" : str3;
        id1Var.a(true);
        md1Var.a(id1Var);
        return new fv0(uzVar.f17451f, iOptInt, map, bArr, strJoin);
    }
}
