package wq;

import java.net.Authenticator;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.SocketAddress;
import java.util.List;
import kotlin.jvm.internal.k;
import kotlin.jvm.internal.t;
import kotlin.text.y;
import sm.c0;
import uq.b;
import uq.b0;
import uq.d0;
import uq.f0;
import uq.h;
import uq.o;
import uq.q;
import uq.v;

/* loaded from: classes3.dex */
public final class a implements b {

    /* renamed from: d, reason: collision with root package name */
    private final q f49709d;

    /* renamed from: wq.a$a, reason: collision with other inner class name */
    /* loaded from: classes3.dex */
    public /* synthetic */ class C0994a {

        /* renamed from: a, reason: collision with root package name */
        public static final /* synthetic */ int[] f49710a;

        static {
            int[] iArr = new int[Proxy.Type.values().length];
            iArr[Proxy.Type.DIRECT.ordinal()] = 1;
            f49710a = iArr;
        }
    }

    public a(q defaultDns) {
        t.g(defaultDns, "defaultDns");
        this.f49709d = defaultDns;
    }

    private final InetAddress b(Proxy proxy, v vVar, q qVar) {
        Object c02;
        Proxy.Type type = proxy.type();
        if ((type == null ? -1 : C0994a.f49710a[type.ordinal()]) == 1) {
            c02 = c0.c0(qVar.a(vVar.i()));
            return (InetAddress) c02;
        }
        SocketAddress address = proxy.address();
        t.e(address, "null cannot be cast to non-null type java.net.InetSocketAddress");
        InetAddress address2 = ((InetSocketAddress) address).getAddress();
        t.f(address2, "address() as InetSocketAddress).address");
        return address2;
    }

    @Override // uq.b
    public b0 a(f0 f0Var, d0 response) {
        Proxy proxy;
        boolean z10;
        q qVar;
        PasswordAuthentication requestPasswordAuthentication;
        uq.a a10;
        t.g(response, "response");
        List<h> e10 = response.e();
        b0 C = response.C();
        v k10 = C.k();
        boolean z11 = response.f() == 407;
        if (f0Var == null || (proxy = f0Var.b()) == null) {
            proxy = Proxy.NO_PROXY;
        }
        for (h hVar : e10) {
            z10 = y.z("Basic", hVar.d(), true);
            if (z10) {
                if (f0Var == null || (a10 = f0Var.a()) == null || (qVar = a10.c()) == null) {
                    qVar = this.f49709d;
                }
                if (z11) {
                    SocketAddress address = proxy.address();
                    t.e(address, "null cannot be cast to non-null type java.net.InetSocketAddress");
                    InetSocketAddress inetSocketAddress = (InetSocketAddress) address;
                    String hostName = inetSocketAddress.getHostName();
                    t.f(proxy, "proxy");
                    requestPasswordAuthentication = Authenticator.requestPasswordAuthentication(hostName, b(proxy, k10, qVar), inetSocketAddress.getPort(), k10.s(), hVar.c(), hVar.d(), k10.u(), Authenticator.RequestorType.PROXY);
                } else {
                    String i10 = k10.i();
                    t.f(proxy, "proxy");
                    requestPasswordAuthentication = Authenticator.requestPasswordAuthentication(i10, b(proxy, k10, qVar), k10.o(), k10.s(), hVar.c(), hVar.d(), k10.u(), Authenticator.RequestorType.SERVER);
                }
                if (requestPasswordAuthentication != null) {
                    String str = z11 ? "Proxy-Authorization" : "Authorization";
                    String userName = requestPasswordAuthentication.getUserName();
                    t.f(userName, "auth.userName");
                    char[] password = requestPasswordAuthentication.getPassword();
                    t.f(password, "auth.password");
                    return C.h().m(str, o.a(userName, new String(password), hVar.b())).b();
                }
            }
        }
        return null;
    }

    public /* synthetic */ a(q qVar, int i10, k kVar) {
        this((i10 & 1) != 0 ? q.f37580b : qVar);
    }
}
