package g5;

import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.media3.exoplayer.rtsp.RtspHeaders;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public final class r {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final Map f26217d;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public boolean f26218a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public Map f26219b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public boolean f26220c;

    static {
        String property = System.getProperty("http.agent");
        if (!TextUtils.isEmpty(property)) {
            int length = property.length();
            StringBuilder sb = new StringBuilder(property.length());
            for (int i10 = 0; i10 < length; i10++) {
                char cCharAt = property.charAt(i10);
                if ((cCharAt > 31 || cCharAt == '\t') && cCharAt < 127) {
                    sb.append(cCharAt);
                } else {
                    sb.append('?');
                }
            }
            property = sb.toString();
        }
        HashMap map = new HashMap(2);
        if (!TextUtils.isEmpty(property)) {
            map.put(RtspHeaders.USER_AGENT, Collections.singletonList(new s(property)));
        }
        f26217d = Collections.unmodifiableMap(map);
    }

    public r setHeader(@NonNull String str, @Nullable String str2) {
        return setHeader(str, str2 == null ? null : new s(str2));
    }

    public r setHeader(@NonNull String str, @Nullable q qVar) {
        if (this.f26218a) {
            this.f26218a = false;
            HashMap map = new HashMap(this.f26219b.size());
            for (Map.Entry entry : this.f26219b.entrySet()) {
                map.put(entry.getKey(), new ArrayList((Collection) entry.getValue()));
            }
            this.f26219b = map;
        }
        if (qVar == null) {
            this.f26219b.remove(str);
        } else {
            List arrayList = (List) this.f26219b.get(str);
            if (arrayList == null) {
                arrayList = new ArrayList();
                this.f26219b.put(str, arrayList);
            }
            arrayList.clear();
            arrayList.add(qVar);
        }
        if (this.f26220c && RtspHeaders.USER_AGENT.equalsIgnoreCase(str)) {
            this.f26220c = false;
        }
        return this;
    }
}
